Download src/nida/envreport.py from Bilal7Dev/nida-tts: direct link, hf CLI and curl.
- Browser
- Download file 4.12 kB
-
https://huggingface.co/Bilal7Dev/nida-tts/resolve/main/src/nida/envreport.py
- Command line
-
hf download hf://Bilal7Dev/nida-tts/src/nida/envreport.py
-
curl -L -o envreport.py https://huggingface.co/Bilal7Dev/nida-tts/resolve/main/src/nida/envreport.py
4.12 kB
| """Environment report: the first command run on any new Colab runtime.""" | |
| from __future__ import annotations | |
| import importlib | |
| import importlib.metadata | |
| import os | |
| import platform | |
| import shutil | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| import nida | |
| __all__ = ["collect_env", "summarize_env"] | |
| _MEMINFO = Path("/proc/meminfo") | |
| def _torch_info() -> dict[str, object] | None: | |
| try: | |
| torch = importlib.import_module("torch") | |
| except ImportError: | |
| return None | |
| info: dict[str, object] = { | |
| "version": str(torch.__version__), | |
| "cuda_available": bool(torch.cuda.is_available()), | |
| } | |
| if info["cuda_available"]: | |
| props = torch.cuda.get_device_properties(0) | |
| info["device_name"] = str(props.name) | |
| info["device_count"] = int(torch.cuda.device_count()) | |
| info["total_memory_gb"] = round(float(props.total_memory) / 1024**3, 1) | |
| info["compute_capability"] = f"{int(props.major)}.{int(props.minor)}" | |
| info["bf16_supported"] = bool(torch.cuda.is_bf16_supported()) | |
| info["cuda_version"] = str(torch.version.cuda) | |
| return info | |
| def _package_version(name: str) -> str | None: | |
| """Installed distribution version without importing the package (None if absent).""" | |
| try: | |
| return importlib.metadata.version(name) | |
| except importlib.metadata.PackageNotFoundError: | |
| return None | |
| def _ram_total_gb(meminfo: Path = _MEMINFO) -> float | None: | |
| """Total system RAM from ``/proc/meminfo`` (Linux only; None elsewhere).""" | |
| if not meminfo.is_file(): | |
| return None | |
| for line in meminfo.read_text(encoding="ascii").splitlines(): | |
| if line.startswith("MemTotal:"): | |
| parts = line.split() | |
| if len(parts) < 2 or not parts[1].isdigit(): | |
| raise ValueError(f"unexpected MemTotal line in {meminfo}: {line!r}") | |
| return round(int(parts[1]) / 1024**2, 1) # value is in kB | |
| raise ValueError(f"MemTotal not found in {meminfo}") | |
| def _disk_free_gb(path: Path) -> float: | |
| return round(shutil.disk_usage(path).free / 1024**3, 1) | |
| def _nvidia_smi() -> str | None: | |
| exe = shutil.which("nvidia-smi") | |
| if exe is None: | |
| return None | |
| try: | |
| result = subprocess.run( | |
| [exe, "--query-gpu=name,memory.total,driver_version", "--format=csv,noheader"], | |
| capture_output=True, | |
| text=True, | |
| timeout=30, | |
| check=True, | |
| ) | |
| except (subprocess.SubprocessError, OSError) as err: | |
| return f"nvidia-smi failed: {err}" | |
| return result.stdout.strip() | |
| def collect_env() -> dict[str, object]: | |
| """Collect versions and hardware facts. Never raises for missing optional parts.""" | |
| return { | |
| "nida_version": nida.__version__, | |
| "python": sys.version.split()[0], | |
| "platform": platform.platform(), | |
| "cpu_count": os.cpu_count(), | |
| "ram_total_gb": _ram_total_gb(), | |
| "disk_free_gb": _disk_free_gb(Path.cwd()), | |
| "torch": _torch_info(), | |
| "torchaudio_version": _package_version("torchaudio"), | |
| "nvidia_smi": _nvidia_smi(), | |
| } | |
| def summarize_env(env: dict[str, object]) -> dict[str, object]: | |
| """Flatten the facts that decide hardware choices into a ``metrics.json`` payload. | |
| GPU fields are None when torch is missing or CUDA is unavailable. | |
| """ | |
| torch_info = env.get("torch") | |
| gpu = torch_info if isinstance(torch_info, dict) else {} | |
| return { | |
| "python": env.get("python"), | |
| "torch_version": gpu.get("version"), | |
| "torchaudio_version": env.get("torchaudio_version"), | |
| "cuda_available": gpu.get("cuda_available", False), | |
| "cuda_version": gpu.get("cuda_version"), | |
| "gpu_name": gpu.get("device_name"), | |
| "gpu_count": gpu.get("device_count", 0), | |
| "gpu_memory_gb": gpu.get("total_memory_gb"), | |
| "compute_capability": gpu.get("compute_capability"), | |
| "bf16_supported": gpu.get("bf16_supported"), | |
| "cpu_count": env.get("cpu_count"), | |
| "ram_total_gb": env.get("ram_total_gb"), | |
| "disk_free_gb": env.get("disk_free_gb"), | |
| } | |