Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
1b3ba50
draft changes for fixing lag caused by b64 serialization in gallery c…
proxyphi Sep 14, 2022
2422bf9
Merge branch 'main' into gallery-b64-format
proxyphi Sep 14, 2022
738d79b
handle URLs properly
proxyphi Sep 14, 2022
0fc1805
update test_static to expect file URL instead of b64 encoding
proxyphi Sep 14, 2022
c932578
Results from running format_backend.sh and format_frontend.sh
proxyphi Sep 14, 2022
7aeb2fc
fix URL / path case to create and return tempfiles. tests need to be …
proxyphi Sep 14, 2022
a854713
formatting
proxyphi Sep 14, 2022
2003580
Merge branch 'main' into gallery-b64-format
abidlabs Sep 15, 2022
6c8f2f5
small cleanups
abidlabs Sep 15, 2022
10f34ff
cleanups
abidlabs Sep 15, 2022
165cf86
formatting + repair test in image tests
proxyphi Sep 15, 2022
f418166
Update tests to reflect new expected output of image.postprocess
proxyphi Sep 15, 2022
6cd7024
Merge branch 'main' into gallery-b64-format
abidlabs Sep 15, 2022
526658a
* change to Image.preprocess to handle dict input
proxyphi Sep 17, 2022
f3e2142
Merge branch 'gallery-b64-format' of github.com:proxyphi/gradio into …
proxyphi Sep 17, 2022
43e2026
Merge branch 'main' into gallery-b64-format
abidlabs Sep 19, 2022
8f197b6
fix test components
abidlabs Sep 19, 2022
0d59e20
formatting
abidlabs Sep 19, 2022
4e2197b
fixing binary
abidlabs Sep 19, 2022
5db3dfd
fixes to external
abidlabs Sep 20, 2022
d69f26a
fixes to external
abidlabs Sep 20, 2022
c773bcd
removed examples extra code
abidlabs Sep 20, 2022
77d22fb
formatting
abidlabs Sep 20, 2022
f8980b4
remove imageserializable
abidlabs Sep 20, 2022
74302f2
Merge branch 'main' into gallery-b64-format
abidlabs Sep 20, 2022
dba4717
merge conflict
abidlabs Sep 20, 2022
5042b5a
formatting
abidlabs Sep 20, 2022
e344071
fix gallery test
abidlabs Sep 20, 2022
3bb5b38
changes
aliabid94 Sep 22, 2022
3966678
Merge branch 'main' into gallery-b64-format
pngwn Oct 5, 2022
4534708
revert image changes
pngwn Oct 5, 2022
f29abbc
tweaks
pngwn Oct 5, 2022
defb8cd
revert image changes
pngwn Oct 5, 2022
bedeee2
revert image changes
pngwn Oct 5, 2022
76b8ff8
remove test
pngwn Oct 5, 2022
d07cfc8
cleanup
pngwn Oct 5, 2022
6feb48e
cleanup
pngwn Oct 5, 2022
268e672
testing
abidlabs Oct 5, 2022
e5495ad
untesting
abidlabs Oct 5, 2022
cb4008f
fixes
pngwn Oct 6, 2022
1c60be5
fix error
pngwn Oct 6, 2022
e5c342c
Merge branch 'main' into gallery-b64-format
pngwn Oct 6, 2022
ee739ba
Merge branch 'main' into gallery-b64-format
abidlabs Oct 6, 2022
bc82fe3
changelog
abidlabs Oct 7, 2022
a661179
test components
abidlabs Oct 7, 2022
ddaf18f
test components pass
abidlabs Oct 7, 2022
d1924ae
fix examples
abidlabs Oct 7, 2022
435688b
tests fixes
abidlabs Oct 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ No changes to highlight.
No changes to highlight.

