File size: 10,147 Bytes
9a19108 37db736 9a19108 37db736 9a19108 37db736 9a19108 0eaf572 37db736 9a19108 0eaf572 37db736 9a19108 37db736 9a19108 d1d6c1b 76c3b57 9a19108 76c3b57 9a19108 76c3b57 9a19108 d1d6c1b 76c3b57 9a19108 76c3b57 9a19108 76c3b57 9a19108 d1d6c1b 9a19108 37db736 9a19108 37db736 9a19108 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
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%") |