File size: 10,147 Bytes
9a19108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37db736
9a19108
 
 
 
 
 
 
37db736
9a19108
37db736
9a19108
0eaf572
37db736
9a19108
 
 
 
0eaf572
37db736
 
9a19108
 
37db736
 
 
9a19108
 
 
 
 
 
d1d6c1b
76c3b57
9a19108
 
 
76c3b57
9a19108
 
 
 
76c3b57
9a19108
d1d6c1b
 
 
 
76c3b57
9a19108
 
 
76c3b57
9a19108
 
 
 
76c3b57
9a19108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1d6c1b
 
 
 
 
 
 
 
 
 
 
9a19108
37db736
 
 
9a19108
 
37db736
9a19108
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
global SPECIAL_BACKGROUNDS
    
    logger.info(f"Backgrounds ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ: {BACKGROUNDS_DIR}")
    logger.info(f"๋””๋ ‰ํ† ๋ฆฌ ๋‚ด ํŒŒ์ผ ๋ชฉ๋ก: {os.listdir(BACKGROUNDS_DIR)}")
    
    SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json")
    STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json")
    NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json")
    INDOOR_BACKGROUNDS = load_background_json("indoor_backgrounds.json")
    SPECIAL_BACKGROUNDS = load_background_json("special_backgrounds.json")
    
    # ๊ธฐ๋ณธ๊ฐ’ ์„ค์ • (ํŒŒ์ผ์ด ์—†๊ฑฐ๋‚˜ ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ)
    if not SIMPLE_BACKGROUNDS:
        SIMPLE_BACKGROUNDS = {"ํด๋ž˜์‹ ํ™”์ดํŠธ": "clean white background with soft even lighting"}
    if not STUDIO_BACKGROUNDS:
        STUDIO_BACKGROUNDS = {"๋ฏธ๋‹ˆ๋ฉ€ ํ”Œ๋žซ๋ ˆ์ด": "minimalist flat lay with clean white background"}
    if not NATURE_BACKGROUNDS:
        NATURE_BACKGROUNDS = {"์—ด๋Œ€ ํ•ด๋ณ€": "tropical beach with crystal clear water"}
    if not INDOOR_BACKGROUNDS:
        INDOOR_BACKGROUNDS = {"๋ฏธ๋‹ˆ๋ฉ€ ์Šค์นธ๋””๋‚˜๋น„์•ˆ ๊ฑฐ์‹ค": "minimalist Scandinavian living room"}
    if not SPECIAL_BACKGROUNDS:
        SPECIAL_BACKGROUNDS = {"๋„ค์˜จ ๋ผ์ดํŠธ": "neon light background with vibrant glowing elements"}
    
    logger.info("๋ชจ๋“  ๋ฐฐ๊ฒฝ ์˜ต์…˜ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ")

# ------------------- ์˜ˆ์‹œ ํƒญ์„ ์œ„ํ•œ ํ•จ์ˆ˜ -------------------
def load_image_cached(image_path):
    """์ด๋ฏธ์ง€๋ฅผ ์บ์‹œํ•˜์—ฌ ๋กœ๋“œํ•˜๋Š” ํ•จ์ˆ˜"""
    global IMAGE_CACHE
    if image_path not in IMAGE_CACHE:
        try:
            img = Image.open(image_path)
            # ํฐ ์ด๋ฏธ์ง€๋Š” ๋ฏธ๋ฆฌ ๋ฆฌ์‚ฌ์ด์ฆˆํ•˜์—ฌ ์บ์‹œ
            if max(img.size) > 1000:
                ratio = 1000 / max(img.size)
                new_size = (int(img.size[0] * ratio), int(img.size[1] * ratio))
                img = img.resize(new_size, Image.Resampling.LANCZOS)
            IMAGE_CACHE[image_path] = img
        except Exception as e:
            logger.error(f"์ด๋ฏธ์ง€ ๋กœ๋“œ ์‹คํŒจ: {image_path}, ์—๋Ÿฌ: {e}")
            return None
    return IMAGE_CACHE[image_path]

