HFShishir commited on
Commit
bed1bc0
·
verified ·
1 Parent(s): dacba0a

Export app without blocking main thread for ZeroGPU

Browse files
Files changed (1) hide show
  1. gradio_app.py +6 -8
gradio_app.py CHANGED
@@ -51,7 +51,8 @@ fastapi_app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handle
51
  @fastapi_app.middleware("http")
52
  async def app_secret_security_middleware(request: Request, call_next):
53
  path = request.url.path
54
- if path in ["/", "/health", "/docs", "/openapi.json", "/redoc", "/gradio"]:
 
55
  return await call_next(request)
56
 
57
  app_secret = request.headers.get("X-App-Secret")
@@ -121,10 +122,7 @@ with gr.Blocks(title="BytePulse AI Backend", theme=gr.themes.Soft()) as demo:
121
  out = gr.Textbox()
122
  btn.click(fn=dummy_gpu_func, inputs=out, outputs=out)
123
 
124
- # Mount Gradio inside FastAPI under /gradio path so that root routes belong to FastAPI
125
- app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
126
-
127
- # Run uvicorn server directly at module load time (without __name__ == '__main__')
128
- # This blocks the importer thread and keeps the Space container running on port 7860!
129
- import uvicorn
130
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
51
  @fastapi_app.middleware("http")
52
  async def app_secret_security_middleware(request: Request, call_next):
53
  path = request.url.path
54
+ # Allow essential routes without secret header
55
+ if path in ["/", "/health", "/docs", "/openapi.json", "/redoc"]:
56
  return await call_next(request)
57
 
58
  app_secret = request.headers.get("X-App-Secret")
 
122
  out = gr.Textbox()
123
  btn.click(fn=dummy_gpu_func, inputs=out, outputs=out)
124
 
125
+ # Mount Gradio at root '/' (FastAPI routes like /health, /api/v1 are registered on fastapi_app directly)
126
+ # Under Gradio SDK, the space runner imports the returned ASGI app 'app' and starts it.
127
+ # We do not block the thread by calling uvicorn.run or demo.launch().
128
+ app = gr.mount_gradio_app(fastapi_app, demo, path="/")