Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application root_path not working as it gets discarded by configure_app #9788

Open
1 task done
amol- opened this issue Oct 22, 2024 · 4 comments
Open
1 task done

Application root_path not working as it gets discarded by configure_app #9788

amol- opened this issue Oct 22, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@amol-
Copy link

amol- commented Oct 22, 2024

Describe the bug

The root_path setting is necessary to serve FastAPI applications under subpaths served via a proxy and the ASGI frameworks are expected to strip it from the request path during the routing process, see django/asgiref#229 (comment)

But Gradio currently doesn't handle root_path correctly when set via create_app thus making very hard to serve Gradio applications mounted in subpaths. This is probably related to #8073 #9101 and #9529

When creating a Gradio application, it accepts additional options that are
forwarded to the FastAPI application. This happens at
https://github.com/gradio-app/gradio/blob/main/gradio/routes.py#L236

and is correctly exposed via the create_app factory
https://github.com/gradio-app/gradio/blob/main/gradio/routes.py#L342

But when trying to serve Gradio under subpath, setting the root_path
via create_app doesn't seem to do anything:

app = gr.routes.App.create_app(demo, app_kwargs={"root_path": "/proxy"})

The root_path is actually unset independently from the value provided,
this is caused by the configure_app method invoked by the create_app
factory: https://github.com/gradio-app/gradio/blob/main/gradio/routes.py#L345

The configure_app overwrites the application root_path with whatever
root_path the mounted block has (which is empty), thus making the root_path setting
pointless: https://github.com/gradio-app/gradio/blob/main/gradio/routes.py#L298

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr

def greet(name):
    return "Hello, " + name + "!"

with gr.Blocks() as demo:
    gr.Markdown("Welcome to your Gradio application!")

    name = gr.Textbox(label="Name", elem_id="name")
    greeting = gr.Textbox(label="Greeting", elem_id="greeting")
    button = gr.Button("Greet", elem_id="greet")
    button.click(fn=greet,
                 inputs=name,
                 outputs=greeting,
                 api_name="greet")

app = gr.routes.App.create_app(demo, app_kwargs={"root_path": "/proxy"})

with following NGinx configuration

        location /proxy/ {
              proxy_pass http://127.0.0.1:8000;
              proxy_set_header   Host $host;
              proxy_set_header   X-Real-IP $remote_addr;
              proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header   X-Forwarded-Proto $scheme;
        }

(the same configuration works correctly if it's a FastAPI application instead of a Gradio application)

Screenshot

No response

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 5.2.1
gradio_client version: 1.4.1

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
anyio: 4.6.2.post1
fastapi: 0.115.2
ffmpy: 0.4.0
gradio-client==1.4.1 is not installed.
httpx: 0.27.2
huggingface-hub: 0.25.2
jinja2: 3.1.4
markupsafe: 2.1.5
numpy: 2.1.2
orjson: 3.10.7
packaging: 24.1
pandas: 2.2.3
pillow: 10.4.0
pydantic: 2.9.2
pydub: 0.25.1
python-multipart: 0.0.12
pyyaml: 6.0.2
ruff: 0.6.9
semantic-version: 2.10.0
starlette: 0.40.0
tomlkit==0.12.0 is not installed.
typer: 0.12.5
typing-extensions: 4.12.2
urllib3: 2.2.3
uvicorn: 0.32.0
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.


gradio_client dependencies in your environment:

fsspec: 2024.9.0
httpx: 0.27.2
huggingface-hub: 0.25.2
packaging: 24.1
typing-extensions: 4.12.2
websockets: 12.0

Severity

Blocking usage of gradio

@amol- amol- added the bug Something isn't working label Oct 22, 2024
@cornzz
Copy link
Contributor

cornzz commented Oct 24, 2024

Hi, does this also happen when you do demo.launch(root_path="/proxy") instead of using gr.routes.App.create_app()?

@amol-
Copy link
Author

amol- commented Oct 24, 2024

Hi, does this also happen when you do demo.launch(root_path="/proxy") instead of using gr.routes.App.create_app()?

When using demo.launch it can't happen because you are setting the root_path for the block itself. So while the app gets it overwritten by the block via configure_app it doesn't have any impact as it gets actually overwritten with the expected one.

I think that configure_app should copy the root_path from the block only when it's not empty

@freddyaboulton
Copy link
Collaborator

Hi @amol- why are you calling App.create_app manually? That's not a user-facing class/method . root_path is set via launch as @cornzz commented.

@amol-
Copy link
Author

amol- commented Nov 1, 2024

Hi @amol- why are you calling App.create_app manually? That's not a user-facing class/method . root_path is set via launch as @cornzz commented.

Thanks for your reply!
What I need is to get an ASGI application, as the Gradio application has to be wrapped in custom ASGI middlewares in our environment. App.create_app seemed to be the expected way to go to create an ASGI app, I guess I could have used Block.mount_gradio_app on top of an empty FastAPI app, but that seemed to be more an hack than the way to go.

Also, I haven't tried yet, so I have to confirm this, but it seems to me that the implementation of Block.mount_gradio_app would suffer from the same issue as the app_kwargs provided there seem to suffer from the same overwrite problem by looking at the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants