corrected memory issue
Browse files42 unmodified lines4344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495525354559697989910010110242 unmodified linesST_DIALOG = getattr(st, "dialog", None) or getattr(st, "experimental_dialog", None)
def _get_container_memory(): """ Read memory from cgroups when running in a container (Docker, HF Spaces). psutil reports host memory in containers, which can be misleading (e.g. 128 GB vs 16 GB limit). Returns (used_bytes, total_bytes) or None to fall back to psutil. """ try: # cgroup v2 (modern Docker, HF Spaces) for base in ("/sys/fs/cgroup", "/sys/fs/cgroup/self"): try: with open(f"{base}/memory.max", "r") as f: max_val = f.read().strip() if max_val == "max": return None # No limit, use psutil total = int(max_val) with open(f"{base}/memory.current", "r") as f: used = int(f.read().strip()) return (used, total) except (FileNotFoundError, ValueError): continue # cgroup v1 try: with open("/sys/fs/cgroup/memory/memory.limit_in_bytes", "r") as f: total = int(f.read().strip()) with open("/sys/fs/cgroup/memory/memory.usage_in_bytes", "r") as f: used = int(f.read().strip()) if total > 2**50: # Often 9223372036854771712 when unlimited return None return (used, total) except (FileNotFoundError, ValueError): pass except Exception: pass return None
def render_system_status(): """Render a small live CPU/memory status panel in the sidebar.""" if not HAS_PSUTIL: return try: cpu = psutil.cpu_percent(interval=0.1) # Prefer cgroup memory in containers (Docker, HF Spaces); psutil shows host memory container_mem = _get_container_memory() if container_mem is not None: used_bytes, total_bytes = container_mem mem_used_gb = used_bytes / (1024**3) mem_total_gb = total_bytes / (1024**3) mem_pct = 100 * used_bytes / total_bytes if total_bytes > 0 else 0 else: mem = psutil.virtual_memory() mem_used_gb = mem.used / (1024**3) mem_total_gb = mem.total / (1024**3) mem_pct = mem.percent mem = psutil.virtual_memory() mem_used_gb = mem.used / (1024**3) mem_total_gb = mem.total / (1024**3) mem_pct = mem.percent st.sidebar.markdown( f""" <div style="S2FApp/ui/components.py+48-4