Update app.py
Browse files
app.py
CHANGED
@@ -1,2 +1,120 @@
|
|
1 |
import os
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import json
|
3 |
+
import logging
|
4 |
+
from PIL import Image
|
5 |
+
import gradio as gr
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from db_examples import product_background_examples
|
8 |
+
|
9 |
+
# 환경 변수 로드
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# 로깅 설정
|
13 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
# 캐시된 이미지 저장소
|
17 |
+
IMAGE_CACHE = {}
|
18 |
+
|
19 |
+
# CSS 및 아이콘 링크 (예시 탭 전용)
|
20 |
+
custom_css = """
|
21 |
+
:root {
|
22 |
+
--background-color: #FFFFFF;
|
23 |
+
--text-color: #334155;
|
24 |
+
--border-radius: 18px;
|
25 |
+
--shadow: 0 8px 30px rgba(0,0,0,0.08);
|
26 |
+
}
|
27 |
+
|
28 |
+
.example-gallery {
|
29 |
+
display: grid;
|
30 |
+
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
31 |
+
gap: 20px;
|
32 |
+
padding: 20px;
|
33 |
+
}
|
34 |
+
.example-item img {
|
35 |
+
width: 100%;
|
36 |
+
height: 120px;
|
37 |
+
object-fit: cover;
|
38 |
+
border-radius: var(--border-radius);
|
39 |
+
}
|
40 |
+
|
41 |
+
.image-container {
|
42 |
+
border-radius: var(--border-radius);
|
43 |
+
overflow: hidden;
|
44 |
+
background-color: white;
|
45 |
+
transition: box-shadow 0.3s;
|
46 |
+
}
|
47 |
+
.image-container:hover {
|
48 |
+
box-shadow: var(--shadow);
|
49 |
+
}
|
50 |
+
|
51 |
+
.footer { visibility: hidden; }
|
52 |
+
"""
|
53 |
+
fontawesome_link = """
|
54 |
+
<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css\" crossorigin=\"anonymous\" referrerpolicy=\"no-referrer\" />
|
55 |
+
"""
|
56 |
+
|
57 |
+
# 예시 이미지 캐싱 및 로드
|
58 |
+
|
59 |
+
def load_image_cached(path):
|
60 |
+
if path not in IMAGE_CACHE:
|
61 |
+
try:
|
62 |
+
img = Image.open(path)
|
63 |
+
IMAGE_CACHE[path] = img
|
64 |
+
except Exception as e:
|
65 |
+
logger.error(f"이미지 로드 실패: {path} - {e}")
|
66 |
+
return None
|
67 |
+
return IMAGE_CACHE[path]
|
68 |
+
|
69 |
+
|
70 |
+
def preload_example_images():
|
71 |
+
for ex in product_background_examples:
|
72 |
+
load_image_cached(ex[0])
|
73 |
+
load_image_cached(ex[5])
|
74 |
+
|
75 |
+
# 예시 선택 핸들러
|
76 |
+
|
77 |
+
def load_example(evt: gr.SelectData):
|
78 |
+
ex = product_background_examples[evt.index]
|
79 |
+
return ex[0], ex[1], ex[2], ex[3], ex[4] or "(없음)", ex[5]
|
80 |
+
|
81 |
+
# 첫 번째 예시 자동 로드
|
82 |
+
|
83 |
+
def load_first_example():
|
84 |
+
if product_background_examples:
|
85 |
+
ex = product_background_examples[0]
|
86 |
+
return ex[0], ex[1], ex[2], ex[3], ex[4] or "(없음)", ex[5]
|
87 |
+
return None, "", "", "", "", None
|
88 |
+
|
89 |
+
# Gradio 앱 생성
|
90 |
+
|
91 |
+
def create_app():
|
92 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Default(
|
93 |
+
primary_hue="orange", secondary_hue="orange",
|
94 |
+
font=[gr.themes.GoogleFont("Noto Sans KR"), "ui-sans-serif", "system-ui"]
|
95 |
+
)) as demo:
|
96 |
+
gr.HTML(fontawesome_link)
|
97 |
+
with gr.Tabs():
|
98 |
+
with gr.TabItem("예시 결과 보기"):
|
99 |
+
with gr.Column():
|
100 |
+
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/681/681443.png"> 상품 배경 이미지 예시 갤러리</div>')
|
101 |
+
with gr.Row(elem_classes="example-gallery"):
|
102 |
+
for idx, ex in enumerate(product_background_examples):
|
103 |
+
item = gr.Image(value=ex[5], show_label=False, elem_classes="example-item")
|
104 |
+
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], _js=None)
|
105 |
+
# 메인 뷰
|
106 |
+
with gr.Row():
|
107 |
+
example_input_image = gr.Image(label="입력 이미지", elem_classes="image-container")
|
108 |
+
with gr.Column():
|
109 |
+
example_bg_type = gr.Textbox(label="배경 유형", interactive=False)
|
110 |
+
example_bg_option = gr.Textbox(label="배경 선택", interactive=False)
|
111 |
+
example_product_name = gr.Textbox(label="상품명", interactive=False)
|
112 |
+
example_additional_info = gr.Textbox(label="추가 요청사항", interactive=False)
|
113 |
+
example_output_image = gr.Image(label="결과 이미지", elem_classes="image-container")
|
114 |
+
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])
|
115 |
+
return demo
|
116 |
+
|
117 |
+
if __name__ == "__main__":
|
118 |
+
preload_example_images()
|
119 |
+
app = create_app()
|
120 |
+
app.launch(share=False, inbrowser=True)
|