From 431140b903970d5ab4afa1212da832109f1bfb97 Mon Sep 17 00:00:00 2001 From: Freddy Boulton Date: Wed, 12 Jul 2023 21:46:54 -0400 Subject: [PATCH] Fix bug related to preprocessing URLs for video (#4904) * Support urls * Add unit test * CHANGELOG * Bump client version * use url_like --- CHANGELOG.md | 1 + gradio/components/audio.py | 4 ++-- gradio/components/video.py | 12 +++++++++--- requirements.txt | 2 +- test/test_components.py | 13 +++++++++++++ 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2ec1ae14c..af05f93316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/gradio/components/audio.py b/gradio/components/audio.py index 985b367315..7263c2bf79 100644 --- a/gradio/components/audio.py +++ b/gradio/components/audio.py @@ -194,7 +194,7 @@ class Audio( ) 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) @@ -322,7 +322,7 @@ class Audio( """ 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 diff --git a/gradio/components/video.py b/gradio/components/video.py index 7c475699f4..9c808e6cc7 100644 --- a/gradio/components/video.py +++ b/gradio/components/video.py @@ -204,7 +204,11 @@ class Video( 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)) @@ -312,12 +316,14 @@ class Video( 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() diff --git a/requirements.txt b/requirements.txt index 818bed9c73..faa08fe030 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/test/test_components.py b/test/test_components.py index 4a566a3e44..14af3a5d9b 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -1499,6 +1499,19 @@ class TestVideo: 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):