|
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}") |
|
|
|
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%") |