No elaborate script is needed these days, the kernel's runtime power management suspends the card automatically as long as /sys/bus/pci/devices/…/power/control is "auto" and the device isn't being used (the nvidia/nouveau module is unloaded).
Documentation for the nvidia module claims [0] runtime pm is supported even when the module _is_ loaded for GPUs since Turing, but I haven't been able to test that (mine is older than that).
[0] https://download.nvidia.com/XFree86/Linux-x86_64/435.17/READ...