ginipick commited on
Commit
2c8d28e
ยท
verified ยท
1 Parent(s): b3f8aba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -45
app.py CHANGED
@@ -349,6 +349,7 @@ def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str
349
 
350
 
351
 
 
352
  def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
353
  aspect_ratio: str = "1:1", position: str = "bottom-center",
354
  scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
@@ -356,7 +357,7 @@ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
356
  if img is None or prompt.strip() == "":
357
  raise gr.Error("Please provide both image and prompt")
358
 
359
- print(f"Processing with position: {position}, scale: {scale_percent}")
360
 
361
  try:
362
  prompt = translate_to_english(prompt)
@@ -369,13 +370,13 @@ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
369
 
370
  if bg_prompt:
371
  try:
 
372
  combined = combine_with_background(
373
  foreground=results[2],
374
  background=results[1],
375
  position=position,
376
  scale_percent=scale_percent
377
  )
378
- print(f"Combined image created with position: {position}")
379
  return combined, results[2]
380
  except Exception as e:
381
  print(f"Combination error: {str(e)}")
@@ -510,6 +511,8 @@ footer {display: none}
510
  gap: 8px;
511
  margin: 1em 0;
512
  }
 
 
513
  .position-btn {
514
  padding: 10px;
515
  border: 1px solid #ddd;
@@ -517,18 +520,25 @@ footer {display: none}
517
  background: white;
518
  cursor: pointer;
519
  transition: all 0.3s ease;
 
 
 
 
 
520
  }
 
521
  .position-btn:hover {
522
  background: #e3f2fd;
523
  }
 
524
  .position-btn.selected {
525
- background: #2196F3;
526
  color: white;
 
527
  }
528
  """
529
 
530
 
531
-
532
  def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
533
  """Helper function to draw text with stroke"""
534
  # Draw the stroke/outline
@@ -665,6 +675,9 @@ def add_text_to_image(
665
  return input_image
666
 
667
 
 
 
 
668
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
669
  gr.HTML("""
670
  <div class="main-title">
@@ -673,10 +686,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
673
  </div>
674
  """)
675
 
 
 
 
676
  with gr.Row(equal_height=True):
677
  # ์™ผ์ชฝ ํŒจ๋„ (์ž…๋ ฅ)
678
  with gr.Column(scale=1):
679
- with gr.Group(elem_classes="input-panel"): # Box๋ฅผ Group์œผ๋กœ ๋ณ€๊ฒฝ
680
  input_image = gr.Image(
681
  type="pil",
682
  label="Upload Image",
@@ -704,21 +720,21 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
704
  scale=1
705
  )
706
 
707
- with gr.Group(elem_classes="controls-panel", visible=False) as object_controls: # Box๋ฅผ Group์œผ๋กœ ๋ณ€๊ฒฝ
708
  with gr.Column(scale=1):
 
709
  with gr.Row():
710
- position = gr.State(value="bottom-center")
711
- btn_top_left = gr.Button("โ†–")
712
- btn_top_center = gr.Button("โ†‘")
713
- btn_top_right = gr.Button("โ†—")
714
  with gr.Row():
715
- btn_middle_left = gr.Button("โ†")
716
- btn_middle_center = gr.Button("โ€ข")
717
- btn_middle_right = gr.Button("โ†’")
718
  with gr.Row():
719
- btn_bottom_left = gr.Button("โ†™")
720
- btn_bottom_center = gr.Button("โ†“")
721
- btn_bottom_right = gr.Button("โ†˜")
722
  with gr.Column(scale=1):
723
  scale_slider = gr.Slider(
724
  minimum=10,
@@ -737,7 +753,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
737
 
738
  # ์˜ค๋ฅธ์ชฝ ํŒจ๋„ (์ถœ๋ ฅ)
739
  with gr.Column(scale=1):
740
- with gr.Group(elem_classes="output-panel"): # Box๋ฅผ Group์œผ๋กœ ๋ณ€๊ฒฝ
741
  with gr.Tab("Result"):
742
  combined_image = gr.Image(
743
  label="Combined Result",
@@ -817,30 +833,36 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
817
  type="pil",
818
  height=200
819
  )
820
- # ์˜ˆ์ œ ์ด๋ฏธ์ง€ ์„น์…˜ ์ˆ˜์ •
 
821
  gr.HTML("""
