Skip to content

Commit

Permalink
Fix bug related to preprocessing URLs for video (#4904)
Browse files Browse the repository at this point in the history
* Support urls

* Add unit test

* CHANGELOG

* Bump client version

* use url_like
  • Loading branch information
freddyaboulton authored Jul 13, 2023
1 parent d3e1d27 commit 431140b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

* The `.change()` event is fixed in `Video` and `Image` so that it only fires once by [@abidlabs](https://github.com/abidlabs) in [PR 4793](https://github.com/gradio-app/gradio/pull/4793)
* The `.change()` event is fixed in `Audio` so that fires when the component value is programmatically updated by [@abidlabs](https://github.com/abidlabs) in [PR 4793](https://github.com/gradio-app/gradio/pull/4793)
- Fixed bug where `gr.Video` could not preprocess urls by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 4904](https://github.com/gradio-app/gradio/pull/4904)

## Other Changes:

Expand Down
4 changes: 2 additions & 2 deletions gradio/components/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def preprocess(
)
crop_min, crop_max = x.get("crop_min", 0), x.get("crop_max", 100)
if is_file:
if utils.validate_url(file_name):
if client_utils.is_http_url_like(file_name):
temp_file_path = self.download_temp_copy_if_needed(file_name)
else:
temp_file_path = self.make_temp_copy_if_needed(file_name)
Expand Down Expand Up @@ -322,7 +322,7 @@ def postprocess(
"""
if y is None:
return None
if isinstance(y, str) and utils.validate_url(y):
if isinstance(y, str) and client_utils.is_http_url_like(y):
return {"name": y, "data": None, "is_file": True}
if isinstance(y, tuple):
sample_rate, data = y
Expand Down
12 changes: 9 additions & 3 deletions gradio/components/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ def preprocess(

if is_file:
assert file_name is not None, "Received file data without a file name."
file_name = Path(self.make_temp_copy_if_needed(file_name))
if client_utils.is_http_url_like(file_name):
fn = self.download_temp_copy_if_needed
else:
fn = self.make_temp_copy_if_needed
file_name = Path(fn(file_name))
else:
assert file_data is not None, "Received empty file data."
file_name = Path(self.base64_to_temp_file_if_needed(file_data, file_name))
Expand Down Expand Up @@ -312,12 +316,14 @@ def _format_video(self, video: str | Path | None) -> FileData | None:
else:
conversion_needed = True

is_url = client_utils.is_http_url_like(video)

# For cases where the video is a URL and does not need to be converted to another format, we can just return the URL
if utils.validate_url(video) and not (conversion_needed):
if is_url and not (conversion_needed):
return {"name": video, "data": None, "is_file": True}

# For cases where the video needs to be converted to another format
if utils.validate_url(video):
if is_url:
video = self.download_temp_copy_if_needed(video)
if (
processing_utils.ffmpeg_installed()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ aiohttp~=3.0
altair>=4.2.0,<6.0
fastapi
ffmpy
gradio_client>=0.2.7
gradio_client>=0.2.9
httpx
huggingface_hub>=0.14.0
Jinja2<4.0
Expand Down
13 changes: 13 additions & 0 deletions test/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,19 @@ def test_video_preprocessing_flips_video_for_webcam(self, mock_ffmpeg):
assert ".avi" in list(output_params.keys())[0]
assert ".avi" in output_file

@pytest.mark.flaky
def test_preprocess_url(self):
output = gr.Video().preprocess(
{
"name": "https://gradio-builds.s3.amazonaws.com/demo-files/a.mp4",
"is_file": True,
"data": None,
"size": None,
"orig_name": "https://gradio-builds.s3.amazonaws.com/demo-files/a.mp4",
}
)
assert Path(output).name == "a.mp4" and not client_utils.probe_url(output)


class TestTimeseries:
def test_component_functions(self):
Expand Down

1 comment on commit 431140b

@vercel
Copy link

@vercel vercel bot commented on 431140b Jul 13, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.