adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
60 lines
1.3 KiB
Python
60 lines
1.3 KiB
Python
"""
|
|
Test script for https://github.com/gradio-app/gradio/issues/11848
|
|
Gradio does not show media when FastAPI is behind a reverse proxy (root_path).
|
|
|
|
Simulates a reverse proxy by wrapping the FastAPI app in an outer
|
|
Starlette app mounted at /myapp. Gradio is mounted at /gradio inside.
|
|
|
|
Open http://localhost:8000/myapp/gradio/ to test.
|
|
"""
|
|
|
|
import os
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from starlette.applications import Starlette
|
|
from starlette.routing import Mount
|
|
import gradio as gr
|
|
|
|
CUSTOM_PATH = "/gradio"
|
|
PROXY_PREFIX = "/myapp"
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/")
|
|
def read_main():
|
|
return {"message": "This is your main app"}
|
|
|
|
|
|
def identity(image):
|
|
return image
|
|
|
|
|
|
demo = gr.Interface(
|
|
fn=identity,
|
|
inputs=gr.Image(type="filepath"),
|
|
outputs=gr.Image(label="Output Image"),
|
|
)
|
|
|
|
app = gr.mount_gradio_app(app, demo, path=CUSTOM_PATH, root_path=f"{PROXY_PREFIX}{CUSTOM_PATH}")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(outer_app):
|
|
async with app.router.lifespan_context(app):
|
|
yield
|
|
|
|
|
|
outer_app = Starlette(
|
|
routes=[Mount(PROXY_PREFIX, app=app)],
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
port = int(os.environ.get("GRADIO_SERVER_PORT", 8000))
|
|
print(f"\nOpen http://localhost:{port}{PROXY_PREFIX}{CUSTOM_PATH}/\n")
|
|
uvicorn.run(outer_app, host="0.0.0.0", port=port)
|