822
- <div class="example-section">
823
- <h3>Example Results</h3>
824
- <img src="./assets/example.png" alt="Example results" style="max-width: 100%; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);" />
825
- </div>
 
 
826
  """)
827
 
828
-
829
- # ๊ฐ ๋ฒ„ํŠผ์— ๋Œ€ํ•œ ํด๋ฆญ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
830
- def update_position(new_position):
831
  return new_position
832
 
833
- btn_top_left.click(fn=lambda: update_position("top-left"), outputs=position)
834
- btn_top_center.click(fn=lambda: update_position("top-center"), outputs=position)
835
- btn_top_right.click(fn=lambda: update_position("top-right"), outputs=position)
836
- btn_middle_left.click(fn=lambda: update_position("middle-left"), outputs=position)
837
- btn_middle_center.click(fn=lambda: update_position("middle-center"), outputs=position)
838
- btn_middle_right.click(fn=lambda: update_position("middle-right"), outputs=position)
839
- btn_bottom_left.click(fn=lambda: update_position("bottom-left"), outputs=position)
840
- btn_bottom_center.click(fn=lambda: update_position("bottom-center"), outputs=position)
841
- btn_bottom_right.click(fn=lambda: update_position("bottom-right"), outputs=position)
842
-
843
- # Event bindings
 
 
 
844
  input_image.change(
845
  fn=update_process_button,
846
  inputs=[input_image, text_prompt],
@@ -855,14 +877,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
855
  queue=False
856
  )
857
 
858
- def update_controls(bg_prompt):
859
- """๋ฐฐ๊ฒฝ ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ปจํŠธ๋กค ํ‘œ์‹œ ์—…๋ฐ์ดํŠธ"""
860
- is_visible = bool(bg_prompt)
861
- return [
862
- gr.update(visible=is_visible), # aspect_ratio
863
- gr.update(visible=is_visible), # object_controls
864
- ]
865
-
866
  bg_prompt.change(
867
  fn=update_controls,
868
  inputs=bg_prompt,
@@ -870,6 +884,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
870
  queue=False
871
  )
872
 
 
873
  process_btn.click(
874
  fn=process_prompt,
875
  inputs=[
@@ -877,13 +892,23 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
877
  text_prompt,
878
  bg_prompt,
879
  aspect_ratio,
880
- position,
881
  scale_slider
882
  ],
883
  outputs=[combined_image, extracted_image],
884
  queue=True
885
  )
886
 
 
 
 
 
 
 
 
 
 
 
887
  # ํ…์ŠคํŠธ ์ถ”๊ฐ€ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ ์ˆ˜์ •
888
  add_text_btn.click(
889
  fn=add_text_to_image,
@@ -902,6 +927,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
902
  outputs=combined_image
903
  )
904
 
 
905
  demo.queue(max_size=5) # ํ ํฌ๊ธฐ ์ œํ•œ
906
  demo.launch(
907
  server_name="0.0.0.0",
 
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]:
 
357
  if img is None or prompt.strip() == "":
358
  raise gr.Error("Please provide both image and prompt")
359
 
360
+ print(f"Processing with position: {position}, scale: {scale_percent}") # ๋””๋ฒ„๊น…์šฉ
361
 
362
  try:
363
  prompt = translate_to_english(prompt)
 
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],
377
  position=position,
378
  scale_percent=scale_percent
379
  )
 
380
  return combined, results[2]
381
  except Exception as e:
382
  print(f"Combination error: {str(e)}")
 
511
  gap: 8px;
512
  margin: 1em 0;
513
  }
514
+
515
+
516
  .position-btn {
517
  padding: 10px;
518
  border: 1px solid #ddd;
 
520
  background: white;
521
  cursor: pointer;
522
  transition: all 0.3s ease;
523
+ width: 40px;
524
+ height: 40px;
525
+ display: flex;
526
+ align-items: center;
527
+ justify-content: center;
528
  }
529
+
530
  .position-btn:hover {
531
  background: #e3f2fd;
532
  }
533
+
534
  .position-btn.selected {
535
+ background-color: #2196F3;
536
  color: white;
537
+ border-color: #1976D2;
538
  }
539
  """
540
 
541
 
 
542
  def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
543
  """Helper function to draw text with stroke"""
544
  # Draw the stroke/outline
 
675
  return input_image
676
 
677
 
678
+ # ์ „์—ญ ๋ณ€์ˆ˜ ์„ค์ •
679
+ current_position = None # position ์ƒํƒœ๋ฅผ ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
680
+
681
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
682
  gr.HTML("""
683
  <div class="main-title">
 
686
  </div>
687
  """)
688
 
689
+ # ์ „์—ญ position ์ƒํƒœ ์ถ”๊ฐ€
690
+ current_position = gr.State("bottom-center")
691
+
692
  with gr.Row(equal_height=True):
693
  # ์™ผ์ชฝ ํŒจ๋„ (์ž…๋ ฅ)
694
  with gr.Column(scale=1):
695
+ with gr.Group(elem_classes="input-panel"):
696
  input_image = gr.Image(
697
  type="pil",
698
  label="Upload Image",
 
720
  scale=1
721
  )
722
 
723
+ with gr.Group(elem_classes="controls-panel", visible=False) as object_controls:
724
  with gr.Column(scale=1):
