From b7a7e590945e938714a5c9a8694d4f188d54b314 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Thu, 23 Jan 2025 08:35:53 -0800 Subject: [PATCH] Support presigned URLs with gr.Video, gr.Model3D, and other components (#10406) * thanks cursor * add changeset * add changeset * add changeset * changes * changes * video * changes --------- Co-authored-by: gradio-pr-bot --- .changeset/lemon-hotels-retire.md | 5 +++++ gradio/processing_utils.py | 11 +++++++---- test/components/test_video.py | 11 +++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 .changeset/lemon-hotels-retire.md diff --git a/.changeset/lemon-hotels-retire.md b/.changeset/lemon-hotels-retire.md new file mode 100644 index 0000000000000..95263b970895f --- /dev/null +++ b/.changeset/lemon-hotels-retire.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Support presigned URLs with gr.Video, gr.Model3D, and other components diff --git a/gradio/processing_utils.py b/gradio/processing_utils.py index c8e3fabd0ce32..02a8e9ab06965 100644 --- a/gradio/processing_utils.py +++ b/gradio/processing_utils.py @@ -328,15 +328,18 @@ def wrapper(*args: Any, **kwargs: Any) -> Awaitable[T]: async def async_ssrf_protected_download(url: str, cache_dir: str) -> str: temp_dir = Path(cache_dir) / hash_url(url) temp_dir.mkdir(exist_ok=True, parents=True) - filename = client_utils.strip_invalid_filename_characters(Path(url).name) - full_temp_file_path = str(abspath(temp_dir / filename)) + parsed_url = urlparse(url) + base_path = parsed_url.path.rstrip("/") + filename = ( + client_utils.strip_invalid_filename_characters(Path(base_path).name) or "file" + ) + + full_temp_file_path = str(abspath(temp_dir / filename)) if Path(full_temp_file_path).exists(): return full_temp_file_path - parsed_url = urlparse(url) hostname = parsed_url.hostname - response = await sh.get( url, domain_whitelist=PUBLIC_HOSTNAME_WHITELIST, _transport=async_transport ) diff --git a/test/components/test_video.py b/test/components/test_video.py index 072aac6c3d590..cc56fd7247519 100644 --- a/test/components/test_video.py +++ b/test/components/test_video.py @@ -99,10 +99,21 @@ async def test_component_functions(self): output_with_subtitles = output_with_subtitles.model_dump() assert output_with_subtitles["subtitles"]["path"].endswith(".vtt") + video = gr.Video(format="wav") + video_url_with_query_param = "https://github.com/gradio-app/gradio/raw/refs/heads/main/test/test_files/playable_but_bad_container.mp4?query=fake" + postprocessed_video_with_query_param = video.postprocess( + video_url_with_query_param + ) + assert postprocessed_video_with_query_param + assert postprocessed_video_with_query_param.model_dump()["video"][ + "path" + ].endswith("playable_but_bad_container.wav") + p_video = gr.Video() video_with_subtitle = gr.Video() postprocessed_video = p_video.postprocess(Path(y_vid_path)) assert postprocessed_video + postprocessed_video = postprocessed_video.model_dump() postprocessed_video_with_subtitle = video_with_subtitle.postprocess( (Path(y_vid_path), Path(subtitles_path))