From 6c73e79c4e540cf0a2623ccad51eac3f9c96366c Mon Sep 17 00:00:00 2001 From: CoffeeVampir3 <48565901+CoffeeVampir3@users.noreply.github.com> Date: Sun, 27 Nov 2022 16:12:58 -1000 Subject: [PATCH] Fixed a type bug where the validation was "binary" (#2727) * Fixed a type bug where the validation was "binary" In line 2113 the type was validated as either file or binary: ```python valid_types = ["file", "binary"] if type not in valid_types: raise ValueError( f"Invalid value for parameter `type`: {type}. Please choose from one of: {valid_types}" ) ``` However line 2183 preprocess single file wanted either "file" or "bytes" ```python elif self.type == "bytes": ``` I have arbitrarily picked binary as the one to match, but now they are both consistent. * added test, changelog, formatting Co-authored-by: Abubakar Abid --- CHANGELOG.md | 1 + gradio/components.py | 14 ++++++++++++-- test/test_components.py | 7 +++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6197c83b04..d720c46e83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ No changes to highlight. ## Bug Fixes: * Fixed bug where requests timeout is missing from utils.version_check() by [@yujiehecs](https://github.com/yujiehecs) in [PR 2729](https://github.com/gradio-app/gradio/pull/2729) +* Fixed bug where so that the `File` component can properly preprocess files to "binary" byte-string format by [CoffeeVampir3](https://github.com/CoffeeVampir3) in [PR 2727](https://github.com/gradio-app/gradio/pull/2727) ## Documentation Changes: No changes to highlight. diff --git a/gradio/components.py b/gradio/components.py index 08b52b3023..4e84549232 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -2110,11 +2110,19 @@ class File(Changeable, Clearable, Uploadable, IOComponent, FileSerializable): self.temp_dir = tempfile.mkdtemp() self.file_count = file_count self.file_types = file_types - valid_types = ["file", "binary"] + valid_types = [ + "file", + "binary", + "bytes", + ] # "bytes" is included for backwards compatibility if type not in valid_types: raise ValueError( f"Invalid value for parameter `type`: {type}. Please choose from one of: {valid_types}" ) + if type == "bytes": + warnings.warn( + "The `bytes` type is deprecated and may not work as expected. Please use `binary` instead." + ) self.type = type self.test_input = None IOComponent.__init__( @@ -2180,7 +2188,9 @@ class File(Changeable, Clearable, Uploadable, IOComponent, FileSerializable): ) file.orig_name = file_name return file - elif self.type == "bytes": + elif ( + self.type == "binary" or self.type == "bytes" + ): # "bytes" is included for backwards compatibility if is_file: with open(file_name, "rb") as file_data: return file_data.read() diff --git a/test/test_components.py b/test/test_components.py index d409481f21..8d2d655351 100644 --- a/test/test_components.py +++ b/test/test_components.py @@ -842,6 +842,10 @@ class TestFile: x_file["is_example"] = True assert file_input.preprocess(x_file) is not None + file_input = gr.File(type="binary") + output = file_input.preprocess(x_file) + assert type(output) == bytes + def test_in_interface_as_input(self): """ Interface, process @@ -854,8 +858,7 @@ class TestFile: iface = gr.Interface(get_size_of_file, "file", "number") assert iface(x_file) == 10558 - @pytest.mark.asyncio - async def test_as_component_as_output(self): + def test_as_component_as_output(self): """ Interface, process """