Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -201,29 +201,6 @@ def _process(img: Image.Image, prompt: str | BoundingBox | None, bg_prompt: str
|
|
201 |
except Exception as e:
|
202 |
raise gr.Error(f"Processing failed: {str(e)}")
|
203 |
|
204 |
-
def process_bbox(img: Image.Image, box_input: str) -> tuple[list[Image.Image], str]:
|
205 |
-
try:
|
206 |
-
if img is None or box_input.strip() == "":
|
207 |
-
raise gr.Error("Please provide both image and bounding box coordinates")
|
208 |
-
|
209 |
-
# Parse box coordinates
|
210 |
-
try:
|
211 |
-
coords = eval(box_input)
|
212 |
-
if not isinstance(coords, list) or len(coords) != 4:
|
213 |
-
raise ValueError("Invalid box format")
|
214 |
-
bbox = tuple(int(x) for x in coords)
|
215 |
-
except:
|
216 |
-
raise gr.Error("Invalid box format. Please provide [xmin, ymin, xmax, ymax]")
|
217 |
-
|
218 |
-
# Process the image
|
219 |
-
results, download_path = _process(img, bbox)
|
220 |
-
|
221 |
-
# Convert results to list for gallery
|
222 |
-
gallery_images = list(results)
|
223 |
-
|
224 |
-
return gallery_images, download_path
|
225 |
-
except Exception as e:
|
226 |
-
raise gr.Error(str(e))
|
227 |
|
228 |
|
229 |
def on_change_bbox(prompts: dict[str, Any] | None):
|
@@ -233,21 +210,39 @@ def on_change_bbox(prompts: dict[str, Any] | None):
|
|
233 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
234 |
return gr.update(interactive=bool(img and prompt))
|
235 |
|
236 |
-
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None) -> tuple[
|
237 |
try:
|
238 |
if img is None or prompt.strip() == "":
|
239 |
raise gr.Error("Please provide both image and prompt")
|
240 |
|
241 |
# Process the image
|
242 |
-
results,
|
243 |
|
244 |
-
#
|
245 |
-
|
246 |
-
|
247 |
-
return gallery_images, download_path
|
248 |
except Exception as e:
|
249 |
raise gr.Error(str(e))
|
250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
|
252 |
# Event handler functions 수정
|
253 |
def update_process_button(img, prompt):
|
@@ -322,6 +317,7 @@ button.primary:hover {
|
|
322 |
|
323 |
# UI 부분만 수정
|
324 |
# Main Gradio app
|
|
|
325 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
326 |
gr.HTML("""
|
327 |
<div class="main-title">
|
@@ -357,15 +353,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
357 |
)
|
358 |
|
359 |
with gr.Column(scale=1):
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
|
|
|
|
|
|
|
|
|
|
369 |
|
370 |
# Box-based extraction tab
|
371 |
with gr.TabItem("Extract by Box"):
|
@@ -388,15 +389,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
388 |
)
|
389 |
|
390 |
with gr.Column(scale=1):
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
|
|
|
|
|
|
|
|
|
|
400 |
|
401 |
# Event bindings
|
402 |
input_image.change(
|
@@ -416,7 +422,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
416 |
process_btn.click(
|
417 |
fn=process_prompt,
|
418 |
inputs=[input_image, text_prompt, bg_prompt],
|
419 |
-
outputs=[
|
420 |
queue=True
|
421 |
)
|
422 |
|
@@ -437,7 +443,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
437 |
box_btn.click(
|
438 |
fn=process_bbox,
|
439 |
inputs=[box_image, box_input],
|
440 |
-
outputs=[
|
441 |
queue=True
|
442 |
)
|
443 |
|
|
|
201 |
except Exception as e:
|
202 |
raise gr.Error(f"Processing failed: {str(e)}")
|
203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
|
206 |
def on_change_bbox(prompts: dict[str, Any] | None):
|
|
|
210 |
def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str | None = None):
|
211 |
return gr.update(interactive=bool(img and prompt))
|
212 |
|
213 |
+
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None) -> tuple[Image.Image, Image.Image]:
|
214 |
try:
|
215 |
if img is None or prompt.strip() == "":
|
216 |
raise gr.Error("Please provide both image and prompt")
|
217 |
|
218 |
# Process the image
|
219 |
+
results, _ = _process(img, prompt, bg_prompt)
|
220 |
|
221 |
+
# 합성된 이미지와 추출된 이미지만 반환
|
222 |
+
return results[1], results[2]
|
|
|
|
|
223 |
except Exception as e:
|
224 |
raise gr.Error(str(e))
|
225 |
|
226 |
+
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
227 |
+
try:
|
228 |
+
if img is None or box_input.strip() == "":
|
229 |
+
raise gr.Error("Please provide both image and bounding box coordinates")
|
230 |
+
|
231 |
+
try:
|
232 |
+
coords = eval(box_input)
|
233 |
+
if not isinstance(coords, list) or len(coords) != 4:
|
234 |
+
raise ValueError("Invalid box format")
|
235 |
+
bbox = tuple(int(x) for x in coords)
|
236 |
+
except:
|
237 |
+
raise gr.Error("Invalid box format. Please provide [xmin, ymin, xmax, ymax]")
|
238 |
+
|
239 |
+
# Process the image
|
240 |
+
results, _ = _process(img, bbox)
|
241 |
+
|
242 |
+
# 합성된 이미지와 추출된 이미지만 반환
|
243 |
+
return results[1], results[2]
|
244 |
+
except Exception as e:
|
245 |
+
raise gr.Error(str(e))
|
246 |
|
247 |
# Event handler functions 수정
|
248 |
def update_process_button(img, prompt):
|
|
|
317 |
|
318 |
# UI 부분만 수정
|
319 |
# Main Gradio app
|
320 |
+
# UI 부분 수정
|
321 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
322 |
gr.HTML("""
|
323 |
<div class="main-title">
|
|
|
353 |
)
|
354 |
|
355 |
with gr.Column(scale=1):
|
356 |
+
with gr.Row():
|
357 |
+
combined_image = gr.Image(
|
358 |
+
label="Combined Result",
|
359 |
+
show_download_button=True,
|
360 |
+
type="pil",
|
361 |
+
height=512
|
362 |
+
)
|
363 |
+
with gr.Row():
|
364 |
+
extracted_image = gr.Image(
|
365 |
+
label="Extracted Object",
|
366 |
+
show_download_button=True,
|
367 |
+
type="pil",
|
368 |
+
height=256
|
369 |
+
)
|
370 |
|
371 |
# Box-based extraction tab
|
372 |
with gr.TabItem("Extract by Box"):
|
|
|
389 |
)
|
390 |
|
391 |
with gr.Column(scale=1):
|
392 |
+
with gr.Row():
|
393 |
+
box_combined_image = gr.Image(
|
394 |
+
label="Combined Result",
|
395 |
+
show_download_button=True,
|
396 |
+
type="pil",
|
397 |
+
height=512
|
398 |
+
)
|
399 |
+
with gr.Row():
|
400 |
+
box_extracted_image = gr.Image(
|
401 |
+
label="Extracted Object",
|
402 |
+
show_download_button=True,
|
403 |
+
type="pil",
|
404 |
+
height=256
|
405 |
+
)
|
406 |
|
407 |
# Event bindings
|
408 |
input_image.change(
|
|
|
422 |
process_btn.click(
|
423 |
fn=process_prompt,
|
424 |
inputs=[input_image, text_prompt, bg_prompt],
|
425 |
+
outputs=[combined_image, extracted_image], # 출력 변경
|
426 |
queue=True
|
427 |
)
|
428 |
|
|
|
443 |
box_btn.click(
|
444 |
fn=process_bbox,
|
445 |
inputs=[box_image, box_input],
|
446 |
+
outputs=[box_combined_image, box_extracted_image], # 출력 변경
|
447 |
queue=True
|
448 |
)
|
449 |
|