From 477f45cb43be957684eb392e3d62c09490c22391 Mon Sep 17 00:00:00 2001 From: Freddy Boulton Date: Tue, 8 Oct 2024 15:28:05 -0700 Subject: [PATCH] Only move files to the cache that have a meta key (#9589) * Fix code * add changeset * Code * test * tests * add changeset * lint --------- Co-authored-by: gradio-pr-bot --- .changeset/evil-clocks-visit.md | 6 ++++++ client/python/gradio_client/client.py | 5 ++--- gradio/blocks.py | 2 +- gradio/processing_utils.py | 6 ++++-- test/components/test_json.py | 6 ++++++ 5 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .changeset/evil-clocks-visit.md diff --git a/.changeset/evil-clocks-visit.md b/.changeset/evil-clocks-visit.md new file mode 100644 index 0000000000000..c31c3eb685db4 --- /dev/null +++ b/.changeset/evil-clocks-visit.md @@ -0,0 +1,6 @@ +--- +"gradio": minor +"gradio_client": minor +--- + +feat:Only move files to the cache that have a meta key diff --git a/client/python/gradio_client/client.py b/client/python/gradio_client/client.py index f832aa2432cab..157d4184cb2c1 100644 --- a/client/python/gradio_client/client.py +++ b/client/python/gradio_client/client.py @@ -1366,9 +1366,8 @@ def _upload_file(self, f: dict, data_index: int) -> dict[str, str]: # use the suffix of the original name to determine format to save it to in cache. return { "path": file_path, - "orig_name": utils.strip_invalid_filename_characters(orig_name.name) - if orig_name.suffix - else None, + "orig_name": utils.strip_invalid_filename_characters(orig_name.name), + "meta": {"_type": "gradio.FileData"} if orig_name.suffix else None, } def _download_file(self, x: dict) -> str: diff --git a/gradio/blocks.py b/gradio/blocks.py index b3dd69d74dd86..b0babf0672de2 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -384,7 +384,7 @@ def serve_static_file( if client_utils.is_http_url_like(url_or_file_path): return FileData(path=url_or_file_path, url=url_or_file_path).model_dump() else: - data = {"path": url_or_file_path} + data = {"path": url_or_file_path, "meta": {"_type": "gradio.FileData"}} try: return client_utils.synchronize_async( processing_utils.async_move_files_to_cache, data, self diff --git a/gradio/processing_utils.py b/gradio/processing_utils.py index ae3a35674ce25..783b935917900 100644 --- a/gradio/processing_utils.py +++ b/gradio/processing_utils.py @@ -581,7 +581,9 @@ def _move_to_cache(d: dict): if isinstance(data, (GradioRootModel, GradioModel)): data = data.model_dump() - return client_utils.traverse(data, _move_to_cache, client_utils.is_file_obj) + return client_utils.traverse( + data, _move_to_cache, client_utils.is_file_obj_with_meta + ) def _check_allowed(path: str | Path, check_in_upload_folder: bool): @@ -693,7 +695,7 @@ async def _move_to_cache(d: dict): if isinstance(data, (GradioRootModel, GradioModel)): data = data.model_dump() return await client_utils.async_traverse( - data, _move_to_cache, client_utils.is_file_obj + data, _move_to_cache, client_utils.is_file_obj_with_meta ) diff --git a/test/components/test_json.py b/test/components/test_json.py index 5530bd15f31ef..b72c685053cc7 100644 --- a/test/components/test_json.py +++ b/test/components/test_json.py @@ -86,6 +86,12 @@ def get_avg_age_per_gender(data): "O": 20, } + @pytest.mark.asyncio + async def test_dict_with_path_key_not_moved(self): + iface = gr.Interface(lambda x: x, "json", "json") + y_data = {"assets": {"path": "foo"}} + assert (await iface.process_api(0, [y_data]))["data"][0].model_dump() == y_data + @pytest.mark.parametrize( "value, expected", [