Haoming02 commited on
Commit
6bd9d57
1 Parent(s): de84137
Files changed (1) hide show
  1. app.py +26 -43
app.py CHANGED
@@ -42,24 +42,13 @@ with gr.Blocks() as demo:
42
  # ===================================================== #
43
  # parameter name is also shown as parameter name in API #
44
  # ===================================================== #
45
- def proc_image(
46
- image: dict | str, width: int, height: int
47
- ) -> list[str, Image.Image]:
48
 
49
- if isinstance(image, dict):
50
- image = Image.open(image["path"])
51
-
52
- elif isinstance(image, str):
53
- if os.path.isfile(image):
54
- image = Image.open(image)
55
- else:
56
- # ==================================== #
57
- # This works with base64 image via API #
58
- # ==================================== #
59
- image = Image.open(BytesIO(base64.b64decode(image)))
60
-
61
- else:
62
- raise ValueError("Unsupported Image Type")
63
 
64
  if isinstance(width, float):
65
  width = int(width)
@@ -72,7 +61,11 @@ with gr.Blocks() as demo:
72
 
73
  image = image.convert("RGB").resize((width, height))
74
 
75
- return [log, image]
 
 
 
 
76
 
77
  with gr.Row():
78
 
@@ -81,18 +74,15 @@ with gr.Blocks() as demo:
81
  # ==================================== #
82
  # variable name does not matter in API #
83
  # ==================================== #
84
- foo1 = gr.Image(
85
- value=None,
86
- image_mode="RGB",
87
- sources="upload",
88
- type="filepath",
89
- label="Input",
90
  show_label=True,
91
- show_download_button=False,
92
  visible=True,
93
  interactive=True,
94
- show_share_button=False,
95
- show_fullscreen_button=False,
96
  )
97
 
98
  # =============================================== #
@@ -119,18 +109,15 @@ with gr.Blocks() as demo:
119
  interactive=True,
120
  )
121
 
122
- bar = gr.Image(
123
- value=None,
124
- image_mode="RGB",
125
- sources="upload",
126
- type="pil",
127
- label="Output",
128
  show_label=True,
129
- show_download_button=False,
130
  visible=True,
131
  interactive=False,
132
- show_share_button=False,
133
- show_fullscreen_button=False,
134
  )
135
 
136
  yeet1 = gr.Button("Resize")
@@ -156,7 +143,6 @@ with gr.Blocks() as demo:
156
 
157
  # =================================== #
158
  # api_name is used as endpoint in API #
159
- # order is based on event defination #
160
  # =================================== #
161
  yeet2.click(
162
  fn=whatever,
@@ -167,10 +153,9 @@ with gr.Blocks() as demo:
167
  api_name="systemInfo",
168
  )
169
 
170
- # ======================================================== #
171
- # disable preprocess to allow arbitrary type from API #
172
- # Note: parameter type has to be manually handled this way #
173
- # ======================================================== #
174
  yeet1.click(
175
  fn=proc_image,
176
  inputs=[foo1, foo2, foo3],
@@ -178,8 +163,6 @@ with gr.Blocks() as demo:
178
  show_progress="hidden",
179
  show_api=True,
180
  api_name="resizeImage",
181
- preprocess=False,
182
- postprocess=False,
183
  )
184
 
185
  demo.launch(show_error=True)
 
42
  # ===================================================== #
43
  # parameter name is also shown as parameter name in API #
44
  # ===================================================== #
45
+ def proc_image(image: str, width: int, height: int) -> list[str, Image.Image]:
46
+ assert isinstance(image, str)
 
47
 
48
+ # ==================================== #
49
+ # This works with base64 image via API #
50
+ # ==================================== #
51
+ image = Image.open(BytesIO(base64.b64decode(image)))
 
 
 
 
 
 
 
 
 
 
52
 
53
  if isinstance(width, float):
54
  width = int(width)
 
61
 
62
  image = image.convert("RGB").resize((width, height))
63
 
64
+ with BytesIO() as buffer:
65
+ image.save(buffer, format="PNG")
66
+ b64_image = base64.b64encode(buffer.getvalue()).decode()
67
+
68
+ return [log, b64_image]
69
 
70
  with gr.Row():
71
 
 
74
  # ==================================== #
75
  # variable name does not matter in API #
76
  # ==================================== #
77
+ foo1 = gr.Text(
78
+ value="",
79
+ label="Input Image",
 
 
 
80
  show_label=True,
81
+ info="Image in base64",
82
  visible=True,
83
  interactive=True,
84
+ lines=1,
85
+ max_lines=1,
86
  )
87
 
88
  # =============================================== #
 
109
  interactive=True,
110
  )
111
 
112
+ bar = gr.Text(
113
+ value="",
114
+ label="Output Image",
 
 
 
115
  show_label=True,
116
+ info="Image in base64",
117
  visible=True,
118
  interactive=False,
119
+ lines=1,
120
+ max_lines=1,
121
  )
122
 
123
  yeet1 = gr.Button("Resize")
 
143
 
144
  # =================================== #
145
  # api_name is used as endpoint in API #
 
146
  # =================================== #
147
  yeet2.click(
148
  fn=whatever,
 
153
  api_name="systemInfo",
154
  )
155
 
156
+ # =============================================== #
157
+ # API order is based on event declaration in code #
158
+ # =============================================== #
 
159
  yeet1.click(
160
  fn=proc_image,
161
  inputs=[foo1, foo2, foo3],
 
163
  show_progress="hidden",
164
  show_api=True,
165
  api_name="resizeImage",
 
 
166
  )
167
 
168
  demo.launch(show_error=True)