Spaces:
Running
Running
Commit ·
826a3c8
1
Parent(s): de5b38c
Mount API routes on Gradio's app instead of wrapping — fixes HF Spaces
Browse files
app.py
CHANGED
|
@@ -518,32 +518,34 @@ def ask_question(question: str) -> dict:
|
|
| 518 |
}
|
| 519 |
|
| 520 |
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
)
|
| 536 |
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
|
| 545 |
-
@fastapi_app.get("/api/remaining")
|
| 546 |
-
async def api_remaining():
|
| 547 |
-
return {"remaining": _remaining(), "limit": DAILY_LIMIT}
|
| 548 |
|
| 549 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|