Spaces:
Sleeping
Sleeping
| import os | |
| import psutil | |
| import platform | |
| import torch | |
| from typing import Dict, Any, Optional | |
| def get_cpu_name() -> str: | |
| # Attempt model name read on Linux | |
| try: | |
| with open("/proc/cpuinfo", "r") as f: | |
| for line in f: | |
| if "model name" in line: | |
| return line.split(":")[1].strip() | |
| except Exception: | |
| pass | |
| proc = platform.processor() | |
| return proc if proc else "Intel(R) Xeon(R) CPU" | |
| def get_system_ram_mb() -> float: | |
| try: | |
| process = psutil.Process(os.getpid()) | |
| return process.memory_info().rss / (1024 * 1024) | |
| except Exception: | |
| return 0.0 | |
| def get_gpu_vram_mb() -> Optional[float]: | |
| if torch.cuda.is_available(): | |
| try: | |
| return torch.cuda.memory_allocated() / (1024 * 1024) | |
| except Exception: | |
| return 0.0 | |
| return None | |
| def get_gpu_name() -> Optional[str]: | |
| if torch.cuda.is_available(): | |
| try: | |
| return torch.cuda.get_device_name(0) | |
| except Exception: | |
| return "NVIDIA GPU" | |
| return None | |
| def get_gpu_total_memory_mb() -> Optional[float]: | |
| if torch.cuda.is_available(): | |
| try: | |
| return torch.cuda.get_device_properties(0).total_memory / (1024 * 1024) | |
| except Exception: | |
| return 0.0 | |
| return None | |
| def get_hardware_info() -> Dict[str, Any]: | |
| return { | |
| "device_type": "gpu" if torch.cuda.is_available() else "cpu", | |
| "device_name": get_cpu_name(), | |
| "cpu_count": psutil.cpu_count(logical=True), | |
| "system_ram_mb": int(psutil.virtual_memory().total / (1024 * 1024)), | |
| "gpu_name": get_gpu_name(), | |
| "gpu_vram_mb": get_gpu_total_memory_mb() | |
| } | |