| """ | |
| System Info Module | |
| System monitoring and space information. | |
| """ | |
| import os | |
| import json | |
| import platform | |
| import psutil | |
| from .config import STORAGE_DIR, HF_TOKEN | |
| from .utils import format_size | |
| def get_system_info() -> str: | |
| """Get detailed system information""" | |
| try: | |
| mem = psutil.virtual_memory() | |
| disk = psutil.disk_usage('/') | |
| cpu = psutil.cpu_percent(interval=0.1) | |
| return json.dumps({ | |
| "system": { | |
| "platform": platform.system(), | |
| "python": platform.python_version(), | |
| }, | |
| "cpu": { | |
| "cores": psutil.cpu_count(), | |
| "usage": f"{cpu}%", | |
| }, | |
| "memory": { | |
| "total": format_size(mem.total), | |
| "used": format_size(mem.used), | |
| "percent": f"{mem.percent}%", | |
| }, | |
| "disk": { | |
| "total": format_size(disk.total), | |
| "free": format_size(disk.free), | |
| "percent": f"{disk.percent}%", | |
| } | |
| }, indent=2) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |
| def get_system_dashboard() -> str: | |
| """Get ASCII dashboard with bars""" | |
| try: | |
| mem = psutil.virtual_memory() | |
| disk = psutil.disk_usage('/') | |
| cpu = psutil.cpu_percent(interval=0.1) | |
| def bar(pct): | |
| filled = int(pct / 5) | |
| return 'β' * filled + 'β' * (20 - filled) | |
| return f""" | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β π SYSTEM DASHBOARD β | |
| β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ | |
| β πΎ RAM: [{bar(mem.percent)}] {mem.percent:5.1f}% β | |
| β {format_size(mem.used):>10} / {format_size(mem.total):<10} β | |
| β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ | |
| β πΏ DISK: [{bar(disk.percent)}] {disk.percent:5.1f}% β | |
| β {format_size(disk.used):>10} / {format_size(disk.total):<10} β | |
| β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ | |
| β π₯ CPU: [{bar(cpu)}] {cpu:5.1f}% β | |
| β {psutil.cpu_count()} cores β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| """.strip() | |
| except Exception as e: | |
| return f"Error: {e}" | |
| def get_space_info() -> str: | |
| """Get HuggingFace space information""" | |
| return json.dumps({ | |
| "space_id": os.getenv("SPACE_ID", "local"), | |
| "author": os.getenv("SPACE_AUTHOR_NAME", "unknown"), | |
| "hardware": os.getenv("SPACE_HARDWARE", "cpu-basic"), | |
| "hf_token": "set" if HF_TOKEN else "not_set", | |
| "storage_path": str(STORAGE_DIR), | |
| }, indent=2) | |
| def get_storage_stats() -> str: | |
| """Get storage statistics""" | |
| try: | |
| files = list(STORAGE_DIR.rglob('*')) | |
| total = sum(f.stat().st_size for f in files if f.is_file()) | |
| return json.dumps({ | |
| "files": len([f for f in files if f.is_file()]), | |
| "folders": len([f for f in files if f.is_dir()]), | |
| "total_size": format_size(total), | |
| }, indent=2) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) | |