## Full Changelog:
No changes to highlight.
* Speeds up Gallery component by using temporary files instead of base64 representation in the front-end by [@proxyphi](https://github.com/proxyphi), [@pngwn](https://github.com/pngwn), and [@abidlabs](https://github.com/abidlabs) in [PR 2265](https://github.com/gradio-app/gradio/pull/2265)

## Contributors Shoutout:
No changes to highlight.
Expand Down
36 changes: 20 additions & 16 deletions gradio/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3224,6 +3224,7 @@ def __init__(
visible: If False, component will be hidden.
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
"""
self.temp_dir = tempfile.mkdtemp()
super().__init__(
label=label,
show_label=show_label,
Expand Down Expand Up @@ -3265,7 +3266,7 @@ def postprocess(
Parameters:
y: list of images, or list of (image, caption) tuples
Returns:
list of base64 data or (base64 image data, caption) pairs
list of string file paths to images in temp directory
"""
if y is None:
return []
Expand All @@ -3275,19 +3276,26 @@ def postprocess(
if isinstance(img, tuple) or isinstance(img, list):
img, caption = img
if isinstance(img, np.ndarray):
img = processing_utils.encode_array_to_base64(img)
file = processing_utils.save_array_to_file(img, dir=self.temp_dir)
elif isinstance(img, PIL.Image.Image):
img = processing_utils.encode_pil_to_base64(img)
file = processing_utils.save_pil_to_file(img, dir=self.temp_dir)
elif isinstance(img, str):
img = processing_utils.encode_url_or_file_to_base64(img)
if utils.validate_url(img):
file = processing_utils.download_to_file(img, dir=self.temp_dir)
else:
file = processing_utils.create_tmp_copy_of_file(
img, dir=self.temp_dir
)
else:
raise ValueError(
"Unknown type. Please choose from: 'numpy', 'pil', 'file'."
)
raise ValueError(f"Cannot process type as image: {type(img)}")

if caption is not None:
output.append([img, caption])
output.append(
[{"name": file.name, "data": None, "is_file": True}, caption]
)
else:
output.append(img)
output.append({"name": file.name, "data": None, "is_file": True})

return output

def style(
Expand Down Expand Up @@ -3322,15 +3330,11 @@ def deserialize(
for img_data in x:
if isinstance(img_data, list) or isinstance(img_data, tuple):
img_data, caption = img_data
prefix = f"[{utils.strip_invalid_filename_characters(caption)}]-"
else:
caption = None
prefix = None
file_obj = processing_utils.decode_base64_to_file(
img_data, dir=gallery_path, encryption_key=encryption_key, prefix=prefix
)
name = FileSerializable.deserialize(self, img_data, gallery_path)
if caption is not None:
captions[file_obj.name] = caption
captions[name] = caption
if len(captions):
captions_file = os.path.join(gallery_path, "captions.json")
with open(captions_file, "w") as captions_json:
Expand All @@ -3350,7 +3354,7 @@ def serialize(self, x: Any, load_dir: str = "", called_directly: bool = False):
caption = captions.get(file_path)
else:
caption = None
img = ImgSerializable.serialize(self, file_path)
img = FileSerializable.serialize(self, file_path)
if caption:
files.append([img, caption])
else:
Expand Down
17 changes: 5 additions & 12 deletions gradio/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import gradio
from gradio import components, exceptions, utils
from gradio.processing_utils import to_binary

if TYPE_CHECKING:
from gradio.components import DataframeData
Expand Down Expand Up @@ -158,9 +159,7 @@ def encode_to_base64(r: requests.Response) -> str:
# example model: ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition
"inputs": components.Audio(source="upload", type="filepath", label="Input"),
"outputs": components.Label(label="Class"),
"preprocess": lambda i: base64.b64decode(
i["data"].split(",")[1]
), # convert the base64 representation to binary
"preprocess": lambda i: to_binary,
"postprocess": lambda r: postprocess_label(
{i["label"].split(", ")[0]: i["score"] for i in r.json()}
),
Expand All @@ -169,18 +168,14 @@ def encode_to_base64(r: requests.Response) -> str:
# example model: speechbrain/mtl-mimic-voicebank
"inputs": components.Audio(source="upload", type="filepath", label="Input"),
"outputs": components.Audio(label="Output"),
"preprocess": lambda i: base64.b64decode(
i["data"].split(",")[1]
), # convert the base64 representation to binary
"preprocess": to_binary,
"postprocess": encode_to_base64,
},
"automatic-speech-recognition": {
# example model: jonatasgrosman/wav2vec2-large-xlsr-53-english
"inputs": components.Audio(source="upload", type="filepath", label="Input"),
"outputs": components.Textbox(label="Output"),
"preprocess": lambda i: base64.b64decode(
i["data"].split(",")[1]
), # convert the base64 representation to binary
"preprocess": to_binary,
"postprocess": lambda r: r.json()["text"],
},
"feature-extraction": {
Expand All @@ -202,9 +197,7 @@ def encode_to_base64(r: requests.Response) -> str:
# Example: google/vit-base-patch16-224
"inputs": components.Image(type="filepath", label="Input Image"),
"outputs": components.Label(label="Classification"),
"preprocess": lambda i: base64.b64decode(
i.split(",")[1]
), # convert the base64 representation to binary
"preprocess": to_binary,
"postprocess": lambda r: postprocess_label(
{i["label"].split(", ")[0]: i["score"] for i in r.json()}
),
Expand Down
17 changes: 17 additions & 0 deletions gradio/processing_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import base64
import json
import mimetypes
Expand All @@ -8,6 +10,7 @@
import tempfile
import warnings
from io import BytesIO
from typing import Dict

import numpy as np
import requests
Expand All @@ -21,9 +24,23 @@
from pydub import AudioSegment


#########################
# GENERAL
#########################


def to_binary(x: str | Dict) -> bytes:
"""Converts a base64 string or dictionary to a binary string that can be sent in a POST."""
if isinstance(x, dict):
x = encode_url_or_file_to_base64(x["name"])
return base64.b64decode(x.split(",")[1])


#########################
# IMAGE PRE-PROCESSING
#########################


def decode_base64_to_image(encoding):
content = encoding.split(";")[1]
image_encoded = content.split(",")[1]
Expand Down
Loading