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 <abubakar@huggingface.co>
This commit is contained in:
CoffeeVampir3 2022-11-27 16:12:58 -10:00 committed by GitHub
parent f9f1f41d42
commit 6c73e79c4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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
"""