dexifried commited on
Commit
f1507d9
·
1 Parent(s): 1dede1f

Fix: use gr.Blocks.launch(), add REST API via demo.app, add warmup GPU func

Browse files
Files changed (1) hide show
  1. app.py +30 -4
app.py CHANGED
@@ -13,6 +13,7 @@ if not hasattr(huggingface_hub, "HfFolder"):
13
  huggingface_hub.HfFolder = _HfFolder
14
 
15
  import gradio as gr
 
16
  import spaces
17
 
18
  logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
@@ -49,7 +50,7 @@ TOOL_DEFS = {
49
  }
50
 
51
  # ============================================================
52
- # GPU Functions
53
  # ============================================================
54
 
55
  _LLM_PIPELINE = None
@@ -57,6 +58,12 @@ _LLM_TOKENIZER = None
57
  _IMAGE_PIPELINE = None
58
  _CODE_GLOBALS: dict = {}
59
 
 
 
 
 
 
 
60
  @spaces.GPU(duration=120)
61
  def _gpu_llm_chat(messages_json: str, tools_json: str) -> str:
62
  """Qwen3-30B inference on H200. Returns JSON with content + tool_calls."""
@@ -350,6 +357,25 @@ def create_app():
350
 
351
  if __name__ == "__main__":
352
  demo = create_app()
353
- app = gr.mount_gradio_app(api_app, demo, path="/")
354
- import uvicorn
355
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  huggingface_hub.HfFolder = _HfFolder
14
 
15
  import gradio as gr
16
+
17
  import spaces
18
 
19
  logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
 
50
  }
51
 
52
  # ============================================================
53
+ # GPU Functions — must be module-level for ZeroGPU detection
54
  # ============================================================
55
 
56
  _LLM_PIPELINE = None
 
58
  _IMAGE_PIPELINE = None
59
  _CODE_GLOBALS: dict = {}
60
 
61
+ @spaces.GPU(duration=60)
62
+ def _gpu_warmup() -> str:
63
+ """Warmup function for ZeroGPU detection."""
64
+ import torch
65
+ return f"GPU available: {torch.cuda.is_available()}, device: {torch.cuda.get_device_name(0)}"
66
+
67
  @spaces.GPU(duration=120)
68
  def _gpu_llm_chat(messages_json: str, tools_json: str) -> str:
69
  """Qwen3-30B inference on H200. Returns JSON with content + tool_calls."""
 
357
 
358
  if __name__ == "__main__":
359
  demo = create_app()
360
+ # Add REST API endpoint
361
+ @demo.app.post("/api/agent")
362
+ def api_agent(req: dict):
363
+ agent_type = req.get("agent_type", "web")
364
+ message = req.get("message", "")
365
+ if agent_type not in AGENT_REGISTRY:
366
+ agent_type = "web"
367
+ messages = []
368
+ for update in run_agent(message, [], agent_type):
369
+ if isinstance(update, tuple):
370
+ chat_history = update[0]
371
+ status = update[1]
372
+ if "✅" in status and chat_history:
373
+ last = chat_history[-1]
374
+ return {"status": "ok", "agent_type": agent_type, "response": last[1] or ""}
375
+ return {"status": "ok", "agent_type": agent_type, "response": "No response."}
376
+
377
+ @demo.app.get("/api/health")
378
+ def health():
379
+ return {"status": "ok", "model": "Qwen3-30B-A3B", "gpu": "H200"}
380
+
381
+ demo.launch(server_name="0.0.0.0", server_port=7860)