chuckfinca Claude Opus 4.6 (1M context) commited on
Commit
826a3c8
·
1 Parent(s): de5b38c

Mount API routes on Gradio's app instead of wrapping — fixes HF Spaces

Browse files
Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -518,32 +518,34 @@ def ask_question(question: str) -> dict:
518
  }
519
 
520
 
521
- if __name__ == "__main__":
522
- import fastapi
523
- from fastapi.middleware.cors import CORSMiddleware
524
-
525
- WORKSPACE_DIR = load_workspace()
526
- demo = build_app()
527
-
528
- # Mount the JSON API on the same FastAPI app
529
- fastapi_app = fastapi.FastAPI()
530
- fastapi_app.add_middleware(
531
- CORSMiddleware,
532
- allow_origins=["*"],
533
- allow_methods=["POST"],
534
- allow_headers=["*"],
535
- )
536
 
537
- @fastapi_app.post("/api/ask")
538
- async def api_ask(request: fastapi.Request):
539
- body = await request.json()
540
- question = body.get("question", "")
541
- if not question:
542
- return {"error": "No question provided"}
543
- return ask_question(question)
544
 
545
- @fastapi_app.get("/api/remaining")
546
- async def api_remaining():
547
- return {"remaining": _remaining(), "limit": DAILY_LIMIT}
548
 
549
- demo.launch(ssr_mode=False, app=fastapi_app)
 
 
 
 
 
 
 
518
  }
519
 
520
 
521
+ WORKSPACE_DIR = load_workspace()
522
+ demo = build_app()
523
+
524
+ # Add API routes to Gradio's underlying FastAPI app
525
+ import fastapi as _fastapi
526
+ from fastapi.middleware.cors import CORSMiddleware
527
+
528
+ demo.app.add_middleware(
529
+ CORSMiddleware,
530
+ allow_origins=["*"],
531
+ allow_methods=["*"],
532
+ allow_headers=["*"],
533
+ )
534
+
 
535
 
536
+ @demo.app.post("/api/ask")
537
+ async def api_ask(request: _fastapi.Request):
538
+ body = await request.json()
539
+ question = body.get("question", "")
540
+ if not question:
541
+ return {"error": "No question provided"}
542
+ return ask_question(question)
543
 
 
 
 
544
 
545
+ @demo.app.get("/api/remaining")
546
+ async def api_remaining():
547
+ return {"remaining": _remaining(), "limit": DAILY_LIMIT}
548
+
549
+
550
+ if __name__ == "__main__":
551
+ demo.launch(ssr_mode=False)