Nafiul Huda commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,20 @@
|
|
| 1 |
import os
|
| 2 |
import psutil
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
def get_container_cpu_cores():
|
| 6 |
"""Reads cgroup limits to determine allocated vCPUs."""
|
| 7 |
try:
|
| 8 |
-
# Check for CPU quota (CFS scheduler limits)
|
| 9 |
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r") as f:
|
| 10 |
quota = int(f.read().strip())
|
| 11 |
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r") as f:
|
| 12 |
period = int(f.read().strip())
|
| 13 |
-
|
| 14 |
if quota > 0 and period > 0:
|
| 15 |
-
return quota / period
|
| 16 |
except Exception:
|
| 17 |
pass
|
| 18 |
|
| 19 |
-
# Fallback to alternative cgroup v2 path
|
| 20 |
try:
|
| 21 |
with open("/sys/fs/cgroup/cpu.max", "r") as f:
|
| 22 |
parts = f.read().strip().split()
|
|
@@ -25,7 +23,7 @@ def get_container_cpu_cores():
|
|
| 25 |
except Exception:
|
| 26 |
pass
|
| 27 |
|
| 28 |
-
return os.cpu_count() or 1
|
| 29 |
|
| 30 |
def get_container_ram_limit():
|
| 31 |
"""Reads cgroup limits to determine allocated RAM in MB."""
|
|
@@ -33,20 +31,19 @@ def get_container_ram_limit():
|
|
| 33 |
try:
|
| 34 |
with open(path, "r") as f:
|
| 35 |
limit = int(f.read().strip())
|
| 36 |
-
# If limit is a massive number, it means it's unlimited or unmapped
|
| 37 |
if limit < 9223372036854771712:
|
| 38 |
return limit / (1024 ** 2)
|
| 39 |
except Exception:
|
| 40 |
continue
|
| 41 |
return psutil.virtual_memory().total / (1024 ** 2)
|
| 42 |
|
|
|
|
|
|
|
| 43 |
def get_resources():
|
| 44 |
-
# Process-specific metrics
|
| 45 |
process = psutil.Process(os.getpid())
|
| 46 |
current_ram = process.memory_info().rss / (1024 ** 2)
|
| 47 |
current_cpu = process.cpu_percent(interval=0.5)
|
| 48 |
|
| 49 |
-
# Container allocated maximums
|
| 50 |
allocated_cores = get_container_cpu_cores()
|
| 51 |
allocated_ram = get_container_ram_limit()
|
| 52 |
|
|
@@ -56,7 +53,7 @@ def get_resources():
|
|
| 56 |
f"Max Allocated RAM: {allocated_ram:.1f} MB\n\n"
|
| 57 |
f"--- CURRENT LIVE USAGE ---\n"
|
| 58 |
f"Process RAM Usage: {current_ram:.1f} MB\n"
|
| 59 |
-
f"Process CPU Usage: {current_cpu:.1f}%
|
| 60 |
)
|
| 61 |
return metrics
|
| 62 |
|
|
|
|
| 1 |
import os
|
| 2 |
import psutil
|
| 3 |
import gradio as gr
|
| 4 |
+
import spaces # <-- 1. CRITICAL: Import the spaces library
|
| 5 |
|
| 6 |
def get_container_cpu_cores():
|
| 7 |
"""Reads cgroup limits to determine allocated vCPUs."""
|
| 8 |
try:
|
|
|
|
| 9 |
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r") as f:
|
| 10 |
quota = int(f.read().strip())
|
| 11 |
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r") as f:
|
| 12 |
period = int(f.read().strip())
|
|
|
|
| 13 |
if quota > 0 and period > 0:
|
| 14 |
+
return quota / period
|
| 15 |
except Exception:
|
| 16 |
pass
|
| 17 |
|
|
|
|
| 18 |
try:
|
| 19 |
with open("/sys/fs/cgroup/cpu.max", "r") as f:
|
| 20 |
parts = f.read().strip().split()
|
|
|
|
| 23 |
except Exception:
|
| 24 |
pass
|
| 25 |
|
| 26 |
+
return os.cpu_count() or 1
|
| 27 |
|
| 28 |
def get_container_ram_limit():
|
| 29 |
"""Reads cgroup limits to determine allocated RAM in MB."""
|
|
|
|
| 31 |
try:
|
| 32 |
with open(path, "r") as f:
|
| 33 |
limit = int(f.read().strip())
|
|
|
|
| 34 |
if limit < 9223372036854771712:
|
| 35 |
return limit / (1024 ** 2)
|
| 36 |
except Exception:
|
| 37 |
continue
|
| 38 |
return psutil.virtual_memory().total / (1024 ** 2)
|
| 39 |
|
| 40 |
+
# 2. CRITICAL: Add this decorator so ZeroGPU lets the container boot up
|
| 41 |
+
@spaces.GPU
|
| 42 |
def get_resources():
|
|
|
|
| 43 |
process = psutil.Process(os.getpid())
|
| 44 |
current_ram = process.memory_info().rss / (1024 ** 2)
|
| 45 |
current_cpu = process.cpu_percent(interval=0.5)
|
| 46 |
|
|
|
|
| 47 |
allocated_cores = get_container_cpu_cores()
|
| 48 |
allocated_ram = get_container_ram_limit()
|
| 49 |
|
|
|
|
| 53 |
f"Max Allocated RAM: {allocated_ram:.1f} MB\n\n"
|
| 54 |
f"--- CURRENT LIVE USAGE ---\n"
|
| 55 |
f"Process RAM Usage: {current_ram:.1f} MB\n"
|
| 56 |
+
f"Process CPU Usage: {current_cpu:.1f}%"
|
| 57 |
)
|
| 58 |
return metrics
|
| 59 |
|