asdfewef / app.py
ssboost's picture
Update app.py
37db736 verified
raw
history blame
4.28 kB
import os
import json
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 {
--background-color: #FFFFFF;
--text-color: #334155;
--border-radius: 18px;
--shadow: 0 8px 30px rgba(0,0,0,0.08);
}
.example-gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
padding: 20px;
}
.example-item img {
width: 100%;
height: 120px;
object-fit: cover;
border-radius: var(--border-radius);
}
.image-container {
border-radius: var(--border-radius);
overflow: hidden;
background-color: white;
transition: box-shadow 0.3s;
}
.image-container:hover {
box-shadow: var(--shadow);
}
.footer { visibility: hidden; }
"""
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.Tabs():
with gr.TabItem("์˜ˆ์‹œ ๊ฒฐ๊ณผ ๋ณด๊ธฐ"):
with gr.Column():
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/681/681443.png"> ์ƒํ’ˆ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์˜ˆ์‹œ ๊ฐค๋Ÿฌ๋ฆฌ</div>')
with gr.Row(elem_classes="example-gallery"):
for idx, ex in enumerate(product_background_examples):
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], _js=None)
# ๋ฉ”์ธ ๋ทฐ
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")
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)