File size: 6,377 Bytes
da240c7
37db736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1d6c1b
37db736
 
d1d6c1b
 
 
37db736
d1d6c1b
37db736
 
d1d6c1b
37db736
 
 
 
70e1750
37db736
 
 
d1d6c1b
 
 
 
 
 
 
 
 
 
 
 
37db736
70e1750
3cf520d
37db736
 
 
 
 
 
d1d6c1b
 
37db736
d1d6c1b
37db736
 
 
 
d1d6c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37db736
d1d6c1b
 
 
 
 
 
 
 
 
37db736
d1d6c1b
 
37db736
 
 
 
d1d6c1b
37db736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0814c0c
d1d6c1b
0814c0c
d1d6c1b
 
83f9d7b
d1d6c1b
 
 
 
 
 
0814c0c
d1d6c1b
0814c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
debd74e
0814c0c
d1d6c1b
 
 
 
 
 
 
 
 
 
 
 
 
37db736
 
 
 
 
d1d6c1b
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
199
200
201
import os
import logging
from PIL import Image
import gradio as gr
from dotenv import load_dotenv
from db_examples import product_background_examples

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋กœ๋“œ
load_dotenv()

# ๋กœ๊น… ์„ค์ •
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# ์บ์‹œ๋œ ์ด๋ฏธ์ง€ ์ €์žฅ์†Œ
IMAGE_CACHE = {}

# ์ปค์Šคํ…€ CSS ์Šคํƒ€์ผ (์›๋ณธ๊ณผ ์™„์ „ ๋™์ผ)
custom_css = """
:root {
    --primary-color: #FB7F0D;
    --secondary-color: #ff9a8b;
    --accent-color: #FF6B6B;
    --background-color: #FFFFFF;
    --card-bg: #ffffff;
    --text-color: #334155;
    --border-radius: 18px;
    --shadow: 0 8px 30px rgba(251, 127, 13, 0.08);
}

.example-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); /* 200px โ†’ 150px */
    gap: 20px;
    padding: 20px;
}
.example-item {
    cursor: pointer;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: var(--border-radius);
    overflow: hidden;
    transition: all 0.3s ease;
    background: white;
}
.example-item:hover {
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
}
.example-item img {
    width: 80%;
    height: 80px;       /* 100px โ†’ 80px */
    object-fit: cover;
}

.image-container {
    border-radius: var(--border-radius);
    overflow: hidden;
    border: 1px solid rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    background-color: white;
    aspect-ratio: 1 / 1;
}
.image-container:hover {
    box-shadow: var(--shadow);
}
.image-container img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.section-title {
    display: flex;
    align-items: center;
    font-size: 20px;
    font-weight: 700;
    color: #333333;
    margin-bottom: 10px;
    padding-bottom: 5px;
    border-bottom: 2px solid #FB7F0D;
    font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
}
.section-title img {
    margin-right: 10px;
    width: 24px;
    height: 24px;
}

body {
    font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
    background-color: var(--background-color);
    color: var(--text-color);
    line-height: 1.6;
    margin: 0;
    padding: 0;
}
footer { visibility: hidden; }
"""

# FontAwesome ๋งํฌ
fontawesome_link = """
<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css\" crossorigin=\"anonymous\" referrerpolicy=\"no-referrer\" />
"""

# ์˜ˆ์‹œ ์ด๋ฏธ์ง€ ์บ์‹ฑ ๋ฐ ๋กœ๋“œ ํ•จ์ˆ˜

def load_image_cached(path):
    if path not in IMAGE_CACHE:
        try:
            img = Image.open(path)
            IMAGE_CACHE[path] = img
        except Exception as e:
            logger.error(f"์ด๋ฏธ์ง€ ๋กœ๋“œ ์‹คํŒจ: {path} - {e}")
            return None
    return IMAGE_CACHE[path]


def preload_example_images():
    for ex in product_background_examples:
        load_image_cached(ex[0])
        load_image_cached(ex[5])

# ์˜ˆ์‹œ ์„ ํƒ ํ•ธ๋“ค๋Ÿฌ

def load_example(evt: gr.SelectData):
    ex = product_background_examples[evt.index]
    return ex[0], ex[1], ex[2], ex[3], ex[4] or "(์—†์Œ)", ex[5]

# ์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ ์ž๋™ ๋กœ๋“œ

def load_first_example():
    if product_background_examples:
        ex = product_background_examples[0]
        return ex[0], ex[1], ex[2], ex[3], ex[4] or "(์—†์Œ)", ex[5]
    return None, "", "", "", "", None

# Gradio ์•ฑ ์ƒ์„ฑ

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-section-group 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="์ž…๋ ฅ ์ด๋ฏธ์ง€", elem_classes="image-container")
                with gr.Column():
                    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="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€", elem_classes="image-container")

        # ๋ฐฐ๊ฒฝ ์œ ํ˜•๋ณ„ ๊ฐค๋Ÿฌ๋ฆฌ ๋ทฐ
        with gr.Column(elem_classes="custom-section-group"):
            # ์œ ํ˜•๋ณ„ ๊ทธ๋ฃนํ•‘
            bg_groups = {}
            for idx, ex in enumerate(product_background_examples):
                bg_groups.setdefault(ex[1], []).append((idx, ex))
            for bg_type, items in bg_groups.items():
                # ๊ทธ๋ฃน ํ—ค๋”
                gr.HTML(f'<div class="section-title">{bg_type}</div>')
                # ๊ทธ๋ฃน ์•„์ดํ…œ
                with gr.Row(elem_classes="example-gallery"):
                    for idx, ex in items:
                        item = gr.Image(value=ex[5], show_label=False, elem_classes="example-item")
                        item.select(
                            fn=load_example,
                            inputs=None,
                            outputs=[
                                example_input_image,
                                example_bg_type,
                                example_bg_option,
                                example_product_name,
                                example_additional_info,
                                example_output_image
                            ]
                        )

        # ์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ ์ž๋™ ๋กœ๋“œ
        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__":
    preload_example_images()
    app = create_app()
    app.launch(share=False, inbrowser=True, width="100%")