-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 validate url #2957
Fix validate url #2957
Conversation
All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2957-all-demos |
e344cfd
to
756a7e9
Compare
This works great @freddyaboulton, nice catch! Regarding the speed issue, we could change the def validate_url(possible_url: str) -> bool:
headers = {"User-Agent": "gradio (https://gradio.app/; team@gradio.app)"}
try:
return requests.head(possible_url, headers=headers).ok
except Exception:
return False Thoughts? A quick benchmark shows that the time to run import requests
import time
t = time.time()
headers = {"User-Agent": "gradio (https://gradio.app/; team@gradio.app)"}
requests.get("https://edmullen.net/test/rc.jpg", headers=headers).ok
print(time.time() - t)
t = time.time()
requests.head("https://edmullen.net/test/rc.jpg", headers=headers).ok
print(time.time() - t) |
756a7e9
to
77d5790
Compare
Thanks for the suggestion @abidlabs ! The one snag is that the gradio file route does not support head requests. I made that addition to the routes.py file but that means that we will first try Let me know what you think before I merge! |
Great catch @freddyaboulton, LGTM |
Thank you for the re-review! |
Description
ImgSerializable.serialize
would fail on an image url becausePath(load_dir) / x
would strip out some of the slashes in the url which would cause the url to be treated as a file.Doing
Path(load_dir) / x
basically assumesx
is a filepath instead of a url but that has not been determined at the time that line is reached so I refactored the logic a bit so thatPath(load_dir) / x
is only called ifx
is not a url.This comes at the cost of two get requests to the external url but the benefit is that we don't have to modify any of the public apis of our util functions.
Checklist:
A note about the CHANGELOG
Hello 👋 and thank you for contributing to Gradio!
All pull requests must update the change log located in CHANGELOG.md, unless the pull request is labeled with the "no-changelog-update" label.
Please add a brief summary of the change to the Upcoming Release > Full Changelog section of the CHANGELOG.md file and include
a link to the PR (formatted in markdown) and a link to your github profile (if you like). For example, "* Added a cool new feature by
[@myusername](link-to-your-github-profile)
in[PR 11111](https://github.com/gradio-app/gradio/pull/11111)
".If you would like to elaborate on your change further, feel free to include a longer explanation in the other sections.
If you would like an image/gif/video showcasing your feature, it may be best to edit the CHANGELOG file using the
GitHub web UI since that lets you upload files directly via drag-and-drop.