Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -268,16 +268,14 @@ def resize_object(image: Image.Image, scale_percent: float) -> Image.Image:
|
|
268 |
def combine_with_background(foreground: Image.Image, background: Image.Image,
|
269 |
position: str = "bottom-center", scale_percent: float = 100) -> Image.Image:
|
270 |
"""전경과 배경 합성 함수"""
|
271 |
-
|
272 |
-
result = background.convert('RGBA')
|
273 |
|
274 |
-
|
275 |
scaled_foreground = resize_object(foreground, scale_percent)
|
276 |
|
277 |
-
# 오브젝트 위치 계산
|
278 |
x, y = calculate_object_position(position, result.size, scaled_foreground.size)
|
|
|
279 |
|
280 |
-
# 합성
|
281 |
result.paste(scaled_foreground, (x, y), scaled_foreground)
|
282 |
return result
|
283 |
|
@@ -348,12 +346,11 @@ def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str
|
|
348 |
return gr.update(interactive=bool(img and prompt))
|
349 |
|
350 |
|
351 |
-
|
352 |
-
# process_prompt 함수 수정
|
353 |
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
354 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
355 |
scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
|
356 |
-
try:
|
|
|
357 |
if img is None or prompt.strip() == "":
|
358 |
raise gr.Error("Please provide both image and prompt")
|
359 |
|
@@ -368,9 +365,18 @@ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
|
368 |
|
369 |
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
370 |
|
|
|
371 |
if bg_prompt:
|
372 |
try:
|
373 |
print(f"Using position: {position}") # 디버깅용
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
374 |
combined = combine_with_background(
|
375 |
foreground=results[2],
|
376 |
background=results[1],
|
@@ -381,14 +387,8 @@ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
|
381 |
except Exception as e:
|
382 |
print(f"Combination error: {str(e)}")
|
383 |
return results[1], results[2]
|
384 |
-
|
385 |
-
|
386 |
-
except Exception as e:
|
387 |
-
print(f"Error in process_prompt: {str(e)}")
|
388 |
-
raise gr.Error(str(e))
|
389 |
-
finally:
|
390 |
-
clear_memory()
|
391 |
-
|
392 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
393 |
try:
|
394 |
if img is None or box_input.strip() == "":
|
@@ -675,15 +675,10 @@ def add_text_to_image(
|
|
675 |
return input_image
|
676 |
|
677 |
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
print(f"Position updated to: {new_position}") # 디버깅용
|
683 |
-
updates = []
|
684 |
-
for btn in buttons:
|
685 |
-
updates.append(gr.update(elem_classes=["position-btn", "selected"] if btn == new_position else ["position-btn"]))
|
686 |
-
return [new_position] + updates
|
687 |
|
688 |
def update_controls(bg_prompt):
|
689 |
"""배경 프롬프트 입력 여부에 따라 컨트롤 표시 업데이트"""
|
@@ -694,6 +689,8 @@ def update_controls(bg_prompt):
|
|
694 |
]
|
695 |
|
696 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
|
|
|
697 |
gr.HTML("""
|
698 |
<div class="main-title">
|
699 |
<h1>🎨 GiniGen Canvas-o3</h1>
|
@@ -859,20 +856,27 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
859 |
</style>
|
860 |
""")
|
861 |
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
866 |
|
867 |
-
for btn, pos in
|
868 |
-
["top-left", "top-center", "top-right",
|
869 |
-
"middle-left", "middle-center", "middle-right",
|
870 |
-
"bottom-left", "bottom-center", "bottom-right"]):
|
871 |
btn.click(
|
872 |
fn=lambda p=pos: update_position(p),
|
873 |
-
outputs=
|
874 |
)
|
875 |
|
|
|
|
|
876 |
# 이벤트 바인딩
|
877 |
bg_prompt.change(
|
878 |
fn=update_controls,
|
|
|
268 |
def combine_with_background(foreground: Image.Image, background: Image.Image,
|
269 |
position: str = "bottom-center", scale_percent: float = 100) -> Image.Image:
|
270 |
"""전경과 배경 합성 함수"""
|
271 |
+
print(f"Combining with position: {position}, scale: {scale_percent}")
|
|
|
272 |
|
273 |
+
result = background.convert('RGBA')
|
274 |
scaled_foreground = resize_object(foreground, scale_percent)
|
275 |
|
|
|
276 |
x, y = calculate_object_position(position, result.size, scaled_foreground.size)
|
277 |
+
print(f"Calculated position coordinates: ({x}, {y})")
|
278 |
|
|
|
279 |
result.paste(scaled_foreground, (x, y), scaled_foreground)
|
280 |
return result
|
281 |
|
|
|
346 |
return gr.update(interactive=bool(img and prompt))
|
347 |
|
348 |
|
|
|
|
|
349 |
def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
350 |
aspect_ratio: str = "1:1", position: str = "bottom-center",
|
351 |
scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
|
352 |
+
try:
|
353 |
+
|
354 |
if img is None or prompt.strip() == "":
|
355 |
raise gr.Error("Please provide both image and prompt")
|
356 |
|
|
|
365 |
|
366 |
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
367 |
|
368 |
+
|
369 |
if bg_prompt:
|
370 |
try:
|
371 |
print(f"Using position: {position}") # 디버깅용
|
372 |
+
# 위치 값 검증
|
373 |
+
valid_positions = ["top-left", "top-center", "top-right",
|
374 |
+
"middle-left", "middle-center", "middle-right",
|
375 |
+
"bottom-left", "bottom-center", "bottom-right"]
|
376 |
+
if position not in valid_positions:
|
377 |
+
position = "bottom-center"
|
378 |
+
print(f"Invalid position, using default: {position}")
|
379 |
+
|
380 |
combined = combine_with_background(
|
381 |
foreground=results[2],
|
382 |
background=results[1],
|
|
|
387 |
except Exception as e:
|
388 |
print(f"Combination error: {str(e)}")
|
389 |
return results[1], results[2]
|
390 |
+
|
391 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
393 |
try:
|
394 |
if img is None or box_input.strip() == "":
|
|
|
675 |
return input_image
|
676 |
|
677 |
|
678 |
+
def update_position(new_position):
|
679 |
+
"""위치 업데이트 함수"""
|
680 |
+
print(f"Position updated to: {new_position}")
|
681 |
+
return new_position
|
|
|
|
|
|
|
|
|
|
|
682 |
|
683 |
def update_controls(bg_prompt):
|
684 |
"""배경 프롬프트 입력 여부에 따라 컨트롤 표시 업데이트"""
|
|
|
689 |
]
|
690 |
|
691 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
692 |
+
position = gr.State(value="bottom-center") # 여기로 이동
|
693 |
+
|
694 |
gr.HTML("""
|
695 |
<div class="main-title">
|
696 |
<h1>🎨 GiniGen Canvas-o3</h1>
|
|
|
856 |
</style>
|
857 |
""")
|
858 |
|
859 |
+
# 버튼 클릭 이벤트 바인딩
|
860 |
+
position_mapping = {
|
861 |
+
btn_top_left: "top-left",
|
862 |
+
btn_top_center: "top-center",
|
863 |
+
btn_top_right: "top-right",
|
864 |
+
btn_middle_left: "middle-left",
|
865 |
+
btn_middle_center: "middle-center",
|
866 |
+
btn_middle_right: "middle-right",
|
867 |
+
btn_bottom_left: "bottom-left",
|
868 |
+
btn_bottom_center: "bottom-center",
|
869 |
+
btn_bottom_right: "bottom-right"
|
870 |
+
}
|
871 |
|
872 |
+
for btn, pos in position_mapping.items():
|
|
|
|
|
|
|
873 |
btn.click(
|
874 |
fn=lambda p=pos: update_position(p),
|
875 |
+
outputs=position
|
876 |
)
|
877 |
|
878 |
+
|
879 |
+
|
880 |
# 이벤트 바인딩
|
881 |
bg_prompt.change(
|
882 |
fn=update_controls,
|