Spaces:
Running
Running
removed system status
Browse files- ui/system_status.py +0 -74
ui/system_status.py
DELETED
|
@@ -1,74 +0,0 @@
|
|
| 1 |
-
"""System status UI component (CPU/memory)."""
|
| 2 |
-
import streamlit as st
|
| 3 |
-
|
| 4 |
-
try:
|
| 5 |
-
import psutil
|
| 6 |
-
HAS_PSUTIL = True
|
| 7 |
-
except ImportError:
|
| 8 |
-
HAS_PSUTIL = False
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def _get_container_memory():
|
| 12 |
-
"""
|
| 13 |
-
Read memory from cgroups when running in a container (Docker, HF Spaces).
|
| 14 |
-
psutil reports host memory in containers, which can be misleading (e.g. 128 GB vs 16 GB limit).
|
| 15 |
-
Returns (used_bytes, total_bytes) or None to fall back to psutil.
|
| 16 |
-
"""
|
| 17 |
-
try:
|
| 18 |
-
# cgroup v2 (modern Docker, HF Spaces)
|
| 19 |
-
for base in ("/sys/fs/cgroup", "/sys/fs/cgroup/self"):
|
| 20 |
-
try:
|
| 21 |
-
with open(f"{base}/memory.max", "r") as f:
|
| 22 |
-
max_val = f.read().strip()
|
| 23 |
-
if max_val == "max":
|
| 24 |
-
return None # No limit, use psutil
|
| 25 |
-
total = int(max_val)
|
| 26 |
-
with open(f"{base}/memory.current", "r") as f:
|
| 27 |
-
used = int(f.read().strip())
|
| 28 |
-
return (used, total)
|
| 29 |
-
except (FileNotFoundError, ValueError):
|
| 30 |
-
continue
|
| 31 |
-
# cgroup v1
|
| 32 |
-
try:
|
| 33 |
-
with open("/sys/fs/cgroup/memory/memory.limit_in_bytes", "r") as f:
|
| 34 |
-
total = int(f.read().strip())
|
| 35 |
-
with open("/sys/fs/cgroup/memory/memory.usage_in_bytes", "r") as f:
|
| 36 |
-
used = int(f.read().strip())
|
| 37 |
-
if total > 2**50: # Often 9223372036854771712 when unlimited
|
| 38 |
-
return None
|
| 39 |
-
return (used, total)
|
| 40 |
-
except (FileNotFoundError, ValueError):
|
| 41 |
-
pass
|
| 42 |
-
except Exception:
|
| 43 |
-
pass
|
| 44 |
-
return None
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def render_system_status():
|
| 48 |
-
"""Render CPU/memory status in the sidebar (always visible)."""
|
| 49 |
-
if not HAS_PSUTIL:
|
| 50 |
-
return
|
| 51 |
-
try:
|
| 52 |
-
cpu = psutil.cpu_percent(interval=0.1)
|
| 53 |
-
container_mem = _get_container_memory()
|
| 54 |
-
if container_mem is not None:
|
| 55 |
-
used_bytes, total_bytes = container_mem
|
| 56 |
-
mem_used_gb = used_bytes / (1024**3)
|
| 57 |
-
mem_total_gb = total_bytes / (1024**3)
|
| 58 |
-
mem_pct = 100 * used_bytes / total_bytes if total_bytes > 0 else 0
|
| 59 |
-
else:
|
| 60 |
-
mem = psutil.virtual_memory()
|
| 61 |
-
mem_used_gb = mem.used / (1024**3)
|
| 62 |
-
mem_total_gb = mem.total / (1024**3)
|
| 63 |
-
mem_pct = mem.percent
|
| 64 |
-
st.sidebar.markdown(
|
| 65 |
-
f"""
|
| 66 |
-
<div class="system-status">
|
| 67 |
-
<span class="status-dot"></span>
|
| 68 |
-
<span><strong>System</strong> CPU {cpu:.0f}% · Mem {mem_pct:.0f}% ({mem_used_gb:.1f}/{mem_total_gb:.1f} GB)</span>
|
| 69 |
-
</div>
|
| 70 |
-
""",
|
| 71 |
-
unsafe_allow_html=True,
|
| 72 |
-
)
|
| 73 |
-
except Exception:
|
| 74 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|