aiqtech commited on
Commit
b785f0c
·
verified ·
1 Parent(s): b52ff62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +204 -210
app.py CHANGED
@@ -606,229 +606,223 @@ def update_process_button(img, prompt):
606
  variant="primary" if bool(img and prompt) else "secondary"
607
  )
608
 
609
- demo = gr.Blocks(
610
- theme=gr.themes.Soft(),
611
- css=css,
612
- analytics_enabled=False, # 분석 비활성화
613
- cache_examples=False # 예제 캐싱 비활성화
614
- )
615
 
616
-
617
- gr.HTML("""
618
- <div class="main-title">
619
- <h1>🎨GiniGen Canvas</h1>
620
- <p>AI Integrated Image Creator: Extract objects, generate backgrounds, and adjust ratios and positions to create complete images with AI.</p>
621
- </div>
622
- """)
623
-
624
- with gr.Row():
625
- # 입력 컬럼
626
- with gr.Column(scale=1):
627
- input_image = gr.Image(
628
- type="pil",
629
- label="Upload Image",
630
- interactive=True
631
- )
632
- text_prompt = gr.Textbox(
633
- label="Object to Extract",
634
- placeholder="Enter what you want to extract...",
635
- interactive=True
636
- )
637
-
638
- # 배경 및 비율 설정
639
- with gr.Row():
640
- bg_prompt = gr.Textbox(
641
- label="Background Prompt (optional)",
642
- placeholder="Describe the background...",
643
- interactive=True,
644
- scale=3
645
  )
646
- aspect_ratio = gr.Dropdown(
647
- choices=["1:1", "16:9", "9:16", "4:3"],
648
- value="1:1",
649
- label="Aspect Ratio",
650
- interactive=True,
651
- visible=True,
652
- scale=1
653
  )
654
-
655
- # 오브젝트 컨트롤
656
- with gr.Row(visible=False) as object_controls:
657
- # 위치 컨트롤
658
- with gr.Column(scale=1):
659
- position = gr.State(value="bottom-center")
660
- with gr.Row():
661
- btn_top_left = gr.Button("↖")
662
- btn_top_center = gr.Button("↑")
663
- btn_top_right = gr.Button("↗")
664
- with gr.Row():
665
- btn_middle_left = gr.Button("←")
666
- btn_middle_center = gr.Button("•")
667
- btn_middle_right = gr.Button("→")
668
- with gr.Row():
669
- btn_bottom_left = gr.Button("↙")
670
- btn_bottom_center = gr.Button("↓")
671
- btn_bottom_right = gr.Button("↘")
672
 
673
- # 크기 컨트롤
674
- with gr.Column(scale=1):
675
- scale_slider = gr.Slider(
676
- minimum=10,
677
- maximum=200,
678
- value=50,
679
- step=5,
680
- label="Object Size (%)"
 
 
 
 
 
 
 
681
  )
682
 
683
- # 텍스트 입력 섹션
684
- with gr.Group() as text_group:
685
- text_input = gr.Textbox(
686
- label="Text to Add",
687
- placeholder="Enter text..."
688
- )
689
- with gr.Row():
690
  with gr.Column(scale=1):
691
- font_size = gr.Slider(
692
- minimum=10,
693
- maximum=800,
694
- value=400,
695
- step=10,
696
- label="Font Size"
697
- )
698
- thickness = gr.Slider(
699
- minimum=0,
700
- maximum=20,
701
- value=0,
702
- step=1,
703
- label="Text Thickness"
704
- )
705
- color_dropdown = gr.Dropdown(
706
- choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
707
- value="White",
708
- label="Text Color"
709
- )
710
  with gr.Column(scale=1):
711
- opacity_slider = gr.Slider(
712
- minimum=0,
713
- maximum=255,
714
- value=255,
715
- step=1,
716
- label="Opacity"
717
- )
718
- text_x_position = gr.Slider(
719
- minimum=0,
720
- maximum=100,
721
- value=50,
722
- step=1,
723
- label="Text X Position (%)"
724
- )
725
- text_y_position = gr.Slider(
726
- minimum=0,
727
- maximum=100,
728
  value=50,
729
- step=1,
730
- label="Text Y Position (%)"
731
  )
732
 
733
- # 처리 버튼
734
- process_btn = gr.Button(
735
- "Process",
736
- variant="primary",
737
- interactive=False
738
- )
739
-
740
- # 출력 컬럼
741
- with gr.Column(scale=1):
742
- combined_image = gr.Image(
743
- label="Combined Result",
744
- show_download_button=True,
745
- type="pil",
746
- height=512
747
- )
748
- extracted_image = gr.Image(
749
- label="Extracted Object",
750
- show_download_button=True,
751
- type="pil",
752
- height=256
753
- )
754
-
755
-
756
-
757
- def get_text_params():
758
- return {
759
- 'text': text_input.value,
760
- 'font_size': font_size.value,
761
- 'thickness': thickness.value,
762
- 'color': color_dropdown.value,
763
- 'opacity': opacity_slider.value,
764
- 'x_position': text_x_position.value,
765
- 'y_position': text_y_position.value
766
- }
767
-
768
- # 이벤트 바인딩
769
- input_image.change(
770
- fn=update_process_button,
771
- inputs=[input_image, text_prompt],
772
- outputs=process_btn,
773
- queue=False
774
- )
775
-
776
- text_prompt.change(
777
- fn=update_process_button,
778
- inputs=[input_image, text_prompt],
779
- outputs=process_btn,
780
- queue=False
781
- )
 
 
 
 
 
 
 
