Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,78 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import logging
|
| 5 |
-
from typing import Dict, Any
|
| 6 |
from pydantic import BaseModel
|
| 7 |
|
| 8 |
-
# Configure logging
|
| 9 |
logging.basicConfig(filename="ui.log", level=logging.INFO)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
# Pydantic models (mirroring backend)
|
| 13 |
class ResourceLimits(BaseModel):
|
| 14 |
cpu_limit: Optional[int] = None
|
| 15 |
memory_limit: Optional[int] = None
|
| 16 |
gpu_power_limit: Optional[int] = None
|
| 17 |
|
| 18 |
-
# API client class
|
| 19 |
class APIClient:
|
| 20 |
def __init__(self, base_url: str, api_key: str = None):
|
| 21 |
-
self.base_url = base_url
|
| 22 |
self.api_key = api_key
|
| 23 |
-
# Methods for each endpoint (get_metrics, set_limits, etc.)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def main():
|
|
|
|
| 31 |
with gr.Blocks(theme=gr.themes.Default()) as app:
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
main()
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import logging
|
| 5 |
+
from typing import Optional, Dict, Any
|
| 6 |
from pydantic import BaseModel
|
| 7 |
|
|
|
|
| 8 |
logging.basicConfig(filename="ui.log", level=logging.INFO)
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
|
|
|
| 11 |
class ResourceLimits(BaseModel):
|
| 12 |
cpu_limit: Optional[int] = None
|
| 13 |
memory_limit: Optional[int] = None
|
| 14 |
gpu_power_limit: Optional[int] = None
|
| 15 |
|
|
|
|
| 16 |
class APIClient:
|
| 17 |
def __init__(self, base_url: str, api_key: str = None):
|
| 18 |
+
self.base_url = base_url.rstrip("/")
|
| 19 |
self.api_key = api_key
|
|
|
|
| 20 |
|
| 21 |
+
def _headers(self):
|
| 22 |
+
return {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
|
| 23 |
+
|
| 24 |
+
def get_metrics(self) -> Dict[str, Any]:
|
| 25 |
+
try:
|
| 26 |
+
resp = requests.get(f"{self.base_url}/metrics", headers=self._headers(), timeout=10)
|
| 27 |
+
resp.raise_for_status()
|
| 28 |
+
return resp.json()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error(f"Error fetching metrics: {e}")
|
| 31 |
+
return {}
|
| 32 |
+
|
| 33 |
+
def set_limits(self, limits: ResourceLimits) -> bool:
|
| 34 |
+
try:
|
| 35 |
+
resp = requests.post(
|
| 36 |
+
f"{self.base_url}/limits",
|
| 37 |
+
json=limits.dict(exclude_none=True),
|
| 38 |
+
headers=self._headers(),
|
| 39 |
+
timeout=10,
|
| 40 |
+
)
|
| 41 |
+
resp.raise_for_status()
|
| 42 |
+
return True
|
| 43 |
+
except Exception as e:
|
| 44 |
+
logger.error(f"Error setting limits: {e}")
|
| 45 |
+
return False
|
| 46 |
+
|
| 47 |
+
def build_dashboard(api_client: APIClient):
|
| 48 |
+
def fetch_metrics():
|
| 49 |
+
return api_client.get_metrics()
|
| 50 |
+
|
| 51 |
+
def update_limits(cpu, memory, gpu):
|
| 52 |
+
limits = ResourceLimits(cpu_limit=cpu, memory_limit=memory, gpu_power_limit=gpu)
|
| 53 |
+
success = api_client.set_limits(limits)
|
| 54 |
+
return "Limits updated!" if success else "Failed to update limits."
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
metrics_output = gr.JSON(label="Current Metrics")
|
| 59 |
+
refresh_btn = gr.Button("Refresh Metrics")
|
| 60 |
+
with gr.Column():
|
| 61 |
+
cpu = gr.Number(label="CPU Limit")
|
| 62 |
+
memory = gr.Number(label="Memory Limit")
|
| 63 |
+
gpu = gr.Number(label="GPU Power Limit")
|
| 64 |
+
set_btn = gr.Button("Set Limits")
|
| 65 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 66 |
+
|
| 67 |
+
refresh_btn.click(fetch_metrics, outputs=metrics_output)
|
| 68 |
+
set_btn.click(update_limits, inputs=[cpu, memory, gpu], outputs=status)
|
| 69 |
|
| 70 |
def main():
|
| 71 |
+
api_client = APIClient(base_url="http://localhost:8000")
|
| 72 |
with gr.Blocks(theme=gr.themes.Default()) as app:
|
| 73 |
+
gr.Markdown("# Resource Dashboard")
|
| 74 |
+
build_dashboard(api_client)
|
| 75 |
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
main()
|