Update app.py
Browse files
app.py
CHANGED
@@ -12,10 +12,10 @@ load_dotenv()
|
|
12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
-
#
|
16 |
IMAGE_CACHE = {}
|
17 |
|
18 |
-
#
|
19 |
custom_css = """
|
20 |
:root {
|
21 |
--primary-color: #FB7F0D;
|
@@ -30,7 +30,7 @@ custom_css = """
|
|
30 |
|
31 |
.example-gallery {
|
32 |
display: grid;
|
33 |
-
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
|
34 |
gap: 20px;
|
35 |
padding: 20px;
|
36 |
}
|
@@ -48,7 +48,7 @@ custom_css = """
|
|
48 |
}
|
49 |
.example-item img {
|
50 |
width: 80%;
|
51 |
-
height: 80px;
|
52 |
object-fit: cover;
|
53 |
}
|
54 |
|
@@ -97,23 +97,29 @@ body {
|
|
97 |
footer { visibility: hidden; }
|
98 |
"""
|
99 |
|
100 |
-
#
|
101 |
fontawesome_link = """
|
102 |
<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css\" crossorigin=\"anonymous\" referrerpolicy=\"no-referrer\" />
|
103 |
"""
|
104 |
|
105 |
-
#
|
106 |
|
107 |
def load_image_cached(path):
|
108 |
if path not in IMAGE_CACHE:
|
109 |
try:
|
110 |
img = Image.open(path)
|
|
|
|
|
|
|
|
|
|
|
111 |
IMAGE_CACHE[path] = img
|
112 |
except Exception as e:
|
113 |
logger.error(f"이미지 로드 실패: {path} - {e}")
|
114 |
return None
|
115 |
return IMAGE_CACHE[path]
|
116 |
|
|
|
117 |
|
118 |
def preload_example_images():
|
119 |
for ex in product_background_examples:
|
@@ -142,31 +148,51 @@ def create_app():
|
|
142 |
font=[gr.themes.GoogleFont("Noto Sans KR"), "ui-sans-serif", "system-ui"]
|
143 |
)) as demo:
|
144 |
gr.HTML(fontawesome_link)
|
|
|
145 |
# 메인 뷰: 첫 번째 예시
|
146 |
with gr.Column(elem_classes="custom-section-group custom-frame"):
|
147 |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/681/681443.png"> 배경 유형별 예시</div>')
|
148 |
with gr.Row():
|
149 |
-
example_input_image = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
with gr.Column():
|
151 |
example_bg_type = gr.Textbox(label="배경 유형", interactive=False)
|
152 |
example_bg_option = gr.Textbox(label="배경 선택", interactive=False)
|
153 |
example_product_name = gr.Textbox(label="상품명", interactive=False)
|
154 |
example_additional_info = gr.Textbox(label="추가 요청사항", interactive=False)
|
155 |
-
example_output_image = gr.Image(
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
with gr.Column(elem_classes="custom-section-group"):
|
159 |
-
# 유형별 그룹핑
|
160 |
bg_groups = {}
|
161 |
for idx, ex in enumerate(product_background_examples):
|
162 |
bg_groups.setdefault(ex[1], []).append((idx, ex))
|
163 |
for bg_type, items in bg_groups.items():
|
164 |
-
# 그룹 헤더
|
165 |
gr.HTML(f'<div class="section-title">{bg_type}</div>')
|
166 |
-
# 그룹 아이템
|
167 |
with gr.Row(elem_classes="example-gallery"):
|
168 |
for idx, ex in items:
|
169 |
-
item = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
item.select(
|
171 |
fn=load_example,
|
172 |
inputs=None,
|
|
|
12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
+
# 이미지 캐시
|
16 |
IMAGE_CACHE = {}
|
17 |
|
18 |
+
# CSS (원본 스타일 유지 + 예시 크기 축소)
|
19 |
custom_css = """
|
20 |
:root {
|
21 |
--primary-color: #FB7F0D;
|
|
|
30 |
|
31 |
.example-gallery {
|
32 |
display: grid;
|
33 |
+
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
|
34 |
gap: 20px;
|
35 |
padding: 20px;
|
36 |
}
|
|
|
48 |
}
|
49 |
.example-item img {
|
50 |
width: 80%;
|
51 |
+
height: 80px;
|
52 |
object-fit: cover;
|
53 |
}
|
54 |
|
|
|
97 |
footer { visibility: hidden; }
|
98 |
"""
|
99 |
|
100 |
+
# 아이콘 링크
|
101 |
fontawesome_link = """
|
102 |
<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css\" crossorigin=\"anonymous\" referrerpolicy=\"no-referrer\" />
|
103 |
"""
|
104 |
|
105 |
+
# 이미지 로드 및 캐시
|
106 |
|
107 |
def load_image_cached(path):
|
108 |
if path not in IMAGE_CACHE:
|
109 |
try:
|
110 |
img = Image.open(path)
|
111 |
+
# 최대 크기 1000px로 축소
|
112 |
+
max_dim = max(img.size)
|
113 |
+
if max_dim > 1000:
|
114 |
+
ratio = 1000 / max_dim
|
115 |
+
img = img.resize((int(img.width*ratio), int(img.height*ratio)), Image.Resampling.LANCZOS)
|
116 |
IMAGE_CACHE[path] = img
|
117 |
except Exception as e:
|
118 |
logger.error(f"이미지 로드 실패: {path} - {e}")
|
119 |
return None
|
120 |
return IMAGE_CACHE[path]
|
121 |
|
122 |
+
# 예시 미리 로드
|
123 |
|
124 |
def preload_example_images():
|
125 |
for ex in product_background_examples:
|
|
|
148 |
font=[gr.themes.GoogleFont("Noto Sans KR"), "ui-sans-serif", "system-ui"]
|
149 |
)) as demo:
|
150 |
gr.HTML(fontawesome_link)
|
151 |
+
|
152 |
# 메인 뷰: 첫 번째 예시
|
153 |
with gr.Column(elem_classes="custom-section-group custom-frame"):
|
154 |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/681/681443.png"> 배경 유형별 예시</div>')
|
155 |
with gr.Row():
|
156 |
+
example_input_image = gr.Image(
|
157 |
+
label="입력 이미지",
|
158 |
+
elem_classes="image-container",
|
159 |
+
interactive=False,
|
160 |
+
show_download_button=False,
|
161 |
+
show_share_button=False,
|
162 |
+
container=False
|
163 |
+
)
|
164 |
with gr.Column():
|
165 |
example_bg_type = gr.Textbox(label="배경 유형", interactive=False)
|
166 |
example_bg_option = gr.Textbox(label="배경 선택", interactive=False)
|
167 |
example_product_name = gr.Textbox(label="상품명", interactive=False)
|
168 |
example_additional_info = gr.Textbox(label="추가 요청사항", interactive=False)
|
169 |
+
example_output_image = gr.Image(
|
170 |
+
label="결과 이미지",
|
171 |
+
elem_classes="image-container",
|
172 |
+
interactive=False,
|
173 |
+
show_download_button=False,
|
174 |
+
show_share_button=False,
|
175 |
+
container=False
|
176 |
+
)
|
177 |
+
|
178 |
+
# 갤러리 뷰: 유형별 그룹화
|
179 |
with gr.Column(elem_classes="custom-section-group"):
|
|
|
180 |
bg_groups = {}
|
181 |
for idx, ex in enumerate(product_background_examples):
|
182 |
bg_groups.setdefault(ex[1], []).append((idx, ex))
|
183 |
for bg_type, items in bg_groups.items():
|
|
|
184 |
gr.HTML(f'<div class="section-title">{bg_type}</div>')
|
|
|
185 |
with gr.Row(elem_classes="example-gallery"):
|
186 |
for idx, ex in items:
|
187 |
+
item = gr.Image(
|
188 |
+
value=ex[5],
|
189 |
+
show_label=False,
|
190 |
+
elem_classes="example-item",
|
191 |
+
interactive=False,
|
192 |
+
show_download_button=False,
|
193 |
+
show_share_button=False,
|
194 |
+
container=False
|
195 |
+
)
|
196 |
item.select(
|
197 |
fn=load_example,
|
198 |
inputs=None,
|