782
 
783
- bg_prompt.change(
784
- fn=update_controls,
785
- inputs=[bg_prompt],
786
- outputs=[aspect_ratio, object_controls],
787
- queue=False
788
- )
 
 
 
 
 
 
 
 
789
 
790
- # 위치 버튼 이벤트 (한 번만 정의)
791
- for btn, pos in [
792
- (btn_top_left, "top-left"), (btn_top_center, "top-center"), (btn_top_right, "top-right"),
793
- (btn_middle_left, "middle-left"), (btn_middle_center, "middle-center"), (btn_middle_right, "middle-right"),
794
- (btn_bottom_left, "bottom-left"), (btn_bottom_center, "bottom-center"), (btn_bottom_right, "bottom-right")
795
- ]:
796
- btn.click(fn=lambda p=pos: p, outputs=position)
797
-
798
- # 메인 프로세스 이벤트
799
- process_btn.click(
800
- fn=process_prompt,
801
- inputs=[
802
- input_image,
803
- text_prompt,
804
- bg_prompt,
805
- aspect_ratio,
806
- position,
807
- scale_slider,
808
- gr.State(get_text_params) # State 객체로 감싸서 전달
809
- ],
810
- outputs=[combined_image, extracted_image],
811
- queue=True
812
- )
 
 
 
813
 
814
- # HF 토큰 및 파이프라인 초기화
815
- if HF_TOKEN:
816
- login(token=HF_TOKEN, add_to_git_credential=False)
 
 
 
817
 
818
- pipe = FluxPipeline.from_pretrained(
819
- "black-forest-labs/FLUX.1-dev",
820
- torch_dtype=torch.float16,
821
- token=HF_TOKEN
822
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823
 
824
- demo.queue(max_size=3) # 크기 감소
825
- demo.launch(
826
- server_name="0.0.0.0",
827
- server_port=7860,
828
- share=False,
829
- max_threads=2,
830
- enable_queue=True,
831
- cache_examples=False,
832
- show_error=True,
833
- show_tips=False
834
- )
 
 
606
  variant="primary" if bool(img and prompt) else "secondary"
607
  )
608
 
609
+ if __name__ == "__main__":
610
+ # 모델 초기화
611
+ initialize_models()
 
 
 
612
 
613
+ # Gradio UI 정의
614
+ with gr.Blocks(
615
+ theme=gr.themes.Soft(),
616
+ css=css,
617
+ analytics_enabled=False,
618
+ cache_examples=False
619
+ ) as demo:
620
+ # HTML 헤더
621
+ gr.HTML("""
622
+ <div class="main-title">
623
+ <h1>🎨GiniGen Canvas</h1>
624
+ <p>AI Integrated Image Creator: Extract objects, generate backgrounds, and adjust ratios and positions to create complete images with AI.</p>
625
+ </div>
626
+ """)
627
+
628
+ with gr.Row():
629
+ # 입력 컬럼
630
+ with gr.Column(scale=1):
631
+ input_image = gr.Image(
632
+ type="pil",
633
+ label="Upload Image",
634
+ interactive=True
 
 
 
 
 
 
 
635
  )
