Nafiul Huda commited on
Commit
38a6be8
·
verified ·
1 Parent(s): 86ce200

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 # This gives the true fractional vCPU allocation
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()
23
+ if len(parts) == 2 and parts[0] != "max":
24
+ return int(parts[0]) / int(parts[1])
25
+ except Exception:
26
+ pass
27
+
28
+ return os.cpu_count() or 1 # Fallback to total system cores
29
+
30
+ def get_container_ram_limit():
31
+ """Reads cgroup limits to determine allocated RAM in MB."""
32
+ for path in ["/sys/fs/cgroup/memory/memory.limit_in_bytes", "/sys/fs/cgroup/memory.max"]:
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
+
53
+ metrics = (
54
+ f"--- YOUR CONTAINER LIMITS ---\n"
55
+ f"Max Allocated vCPUs: {allocated_cores:.2f} cores\n"
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}% (relative to 1 core)"
60
+ )
61
+ return metrics
62
+
63
+ # Gradio Interface Setup
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("### Hugging Face ZeroGPU Container Resource Inspector")
66
+ output_text = gr.Textbox(label="Resource Info", lines=8)
67
+ refresh_btn = gr.Button("Refresh Specs")
68
+
69
+ refresh_btn.click(fn=get_resources, outputs=output_text)
70
+ demo.load(fn=get_resources, outputs=output_text)
71
+
72
+ if __name__ == "__main__":
73
+ demo.launch(server_name="0.0.0.0")