asdfewef / app.py
ssboost's picture
Update app.py
9a19108 verified
raw
history blame
10.1 kB
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%")