From bc61ff6b1603eedf3111f1b5c3d2751629902d98 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Thu, 14 Mar 2024 17:19:15 -0700 Subject: [PATCH] Several fixes to `gr.load` (#7706) * stash pop * add changeset * add changeset * state fix * add changeset * push * ignore * format --------- Co-authored-by: gradio-pr-bot --- .changeset/sour-lizards-follow.md | 6 ++++++ client/python/gradio_client/client.py | 8 +++++++- gradio/blocks.py | 8 ++++++-- gradio/components/dataset.py | 18 +++++++++++------- gradio/helpers.py | 5 +++-- 5 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 .changeset/sour-lizards-follow.md diff --git a/.changeset/sour-lizards-follow.md b/.changeset/sour-lizards-follow.md new file mode 100644 index 0000000000..d25f446a2e --- /dev/null +++ b/.changeset/sour-lizards-follow.md @@ -0,0 +1,6 @@ +--- +"gradio": patch +"gradio_client": patch +--- + +fix:Several fixes to `gr.load` diff --git a/client/python/gradio_client/client.py b/client/python/gradio_client/client.py index c732550e4a..aee2bc45b3 100644 --- a/client/python/gradio_client/client.py +++ b/client/python/gradio_client/client.py @@ -1115,7 +1115,13 @@ class Endpoint: return tuple(data) def reduce_singleton_output(self, *data) -> Any: - if len([oct for oct in self.output_component_types if not oct.skip]) == 1: + if self.client._skip_components: + effective_output_components = [ + o for o in self.output_component_types if not o.skip + ] + else: + effective_output_components = self.output_component_types + if len(effective_output_components) == 1: return data[0] else: return data diff --git a/gradio/blocks.py b/gradio/blocks.py index 1335985ed3..7292030956 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -272,7 +272,9 @@ class Block: return temp_file_path - def serve_static_file(self, url_or_file_path: str | Path | None) -> dict | None: + def serve_static_file( + self, url_or_file_path: str | Path | dict | None + ) -> dict | None: """If a file is a local file, moves it to the block's cache directory and returns a FileData-type dictionary corresponding to the file. If the file is a URL, returns a FileData-type dictionary corresponding to the URL. This ensures that the file is @@ -281,12 +283,14 @@ class Block: Examples: >>> block.serve_static_file("https://gradio.app/logo.png") -> {"path": "https://gradio.app/logo.png", "url": "https://gradio.app/logo.png"} >>> block.serve_static_file("logo.png") -> {"path": "logo.png", "url": "/file=logo.png"} + >>> block.serve_static_file({"path": "logo.png", "url": "/file=logo.png"}) -> {"path": "logo.png", "url": "/file=logo.png"} """ if url_or_file_path is None: return None + if isinstance(url_or_file_path, dict): + return url_or_file_path if isinstance(url_or_file_path, Path): url_or_file_path = str(url_or_file_path) - 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: diff --git a/gradio/components/dataset.py b/gradio/components/dataset.py index 1dc648d45e..25b05efbb2 100644 --- a/gradio/components/dataset.py +++ b/gradio/components/dataset.py @@ -27,6 +27,7 @@ class Dataset(Component): *, label: str | None = None, components: list[Component] | list[str], + component_props: list[dict[str, Any]] | None = None, samples: list[list[Any]] | None = None, headers: list[str] | None = None, type: Literal["values", "index"] = "values", @@ -67,13 +68,16 @@ class Dataset(Component): self.scale = scale self.min_width = min_width self._components = [get_component_instance(c) for c in components] - self.component_props = [ - component.recover_kwargs( - component.get_config(), - ["value"], - ) - for component in self._components - ] + if component_props is None: + self.component_props = [ + component.recover_kwargs( + component.get_config(), + ["value"], + ) + for component in self._components + ] + else: + self.component_props = component_props # Narrow type to Component if not all(isinstance(c, Component) for c in self._components): diff --git a/gradio/helpers.py b/gradio/helpers.py index 0c96d986e5..17ad3f50c7 100644 --- a/gradio/helpers.py +++ b/gradio/helpers.py @@ -258,10 +258,11 @@ class Examples: processed_example = self.non_none_processed_examples[example_id] if len(self.inputs_with_examples) == 1: return update( - value=processed_example[0], **self.dataset.component_props[0] + value=processed_example[0], + **self.dataset.component_props[0], # type: ignore ) return [ - update(value=processed_example[i], **self.dataset.component_props[i]) + update(value=processed_example[i], **self.dataset.component_props[i]) # type: ignore for i in range(len(self.inputs_with_examples)) ]