725
+ position = gr.State(value="bottom-center") # ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
726
  with gr.Row():
727
+ btn_top_left = gr.Button("โ†–", elem_classes="position-btn")
728
+ btn_top_center = gr.Button("โ†‘", elem_classes="position-btn")
729
+ btn_top_right = gr.Button("โ†—", elem_classes="position-btn")
 
730
  with gr.Row():
731
+ btn_middle_left = gr.Button("โ†", elem_classes="position-btn")
732
+ btn_middle_center = gr.Button("โ€ข", elem_classes="position-btn")
733
+ btn_middle_right = gr.Button("โ†’", elem_classes="position-btn")
734
  with gr.Row():
735
+ btn_bottom_left = gr.Button("โ†™", elem_classes="position-btn")
736
+ btn_bottom_center = gr.Button("โ†“", elem_classes="position-btn", value="selected")
737
+ btn_bottom_right = gr.Button("โ†˜", elem_classes="position-btn")
738
  with gr.Column(scale=1):
739
  scale_slider = gr.Slider(
740
  minimum=10,
 
753
 
754
  # ์˜ค๋ฅธ์ชฝ ํŒจ๋„ (์ถœ๋ ฅ)
755
  with gr.Column(scale=1):
756
+ with gr.Group(elem_classes="output-panel"):
757
  with gr.Tab("Result"):
758
  combined_image = gr.Image(
759
  label="Combined Result",
 
833
  type="pil",
834
  height=200
835
  )
836
+
837
+ # CSS ํด๋ž˜์Šค๋ฅผ ์œ„ํ•œ ์Šคํƒ€์ผ ์ถ”๊ฐ€
838
  gr.HTML("""
839
+ <style>
840
+ .position-btn.selected {
841
+ background-color: #2196F3 !important;
842
+ color: white !important;
843
+ }
844
+ </style>
845
  """)
846
 
847
+ # ์œ„์น˜ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜
848
+ def update_position(new_position, current):
849
+ print(f"Position updated from {current} to {new_position}") # ๋””๋ฒ„๊น…์šฉ
850
  return new_position
851
 
852
+ # ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
853
+ btn_top_left.click(fn=update_position, inputs=["top-left", position], outputs=position)
854
+ btn_top_center.click(fn=update_position, inputs=["top-center", position], outputs=position)
855
+ btn_top_right.click(fn=update_position, inputs=["top-right", position], outputs=position)
856
+ btn_middle_left.click(fn=update_position, inputs=["middle-left", position], outputs=position)
857
+ btn_middle_center.click(fn=update_position, inputs=["middle-center", position], outputs=position)
858
+ btn_middle_right.click(fn=update_position, inputs=["middle-right", position], outputs=position)
859
+ btn_bottom_left.click(fn=update_position, inputs=["bottom-left", position], outputs=position)
860
+ btn_bottom_center.click(fn=update_position, inputs=["bottom-center", position], outputs=position)
861
+ btn_bottom_right.click(fn=update_position, inputs=["bottom-right", position], outputs=position)
862
+
863
+
864
+
865
+ # ๊ธฐ์กด ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
866
  input_image.change(
867
  fn=update_process_button,
868
  inputs=[input_image, text_prompt],
 
877
  queue=False
878
  )
879
 
 
 
 
 
 
 
 
 
880
  bg_prompt.change(
881
  fn=update_controls,
882
  inputs=bg_prompt,
 
884
  queue=False
885
  )
886
 
887
+ # Process ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์ˆ˜์ •
888
  process_btn.click(
889
  fn=process_prompt,
890
  inputs=[
 
892
  text_prompt,
893
  bg_prompt,
894
  aspect_ratio,
895
+ position, # position ์ƒํƒœ ์ „๋‹ฌ
896
  scale_slider
897
  ],
898
  outputs=[combined_image, extracted_image],
899
  queue=True
900
  )
901
 
902
+
903
+ def update_controls(bg_prompt):
904
+ """๋ฐฐ๊ฒฝ ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ปจํŠธ๋กค ํ‘œ์‹œ ์—…๋ฐ์ดํŠธ"""
905
+ is_visible = bool(bg_prompt)
906
+ return [
907
+ gr.update(visible=is_visible), # aspect_ratio
908
+ gr.update(visible=is_visible), # object_controls
909
+ ]
910
+
911
+
912
  # ํ…์ŠคํŠธ ์ถ”๊ฐ€ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ ์ˆ˜์ •
913
  add_text_btn.click(
914
  fn=add_text_to_image,
 
927
  outputs=combined_image
928
  )
929
 
930
+
931
  demo.queue(max_size=5) # ํ ํฌ๊ธฐ ์ œํ•œ
932
  demo.launch(
933
  server_name="0.0.0.0",