mirror of
https://github.com/gradio-app/gradio.git
synced 2025-02-11 11:19:58 +08:00
* Add tests * Fix tests * Remove redundant test * fix for windows * Add tests * Fix tests * Remove redundant test * fix for windows * Add uploadbtn * Add support for upload btn * revert * Add size to FileSerializable * Serialize multiple files * lint * Make changes to deserialize * Fix multiple file upload case * Modify test --------- Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
import os
|
|
import tempfile
|
|
|
|
from gradio import components
|
|
|
|
from gradio_client.serializing import COMPONENT_MAPPING, FileSerializable
|
|
from gradio_client.utils import encode_url_or_file_to_base64
|
|
|
|
|
|
def test_check_component_fallback_serializers():
|
|
for component_name, class_type in COMPONENT_MAPPING.items():
|
|
if component_name == "dataset": # cannot be instantiated without parameters
|
|
continue
|
|
component = components.get_component_instance(component_name)
|
|
assert isinstance(component, class_type)
|
|
|
|
|
|
def test_file_serializing():
|
|
|
|
try:
|
|
serializing = FileSerializable()
|
|
with tempfile.NamedTemporaryFile(delete=False, mode="w") as f1:
|
|
with tempfile.NamedTemporaryFile(delete=False, mode="w") as f2:
|
|
f1.write("Hello World!")
|
|
f2.write("Greetings!")
|
|
|
|
output = serializing.serialize(f1.name)
|
|
assert output["data"] == encode_url_or_file_to_base64(f1.name)
|
|
output = serializing.serialize([f1.name, f2.name])
|
|
assert output[0]["data"] == encode_url_or_file_to_base64(f1.name)
|
|
assert output[1]["data"] == encode_url_or_file_to_base64(f2.name)
|
|
|
|
# no-op for dict
|
|
assert serializing.serialize(output) == output
|
|
|
|
files = serializing.deserialize(output)
|
|
assert open(files[0]).read() == "Hello World!"
|
|
assert open(files[1]).read() == "Greetings!"
|
|
finally:
|
|
os.remove(f1.name)
|
|
os.remove(f2.name)
|