def preload_example_images():
    """์˜ˆ์‹œ ์ด๋ฏธ์ง€๋“ค์„ ๋ฏธ๋ฆฌ ๋กœ๋“œํ•˜๋Š” ํ•จ์ˆ˜"""
    for example in product_background_examples:
        load_image_cached(example[0])  # ์ž…๋ ฅ ์ด๋ฏธ์ง€
        load_image_cached(example[5])  # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€

def create_app():
    with gr.Blocks(css=custom_css, theme=gr.themes.Default(
        primary_hue="orange",
        secondary_hue="orange",
        font=[gr.themes.GoogleFont("Noto Sans KR"), "ui-sans-serif", "system-ui"]
    )) as demo:
        gr.HTML(fontawesome_link)
        
        # ์ƒํ’ˆ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์˜ˆ์‹œ ๊ฐค๋Ÿฌ๋ฆฌ ์„น์…˜
        with gr.Column(elem_classes="custom-frame"):
            gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/681/681443.png"> ์ƒํ’ˆ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์˜ˆ์‹œ ๊ฐค๋Ÿฌ๋ฆฌ</div>')
            
            # ์ƒ๋‹จ ๋ฉ”์ธ ๋ทฐ ์˜์—ญ
            with gr.Row():
                example_input_image = gr.Image(
                    label="์ž…๋ ฅ ์ด๋ฏธ์ง€", 
                    height=400,
                    width=400,
                    elem_classes="image-container",
                    show_label=True,
                    show_download_button=True,
                    container=True,
                    scale=1
                )
                with gr.Column(elem_classes="example-params"):
                    example_bg_type = gr.Textbox(label="๋ฐฐ๊ฒฝ ์œ ํ˜•", interactive=False)
                    example_bg_option = gr.Textbox(label="๋ฐฐ๊ฒฝ ์„ ํƒ", interactive=False)
                    example_product_name = gr.Textbox(label="์ƒํ’ˆ๋ช…", interactive=False)
                    example_additional_info = gr.Textbox(label="์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ", interactive=False)
                example_output_image = gr.Image(
                    label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€", 
                    height=400,
                    width=400,
                    elem_classes="image-container",
                    show_label=True,
                    show_download_button=True,
                    container=True,
                    scale=1
                )
        
        # ๋ฐฐ๊ฒฝ ์œ ํ˜•๋ณ„ ๊ฐค๋Ÿฌ๋ฆฌ ์„น์…˜
        with gr.Column(elem_classes="custom-frame"):
            gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/3068/3068327.png"> ๋ฐฐ๊ฒฝ ์œ ํ˜•๋ณ„ ์˜ˆ์‹œ</div>')
            
            # ์˜ˆ์‹œ๋“ค์„ ๋ฐฐ๊ฒฝ ์œ ํ˜•๋ณ„๋กœ ๊ทธ๋ฃนํ™”
            examples_by_type = {}
            for example in product_background_examples:
                bg_type = example[1]  # ๋ฐฐ๊ฒฝ ์œ ํ˜•
                if bg_type not in examples_by_type:
                    examples_by_type[bg_type] = []
                examples_by_type[bg_type].append(example)
            
            # ๋ฐฐ๊ฒฝ ์œ ํ˜•๋ณ„ ๊ฐค๋Ÿฌ๋ฆฌ ์„น์…˜ ์ƒ์„ฑ
            for bg_type, examples in examples_by_type.items():
                gr.Markdown(f"### {bg_type}")
                # 10๊ฐœ์”ฉ ํ•œ ์ค„๋กœ ํ‘œ์‹œ
                for i in range(0, len(examples), 10):
                    with gr.Row():
                        for j in range(10):
                            if i + j < len(examples):
                                example = examples[i + j]
                                with gr.Column(scale=1, min_width=100):
                                    # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋งŒ ํ‘œ์‹œ
                                    display_img = gr.Image(
                                        value=example[5], 
                                        label=None,  # ๋ ˆ์ด๋ธ” ์ œ๊ฑฐ
                                        elem_classes="example-item",
                                        height=120,  # ํฌ๊ธฐ ์ถ•์†Œ
                                        width=120,   # ํฌ๊ธฐ ์ถ•์†Œ
                                        show_label=False,  # ๋ ˆ์ด๋ธ” ์ˆจ๊ธฐ๊ธฐ
                                        show_download_button=False,  # ๋‹ค์šด๋กœ๋“œ ๋ฒ„ํŠผ ์ˆจ๊ธฐ๊ธฐ
                                        show_share_button=False,  # ๊ณต์œ  ๋ฒ„ํŠผ ์ˆจ๊ธฐ๊ธฐ
                                        container=False  # ์ปจํ…Œ์ด๋„ˆ ํ…Œ๋‘๋ฆฌ ์ œ๊ฑฐ
                                    )
                                    
                                    def make_example_handler(ex):
                                        def handler():
                                            # ๋กœ๋”ฉ ์ƒํƒœ ํ‘œ์‹œ๋ฅผ ์œ„ํ•œ ์ค‘๊ฐ„ ์—…๋ฐ์ดํŠธ
                                            yield (
                                                gr.update(value=None),  # ์ž„์‹œ๋กœ ์ด๋ฏธ์ง€ ๋น„์šฐ๊ธฐ
                                                gr.update(value="๋กœ๋”ฉ ์ค‘..."),
                                                gr.update(value="๋กœ๋”ฉ ์ค‘..."),
                                                gr.update(value="๋กœ๋”ฉ ์ค‘..."),
                                                gr.update(value="๋กœ๋”ฉ ์ค‘..."),
                                                gr.update(value=None)
                                            )
                                            
                                            # ์‹ค์ œ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
                                            yield (
                                                ex[0],  # ์ž…๋ ฅ ์ด๋ฏธ์ง€
                                                ex[1],  # ๋ฐฐ๊ฒฝ ์œ ํ˜•
                                                ex[2],  # ๋ฐฐ๊ฒฝ ์„ ํƒ
                                                ex[3],  # ์ƒํ’ˆ๋ช…
                                                ex[4] if ex[4] else "(์—†์Œ)",  # ์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ
                                                ex[5]   # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€
                                            )
                                        return handler
                                    
                                    display_img.select(
                                        fn=make_example_handler(example),
                                        outputs=[
                                            example_input_image,
                                            example_bg_type,
                                            example_bg_option,
                                            example_product_name,
                                            example_additional_info,
                                            example_output_image
                                        ],
                                        queue=True  # ์š”์ฒญ์„ ํ์— ๋„ฃ์–ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ
                                    )
                            else:
                                # ๋นˆ ๊ณต๊ฐ„ ์œ ์ง€
                                with gr.Column(scale=1, min_width=100):
                                    pass
        
        # ํŽ˜์ด์ง€ ๋กœ๋“œ ์‹œ ์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ ์ž๋™ ํ‘œ์‹œ
        def load_first_example():
            if product_background_examples:
                first_example = product_background_examples[0]
                return (
                    first_example[0],  # ์ž…๋ ฅ ์ด๋ฏธ์ง€
                    first_example[1],  # ๋ฐฐ๊ฒฝ ์œ ํ˜•
                    first_example[2],  # ๋ฐฐ๊ฒฝ ์„ ํƒ
                    first_example[3],  # ์ƒํ’ˆ๋ช…
                    first_example[4] if first_example[4] else "(์—†์Œ)",  # ์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ
                    first_example[5]   # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€
                )
            return None, "", "", "", "", None
        
        demo.load(
            fn=load_first_example,
            outputs=[
                example_input_image,
                example_bg_type,
                example_bg_option,
                example_product_name,
                example_additional_info,
                example_output_image
            ]
        )
    
    return demo

if __name__ == "__main__":
    initialize_backgrounds()
    preload_example_images()  # ์˜ˆ์‹œ ์ด๋ฏธ์ง€ ๋ฏธ๋ฆฌ ๋กœ๋“œ
    app = create_app()
    app.queue(max_size=10)  # ์š”์ฒญ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ ์„ค์ •
    app.launch(share=False, inbrowser=True, width="100%")