636
+ text_prompt = gr.Textbox(
637
+ label="Object to Extract",
638
+ placeholder="Enter what you want to extract...",
639
+ interactive=True
 
 
 
640
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641
 
642
+ # 배경 및 비율 설정
643
+ with gr.Row():
644
+ bg_prompt = gr.Textbox(
645
+ label="Background Prompt (optional)",
646
+ placeholder="Describe the background...",
647
+ interactive=True,
648
+ scale=3
649
+ )
650
+ aspect_ratio = gr.Dropdown(
651
+ choices=["1:1", "16:9", "9:16", "4:3"],
652
+ value="1:1",
653
+ label="Aspect Ratio",
654
+ interactive=True,
655
+ visible=True,
656
+ scale=1
657
  )
658
 
659
+ # 오브젝트 컨트롤
660
+ with gr.Row(visible=False) as object_controls:
661
+ # 위치 컨트롤
 
 
 
 
662
  with gr.Column(scale=1):
663
+ position = gr.State(value="bottom-center")
664
+ with gr.Row():
665
+ btn_top_left = gr.Button("↖")
666
+ btn_top_center = gr.Button("↑")
667
+ btn_top_right = gr.Button("↗")
668
+ with gr.Row():
669
+ btn_middle_left = gr.Button("←")
670
+ btn_middle_center = gr.Button("•")
671
+ btn_middle_right = gr.Button("→")
672
+ with gr.Row():
673
+ btn_bottom_left = gr.Button("↙")
674
+ btn_bottom_center = gr.Button("↓")
675
+ btn_bottom_right = gr.Button("↘")
676
+
677
+ # 크기 컨트롤
 
 
 
 
678
  with gr.Column(scale=1):
679
+ scale_slider = gr.Slider(
680
+ minimum=10,
681
+ maximum=200,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682
  value=50,
683
+ step=5,
684
+ label="Object Size (%)"
685
  )
686
 
687
+ # 텍스트 입력 섹션
688
+ with gr.Group() as text_group:
689
+ text_input = gr.Textbox(
690
+ label="Text to Add",
691
+ placeholder="Enter text..."
692
+ )
693
+ with gr.Row():
694
+ with gr.Column(scale=1):
695
+ font_size = gr.Slider(
696
+ minimum=10,
697
+ maximum=800,
698
+ value=400,
699
+ step=10,
700
+ label="Font Size"
701
+ )
702
+ thickness = gr.Slider(
703
+ minimum=0,
704
+ maximum=20,
705
+ value=0,
706
+ step=1,
707
+ label="Text Thickness"
708
+ )
709
+ color_dropdown = gr.Dropdown(
710
+ choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
711
+ value="White",
712
+ label="Text Color"
713
+ )
714
+ with gr.Column(scale=1):
715
+ opacity_slider = gr.Slider(
716
+ minimum=0,
717
+ maximum=255,
718
+ value=255,
719
+ step=1,
720
+ label="Opacity"
721
+ )
722
+ text_x_position = gr.Slider(
723
+ minimum=0,
724
+ maximum=100,
725
+ value=50,
726
+ step=1,
727
+ label="Text X Position (%)"
728
+ )
729
+ text_y_position = gr.Slider(
730
+ minimum=0,
731
+ maximum=100,
732
+ value=50,
733
+ step=1,
734
+ label="Text Y Position (%)"
735
+ )
736
+
737
+ # 처리 버튼
738
+ process_btn = gr.Button(
739
+ "Process",
740
+ variant="primary",
741
+ interactive=False
742
+ )
743
 
744
+ # 출력 컬럼
745
+ with gr.Column(scale=1):
746
+ combined_image = gr.Image(
747
+ label="Combined Result",
748
+ show_download_button=True,
749
+ type="pil",
750
+ height=512
751
+ )
752
+ extracted_image = gr.Image(
753
+ label="Extracted Object",
754
+ show_download_button=True,
755
+ type="pil",
756
+ height=256
757
+ )
758
 
759
+ # 텍스트 파라미터 가져오기 함수
760
+ def get_text_params():
761
+ return {
762
+ 'text': text_input.value,
763
+ 'font_size': font_size.value,
764
+ 'thickness': thickness.value,
765
+ 'color': color_dropdown.value,
766
+ 'opacity': opacity_slider.value,
767
+ 'x_position': text_x_position.value,
768
+ 'y_position': text_y_position.value
769
+ }
770
+
771
+ # 이벤트 바인딩
772
+ input_image.change(
773
+ fn=update_process_button,
774
+ inputs=[input_image, text_prompt],
775
+ outputs=process_btn,
776
+ queue=False
777
+ )
778
+
779
+ text_prompt.change(
780
+ fn=update_process_button,
781
+ inputs=[input_image, text_prompt],
782
+ outputs=process_btn,
783
+ queue=False
784
+ )
785
 
786
+ bg_prompt.change(
787
+ fn=update_controls,
788
+ inputs=[bg_prompt],
789
+ outputs=[aspect_ratio, object_controls],
790
+ queue=False
791
+ )
792
 
793
+ # 위치 버튼 이벤트
794
+ for btn, pos in [
795
+ (btn_top_left, "top-left"), (btn_top_center, "top-center"), (btn_top_right, "top-right"),
796
+ (btn_middle_left, "middle-left"), (btn_middle_center, "middle-center"), (btn_middle_right, "middle-right"),
797
+ (btn_bottom_left, "bottom-left"), (btn_bottom_center, "bottom-center"), (btn_bottom_right, "bottom-right")
798
+ ]:
799
+ btn.click(fn=lambda p=pos: p, outputs=position)
800
+
801
+ # 메인 프로세스 이벤트
802
+ process_btn.click(
803
+ fn=process_prompt,
804
+ inputs=[
805
+ input_image,
806
+ text_prompt,
807
+ bg_prompt,
808
+ aspect_ratio,
809
+ position,
810
+ scale_slider,
811
+ gr.State(get_text_params)
812
+ ],
813
+ outputs=[combined_image, extracted_image],
814
+ queue=True
815
+ )
816
 
817
+ # Gradio 실행
818
+ demo.queue(max_size=3)
819
+ demo.launch(
820
+ server_name="0.0.0.0",
821
+ server_port=7860,
822
+ share=False,
823
+ max_threads=2,
824
+ enable_queue=True,
825
+ cache_examples=False,
826
+ show_error=True,
827
+ show_tips=False
828
+ )