mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-21 01:01:05 +08:00
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:
parent
f9f1f41d42
commit
6c73e79c4e
@ -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.
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user