Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
analytics_enabled=False, # 분석 비활성화
|
| 613 |
-
cache_examples=False # 예제 캐싱 비활성화
|
| 614 |
-
)
|
| 615 |
|
| 616 |
-
|
| 617 |
-
gr.
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 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 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 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.
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
)
|
| 682 |
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
label="Text to Add",
|
| 687 |
-
placeholder="Enter text..."
|
| 688 |
-
)
|
| 689 |
-
with gr.Row():
|
| 690 |
with gr.Column(scale=1):
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
|
| 707 |
-
value="White",
|
| 708 |
-
label="Text Color"
|
| 709 |
-
)
|
| 710 |
with gr.Column(scale=1):
|
| 711 |
-
|
| 712 |
-
minimum=
|
| 713 |
-
maximum=
|
| 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=
|
| 730 |
-
label="
|
| 731 |
)
|
| 732 |
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 782 |
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
|
|
|
|
|
|
|
|
|
| 817 |
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 823 |
|
| 824 |
-
|
| 825 |
-
demo.
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 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 |
+
)
|