Blocks-Components

- move File component
This commit is contained in:
Ömer Faruk Özdemir 2022-03-15 15:09:31 +03:00
parent 7fa8e45b67
commit 01ac97bcd0
2 changed files with 139 additions and 100 deletions

View File

@ -1569,3 +1569,105 @@ class Audio(Component):
def restore_flagged(self, dir, data, encryption_key):
return self.restore_flagged_file(dir, data, encryption_key)["data"]
class File(Component):
"""
Component accepts generic file uploads.
Input type: Union[file-object, bytes, List[Union[file-object, bytes]]]
Demos: zip_to_json, zip_two_files
"""
def __init__(
self,
default: str = "",
*,
file_count: str = "single",
type: str = "file",
label: Optional[str] = None,
**kwargs,
):
"""
Parameters:
default (str): IGNORED
file_count (str): if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
type (str): Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name, "binary" returns an bytes object.
label (str): component name in interface.
"""
if "keep_filename" in kwargs:
warnings.warn("keep_filename is deprecated", DeprecationWarning)
self.file_count = file_count
self.type = type
self.test_input = None
super().__init__(label=label, **kwargs)
def get_template_context(self):
return {
"file_count": self.file_count,
**super().get_template_context(),
}
@classmethod
def get_shortcut_implementations(cls):
return {
"file": {},
"files": {"file_count": "multiple"},
}
def preprocess_example(self, x):
return {"name": x, "data": None, "is_example": True}
def preprocess(self, x: List[Dict[str, str]] | None):
"""
Parameters:
x (List[Dict[name: str, data: str]]): List of JSON objects with filename as 'name' property and base64 data as 'data' property
Returns:
(Union[file-object, bytes, List[Union[file-object, bytes]]]): File objects in requested format
"""
if x is None:
return None
def process_single_file(f):
file_name, data, is_example = (
f["name"],
f["data"],
f.get("is_example", False),
)
if self.type == "file":
if is_example:
return processing_utils.create_tmp_copy_of_file(file_name)
else:
return processing_utils.decode_base64_to_file(
data, file_path=file_name
)
elif self.type == "bytes":
if is_example:
with open(file_name, "rb") as file_data:
return file_data.read()
return processing_utils.decode_base64_to_binary(data)[0]
else:
raise ValueError(
"Unknown type: "
+ str(self.type)
+ ". Please choose from: 'file', 'bytes'."
)
if self.file_count == "single":
if isinstance(x, list):
return process_single_file(x[0])
else:
return process_single_file(x)
else:
return [process_single_file(f) for f in x]
def save_flagged(self, dir, label, data, encryption_key):
"""
Returns: (str) path to file
"""
return self.save_flagged_file(
dir, label, None if data is None else data[0]["data"], encryption_key
)
def generate_sample(self):
return test_data.BASE64_FILE

View File

@ -24,6 +24,7 @@ from gradio.components import (
CheckboxGroup,
Component,
Dropdown,
File,
Image,
Number,
Radio,
@ -352,6 +353,42 @@ class Audio(Audio):
super().__init__(source=source, type=type, label=label, optional=optional)
class File(File):
"""
Component accepts generic file uploads.
Input type: Union[file-object, bytes, List[Union[file-object, bytes]]]
Demos: zip_to_json, zip_two_files
"""
def __init__(
self,
file_count: str = "single",
type: str = "file",
label: Optional[str] = None,
keep_filename: bool = True,
optional: bool = False,
):
"""
Parameters:
file_count (str): if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
type (str): Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name, "binary" returns an bytes object.
label (str): component name in interface.
keep_filename (bool): DEPRECATED. Original filename always kept.
optional (bool): If True, the interface can be submitted with no uploaded image, in which case the input value is None.
"""
warnings.warn(
"Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your components from gradio.components",
DeprecationWarning,
)
super().__init__(
file_count=file_count,
type=type,
label=label,
keep_filename=keep_filename,
optional=optional,
)
class InputComponent(Component):
"""
Input Component. All input components subclass this.
@ -435,106 +472,6 @@ class InputComponent(Component):
}
class File(InputComponent):
"""
Component accepts generic file uploads.
Input type: Union[file-object, bytes, List[Union[file-object, bytes]]]
Demos: zip_to_json, zip_two_files
"""
def __init__(
self,
file_count: str = "single",
type: str = "file",
label: Optional[str] = None,
keep_filename: bool = True,
optional: bool = False,
):
"""
Parameters:
file_count (str): if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
type (str): Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name, "binary" returns an bytes object.
label (str): component name in interface.
keep_filename (bool): DEPRECATED. Original filename always kept.
optional (bool): If True, the interface can be submitted with no uploaded image, in which case the input value is None.
"""
self.file_count = file_count
self.type = type
self.test_input = None
super().__init__(label, optional=optional)
def get_template_context(self):
return {
"file_count": self.file_count,
"optional": self.optional,
**super().get_template_context(),
}
@classmethod
def get_shortcut_implementations(cls):
return {
"file": {},
"files": {"file_count": "multiple"},
}
def preprocess_example(self, x):
return {"name": x, "data": None, "is_example": True}
def preprocess(self, x: List[Dict[str, str]] | None):
"""
Parameters:
x (List[Dict[name: str, data: str]]): List of JSON objects with filename as 'name' property and base64 data as 'data' property
Returns:
(Union[file-object, bytes, List[Union[file-object, bytes]]]): File objects in requested format
"""
if x is None:
return None
def process_single_file(f):
file_name, data, is_example = (
f["name"],
f["data"],
f.get("is_example", False),
)
if self.type == "file":
if is_example:
return processing_utils.create_tmp_copy_of_file(file_name)
else:
return processing_utils.decode_base64_to_file(
data, file_path=file_name
)
elif self.type == "bytes":
if is_example:
with open(file_name, "rb") as file_data:
return file_data.read()
return processing_utils.decode_base64_to_binary(data)[0]
else:
raise ValueError(
"Unknown type: "
+ str(self.type)
+ ". Please choose from: 'file', 'bytes'."
)
if self.file_count == "single":
if isinstance(x, list):
return process_single_file(x[0])
else:
return process_single_file(x)
else:
return [process_single_file(f) for f in x]
def save_flagged(self, dir, label, data, encryption_key):
"""
Returns: (str) path to file
"""
return self.save_flagged_file(
dir, label, None if data is None else data[0]["data"], encryption_key
)
def generate_sample(self):
return test_data.BASE64_FILE
class Dataframe(InputComponent):
"""
Component accepts 2D input through a spreadsheet interface.