ginipick commited on
Commit
d819968
·
verified ·
1 Parent(s): 2dfb3f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -17
app.py CHANGED
@@ -183,16 +183,30 @@ def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str
183
 
184
  return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
185
 
186
- def process_bbox(prompts: dict[str, Any]) -> tuple[tuple[Image.Image, Image.Image], gr.DownloadButton]:
187
- assert isinstance(img := prompts["image"], Image.Image)
188
- assert isinstance(boxes := prompts["boxes"], list)
189
- if len(boxes) == 1:
190
- assert isinstance(box := boxes[0], dict)
191
- bbox = tuple(box[k] for k in ["xmin", "ymin", "xmax", "ymax"])
192
- else:
193
- assert len(boxes) == 0
194
- bbox = None
195
- return _process(img, bbox)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
  def on_change_bbox(prompts: dict[str, Any] | None):
198
  return gr.update(interactive=prompts is not None)
@@ -201,15 +215,28 @@ def on_change_bbox(prompts: dict[str, Any] | None):
201
  def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
202
  return gr.update(interactive=bool(img and prompt))
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
 
206
- # Event handler functions
207
  def update_process_button(img, prompt):
208
  return gr.Button.update(
209
  interactive=bool(img and prompt),
210
  variant="primary" if bool(img and prompt) else "secondary"
211
  )
212
-
213
  def update_box_button(img, box_input):
214
  try:
215
  if img and box_input:
@@ -219,6 +246,8 @@ def update_box_button(img, box_input):
219
  return gr.Button.update(interactive=False, variant="secondary")
220
  except:
221
  return gr.Button.update(interactive=False, variant="secondary")
 
 
222
  # 맨 앞부분에 CSS 정의 추가
223
  css = """
224
  footer {display: none}
@@ -283,7 +312,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
283
  """)
284
 
285
  with gr.Tabs(selected=0):
286
- # Tab for Text-based Object Extraction
287
  with gr.TabItem("Extract by Text"):
288
  with gr.Row():
289
  with gr.Column(scale=1):
@@ -319,8 +348,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
319
  visible=True
320
  )
321
 
322
-
323
- # Tab for Box-based Object Extraction
324
  with gr.TabItem("Extract by Box"):
325
  with gr.Row():
326
  with gr.Column(scale=1):
@@ -331,7 +359,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
331
  )
332
  box_input = gr.Textbox(
333
  label="Bounding Box (xmin, ymin, xmax, ymax)",
334
- placeholder="Enter bounding box coordinates as a list",
335
  interactive=True
336
  )
337
  box_btn = gr.Button(
@@ -347,7 +375,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
347
  visible=True
348
  )
349
  box_download = gr.DownloadButton(
350
- "Download Box Result",
351
  visible=True
352
  )
353
 
 
183
 
184
  return (img, combined, masked_alpha), gr.DownloadButton(value=temp.name, interactive=True)
185
 
186
+ def process_bbox(img: Image.Image, box_input: str) -> tuple[list[Image.Image], str]:
187
+ try:
188
+ if img is None or box_input.strip() == "":
189
+ raise gr.Error("Please provide both image and bounding box coordinates")
190
+
191
+ # Parse box coordinates
192
+ try:
193
+ coords = eval(box_input)
194
+ if not isinstance(coords, list) or len(coords) != 4:
195
+ raise ValueError("Invalid box format")
196
+ bbox = tuple(int(x) for x in coords)
197
+ except:
198
+ raise gr.Error("Invalid box format. Please provide [xmin, ymin, xmax, ymax]")
199
+
200
+ # Process the image
201
+ results, download_path = _process(img, bbox)
202
+
203
+ # Convert results to list for gallery
204
+ gallery_images = list(results)
205
+
206
+ return gallery_images, download_path
207
+ except Exception as e:
208
+ raise gr.Error(str(e))
209
+
210
 
211
  def on_change_bbox(prompts: dict[str, Any] | None):
212
  return gr.update(interactive=prompts is not None)
 
215
  def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
216
  return gr.update(interactive=bool(img and prompt))
217
 
218
+ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None) -> tuple[list[Image.Image], str]:
219
+ try:
220
+ if img is None or prompt.strip() == "":
221
+ raise gr.Error("Please provide both image and prompt")
222
+
223
+ # Process the image
224
+ results, download_path = _process(img, prompt, bg_prompt)
225
+
226
+ # Convert results to list for gallery
227
+ gallery_images = list(results)
228
+
229
+ return gallery_images, download_path
230
+ except Exception as e:
231
+ raise gr.Error(str(e))
232
 
233
 
 
234
  def update_process_button(img, prompt):
235
  return gr.Button.update(
236
  interactive=bool(img and prompt),
237
  variant="primary" if bool(img and prompt) else "secondary"
238
  )
239
+
240
  def update_box_button(img, box_input):
241
  try:
242
  if img and box_input:
 
246
  return gr.Button.update(interactive=False, variant="secondary")
247
  except:
248
  return gr.Button.update(interactive=False, variant="secondary")
249
+
250
+
251
  # 맨 앞부분에 CSS 정의 추가
252
  css = """
253
  footer {display: none}
 
312
  """)
313
 
314
  with gr.Tabs(selected=0):
315
+ # Text-based extraction tab
316
  with gr.TabItem("Extract by Text"):
317
  with gr.Row():
318
  with gr.Column(scale=1):
 
348
  visible=True
349
  )
350
 
351
+ # Box-based extraction tab
 
352
  with gr.TabItem("Extract by Box"):
353
  with gr.Row():
354
  with gr.Column(scale=1):
 
359
  )
360
  box_input = gr.Textbox(
361
  label="Bounding Box (xmin, ymin, xmax, ymax)",
362
+ placeholder="Enter coordinates as [x1, y1, x2, y2]",
363
  interactive=True
364
  )
365
  box_btn = gr.Button(
 
375
  visible=True
376
  )
377
  box_download = gr.DownloadButton(
378
+ "Download Result",
379
  visible=True
380
  )
381