Spaces:
Sleeping
Sleeping
Attach /chat routes when Gradio builds the FastAPI app
Browse files18 unmodified lines1920212223242518 unmodified lines- `GET /personalities`- `GET /personalities/{id}`- `POST /chat` — `{ "personalityId", "message", "history?" }`- UI: `/ui`Public URL: `https://scchess-confidence-api.hf.space`18 unmodified lines19202122232418 unmodified lines- `GET /personalities`- `GET /personalities/{id}`- `POST /chat` — `{ "personalityId", "message", "history?" }`Public URL: `https://scchess-confidence-api.hf.space`
7 unmodified lines8910111213147 unmodified lines16116216316416516613 unmodified lines1801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312327 unmodified linesfrom typing import Anyimport gradio as grimport httpxfrom fastapi import HTTPException, Requestfrom fastapi.responses import JSONResponse147 unmodified linesreturn history, ""with gr.Blocks(title="Confidence Buddy API") as demo:gr.Markdown("## Confidence Buddy API\n"13 unmodified linesclear.click(lambda: ([], ""), outputs=[chatbot, msg])@demo.app.middleware("http")async def optional_app_key(request: Request, call_next):if request.url.path == "/health":return await call_next(request)secret = _app_secret()if secret and request.headers.get("x-app-key") != secret:return JSONResponse({"error": "Unauthorized"}, status_code=401)return await call_next(request)@demo.app.get("/health")async def health():return {"ok": True, "hasApiKey": bool(_api_key())}@demo.app.get("/personalities")async def personalities_list():return list_personalities()@demo.app.get("/personalities/{personality_id}")async def personality_detail(personality_id: str):personality = get_personality(personality_id)if not personality:return JSONResponse({"error": "Personality not found"}, status_code=404)return {"id": personality["id"],"name": personality["name"],"category": personality["category"],"subcategory": personality.get("subcategory"),"greeting": personality["greeting"],"suggestions": personality.get("suggestions", []),}@demo.app.post("/chat")async def chat_endpoint(request: Request):body = await request.json()try:return await deepseek_chat(personality_id=body.get("personalityId", ""),message=body.get("message", ""),history=body.get("history") or [],)except HTTPException as exc:return JSONResponse({"error": exc.detail}, status_code=exc.status_code)if __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")))7 unmodified lines891011121314147 unmodified lines16216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522613 unmodified lines2402412422432447 unmodified linesfrom typing import Anyimport gradio as grimport gradio.routesimport httpxfrom fastapi import HTTPException, Requestfrom fastapi.responses import JSONResponse147 unmodified linesreturn history, ""def _attach_api_routes(app) -> None:"""Flutter-compatible REST routes. Must be attached when Gradio builds the app."""@app.middleware("http")async def optional_app_key(request: Request, call_next):if request.url.path == "/health":return await call_next(request)secret = _app_secret()if secret and request.headers.get("x-app-key") != secret:return JSONResponse({"error": "Unauthorized"}, status_code=401)return await call_next(request)@app.get("/health")async def health():return {"ok": True, "hasApiKey": bool(_api_key())}@app.get("/personalities")async def personalities_list():return list_personalities()@app.get("/personalities/{personality_id}")async def personality_detail(personality_id: str):personality = get_personality(personality_id)if not personality:return JSONResponse({"error": "Personality not found"}, status_code=404)return {"id": personality["id"],"name": personality["name"],"category": personality["category"],"subcategory": personality.get("subcategory"),"greeting": personality["greeting"],"suggestions": personality.get("suggestions", []),}@app.post("/chat")async def chat_endpoint(request: Request):body = await request.json()try:return await deepseek_chat(personality_id=body.get("personalityId", ""),message=body.get("message", ""),history=body.get("history") or [],)except HTTPException as exc:return JSONResponse({"error": exc.detail}, status_code=exc.status_code)_original_create_app = gradio.routes.App.create_appdef _create_app_with_api_routes(blocks, *args, **kwargs):app = _original_create_app(blocks, *args, **kwargs)_attach_api_routes(app)return appgradio.routes.App.create_app = staticmethod(_create_app_with_api_routes)with gr.Blocks(title="Confidence Buddy API") as demo:gr.Markdown("## Confidence Buddy API\n"13 unmodified linesclear.click(lambda: ([], ""), outputs=[chatbot, msg])if __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")))
README.md-1
app.py+60-48