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

Stop caching root url #7374

Merged
merged 11 commits into from Feb 9, 2024
Merged

Stop caching root url #7374

merged 11 commits into from Feb 9, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Feb 9, 2024

Fixes: #7372

The issue was that we were caching the "root_url" the first time the Gradio app was launched. I did this to speed things up, but the problem is that the same Gradio app can be launched on different URLs. For example, when you create a share link, you also might launch the Gradio app locally first (which would set the root_url to "localhost:7860") and then when you open the share link, the "root_url" will remain localhost:7860.

Now, we no longer cache the root url. I profiled this change and it adds pretty negligible latency for most demos (e.g. 2 ms for kitchen_sink). Does add more latency for very complex apps (150 ms for the theme_builder) so maybe we should think of a better solution long-term (when we work on reducing the launch time).

Also properly handles https & does some minor cleanup of this code.

You can confirm this works by visiting e.g. https://gradio-pr-deploys-pr-7374-all-demos.hf.space/demo/altair_plot/config and checking the "root" propery. Or to test locally, launch a Gradio demo with share=True and visit the /config route and confirm that the "root" property is what you expect.

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Feb 9, 2024

🪼 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-builds.s3.amazonaws.com/fbfc2ba0f49759a92c639a6ba10ddf9b7c33b648/gradio-4.17.0-py3-none-any.whl

Install Gradio Python Client from this PR

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

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Feb 9, 2024

🦄 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.

Stop caching root url

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.

@abidlabs abidlabs changed the title Stop caching root Stop caching root url Feb 9, 2024
@abidlabs abidlabs marked this pull request as ready for review February 9, 2024 18:12
@abidlabs abidlabs marked this pull request as draft February 9, 2024 18:15
@abidlabs
Copy link
Member Author

abidlabs commented Feb 9, 2024

Actually wait I think the cached examples are off

@abidlabs abidlabs marked this pull request as ready for review February 9, 2024 18:29
@abidlabs
Copy link
Member Author

abidlabs commented Feb 9, 2024

Should be ready now!

@@ -571,8 +570,8 @@ async def predict(
content={"error": str(error) if show_error else None},
status_code=500,
)
root_path = app.get_blocks().config.get("root", "")
output = add_root_url(output, route_utils.strip_url(root_path))
root_path = route_utils.get_root_url(request)[: -len(f"/api/{api_name}")]
Copy link
Member Author

@abidlabs abidlabs Feb 9, 2024

Choose a reason for hiding this comment

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

Annoying to do this manually, but if you use e.g. request.url.path, it also includes the root url if gradio is mounted within another fastapi app, which we don't want. So I think the most reliable solution is to just remove the suffix manually.

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.

Looks good to me! Thanks @abidlabs ! Tested several demos and the root was properly set in all of them.

@abidlabs
Copy link
Member Author

abidlabs commented Feb 9, 2024

Thanks so much for the review @freddyaboulton!

@abidlabs abidlabs merged commit 7f19ba2 into main Feb 9, 2024
7 checks passed
@abidlabs abidlabs deleted the no-cache-root branch February 9, 2024 23:26
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.

When running on Spaces or using share links, root_url is set to localhost instead of the full URL breaking css
3 participants