Spaces:
Sleeping
Sleeping
| # scripts/utils/gpu.py | |
| import subprocess | |
| import sys | |
| def get_gpu_stats() -> dict | None: | |
| """Return real‑time GPU stats via nvidia‑smi, or None on failure.""" | |
| try: | |
| cmd = ( | |
| "nvidia-smi --query-gpu=utilization.gpu,temperature.gpu,"\ | |
| "memory.used,memory.total --format=csv,noheader,nounits" | |
| ) | |
| kwargs = {"shell": True} | |
| if sys.platform == "win32": | |
| kwargs["creationflags"] = 0x08000000 | |
| out = subprocess.check_output(cmd, **kwargs).decode().strip() | |
| if out: | |
| load, temp, used, total = [p.strip() for p in out.split(",")] | |
| return { | |
| "load": f"{load}%", | |
| "temp": f"{temp}°C", | |
| "vram_used": f"{round(int(used) / 1024, 1)} GB", | |
| "vram_total": f"{round(int(total) / 1024, 1)} GB", | |
| } | |
| except Exception as e: | |
| print(f"GPUStats Error: {e}") | |
| return None |