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

Add REST API endpoint (/api/agent) + client script

Browse files
Files changed (2) hide show
  1. app.py +43 -3
  2. requirements.txt +2 -0
app.py CHANGED
@@ -280,7 +280,44 @@ def run_agent(message, history, agent_type):
280
  yield _get_display(key), "✅ Done"
281
 
282
  # ============================================================
283
- # UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  # ============================================================
285
 
286
  def create_app():
@@ -308,8 +345,11 @@ def create_app():
308
  btn.click(handler, [inp, chat], [chat, gr.Textbox(visible=False)])
309
  if tab_key == "command":
310
  inp.submit(handler, [inp, chat], [chat, gr.Textbox(visible=False)])
311
- gr.Markdown("---\n*DEX Evolution Outpost v1.1 — all local, no API keys*")
312
  return app
313
 
314
  if __name__ == "__main__":
315
- create_app().launch()
 
 
 
 
280
  yield _get_display(key), "✅ Done"
281
 
282
  # ============================================================
283
+ # FastAPI REST API
284
+ # ============================================================
285
+
286
+ from fastapi import FastAPI, HTTPException
287
+ from fastapi.middleware.cors import CORSMiddleware
288
+ from pydantic import BaseModel
289
+
290
+ api_app = FastAPI(title="DEX Evolution Outpost API")
291
+ api_app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
292
+
293
+ class AgentRequest(BaseModel):
294
+ message: str
295
+ agent_type: str = "web"
296
+ stream: bool = False
297
+
298
+ @api_app.post("/api/agent")
299
+ def api_agent(req: AgentRequest):
300
+ """Call an agent and get the final response."""
301
+ agent_type = req.agent_type if req.agent_type in AGENT_REGISTRY else "web"
302
+ messages = []
303
+ for update in run_agent(req.message, [], agent_type):
304
+ if isinstance(update, tuple):
305
+ chat_history = update[0]
306
+ status = update[1]
307
+ if "✅" in status:
308
+ # Extract last bot message
309
+ if chat_history:
310
+ last = chat_history[-1]
311
+ bot_msg = last[1] if last[1] else ""
312
+ return {"status": "ok", "agent_type": agent_type, "response": bot_msg}
313
+ return {"status": "ok", "agent_type": agent_type, "response": "No response."}
314
+
315
+ @api_app.get("/api/health")
316
+ def health():
317
+ return {"status": "ok", "model": "Qwen3-30B-A3B", "gpu": "H200"}
318
+
319
+ # ============================================================
320
+ # UI + Mount
321
  # ============================================================
322
 
323
  def create_app():
 
345
  btn.click(handler, [inp, chat], [chat, gr.Textbox(visible=False)])
346
  if tab_key == "command":
347
  inp.submit(handler, [inp, chat], [chat, gr.Textbox(visible=False)])
348
+ gr.Markdown("---\n*DEX Evolution Outpost v1.1 — all local, no API keys | [API](/api/agent)*")
349
  return 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)
requirements.txt CHANGED
@@ -13,3 +13,5 @@ pandas
13
  numpy
14
  seaborn
15
  scipy
 
 
 
13
  numpy
14
  seaborn
15
  scipy
16
+ fastapi
17
+ uvicorn