Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ load_dotenv()
|
|
17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
-
# ------------------- ๊ธฐ๋ณธ ์ด๋ฏธ์ง ์์ฑ ๊ด๋ จ ํจ์
|
21 |
def save_binary_file(file_name, data):
|
22 |
with open(file_name, "wb") as f:
|
23 |
f.write(data)
|
@@ -144,15 +144,8 @@ def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3)
|
|
144 |
valid_images = [img for img in images if img is not None]
|
145 |
if not valid_images:
|
146 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
147 |
-
|
148 |
-
|
149 |
-
if re.search("[๊ฐ-ํฃ]", processed_prompt):
|
150 |
-
final_prompt = translate_prompt_to_english(processed_prompt)
|
151 |
-
else:
|
152 |
-
final_prompt = processed_prompt
|
153 |
-
else:
|
154 |
-
final_prompt = "Please creatively transform this image into a more vivid and artistic version. Do not include any text or watermarks in the generated image."
|
155 |
-
logger.info("Default prompt generated for single image")
|
156 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
157 |
if result_img is not None:
|
158 |
return result_img, status, final_prompt
|
@@ -168,34 +161,7 @@ def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3)
|
|
168 |
time.sleep(1)
|
169 |
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
170 |
|
171 |
-
|
172 |
-
results = []
|
173 |
-
statuses = []
|
174 |
-
prompts = []
|
175 |
-
num_images = 4
|
176 |
-
max_retries = 3
|
177 |
-
progress(0, desc="์ด๋ฏธ์ง ์์ฑ ์ค๋น ์ค...")
|
178 |
-
for i in range(num_images):
|
179 |
-
progress((i / num_images), desc=f"{i+1}/{num_images} ์ด๋ฏธ์ง ์์ฑ ์ค...")
|
180 |
-
result_img, status, final_prompt = process_images_with_prompt(image1, prompt, i, max_retries)
|
181 |
-
if result_img is not None:
|
182 |
-
results.append(result_img)
|
183 |
-
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
184 |
-
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
185 |
-
else:
|
186 |
-
results.append(None)
|
187 |
-
statuses.append(f"์ด๋ฏธ์ง #{i+1} ์์ฑ ์คํจ: {status}")
|
188 |
-
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
189 |
-
time.sleep(1)
|
190 |
-
progress(1.0, desc="์ด๋ฏธ์ง ์์ฑ ์๋ฃ!")
|
191 |
-
while len(results) < 4:
|
192 |
-
results.append(None)
|
193 |
-
combined_status = "\n".join(statuses)
|
194 |
-
combined_prompts = "\n".join(prompts)
|
195 |
-
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
196 |
-
|
197 |
-
# ------------------- ์ํ ํ๋กฌํํธ ์์ฑ ๊ด๋ จ ํจ์ (์ฐธ์กฐ์ฝ๋) -------------------
|
198 |
-
|
199 |
# ๋ฐฐ๊ฒฝ JSON ํ์ผ ๊ฒฝ๋ก ์ค์
|
200 |
BACKGROUNDS_DIR = "./background"
|
201 |
if not os.path.exists(BACKGROUNDS_DIR):
|
@@ -250,7 +216,7 @@ def generate_system_instruction():
|
|
250 |
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค.
|
251 |
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค.
|
252 |
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๋ค์ ํ๊ฒฝ ์์๋ค์ ํฌํจํ์ธ์:
|
253 |
-
- ์ํ๊ณผ ์ด์ธ๋ฆฌ๋ ์ฃผ๋ณ ํ๊ฒฝ/๋ฐฐ๊ฒฝ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค.
|
254 |
- ํ๊ฒฝ์ ์กฐ๋ช
ํจ๊ณผ(๋ฆผ ๋ผ์ดํธ, ๋ฐฑ๋ผ์ดํธ, ์ํํธ๋ฐ์ค ๋ฑ)๋ฅผ ์ค๋ช
ํฉ๋๋ค.
|
255 |
- ์ํ์ด ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ์กด์ฌํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ ์ ํ ๊ทธ๋ฆผ์์ ๋น ํํ์ ํฌํจํฉ๋๋ค.
|
256 |
- ์ํ์ ์ฉ๋๋ ์ฅ์ ์ ๊ฐ์ ์ ์ผ๋ก ์์ํ๋ ๋ฐฐ๊ฒฝ ์์๋ฅผ ํฌํจํฉ๋๋ค.
|
@@ -261,16 +227,10 @@ def generate_system_instruction():
|
|
261 |
- "professional advertising imagery"
|
262 |
- "studio quality"
|
263 |
- "magazine advertisement quality"
|
264 |
-
10. ๋ฐฐ๊ฒฝ ํ๊ฒฝ ์์๋ฅผ ์ํ
|
265 |
-
- ์คํจ์ผ์ด ์ ํ: ๊นจ๋ํ ์์ค ์ ๋ฐ, ์ฐ์ํ ํ์ฅ๋, ์คํ ๊ฐ์ ํ๊ฒฝ ๋ฑ
|
266 |
-
- ์๋ฃ ์ ํ: ์ธ๋ จ๋ ํ
์ด๋ธ, ํํฐ ํ๊ฒฝ, ์ผ์ธ ํผํฌ๋ ์ฅ๋ฉด ๋ฑ
|
267 |
-
- ์ ์ ์ ํ: ์ธ๋ จ๋ ์์
๊ณต๊ฐ, ํ๋์ ์ธ ๊ฑฐ์ค, ๋ฏธ๋๋ฉํ ์ฑ
์ ๋ฑ
|
268 |
-
- ํจ์
/์๋ฅ: ์ธ๋ จ๋ ์ผ๋ฃธ, ๋์ ๊ฑฐ๋ฆฌ, ์๋ ๊ฐ์คํ ๋ผ์ดํ์คํ์ผ ํ๊ฒฝ ๋ฑ
|
269 |
-
- ์ํ ์ ํ: ๊น๋ํ ์ฃผ๋ฐฉ, ์ํ, ์๋ฆฌ ํ๊ฒฝ ๋ฑ
|
270 |
11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ํํ ๋ฐ์ํฉ๋๋ค.
|
271 |
12. ํ๋กฌํํธ๋ ๋ฏธ๋์ ๋ AI์ ์ต์ ํ๋์ด์ผ ํฉ๋๋ค.
|
272 |
13. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค.
|
273 |
-
์ถ๋ ฅ ํ์์ ์์ด๋ก ๋ ๋จ์ผ ๋จ๋ฝ์ ์์ธํ ํ๋กฌํํธ์ฌ์ผ ํ๋ฉฐ, ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค.
|
274 |
"""
|
275 |
|
276 |
def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
|
@@ -354,91 +314,40 @@ def get_selected_background_info(bg_type, simple, studio, nature, indoor, abstra
|
|
354 |
"english": "white background"
|
355 |
}
|
356 |
|
357 |
-
|
|
|
358 |
if image is None:
|
359 |
-
|
360 |
-
return "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ ํ ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
361 |
product_name = product_name.strip() or "์ ํ"
|
362 |
background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract)
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
ํค ๋ฐ๊ธ: https://makersuite.google.com/
|
373 |
-
"""
|
374 |
-
return prompt, image, "API ํค๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค."
|
375 |
-
preview = f"""
|
376 |
-
<div style="padding:10px; border:1px solid #ddd; border-radius:8px; margin-top:10px;">
|
377 |
-
<h3>ํ๋กฌํํธ ์์ฝ</h3>
|
378 |
-
<p><strong>์ด ๊ธธ์ด:</strong> {len(prompt)} ๊ธ์</p>
|
379 |
-
<p><strong>๋ฐฐ๊ฒฝ:</strong> {background_info['category']} > {background_info['name']}</p>
|
380 |
-
<p><strong>๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ:</strong> {" ".join([param for param in ["--ar 1:1", "--s 750", "--q 2"] if param in prompt])}</p>
|
381 |
-
</div>
|
382 |
-
"""
|
383 |
-
return prompt, image, preview
|
384 |
-
except Exception as e:
|
385 |
-
error_msg = f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
386 |
-
gr.Error(error_msg)
|
387 |
-
return error_msg, image, "์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค."
|
388 |
-
|
389 |
-
# ------------------- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ -------------------
|
390 |
-
|
391 |
-
def create_basic_app():
|
392 |
-
with gr.Blocks() as basic_app:
|
393 |
-
with gr.Column():
|
394 |
-
with gr.Group():
|
395 |
-
with gr.Column():
|
396 |
-
with gr.Row():
|
397 |
-
image1_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋", image_mode="RGB")
|
398 |
-
prompt_input = gr.Textbox(
|
399 |
-
lines=3,
|
400 |
-
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์. '#1'์ผ๋ก ์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.",
|
401 |
-
label="ํ๋กฌํํธ ์
๋ ฅ"
|
402 |
-
)
|
403 |
-
with gr.Row():
|
404 |
-
submit_single_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (1์ฅ)')
|
405 |
-
submit_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (4์ฅ)')
|
406 |
-
with gr.Column():
|
407 |
-
with gr.Row():
|
408 |
-
with gr.Column():
|
409 |
-
output_image1 = gr.Image(label="์ด๋ฏธ์ง #1", type="filepath")
|
410 |
-
output_image3 = gr.Image(label="์ด๋ฏธ์ง #3", type="filepath")
|
411 |
-
with gr.Column():
|
412 |
-
output_image2 = gr.Image(label="์ด๋ฏธ์ง #2", type="filepath")
|
413 |
-
output_image4 = gr.Image(label="์ด๋ฏธ์ง #4", type="filepath")
|
414 |
-
output_text = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=2)
|
415 |
-
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", lines=2)
|
416 |
-
submit_single_btn.click(
|
417 |
-
fn=lambda image1, prompt: process_images_with_prompt(image1, prompt, 0),
|
418 |
-
inputs=[image1_input, prompt_input],
|
419 |
-
outputs=[output_image1, output_text, prompt_display],
|
420 |
)
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
)
|
426 |
-
return basic_app
|
427 |
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
gr.Markdown("์ํ
|
432 |
-
|
433 |
-
product_name = gr.Textbox(label="์ํ๋ช
(ํ๊ตญ์ด ์
๋ ฅ)", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ํ
๋ธ๋ฌ ๋ฑ", interactive=True)
|
434 |
-
background_type = gr.Radio(
|
435 |
-
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"],
|
436 |
-
label="๋ฐฐ๊ฒฝ ์ ํ",
|
437 |
-
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
438 |
-
)
|
439 |
with gr.Row():
|
440 |
with gr.Column(scale=1):
|
441 |
-
|
|
|
|
|
|
|
|
|
|
|
442 |
simple_dropdown = gr.Dropdown(
|
443 |
choices=list(SIMPLE_BACKGROUNDS.keys()),
|
444 |
value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else None,
|
@@ -476,63 +385,38 @@ def create_product_prompt_app():
|
|
476 |
)
|
477 |
additional_info = gr.Textbox(
|
478 |
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
479 |
-
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด
|
480 |
lines=3,
|
481 |
interactive=True
|
482 |
)
|
483 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
484 |
with gr.Column(scale=1):
|
485 |
-
prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ", lines=
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
nature_dropdown: gr.update(visible=(bg_type == "์์ฐ ํ๊ฒฝ")),
|
493 |
-
indoor_dropdown: gr.update(visible=(bg_type == "์ค๋ด ํ๊ฒฝ")),
|
494 |
-
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"))
|
495 |
-
}
|
496 |
-
background_type.change(
|
497 |
-
fn=update_dropdowns,
|
498 |
-
inputs=[background_type],
|
499 |
-
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
500 |
)
|
501 |
-
|
502 |
-
return generate_product_prompt_output(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_text)
|
503 |
-
submit_btn.click(
|
504 |
-
fn=generate_output,
|
505 |
-
inputs=[
|
506 |
-
image_input,
|
507 |
-
background_type,
|
508 |
-
simple_dropdown,
|
509 |
-
studio_dropdown,
|
510 |
-
nature_dropdown,
|
511 |
-
indoor_dropdown,
|
512 |
-
abstract_dropdown,
|
513 |
-
product_name,
|
514 |
-
additional_info
|
515 |
-
],
|
516 |
-
outputs=[
|
517 |
-
prompt_output,
|
518 |
-
image_preview,
|
519 |
-
preview_html
|
520 |
-
]
|
521 |
-
)
|
522 |
-
return product_app
|
523 |
-
|
524 |
-
def main():
|
525 |
-
basic_app = create_basic_app()
|
526 |
-
product_app = create_product_prompt_app()
|
527 |
-
with gr.Blocks() as demo:
|
528 |
-
gr.Markdown("# ํตํฉ ์ด๋ฏธ์ง ์์ฑ ๋ฐ ํ๋กฌํํธ ์์ฑ ์ฑ")
|
529 |
-
with gr.Tabs():
|
530 |
-
with gr.TabItem("์ผ๋ฐ ์ด๋ฏธ์ง ์์ฑ"):
|
531 |
-
basic_app.render()
|
532 |
-
with gr.TabItem("์ํ ํ๋กฌํํธ ์์ฑ ๋ฐ ์ด๋ฏธ์ง ์์ฑ"):
|
533 |
-
product_app.render()
|
534 |
-
demo.queue()
|
535 |
-
demo.launch()
|
536 |
|
537 |
if __name__ == "__main__":
|
538 |
-
|
|
|
|
|
|
17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
+
# ------------------- ๊ธฐ๋ณธ ์ด๋ฏธ์ง ์์ฑ ๊ด๋ จ ํจ์ -------------------
|
21 |
def save_binary_file(file_name, data):
|
22 |
with open(file_name, "wb") as f:
|
23 |
f.write(data)
|
|
|
144 |
valid_images = [img for img in images if img is not None]
|
145 |
if not valid_images:
|
146 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
147 |
+
# ์ด๋ฏธ Gemini API๋ก ์์ฑ๋ ํ๋กฌํํธ๋ ์์ด์ด๋ฏ๋ก ์ถ๊ฐ ๋ฒ์ญ์ ์๋ต
|
148 |
+
final_prompt = prompt.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
150 |
if result_img is not None:
|
151 |
return result_img, status, final_prompt
|
|
|
161 |
time.sleep(1)
|
162 |
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt
|
163 |
|
164 |
+
# ------------------- ์ํ ํ๋กฌํํธ ์์ฑ ๊ด๋ จ ํจ์ -------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
# ๋ฐฐ๊ฒฝ JSON ํ์ผ ๊ฒฝ๋ก ์ค์
|
166 |
BACKGROUNDS_DIR = "./background"
|
167 |
if not os.path.exists(BACKGROUNDS_DIR):
|
|
|
216 |
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค.
|
217 |
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค.
|
218 |
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๋ค์ ํ๊ฒฝ ์์๋ค์ ํฌํจํ์ธ์:
|
219 |
+
- ์ํ๊ณผ ์ด์ธ๋ฆฌ๋ ์ฃผ๋ณ ํ๊ฒฝ/๋ฐฐ๊ฒฝ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค.
|
220 |
- ํ๊ฒฝ์ ์กฐ๋ช
ํจ๊ณผ(๋ฆผ ๋ผ์ดํธ, ๋ฐฑ๋ผ์ดํธ, ์ํํธ๋ฐ์ค ๋ฑ)๋ฅผ ์ค๋ช
ํฉ๋๋ค.
|
221 |
- ์ํ์ด ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ์กด์ฌํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ ์ ํ ๊ทธ๋ฆผ์์ ๋น ํํ์ ํฌํจํฉ๋๋ค.
|
222 |
- ์ํ์ ์ฉ๋๋ ์ฅ์ ์ ๊ฐ์ ์ ์ผ๋ก ์์ํ๋ ๋ฐฐ๊ฒฝ ์์๋ฅผ ํฌํจํฉ๋๋ค.
|
|
|
227 |
- "professional advertising imagery"
|
228 |
- "studio quality"
|
229 |
- "magazine advertisement quality"
|
230 |
+
10. ๋ฐฐ๊ฒฝ ํ๊ฒฝ ์์๋ฅผ ์ํ ์นดํ
๊ณ ๏ฟฝ๏ฟฝ๏ฟฝ์ ๋ง๊ฒ ์ ํํฉ๋๋ค.
|
|
|
|
|
|
|
|
|
|
|
231 |
11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ํํ ๋ฐ์ํฉ๋๋ค.
|
232 |
12. ํ๋กฌํํธ๋ ๋ฏธ๋์ ๋ AI์ ์ต์ ํ๋์ด์ผ ํฉ๋๋ค.
|
233 |
13. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค.
|
|
|
234 |
"""
|
235 |
|
236 |
def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
|
|
|
314 |
"english": "white background"
|
315 |
}
|
316 |
|
317 |
+
# ------------------- ํตํฉ ๊ธฐ๋ฅ: ํ๋กฌํํธ ์์ฑ ํ ์ด๋ฏธ์ง ์์ฑ -------------------
|
318 |
+
def generate_product_image(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_info):
|
319 |
if image is None:
|
320 |
+
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ ํ ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
|
|
321 |
product_name = product_name.strip() or "์ ํ"
|
322 |
background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract)
|
323 |
+
# Gemini API๋ก ํ๋กฌํํธ ์์ฑ
|
324 |
+
generated_prompt = generate_prompt_with_gemini(product_name, background_info, additional_info)
|
325 |
+
if "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค" in generated_prompt:
|
326 |
+
warning_msg = (
|
327 |
+
"[Gemini API ํค ๋๋ฝ]\n"
|
328 |
+
"API ํค ์ค์ ๋ฐฉ๋ฒ:\n"
|
329 |
+
"1. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY=\"your-api-key\"\n"
|
330 |
+
"2. ์ฝ๋ ๋ด ์ง์ ์
๋ ฅ: GEMINI_API_KEY = \"your-api-key\"\n"
|
331 |
+
"ํค ๋ฐ๊ธ: https://makersuite.google.com/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
)
|
333 |
+
return None, warning_msg, warning_msg
|
334 |
+
# ์์ฑ๋ ํ๋กฌํํธ๋ฅผ ํ๋กฌํํธ ์
๋ ฅ์ฐฝ์ ๋ฐ์ํ๊ณ , ๊ทธ ํ๋กฌํํธ๋ก ์ด๋ฏธ์ง ์์ฑ
|
335 |
+
result_image, status, final_prompt = process_images_with_prompt(image, generated_prompt, 0)
|
336 |
+
return result_image, status, final_prompt
|
|
|
|
|
337 |
|
338 |
+
# ------------------- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ -------------------
|
339 |
+
def create_app():
|
340 |
+
with gr.Blocks(title="๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ ๋ฐ ์ด๋ฏธ์ง ์์ฑ") as demo:
|
341 |
+
gr.Markdown("# ๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ ๋ฐ ์ด๋ฏธ์ง ์์ฑ")
|
342 |
+
gr.Markdown("์ํ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ , ์ ํ๋ช
๋ฐ ๋ฐฐ๊ฒฝ ์ต์
๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์
๋ ฅํ๋ฉด Gemini API๋ฅผ ์ด์ฉํ์ฌ ์์ด ํ๋กฌํํธ๋ฅผ ์์ฑํ๊ณ , ๊ทธ ํ๋กฌํํธ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํฉ๋๋ค.")
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
with gr.Row():
|
344 |
with gr.Column(scale=1):
|
345 |
+
product_name = gr.Textbox(label="์ํ๋ช
(ํ๊ตญ์ด ์
๋ ฅ)", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ํ
๋ธ๋ฌ ๋ฑ", interactive=True)
|
346 |
+
background_type = gr.Radio(
|
347 |
+
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"],
|
348 |
+
label="๋ฐฐ๊ฒฝ ์ ํ",
|
349 |
+
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
350 |
+
)
|
351 |
simple_dropdown = gr.Dropdown(
|
352 |
choices=list(SIMPLE_BACKGROUNDS.keys()),
|
353 |
value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else None,
|
|
|
385 |
)
|
386 |
additional_info = gr.Textbox(
|
387 |
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
388 |
+
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ ๊ฐ์ฒด ๋ฑ",
|
389 |
lines=3,
|
390 |
interactive=True
|
391 |
)
|
392 |
+
# ๋ฐฐ๊ฒฝ ์ ํ ๋ณ๊ฒฝ ์ ํด๋น ๋๋กญ๋ค์ด๋ง ๋ณด์ด๋๋ก ์
๋ฐ์ดํธ
|
393 |
+
def update_dropdowns(bg_type):
|
394 |
+
return {
|
395 |
+
simple_dropdown: gr.update(visible=(bg_type == "์ฌํ ๋ฐฐ๊ฒฝ")),
|
396 |
+
studio_dropdown: gr.update(visible=(bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ")),
|
397 |
+
nature_dropdown: gr.update(visible=(bg_type == "์์ฐ ํ๊ฒฝ")),
|
398 |
+
indoor_dropdown: gr.update(visible=(bg_type == "์ค๋ด ํ๊ฒฝ")),
|
399 |
+
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"))
|
400 |
+
}
|
401 |
+
background_type.change(
|
402 |
+
fn=update_dropdowns,
|
403 |
+
inputs=[background_type],
|
404 |
+
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
405 |
+
)
|
406 |
+
image_input = gr.Image(label="์ํ ์ด๋ฏธ์ง ์
๋ก๋", type="pil")
|
407 |
+
generate_btn = gr.Button("ํ๋กฌํํธ ๋ฐ ์ด๋ฏธ์ง ์์ฑ", variant="primary")
|
408 |
with gr.Column(scale=1):
|
409 |
+
prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ (์์ด)", lines=6)
|
410 |
+
image_output = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง", type="filepath")
|
411 |
+
status_output = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=3)
|
412 |
+
generate_btn.click(
|
413 |
+
fn=generate_product_image,
|
414 |
+
inputs=[image_input, background_type, simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown, product_name, additional_info],
|
415 |
+
outputs=[image_output, status_output, prompt_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
)
|
417 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
|
419 |
if __name__ == "__main__":
|
420 |
+
app = create_app()
|
421 |
+
app.queue()
|
422 |
+
app.launch()
|