Files
gitlab-instance-0a899031_d2…/web2_host_caps.py
2026-03-26 05:35:31 -05:00

54 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""本机能力探测GPU 等(供控制台 UI"""
from __future__ import annotations
import platform
import socket
import subprocess
from typing import Any
def detect_nvidia_gpu() -> bool:
if platform.system().lower() not in ("windows", "linux"):
return False
try:
_kw: dict = {"capture_output": True, "timeout": 4}
if platform.system().lower() == "windows":
_kw["creationflags"] = subprocess.CREATE_NO_WINDOW
r = subprocess.run(["nvidia-smi"], **_kw)
return r.returncode == 0
except Exception:
return False
def detect_nvenc_available() -> bool:
if not detect_nvidia_gpu():
return False
try:
_kw = {"capture_output": True, "text": True, "timeout": 8}
if platform.system().lower() == "windows":
_kw["creationflags"] = subprocess.CREATE_NO_WINDOW
r = subprocess.run(["ffmpeg", "-hide_banner", "-encoders"], **_kw)
return r.returncode == 0 and "h264_nvenc" in (r.stdout or "")
except Exception:
return False
def gpu_status_payload() -> dict[str, Any]:
nv = detect_nvidia_gpu()
enc = detect_nvenc_available()
return {
"nvidia_detected": nv,
"nvenc_available": enc,
}
def machine_summary_payload() -> dict[str, Any]:
"""供远程 App 遥测:本机标识与系统信息。"""
return {
"hostname": socket.gethostname(),
"os": platform.system(),
"os_release": platform.release(),
"machine": platform.machine(),
"python": platform.python_version(),
}