Haoming02 commited on
Commit
25fdc62
1 Parent(s): 2f5363d
Files changed (1) hide show
  1. app.py +59 -40
app.py CHANGED
@@ -1,19 +1,22 @@
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
  # ======================================== #
@@ -22,10 +25,10 @@ with gr.Blocks() as demo:
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]}")
@@ -33,16 +36,31 @@ with gr.Blocks() as demo:
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
 
@@ -50,11 +68,10 @@ with gr.Blocks() as demo:
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",
@@ -68,8 +85,11 @@ with gr.Blocks() as demo:
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,
@@ -79,7 +99,7 @@ with gr.Blocks() as demo:
79
  interactive=True,
80
  )
81
 
82
- target_h = gr.Slider(
83
  minimum=1,
84
  maximum=1024,
85
  step=1,
@@ -89,7 +109,7 @@ with gr.Blocks() as demo:
89
  interactive=True,
90
  )
91
 
92
- output_image = gr.Image(
93
  value=None,
94
  image_mode="RGB",
95
  sources="upload",
@@ -103,15 +123,14 @@ with gr.Blocks() as demo:
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 #
@@ -128,19 +147,11 @@ with gr.Blocks() as demo:
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",
@@ -148,5 +159,13 @@ with gr.Blocks() as demo:
148
  api_name="systemInfo",
149
  )
150
 
 
 
 
 
 
 
 
 
151
 
152
  demo.launch()
 
1
+ # === Chosen SDK === #
2
  import gradio as gr
3
 
4
+ # === requirements.txt === #
5
  from PIL import Image
6
  import numpy as np
7
  import torch
8
+ import cv2
9
 
10
  # === Python built-in === #
11
+ from io import BytesIO
12
+ import base64
13
  import sys
14
  import os
15
 
16
+ # =========================================================== #
17
+ # Q: Why use gr.Blocks instead of gr.Interface? #
18
+ # A: Only Blocks supports Layouts (and I'm more used to this) #
19
+ # =========================================================== #
20
  with gr.Blocks() as demo:
21
 
22
  # ======================================== #
 
25
  gr.Markdown("""<h1 align="center">Hello World</h1>""")
26
  gr.Markdown("""<p align="center">For <b>learning</b> purposes only...</p>""")
27
 
28
+ # ======================================= #
29
+ # function name is overridden by api_name #
30
+ # ======================================= #
31
+ def whatever() -> str:
32
  info: list[str] = []
33
  info.append(f"OS: {os.name}")
34
  info.append(f"Python: {sys.version.split(' ', 1)[0]}")
 
36
 
37
  return "\n".join(info)
38
 
39
+ # ==================================================================== #
40
+ # parameter name is also shown as parameter name in API #
41
+ # Note: parameter type has to be manually handled when called from API #
42
+ # ==================================================================== #
43
  def proc_image(
44
+ image: np.ndarray | str, width: int, height: int
45
  ) -> list[str, Image.Image]:
46
 
47
+ if isinstance(image, np.ndarray):
48
+ image = Image.fromarray(image)
49
+ elif isinstance(image, str):
50
+ image = Image.open(BytesIO(base64.b64decode(image)))
51
+ else:
52
+ raise ValueError("Unsupported Image Type")
53
 
54
+ if isinstance(width, float):
55
+ width = int(width)
56
+ if isinstance(height, float):
57
+ height = int(height)
58
 
59
+ input_w, input_h = image.size
60
+
61
+ log = f"Resize a {input_w}x{input_h} image to {width}x{height}..."
62
+
63
+ image = image.convert("RGB").resize((width, height))
64
 
65
  return [log, image]
66
 
 
68
 
69
  with gr.Column():
70
 
71
+ # ==================================== #
72
+ # variable name does not matter in API #
73
+ # ==================================== #
74
+ foo1 = gr.Image(
 
75
  value=None,
76
  image_mode="RGB",
77
  sources="upload",
 
85
  show_fullscreen_button=False,
86
  )
87
 
88
+ # =============================================== #
89
+ # Only value matters (shown as the Default value) #
90
+ # =============================================== #
91
  with gr.Row():
92
+ foo2 = gr.Slider(
93
  minimum=1,
94
  maximum=1024,
95
  step=1,
 
99
  interactive=True,
100
  )
101
 
102
+ foo3 = gr.Slider(
103
  minimum=1,
104
  maximum=1024,
105
  step=1,
 
109
  interactive=True,
110
  )
111
 
112
+ bar = gr.Image(
113
  value=None,
114
  image_mode="RGB",
115
  sources="upload",
 
123
  show_fullscreen_button=False,
124
  )
125
 
126
+ yeet1 = gr.Button("Resize")
127
 
128
  with gr.Column():
129
 
130
+ # ============================== #
131
+ # Button does not show up in API #
132
+ # ============================== #
133
+ yeet2 = gr.Button("Print Info")
 
134
 
135
  # ==================================== #
136
  # label is shown in the returns in API #
 
147
 
148
  # =================================== #
149
  # api_name is used as endpoint in API #
150
+ # order is based on event defination #
151
  # =================================== #
152
 
153
+ yeet2.click(
154
+ fn=whatever,
 
 
 
 
 
 
 
 
 
155
  inputs=None,
156
  outputs=console_logs,
157
  show_progress="hidden",
 
159
  api_name="systemInfo",
160
  )
161
 
162
+ yeet1.click(
163
+ fn=proc_image,
164
+ inputs=[foo1, foo2, foo3],
165
+ outputs=[console_logs, bar],
166
+ show_progress="hidden",
167
+ show_api=True,
168
+ api_name="resizeImage",
169
+ )
170
 
171
  demo.launch()