From b78e5f195cf57125782a92f55a8e576bbd98ac37 Mon Sep 17 00:00:00 2001 From: freddyaboulton Date: Wed, 10 May 2023 15:54:51 -0400 Subject: [PATCH] Fix example inputs --- client/python/gradio_client/serializing.py | 11 +++++++++++ client/python/test/test_client.py | 8 +++++++- gradio/components.py | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/client/python/gradio_client/serializing.py b/client/python/gradio_client/serializing.py index dfec41c1376e8..c0529e6c9976a 100644 --- a/client/python/gradio_client/serializing.py +++ b/client/python/gradio_client/serializing.py @@ -231,11 +231,22 @@ def api_info(self) -> dict[str, dict | bool]: return self._single_file_api_info() def example_inputs(self) -> dict[str, Any]: + return self._single_file_example_inputs() + + def _single_file_example_inputs(self) -> dict[str, Any]: return { "raw": {"is_file": False, "data": media_data.BASE64_FILE}, "serialized": "https://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdf", } + def _multiple_file_example_inputs(self) -> dict[str, Any]: + return { + "raw": [{"is_file": False, "data": media_data.BASE64_FILE}], + "serialized": [ + "https://github.com/gradio-app/gradio/raw/main/test/test_files/sample_file.pdf" + ], + } + def _serialize_single( self, x: str | FileData | None, load_dir: str | Path = "" ) -> FileData | None: diff --git a/client/python/test/test_client.py b/client/python/test/test_client.py index 465c4b9323a75..7647910179451 100644 --- a/client/python/test/test_client.py +++ b/client/python/test/test_client.py @@ -740,20 +740,26 @@ def test_file_io(self, file_io_demo): info = client.view_api(return_format="dict") inputs = info["named_endpoints"]["/predict"]["parameters"] outputs = info["named_endpoints"]["/predict"]["returns"] + assert inputs[0]["type"]["type"] == "array" assert inputs[0]["python_type"] == { "type": "List[str]", "description": "List of filepath(s) or URL(s) to files", } + assert isinstance(inputs[0]["example_input"], list) + assert inputs[1]["python_type"] == { "type": "str", "description": "filepath or URL to file", } - assert inputs[0]["type"]["type"] == "array" + assert isinstance(inputs[1]["example_input"], str) + assert outputs[0]["python_type"] == { "type": "List[str]", "description": "List of filepath(s) or URL(s) to files", } + assert outputs[0]["type"]["type"] == "array" + assert outputs[1]["python_type"] == { "type": "str", "description": "filepath or URL to file", diff --git a/gradio/components.py b/gradio/components.py index bc45926cd2172..9c9e73ec96f4f 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -2797,6 +2797,12 @@ def serialized_info(self): else: return self._multiple_file_serialized_info() + def example_inputs(self) -> dict[str, Any]: + if self.file_count == "single": + return self._single_file_example_inputs() + else: + return self._multiple_file_example_inputs() + @document("style") class Dataframe(Changeable, Selectable, IOComponent, JSONSerializable):