File size: 3,779 Bytes
02daacc |
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
"""
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)})
|