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

Fix FastAPI route addition - move outside Blocks context

Browse files
Files changed (1) hide show
  1. app.py +45 -34
app.py CHANGED
@@ -411,43 +411,54 @@ with gr.Blocks(title="Router Model API - ZeroGPU") as gradio_app:
411
  inputs=[prompt_input, max_tokens_input, temp_input, top_p_input],
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
451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452
  if __name__ == "__main__": # pragma: no cover
453
  app.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
 
411
  inputs=[prompt_input, max_tokens_input, temp_input, top_p_input],
412
  outputs=output,
413
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
 
415
  # Set app to Gradio Blocks for Spaces - ZeroGPU requires Gradio SDK
416
  app = gradio_app
417
 
418
+ # Add FastAPI routes directly to Gradio's app after Blocks context exits
419
+ # Use load event to ensure app is ready
420
+ def add_fastapi_routes():
421
+ """Add FastAPI routes to Gradio's underlying FastAPI app."""
422
+ try:
423
+ from fastapi.responses import JSONResponse
424
+
425
+ @app.app.post("/v1/generate")
426
+ async def fastapi_generate_endpoint(request):
427
+ """FastAPI endpoint mounted in Gradio app."""
428
+ from fastapi import Request
429
+ try:
430
+ data = await request.json()
431
+ payload = GeneratePayload(**data)
432
+ text = _generate_with_gpu(
433
+ prompt=payload.prompt,
434
+ max_new_tokens=payload.max_new_tokens or MAX_NEW_TOKENS,
435
+ temperature=payload.temperature or DEFAULT_TEMPERATURE,
436
+ top_p=payload.top_p or DEFAULT_TOP_P,
437
+ )
438
+ return JSONResponse(content={"text": text})
439
+ except Exception as exc:
440
+ from fastapi import HTTPException
441
+ raise HTTPException(status_code=500, detail=str(exc))
442
+
443
+ @app.app.get("/gradio")
444
+ async def fastapi_gradio_ui():
445
+ """FastAPI /gradio endpoint."""
446
+ return HTMLResponse(interactive_ui())
447
+
448
+ @app.app.get("/")
449
+ async def fastapi_healthcheck():
450
+ """FastAPI healthcheck endpoint."""
451
+ return {
452
+ "status": "ok",
453
+ "model": MODEL_ID,
454
+ "strategy": ACTIVE_STRATEGY or "pending",
455
+ }
456
+ print("FastAPI routes added successfully")
457
+ except Exception as e:
458
+ print(f"Warning: Could not add FastAPI routes: {e}")
459
+
460
+ # Use load event to add routes when app is ready
461
+ app.load(add_fastapi_routes)
462
+
463
  if __name__ == "__main__": # pragma: no cover
464
  app.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))