Skip to content

Quick fix to gr.utils.validate_url#2659

Merged
abidlabs merged 4 commits into
mainfrom
fix-validate
Nov 16, 2022
Merged

Quick fix to gr.utils.validate_url#2659
abidlabs merged 4 commits into
mainfrom
fix-validate

Conversation

@abidlabs
Copy link
Copy Markdown
Member

@abidlabs abidlabs commented Nov 16, 2022

Context: we use gr.utils.validate_url to determine if a string is a URL or a filepath. The logic is quite simple -- we make a GET request to the URL and see if we get a result.

However, some common media hosting sites (e.g. WikiCommons) require a User Agent to be set when making a request -- otherwise, the request is immediately rejected. This fact causes gr.utils.validate_url to fail for these URLs. In addition, since we were checking for a strict 200 response, URLs that redirected to other URLs would fail.

This PR fixes those issues and add tests.

Code to reproduce:

import gradio as gr

with gr.Blocks() as demo:
    gr.Audio("https://upload.wikimedia.org/wikipedia/commons/e/ef/Yellowstone_sound_library_-_Grizzly_Bears_Roar_-_001.mp3")
    
demo.launch()

@github-actions
Copy link
Copy Markdown
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2659-all-demos

Copy link
Copy Markdown
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.

Thanks @abidlabs !

Comment thread test/test_utils.py
assert sanitize_list_for_csv([1, ["ab", "=de"]]) == [1, ["ab", "'=de"]]


class TestValidateURL:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should probably mark as flaky if for whatever reason these urls are unavailable

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks @freddyaboulton!

@abidlabs abidlabs merged commit 8ec0a1e into main Nov 16, 2022
@abidlabs abidlabs deleted the fix-validate branch November 16, 2022 20:21
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.

2 participants