Alovestocode commited on
Commit
266408d
·
verified ·
1 Parent(s): d751a09

Fix: Keep Gradio as main app, mount FastAPI routes via load event, remove uvicorn.run()

Browse files
Files changed (1) hide show
  1. app.py +50 -5
app.py CHANGED
@@ -517,14 +517,59 @@ with gr.Blocks(
517
  fn=lambda: ("", ""),
518
  outputs=[prompt_input, output],
519
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
 
521
  # Enable queued execution so ZeroGPU can schedule GPU work reliably
522
  gradio_app.queue(max_size=8)
523
 
524
- # Mount the Gradio UI on the FastAPI app so both live side-by-side
525
- app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/gradio")
 
526
 
527
  if __name__ == "__main__": # pragma: no cover
528
- import uvicorn
529
-
530
- uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
 
517
  fn=lambda: ("", ""),
518
  outputs=[prompt_input, output],
519
  )
520
+
521
+ # Mount FastAPI routes onto Gradio's app using load event
522
+ # This must be done inside the Blocks context
523
+ def mount_fastapi_routes():
524
+ """Mount FastAPI routes onto Gradio's app after initialization."""
525
+ try:
526
+ from fastapi.responses import JSONResponse
527
+ from starlette.routing import Route
528
+ from starlette.requests import Request
529
+
530
+ # Create route handlers that call the FastAPI endpoint functions
531
+ async def health_handler(request: Request):
532
+ """Handle GET /health requests."""
533
+ result = healthcheck()
534
+ return JSONResponse(content=result)
535
+
536
+ async def generate_handler(request: Request):
537
+ """Handle POST /v1/generate requests."""
538
+ try:
539
+ data = await request.json()
540
+ payload = GeneratePayload(**data)
541
+ # Call the FastAPI endpoint function directly
542
+ result = generate_endpoint(payload)
543
+ return JSONResponse(content={"text": result.text})
544
+ except Exception as exc:
545
+ from fastapi import HTTPException
546
+ raise HTTPException(status_code=500, detail=str(exc))
547
+
548
+ async def console_handler(request: Request):
549
+ """Handle GET /console requests."""
550
+ html_content = interactive_ui()
551
+ return HTMLResponse(content=html_content)
552
+
553
+ # Add routes to Gradio's router
554
+ gradio_app.app.router.routes.append(Route("/health", health_handler, methods=["GET"]))
555
+ gradio_app.app.router.routes.append(Route("/v1/generate", generate_handler, methods=["POST"]))
556
+ gradio_app.app.router.routes.append(Route("/console", console_handler, methods=["GET"]))
557
+ print("FastAPI routes mounted successfully on Gradio app")
558
+ except Exception as e:
559
+ print(f"Warning: Could not mount FastAPI routes: {e}")
560
+ import traceback
561
+ traceback.print_exc()
562
+
563
+ # Mount routes when Gradio app loads (must be inside Blocks context)
564
+ gradio_app.load(mount_fastapi_routes)
565
 
566
  # Enable queued execution so ZeroGPU can schedule GPU work reliably
567
  gradio_app.queue(max_size=8)
568
 
569
+ # Set app to Gradio for Spaces compatibility (sdk: gradio requires Gradio app)
570
+ # Spaces will handle running the server automatically
571
+ app = gradio_app
572
 
573
  if __name__ == "__main__": # pragma: no cover
574
+ # For local testing only - Spaces handles server startup
575
+ app.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))