Spaces:
Sleeping
Sleeping
fix
Browse files
app.py
CHANGED
@@ -1,157 +1,152 @@
|
|
1 |
-
# === Included SDK === #
|
2 |
-
import gradio as gr
|
3 |
-
|
4 |
-
# === Installed from requirements.txt === #
|
5 |
-
from PIL import Image
|
6 |
-
import numpy as np
|
7 |
-
import torch
|
8 |
-
|
9 |
-
# === Python built-in === #
|
10 |
-
import sys
|
11 |
-
import os
|
12 |
-
|
13 |
-
# ============================================================ #
|
14 |
-
# Q: Why use gr.Blocks instead of gr.Interface? #
|
15 |
-
# A: Only Blocks supports Layouts (and I'm more used to this) #
|
16 |
-
# ============================================================ #
|
17 |
-
with gr.Blocks() as demo:
|
18 |
-
|
19 |
-
# ======================================== #
|
20 |
-
# Markdown is pointless in API usage... XD #
|
21 |
-
# ======================================== #
|
22 |
-
gr.Markdown("""<h1 align="center">Hello World</h1>""")
|
23 |
-
gr.Markdown("""<p align="center">For <b>learning</b> purposes only...</p>""")
|
24 |
-
|
25 |
-
# ====================================== #
|
26 |
-
# function name is overrides by api_name #
|
27 |
-
# ====================================== #
|
28 |
-
def log() -> str:
|
29 |
-
info: list[str] = []
|
30 |
-
info.append(f"OS: {os.name}")
|
31 |
-
info.append(f"Python: {sys.version.split(' ', 1)[0]}")
|
32 |
-
info.append(f"CUDA: {torch.cuda.is_available()}")
|
33 |
-
|
34 |
-
return "\n".join(info)
|
35 |
-
|
36 |
-
def proc_image(
|
37 |
-
img: np.ndarray, target_w: int, target_h: int
|
38 |
-
) -> list[str, Image.Image]:
|
39 |
-
|
40 |
-
input_h, input_w, channels = img.shape
|
41 |
-
|
42 |
-
log = f"Resize a {input_w}x{input_h} image to {target_w}x{target_h}..."
|
43 |
-
|
44 |
-
image = Image.fromarray(img)
|
45 |
-
image = image.resize((target_w, target_h))
|
46 |
-
|
47 |
-
return [log, image]
|
48 |
-
|
49 |
-
with gr.Row():
|
50 |
-
|
51 |
-
with gr.Column():
|
52 |
-
|
53 |
-
# ============================= #
|
54 |
-
# Images take base64 str in API #
|
55 |
-
# ============================= #
|
56 |
-
|
57 |
-
input_image = gr.Image(
|
58 |
-
value=None,
|
59 |
-
image_mode="RGB",
|
60 |
-
sources="upload",
|
61 |
-
type="numpy",
|
62 |
-
label="Input",
|
63 |
-
show_label=True,
|
64 |
-
show_download_button=False,
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
api_name="systemInfo",
|
154 |
-
)
|
155 |
-
|
156 |
-
|
157 |
-
demo.launch()
|
|
|
1 |
+
# === Included SDK === #
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
# === Installed from requirements.txt === #
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
import torch
|
8 |
+
|
9 |
+
# === Python built-in === #
|
10 |
+
import sys
|
11 |
+
import os
|
12 |
+
|
13 |
+
# ============================================================ #
|
14 |
+
# Q: Why use gr.Blocks instead of gr.Interface? #
|
15 |
+
# A: Only Blocks supports Layouts (and I'm more used to this) #
|
16 |
+
# ============================================================ #
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
|
19 |
+
# ======================================== #
|
20 |
+
# Markdown is pointless in API usage... XD #
|
21 |
+
# ======================================== #
|
22 |
+
gr.Markdown("""<h1 align="center">Hello World</h1>""")
|
23 |
+
gr.Markdown("""<p align="center">For <b>learning</b> purposes only...</p>""")
|
24 |
+
|
25 |
+
# ====================================== #
|
26 |
+
# function name is overrides by api_name #
|
27 |
+
# ====================================== #
|
28 |
+
def log() -> str:
|
29 |
+
info: list[str] = []
|
30 |
+
info.append(f"OS: {os.name}")
|
31 |
+
info.append(f"Python: {sys.version.split(' ', 1)[0]}")
|
32 |
+
info.append(f"CUDA: {torch.cuda.is_available()}")
|
33 |
+
|
34 |
+
return "\n".join(info)
|
35 |
+
|
36 |
+
def proc_image(
|
37 |
+
img: np.ndarray, target_w: int, target_h: int
|
38 |
+
) -> list[str, Image.Image]:
|
39 |
+
|
40 |
+
input_h, input_w, channels = img.shape
|
41 |
+
|
42 |
+
log = f"Resize a {input_w}x{input_h} image to {target_w}x{target_h}..."
|
43 |
+
|
44 |
+
image = Image.fromarray(img)
|
45 |
+
image = image.resize((target_w, target_h))
|
46 |
+
|
47 |
+
return [log, image]
|
48 |
+
|
49 |
+
with gr.Row():
|
50 |
+
|
51 |
+
with gr.Column():
|
52 |
+
|
53 |
+
# ============================= #
|
54 |
+
# Images take base64 str in API #
|
55 |
+
# ============================= #
|
56 |
+
|
57 |
+
input_image = gr.Image(
|
58 |
+
value=None,
|
59 |
+
image_mode="RGB",
|
60 |
+
sources="upload",
|
61 |
+
type="numpy",
|
62 |
+
label="Input",
|
63 |
+
show_label=True,
|
64 |
+
show_download_button=False,
|
65 |
+
visible=True,
|
66 |
+
interactive=True,
|
67 |
+
show_share_button=False,
|
68 |
+
show_fullscreen_button=False,
|
69 |
+
)
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
target_w = gr.Slider(
|
73 |
+
minimum=1,
|
74 |
+
maximum=1024,
|
75 |
+
step=1,
|
76 |
+
value=512,
|
77 |
+
label="width",
|
78 |
+
show_label=True,
|
79 |
+
interactive=True,
|
80 |
+
)
|
81 |
+
|
82 |
+
target_h = gr.Slider(
|
83 |
+
minimum=1,
|
84 |
+
maximum=1024,
|
85 |
+
step=1,
|
86 |
+
value=512,
|
87 |
+
label="width",
|
88 |
+
show_label=True,
|
89 |
+
interactive=True,
|
90 |
+
)
|
91 |
+
|
92 |
+
output_image = gr.Image(
|
93 |
+
value=None,
|
94 |
+
image_mode="RGB",
|
95 |
+
sources="upload",
|
96 |
+
type="pil",
|
97 |
+
label="Output",
|
98 |
+
show_label=True,
|
99 |
+
show_download_button=False,
|
100 |
+
visible=True,
|
101 |
+
interactive=False,
|
102 |
+
show_share_button=False,
|
103 |
+
show_fullscreen_button=False,
|
104 |
+
)
|
105 |
+
|
106 |
+
btn1 = gr.Button("Resize")
|
107 |
+
|
108 |
+
with gr.Column():
|
109 |
+
|
110 |
+
# ============================= #
|
111 |
+
# Buttons do not show up in API #
|
112 |
+
# ============================= #
|
113 |
+
|
114 |
+
btn2 = gr.Button("Print Info")
|
115 |
+
|
116 |
+
# ==================================== #
|
117 |
+
# label is shown in the returns in API #
|
118 |
+
# ==================================== #
|
119 |
+
|
120 |
+
console_logs = gr.Text(
|
121 |
+
value="...",
|
122 |
+
label="Logs",
|
123 |
+
show_label=True,
|
124 |
+
lines=1,
|
125 |
+
max_lines=8,
|
126 |
+
interactive=False,
|
127 |
+
)
|
128 |
+
|
129 |
+
# =================================== #
|
130 |
+
# api_name is used as endpoint in API #
|
131 |
+
# =================================== #
|
132 |
+
|
133 |
+
btn1.click(
|
134 |
+
fn=proc_image,
|
135 |
+
inputs=[input_image, target_w, target_h],
|
136 |
+
outputs=[console_logs, output_image],
|
137 |
+
show_progress="hidden",
|
138 |
+
show_api=True,
|
139 |
+
api_name="resizeImage",
|
140 |
+
)
|
141 |
+
|
142 |
+
btn2.click(
|
143 |
+
fn=log,
|
144 |
+
inputs=None,
|
145 |
+
outputs=console_logs,
|
146 |
+
show_progress="hidden",
|
147 |
+
show_api=True,
|
148 |
+
api_name="systemInfo",
|
149 |
+
)
|
150 |
+
|
151 |
+
|
152 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|