File size: 940 Bytes
343eed9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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