Alovestocode commited on
Commit
5e1bcda
·
verified ·
1 Parent(s): d3d6314

Add FastAPI routes directly to Gradio app instead of mounting

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -412,28 +412,39 @@ with gr.Blocks(title="Router Model API - ZeroGPU") as gradio_app:
412
  outputs=output,
413
  )
414
 
415
- # Mount FastAPI routes using Gradio's load event
416
- def mount_fastapi_routes():
417
- """Mount FastAPI routes when Gradio app loads."""
 
 
 
418
  try:
419
- # Access Gradio's underlying FastAPI app
420
- gradio_app.app.mount("/v1", fastapi_app)
421
- gradio_app.app.mount("/gradio", fastapi_app)
422
- print("FastAPI routes mounted successfully")
423
- except (AttributeError, Exception) as e:
424
- print(f"FastAPI routes mounting note: {e}")
425
- # Try alternative mounting approach
426
- try:
427
- import starlette
428
- # Mount using Starlette's mount
429
- if hasattr(gradio_app, 'app') and hasattr(gradio_app.app, 'mount'):
430
- gradio_app.app.mount("/v1", fastapi_app)
431
- gradio_app.app.mount("/gradio", fastapi_app)
432
- except Exception as e2:
433
- print(f"Alternative mounting also failed: {e2}")
434
 
435
- # Use load event to mount routes when app is ready
436
- gradio_app.load(mount_fastapi_routes)
 
 
 
 
 
 
 
 
 
 
 
437
 
438
  # Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
439
  app = gradio_app
 
412
  outputs=output,
413
  )
414
 
415
+ # Add FastAPI routes directly to Gradio's app after it's created
416
+ @gradio_app.app.post("/v1/generate")
417
+ async def fastapi_generate_endpoint(request):
418
+ """FastAPI endpoint mounted in Gradio app."""
419
+ from fastapi import Request
420
+ from fastapi.responses import JSONResponse
421
  try:
422
+ data = await request.json()
423
+ payload = GeneratePayload(**data)
424
+ text = _generate_with_gpu(
425
+ prompt=payload.prompt,
426
+ max_new_tokens=payload.max_new_tokens or MAX_NEW_TOKENS,
427
+ temperature=payload.temperature or DEFAULT_TEMPERATURE,
428
+ top_p=payload.top_p or DEFAULT_TOP_P,
429
+ )
430
+ return JSONResponse(content={"text": text})
431
+ except Exception as exc:
432
+ from fastapi import HTTPException
433
+ raise HTTPException(status_code=500, detail=str(exc))
 
 
 
434
 
435
+ @gradio_app.app.get("/gradio")
436
+ async def fastapi_gradio_ui():
437
+ """FastAPI /gradio endpoint."""
438
+ return HTMLResponse(interactive_ui())
439
+
440
+ @gradio_app.app.get("/")
441
+ async def fastapi_healthcheck():
442
+ """FastAPI healthcheck endpoint."""
443
+ return {
444
+ "status": "ok",
445
+ "model": MODEL_ID,
446
+ "strategy": ACTIVE_STRATEGY or "pending",
447
+ }
448
 
449
  # Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
450
  app = gradio_app