-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_components.py
61 lines (54 loc) · 2.18 KB
/
custom_components.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import annotations
import numpy as np
import gradio as gr
import numpy as np
from enum import Enum
from typing import Callable, List, Tuple, Any
from typing_extensions import Literal
from PIL import Image as _Image
from gradio import utils, processing_utils
class _Keywords(Enum):
NO_VALUE = "NO_VALUE" # Used as a sentinel to determine if nothing is provided as a argument for `value` in `Component.update()`
FINISHED_ITERATING = "FINISHED_ITERATING" # Used to skip processing of a component's value (needed for generators + state)
class Gallery(gr.Gallery):
def postprocess(
self,
y: List[np.ndarray | _Image.Image | str]
| List[Tuple[np.ndarray | _Image.Image | str, str]]
| None,
) -> List[str]:
"""
Parameters:
y: list of images, or list of (image, caption) tuples
Returns:
list of string file paths to images in temp directory
"""
if y is None:
return []
output = []
for img in y:
caption = None
if isinstance(img, tuple) or isinstance(img, list):
img, caption = img
elif isinstance(img, np.ndarray):
file = self.img_array_to_temp_file(img, dir=self.DEFAULT_TEMP_DIR)
file_path = str(utils.abspath(file))
self.temp_files.add(file_path)
elif isinstance(img, _Image.Image):
file = self.pil_to_temp_file(img, dir=self.DEFAULT_TEMP_DIR)
file_path = str(utils.abspath(file))
self.temp_files.add(file_path)
elif isinstance(img, str):
# if utils.validate_url(img):
file_path = img
# else:
# file_path = self.make_temp_copy_if_needed(img)
else:
raise ValueError(f"Cannot process type as image: {type(img)}")
if caption is not None:
output.append(
[{"name": file_path, "data": None, "is_file": True}, caption]
)
else:
output.append({"name": file_path, "data": None, "is_file": True})
return output