Spaces:
Sleeping
Sleeping
Mount backend sub-app at root level and prepend mount route priority
Browse files- gradio_app.py +18 -13
gradio_app.py
CHANGED
|
@@ -35,7 +35,7 @@ async def lifespan(app: FastAPI):
|
|
| 35 |
yield
|
| 36 |
stop_scheduler()
|
| 37 |
|
| 38 |
-
# Initialize
|
| 39 |
backend_app = FastAPI(
|
| 40 |
title=settings.APP_NAME,
|
| 41 |
version=settings.VERSION,
|
|
@@ -88,13 +88,13 @@ backend_app.add_middleware(
|
|
| 88 |
allow_headers=["*"],
|
| 89 |
)
|
| 90 |
|
| 91 |
-
# Custom /health endpoint
|
| 92 |
@backend_app.get("/health")
|
| 93 |
@limiter.limit("60/minute")
|
| 94 |
def health_check(request: Request):
|
| 95 |
return {"status": "healthy", "service": settings.APP_NAME, "security": "rate_limited_enabled"}
|
| 96 |
|
| 97 |
-
# Include API v1 Router on backend
|
| 98 |
backend_app.include_router(api_router, prefix="/api/v1")
|
| 99 |
|
| 100 |
# Dummy function decorated with GPU to satisfy Hugging Face ZeroGPU requirement
|
|
@@ -121,16 +121,21 @@ 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 our backend
|
| 125 |
-
#
|
| 126 |
-
demo.app
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
demo.app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# Launch the Gradio app blocks directly (Gradio reads port 7860 from env and starts the server)
|
| 136 |
demo.launch()
|
|
|
|
| 35 |
yield
|
| 36 |
stop_scheduler()
|
| 37 |
|
| 38 |
+
# Initialize unified backend FastAPI app
|
| 39 |
backend_app = FastAPI(
|
| 40 |
title=settings.APP_NAME,
|
| 41 |
version=settings.VERSION,
|
|
|
|
| 88 |
allow_headers=["*"],
|
| 89 |
)
|
| 90 |
|
| 91 |
+
# Custom /health endpoint on backend app
|
| 92 |
@backend_app.get("/health")
|
| 93 |
@limiter.limit("60/minute")
|
| 94 |
def health_check(request: Request):
|
| 95 |
return {"status": "healthy", "service": settings.APP_NAME, "security": "rate_limited_enabled"}
|
| 96 |
|
| 97 |
+
# Include API v1 Router on backend app
|
| 98 |
backend_app.include_router(api_router, prefix="/api/v1")
|
| 99 |
|
| 100 |
# Dummy function decorated with GPU to satisfy Hugging Face ZeroGPU requirement
|
|
|
|
| 121 |
out = gr.Textbox()
|
| 122 |
btn.click(fn=dummy_gpu_func, inputs=out, outputs=out)
|
| 123 |
|
| 124 |
+
# Mount our custom backend app inside Gradio's internal FastAPI app at root '/'
|
| 125 |
+
# Wait! Since Gradio mounts its catch-all routes directly on the root level, we can prepend the custom mounted routes list.
|
| 126 |
+
# We mount our backend_app on demo.app at '/' which merges them!
|
| 127 |
+
# To do this correctly, we can mount backend_app at '/' and then prepend the newly added mount route in demo.app.router.routes!
|
| 128 |
+
pre_insertion_route_count = len(demo.app.router.routes)
|
| 129 |
+
|
| 130 |
+
# Mount backend sub-app on demo.app
|
| 131 |
+
demo.app.mount("/", backend_app)
|
| 132 |
+
|
| 133 |
+
# Prepend the newly added mount route to the beginning of demo.app's routing list
|
| 134 |
+
# This guarantees that FastAPI matches our mounted '/health' and '/api/v1/...' BEFORE Gradio's catch-all wildcard.
|
| 135 |
+
new_route_count = len(demo.app.router.routes) - pre_insertion_route_count
|
| 136 |
+
if new_route_count > 0:
|
| 137 |
+
demo.app.router.routes = demo.app.router.routes[-new_route_count:] + demo.app.router.routes[:-new_route_count]
|
| 138 |
+
logger.info("Successfully prepended custom FastAPI sub-app mount route in Gradio router.")
|
| 139 |
|
| 140 |
# Launch the Gradio app blocks directly (Gradio reads port 7860 from env and starts the server)
|
| 141 |
demo.launch()
|