Spaces:
Running
Running
Empty PR for testing
#9
by
gneubig
- opened
app.py
CHANGED
|
@@ -379,37 +379,14 @@ with demo.route("About", "/about"):
|
|
| 379 |
logger.info("All routes configured")
|
| 380 |
|
| 381 |
# Mount the REST API on /api
|
| 382 |
-
from fastapi import FastAPI
|
| 383 |
-
from fastapi.responses import RedirectResponse
|
| 384 |
-
from starlette.middleware.base import BaseHTTPMiddleware
|
| 385 |
from api import api_app
|
| 386 |
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
"""Middleware to redirect root path "/" to "/home".
|
| 390 |
-
|
| 391 |
-
This fixes the 307 trailing slash redirect issue (Gradio bug #11071) that
|
| 392 |
-
occurs when Gradio is mounted at "/" - FastAPI's default behavior redirects
|
| 393 |
-
"/" to "//", which breaks routing on HuggingFace Spaces.
|
| 394 |
-
|
| 395 |
-
See: https://github.com/gradio-app/gradio/issues/11071
|
| 396 |
-
"""
|
| 397 |
-
async def dispatch(self, request: Request, call_next):
|
| 398 |
-
if request.url.path == "/":
|
| 399 |
-
return RedirectResponse(url="/home", status_code=302)
|
| 400 |
-
return await call_next(request)
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
# Create a parent FastAPI app with redirect_slashes=False to prevent
|
| 404 |
-
# automatic trailing slash redirects that cause issues with Gradio
|
| 405 |
-
root_app = FastAPI(redirect_slashes=False)
|
| 406 |
-
|
| 407 |
-
# Add middleware to handle root path redirect to /home
|
| 408 |
-
root_app.add_middleware(RootRedirectMiddleware)
|
| 409 |
-
|
| 410 |
root_app.mount("/api", api_app)
|
| 411 |
|
| 412 |
-
# Mount Gradio app
|
| 413 |
app = gr.mount_gradio_app(root_app, demo, path="/")
|
| 414 |
logger.info("REST API mounted at /api, Gradio app mounted at /")
|
| 415 |
|
|
|
|
| 379 |
logger.info("All routes configured")
|
| 380 |
|
| 381 |
# Mount the REST API on /api
|
| 382 |
+
from fastapi import FastAPI
|
|
|
|
|
|
|
| 383 |
from api import api_app
|
| 384 |
|
| 385 |
+
# Create a parent FastAPI app that will host both the API and Gradio
|
| 386 |
+
root_app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
root_app.mount("/api", api_app)
|
| 388 |
|
| 389 |
+
# Mount Gradio app - root redirect is handled by the proxy
|
| 390 |
app = gr.mount_gradio_app(root_app, demo, path="/")
|
| 391 |
logger.info("REST API mounted at /api, Gradio app mounted at /")
|
| 392 |
|