Spaces:
Sleeping
Sleeping
Export app without blocking main thread for ZeroGPU
Browse files- 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 |
-
|
|
|
|
| 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
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 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="/")
|
|
|
|
|
|
|
|
|