kaveh commited on
Commit
4354514
·
1 Parent(s): 3480865

corrected memory issue

Browse files
Files changed (1) hide show
  1. S2FApp/ui/components.py +48 -4
S2FApp/ui/components.py CHANGED
@@ -43,16 +43,60 @@ except ImportError:
43
  ST_DIALOG = getattr(st, "dialog", None) or getattr(st, "experimental_dialog", None)
44
 
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def render_system_status():
47
  """Render a small live CPU/memory status panel in the sidebar."""
48
  if not HAS_PSUTIL:
49
  return
50
  try:
51
  cpu = psutil.cpu_percent(interval=0.1)
52
- mem = psutil.virtual_memory()
53
- mem_used_gb = mem.used / (1024**3)
54
- mem_total_gb = mem.total / (1024**3)
55
- mem_pct = mem.percent
 
 
 
 
 
 
 
 
56
  st.sidebar.markdown(
57
  f"""
58
  <div style="
 
43
  ST_DIALOG = getattr(st, "dialog", None) or getattr(st, "experimental_dialog", None)
44
 
45
 
46
+ def _get_container_memory():
47
+ """
48
+ Read memory from cgroups when running in a container (Docker, HF Spaces).
49
+ psutil reports host memory in containers, which can be misleading (e.g. 128 GB vs 16 GB limit).
50
+ Returns (used_bytes, total_bytes) or None to fall back to psutil.
51
+ """
52
+ try:
53
+ # cgroup v2 (modern Docker, HF Spaces)
54
+ for base in ("/sys/fs/cgroup", "/sys/fs/cgroup/self"):
55
+ try:
56
+ with open(f"{base}/memory.max", "r") as f:
57
+ max_val = f.read().strip()
58
+ if max_val == "max":
59
+ return None # No limit, use psutil
60
+ total = int(max_val)
61
+ with open(f"{base}/memory.current", "r") as f:
62
+ used = int(f.read().strip())
63
+ return (used, total)
64
+ except (FileNotFoundError, ValueError):
65
+ continue
66
+ # cgroup v1
67
+ try:
68
+ with open("/sys/fs/cgroup/memory/memory.limit_in_bytes", "r") as f:
69
+ total = int(f.read().strip())
70
+ with open("/sys/fs/cgroup/memory/memory.usage_in_bytes", "r") as f:
71
+ used = int(f.read().strip())
72
+ if total > 2**50: # Often 9223372036854771712 when unlimited
73
+ return None
74
+ return (used, total)
75
+ except (FileNotFoundError, ValueError):
76
+ pass
77
+ except Exception:
78
+ pass
79
+ return None
80
+
81
+
82
  def render_system_status():
83
  """Render a small live CPU/memory status panel in the sidebar."""
84
  if not HAS_PSUTIL:
85
  return
86
  try:
87
  cpu = psutil.cpu_percent(interval=0.1)
88
+ # Prefer cgroup memory in containers (Docker, HF Spaces); psutil shows host memory
89
+ container_mem = _get_container_memory()
90
+ if container_mem is not None:
91
+ used_bytes, total_bytes = container_mem
92
+ mem_used_gb = used_bytes / (1024**3)
93
+ mem_total_gb = total_bytes / (1024**3)
94
+ mem_pct = 100 * used_bytes / total_bytes if total_bytes > 0 else 0
95
+ else:
96
+ mem = psutil.virtual_memory()
97
+ mem_used_gb = mem.used / (1024**3)
98
+ mem_total_gb = mem.total / (1024**3)
99
+ mem_pct = mem.percent
100
  st.sidebar.markdown(
101
  f"""
102
  <div style="