Skip to content

Fix path generation for returned files for bash API #10841

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

Merged
merged 9 commits into from
Mar 21, 2025

Conversation

cansik
Copy link
Contributor

@cansik cansik commented Mar 19, 2025

Description

As explained in #9049 (comment), the root_path calculation was limited to /gradio_api/queue/join requests, which led to incorrect string manipulation for calls to /gradio_api/call/predict (used by the Bash API).

In this PR, a new method is implemented to check whether the request was made to /gradio_api/queue/join or /gradio_api/call and then correctly calculates the root_path to subtract from the URL. This ensures that calls to any /gradio_api/call/* route are handled correctly (e.g., when using Gradio blocks).

Closes: #9049
Closes: #9317
Closes: #10839

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 19, 2025

🪼 branch checks and previews

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

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/70fc8e55892c548ac3dce4f67ff2be06f9d83b49/gradio-5.22.0-py3-none-any.whl

Install Gradio Python Client from this PR

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

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/70fc8e55892c548ac3dce4f67ff2be06f9d83b49/gradio-client-1.13.1.tgz

Use Lite from this PR

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

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 19, 2025

🦄 change detected

This Pull Request includes changes to the following packages.

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

With the following changelog entry.

Fix path generation for returned files for bash API

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.

@cansik
Copy link
Contributor Author

cansik commented Mar 20, 2025

I am trying to fix the issues with the tests, but I don't understand why this test should be considered correct.

# input
(
    "https://www.gradio.app/playground/",
    f"{API_PREFIX}/",
    "http://www.gradio.app/",
    "http://www.gradio.app"
)

# test call
def test_get_root_url(
    request_url: str, route_path: str, root_path: str, expected_root_url: str
):

What is the expected behavior here? That https://www.gradio.app/playground/ turns into http://www.gradio.app by subtracting /gradio_api? This "works" only because {API_PREFIX}/ and playground/ have the same character length:

/gradio_api  
playground/

I think this is a case where the method should fail rather than pass. But would this impact Gradio's functionality?

For example, this input wouldn't work:

(
    "https://www.gradio.app/playground/",
    f"{API_PREFIX}/call/predict",
    "/playground",
    "https://www.gradio.app/playground",
)

# results in: 'https://w/playground'

@abidlabs What is the expected behaviour for the get_root_url method?

@cansik
Copy link
Contributor Author

cansik commented Mar 20, 2025

Also in the test_events.TestEvent.test_event_data the same issue seems to happen:

# inputs
root_path = ''
root_url = 'http://127.0.0.1:7860/gradio_api/run/predict'
route_path = '/gradio_api/api/predict'

# The two strings have the same lenght, that's why it worked till now...
/gradio_api/run/predict
/gradio_api/api/predict

@cansik
Copy link
Contributor Author

cansik commented Mar 20, 2025

I have now removed the exception from the get_root_url method since fixing it would take much longer and is out of scope for this PR. However, this seems to be a serious issue, and it feels like we are just lucky that strings tend to have the same lengths. I will create a separate issue to track and discuss this problem.

@abidlabs
Copy link
Member

Taking a look at this now @cansik

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

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

Fantastic PR @cansik! I reviewed it and the changes look precisely correct. I also went ahead and updated the api docs snippet so that this closes #10839 as well

@abidlabs
Copy link
Member

What is the expected behavior here? That https://www.gradio.app/playground/ turns into http://www.gradio.app by subtracting /gradio_api? This "works" only because {API_PREFIX}/ and playground/ have the same character length:

No the reason this test passes is because if a developer specifies a full URL as the root_path, it takes precedence over everything else and and the stripped version of that url is returned by the get_root_url. So in this case http://www.gradio.app/ -> http://www.gradio.app. Your PR doesn't affect this test.

@abidlabs abidlabs enabled auto-merge (squash) March 21, 2025 12:15
@abidlabs abidlabs merged commit 8ff0a5e into gradio-app:main Mar 21, 2025
22 of 23 checks passed
@tqbl
Copy link

tqbl commented Mar 25, 2025

@cansik I'm having issues with this change. The get_request_url function is supposed to return the request URL, but when the X-Forwarded-Host header is present, the root URL ({scheme}://{host}) is returned instead. The latter causes get_api_call_path to raise an exception, because it's expecting the path component to be present in the URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants