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

Fix root on gradio mounted apps #10882

Merged
merged 7 commits into from
Mar 31, 2025
Merged

Fix root on gradio mounted apps #10882

merged 7 commits into from
Mar 31, 2025

Conversation

aliabid94
Copy link
Collaborator

@aliabid94 aliabid94 commented Mar 25, 2025

When gradio apps are mounted on fastapi and theres a reverse proxy, the root was not correctly pointing to the mounted path of the fastapi app. Fixed now. Fixes: #8073

Test by adding this nginx config:

    server {
        server_name localhost;
        listen 18000;

        location / {
                proxy_pass http://127.0.0.1:8000/;
                proxy_buffering off;
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Host 'localhost:18000';
                proxy_set_header X-Forwarded-Host 'localhost:18000';
                proxy_set_header X-Forwarded-Proto $scheme;

        }
    }

and this app:

import gradio as gr

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


with gr.Blocks() as demo:
    name = gr.Textbox(label="Name")
    output = gr.Textbox(label="Output Box")
    greet_btn = gr.Button("Greet")
    greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")

from fastapi import FastAPI
app = FastAPI()

gr.mount_gradio_app(app, demo, path="/gradio")

# run app
import uvicorn
uvicorn.run(app)

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 25, 2025

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/7fe006e83dfa8da4cd41360e400886be2fc2d791/gradio-5.23.1-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@7fe006e83dfa8da4cd41360e400886be2fc2d791#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/7fe006e83dfa8da4cd41360e400886be2fc2d791/gradio-client-1.14.0.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/7fe006e83dfa8da4cd41360e400886be2fc2d791/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 25, 2025

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Fix root on gradio mounted apps

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just verified the fix nice @aliabid94 ! I think we can remove the print?

@aliabid94 aliabid94 enabled auto-merge (squash) March 25, 2025 23:27
@abidlabs abidlabs disabled auto-merge March 26, 2025 00:25
@abidlabs
Copy link
Member

Any way we could add a test for this?

@aliabid94
Copy link
Collaborator Author

Any way we could add a test for this?

There needs to be an nginx setup for reverse proxying, which would be pretty hard :/

@freddyaboulton
Copy link
Collaborator

An idea: We can host a space in the gradio-test org that is an app behind nginx and then we have a flaky unit test that ensures the events work (and maybe that the css is properly loaded) by calling via the client/requests module.

But I don't think we should do this until we fix most of the "cloud" related issues and can come up with the most representative "test cases"

@abidlabs
Copy link
Member

abidlabs commented Mar 31, 2025

I like that, and perhaps we can also write a simpler unit test that runs locally that "simulates" an nginx reverse proxy using two FastAPI apps. The idea would be to have:

  • FastAPI app 1 running on port 8000, accepts all inbound network requests and then attaches a X-Forwarded-Host header, and then forwards them to a localhost:8001/gradio
  • FastAPI app 2 running on port 8001 which has a mounted Gradio app at /gradio. This FastAPI app should explicitly reject all inbound connections that don't have a X-Forwarded-Host. So only connections that come through FastAPI app 1 should pass through to FastAPI app 2.

Then we basically see if we can make requests to localhost:8000 and see if they get completed successfully

@abidlabs
Copy link
Member

Ok with merging this for now though and like @freddyaboulton said, we can add the tests once we've fixed more cloud issues.

@abidlabs abidlabs merged commit cd7f486 into main Mar 31, 2025
22 checks passed
@abidlabs abidlabs deleted the fix_root_mount branch March 31, 2025 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gradio mounted with fastapi via https in nginx returns wrong path
4 participants