Spaces:
Sleeping
Sleeping
implement ram checking
Browse files- app.py +37 -3
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import random
|
| 2 |
import threading
|
|
|
|
| 3 |
|
| 4 |
import fastapi
|
| 5 |
import gradio as gr
|
|
@@ -9,6 +10,20 @@ from viser_proxy_manager import ViserProxyManager
|
|
| 9 |
from vis_st4rtrack import visualize_st4rtrack
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def main() -> None:
|
| 13 |
app = fastapi.FastAPI()
|
| 14 |
viser_manager = ViserProxyManager(app)
|
|
@@ -21,10 +36,21 @@ def main() -> None:
|
|
| 21 |
# Add the iframe with a border
|
| 22 |
# add_sphere_btn = gr.Button("Add Random Sphere")
|
| 23 |
iframe_html = gr.HTML("")
|
|
|
|
| 24 |
|
| 25 |
-
@demo.load(outputs=[iframe_html])
|
| 26 |
def start_server(request: gr.Request):
|
| 27 |
assert request.session_hash is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
viser_manager.start_server(request.session_hash)
|
| 29 |
|
| 30 |
# Use the request's base URL if available
|
|
@@ -46,8 +72,16 @@ def main() -> None:
|
|
| 46 |
).start()
|
| 47 |
|
| 48 |
return f"""
|
| 49 |
-
<iframe
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# @demo.load(outputs=[iframe_html])
|
| 53 |
# def start_server(request: gr.Request):
|
|
|
|
| 1 |
import random
|
| 2 |
import threading
|
| 3 |
+
import psutil # Add this import for RAM monitoring
|
| 4 |
|
| 5 |
import fastapi
|
| 6 |
import gradio as gr
|
|
|
|
| 10 |
from vis_st4rtrack import visualize_st4rtrack
|
| 11 |
|
| 12 |
|
| 13 |
+
def check_ram_usage(threshold_percent=90):
|
| 14 |
+
"""Check if RAM usage is above the threshold.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
threshold_percent: Maximum RAM usage percentage allowed
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
bool: True if RAM usage is below threshold, False otherwise
|
| 21 |
+
"""
|
| 22 |
+
ram_percent = psutil.virtual_memory().percent
|
| 23 |
+
print(f"Current RAM usage: {ram_percent}%")
|
| 24 |
+
return ram_percent < threshold_percent
|
| 25 |
+
|
| 26 |
+
|
| 27 |
def main() -> None:
|
| 28 |
app = fastapi.FastAPI()
|
| 29 |
viser_manager = ViserProxyManager(app)
|
|
|
|
| 36 |
# Add the iframe with a border
|
| 37 |
# add_sphere_btn = gr.Button("Add Random Sphere")
|
| 38 |
iframe_html = gr.HTML("")
|
| 39 |
+
status_text = gr.Markdown("") # Add status text component
|
| 40 |
|
| 41 |
+
@demo.load(outputs=[iframe_html, status_text])
|
| 42 |
def start_server(request: gr.Request):
|
| 43 |
assert request.session_hash is not None
|
| 44 |
+
|
| 45 |
+
# Check RAM usage before starting visualization
|
| 46 |
+
if not check_ram_usage(threshold_percent=90):
|
| 47 |
+
return """
|
| 48 |
+
<div style="text-align: center; padding: 20px; background-color: #ffeeee; border-radius: 5px;">
|
| 49 |
+
<h2>⚠️ Server is currently under high load</h2>
|
| 50 |
+
<p>Please try again later when resources are available.</p>
|
| 51 |
+
</div>
|
| 52 |
+
""", "**System Status:** High memory usage detected. Visualization not loaded to prevent server overload."
|
| 53 |
+
|
| 54 |
viser_manager.start_server(request.session_hash)
|
| 55 |
|
| 56 |
# Use the request's base URL if available
|
|
|
|
| 72 |
).start()
|
| 73 |
|
| 74 |
return f"""
|
| 75 |
+
<iframe
|
| 76 |
+
src="{protocol}://{host}/viser/{request.session_hash}/"
|
| 77 |
+
width="100%"
|
| 78 |
+
height="500px"
|
| 79 |
+
frameborder="0"
|
| 80 |
+
style="display: block;"
|
| 81 |
+
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
| 82 |
+
loading="lazy"
|
| 83 |
+
></iframe>
|
| 84 |
+
""", "**System Status:** Visualization loaded successfully."
|
| 85 |
|
| 86 |
# @demo.load(outputs=[iframe_html])
|
| 87 |
# def start_server(request: gr.Request):
|
requirements.txt
CHANGED
|
@@ -12,3 +12,4 @@ opencv-python>=4.5.0
|
|
| 12 |
imageio>=2.25.0
|
| 13 |
matplotlib>=3.5.0
|
| 14 |
pyliblzfse>=0.1.0
|
|
|
|
|
|
| 12 |
imageio>=2.25.0
|
| 13 |
matplotlib>=3.5.0
|
| 14 |
pyliblzfse>=0.1.0
|
| 15 |
+
psutil>=5.9.0
|