Haoming02 commited on
Commit
2f5363d
1 Parent(s): 83e4a81
Files changed (1) hide show
  1. app.py +152 -157
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
- container=False,
66
- visible=True,
67
- interactive=True,
68
- show_share_button=False,
69
- show_fullscreen_button=False,
70
- )
71
-
72
- with gr.Row():
73
- target_w = gr.Slider(
74
- minimum=1,
75
- maximum=1024,
76
- step=1,
77
- value=512,
78
- label="width",
79
- show_label=True,
80
- container=False,
81
- interactive=True,
82
- )
83
-
84
- target_h = gr.Slider(
85
- minimum=1,
86
- maximum=1024,
87
- step=1,
88
- value=512,
89
- label="width",
90
- show_label=True,
91
- container=False,
92
- interactive=True,
93
- )
94
-
95
- output_image = gr.Image(
96
- value=None,
97
- image_mode="RGB",
98
- sources="upload",
99
- type="pil",
100
- label="Output",
101
- show_label=True,
102
- show_download_button=False,
103
- container=False,
104
- visible=True,
105
- interactive=False,
106
- show_share_button=False,
107
- show_fullscreen_button=False,
108
- )
109
-
110
- btn1 = gr.Button("Resize", container=False)
111
-
112
- with gr.Column():
113
-
114
- # ============================= #
115
- # Buttons do not show up in API #
116
- # ============================= #
117
-
118
- btn2 = gr.Button("Print Info", container=False)
119
-
120
- # ==================================== #
121
- # label is shown in the returns in API #
122
- # ==================================== #
123
-
124
- console_logs = gr.Text(
125
- value="...",
126
- label="Logs",
127
- show_label=True,
128
- lines=1,
129
- max_lines=8,
130
- interactive=False,
131
- container=True,
132
- )
133
-
134
- # =================================== #
135
- # api_name is used as endpoint in API #
136
- # =================================== #
137
-
138
- btn1.click(
139
- fn=proc_image,
140
- inputs=[input_image, target_w, target_h],
141
- outputs=[console_logs, output_image],
142
- show_progress="hidden",
143
- show_api=True,
144
- api_name="resizeImage",
145
- )
146
-
147
- btn2.click(
148
- fn=log,
149
- inputs=None,
150
- outputs=console_logs,
151
- show_progress="hidden",
152
- show_api=True,
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()