test-100 / app.py
Kims12's picture
Update app.py
4e988f9 verified
raw
history blame
38.1 kB
import os
import tempfile
from PIL import Image
import gradio as gr
import logging
import re
from io import BytesIO
from google import genai
from google.genai import types
# ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋กœ๋“œ
from dotenv import load_dotenv
load_dotenv()
# ๋กœ๊น… ์„ค์ •
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def save_binary_file(file_name, data):
with open(file_name, "wb") as f:
f.write(data)
def translate_prompt_to_english(prompt):
"""
์ž…๋ ฅ๋œ ํ”„๋กฌํ”„ํŠธ์— ํ•œ๊ธ€์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด Geminiโ€‘2.0โ€‘flash ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์–ด๋กœ ๋ฒˆ์—ญํ•ฉ๋‹ˆ๋‹ค.
ํ•œ๊ธ€์ด ์—†์œผ๋ฉด ์›๋ณธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
"""
if not re.search("[๊ฐ€-ํžฃ]", prompt):
return prompt
try:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
logger.error("Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
return prompt
client = genai.Client(api_key=api_key)
translation_prompt = f"Translate the following Korean text to English:\n\n{prompt}"
logger.info(f"Translation prompt: {translation_prompt}")
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[translation_prompt],
config=types.GenerateContentConfig(
response_modalities=['Text'],
temperature=0.2,
top_p=0.95,
top_k=40,
max_output_tokens=512
)
)
translated_text = ""
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
translated_text += part.text
if translated_text.strip():
logger.info(f"Translated text: {translated_text.strip()}")
return translated_text.strip()
else:
logger.warning("๋ฒˆ์—ญ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์›๋ณธ ํ”„๋กฌํ”„ํŠธ ์‚ฌ์šฉ")
return prompt
except Exception as e:
logger.exception("๋ฒˆ์—ญ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
return prompt
def preprocess_prompt(prompt, image1, image2, image3, bg_options=None):
"""
ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ธฐ๋Šฅ ๋ช…๋ น์„ ํ•ด์„
"""
has_img1 = image1 is not None
has_img2 = image2 is not None
has_img3 = image3 is not None
if "#1" in prompt and not has_img1:
prompt = prompt.replace("#1", "์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€(์—†์Œ)")
else:
prompt = prompt.replace("#1", "์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€")
if "#2" in prompt and not has_img2:
prompt = prompt.replace("#2", "๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€(์—†์Œ)")
else:
prompt = prompt.replace("#2", "๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€")
if "#3" in prompt and not has_img3:
prompt = prompt.replace("#3", "์„ธ ๋ฒˆ์งธ ์ด๋ฏธ์ง€(์—†์Œ)")
else:
prompt = prompt.replace("#3", "์„ธ ๋ฒˆ์งธ ์ด๋ฏธ์ง€")
if "1. ์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ" in prompt:
desc_match = re.search(r'#1์„ "(.*?)"์œผ๋กœ ๋ฐ”๊ฟ”๋ผ', prompt)
if desc_match:
description = desc_match.group(1)
prompt = f"์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ {description}์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. ์›๋ณธ ์ด๋ฏธ์ง€์˜ ์ฃผ์š” ๋‚ด์šฉ์€ ์œ ์ง€ํ•˜๋˜ ์ƒˆ๋กœ์šด ์Šคํƒ€์ผ๊ณผ ๋ถ„์œ„๊ธฐ๋กœ ์žฌํ•ด์„ํ•ด์ฃผ์„ธ์š”."
else:
prompt = "์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ์ฐฝ์˜์ ์œผ๋กœ ๋ณ€ํ˜•ํ•ด์ฃผ์„ธ์š”. ๋” ์ƒ์ƒํ•˜๊ณ  ์˜ˆ์ˆ ์ ์ธ ๋ฒ„์ „์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”."
elif "2. ๊ธ€์ž์ง€์šฐ๊ธฐ" in prompt:
text_match = re.search(r'#1์—์„œ "(.*?)"๋ฅผ ์ง€์›Œ๋ผ', prompt)
if text_match:
text_to_remove = text_match.group(1)
prompt = f"์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์—์„œ '{text_to_remove}' ํ…์ŠคํŠธ๋ฅผ ์ฐพ์•„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”. ํ…์ŠคํŠธ๊ฐ€ ์žˆ๋˜ ๋ถ€๋ถ„์„ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ™”๋กญ๊ฒŒ ์ฑ„์›Œ์ฃผ์„ธ์š”."
else:
prompt = "์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์—์„œ ๋ชจ๋“  ํ…์ŠคํŠธ๋ฅผ ์ฐพ์•„ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”. ๊น”๋”ํ•œ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”."
elif "4. ์˜ท๋ฐ”๊พธ๊ธฐ" in prompt:
prompt = "์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์˜ ์ธ๋ฌผ ์˜์ƒ์„ ๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์˜ ์˜์ƒ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. ์˜์ƒ์˜ ์Šคํƒ€์ผ๊ณผ ์ƒ‰์ƒ์€ ๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ๋”ฐ๋ฅด๋˜, ์‹ ์ฒด ๋น„์œจ๊ณผ ํฌ์ฆˆ๋Š” ์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ์œ ์ง€ํ•ด์ฃผ์„ธ์š”."
elif "5. ๋ฐฐ๊ฒฝ๋ฐ”๊พธ๊ธฐ" in prompt or "๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝ" in prompt:
# ๋ฐฐ๊ฒฝ ์˜ต์…˜์ด ์žˆ์œผ๋ฉด ๊ทธ๊ฒƒ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
if bg_options and all([bg_options.get("product_name"), bg_options.get("background_type"), bg_options.get("background_style")]):
product_name = bg_options.get("product_name", "")
product_category = bg_options.get("product_category", "์ผ๋ฐ˜ ์ƒํ’ˆ")
background_type = bg_options.get("background_type", "์ž์—ฐ ๋ฐฐ๊ฒฝ")
background_style = bg_options.get("background_style", "์ˆฒ")
lighting = bg_options.get("lighting", "์ž์—ฐ๊ด‘")
texture = bg_options.get("texture", "")
color_tone = bg_options.get("color_tone", "")
mood = bg_options.get("mood", "")
# ์ด์ปค๋จธ์Šค ์ธ๋„ค์ผ์šฉ ๊ณ ํ’ˆ์งˆ ์ƒ์—…์  ๋ฐฐ๊ฒฝ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
prompt = f"์ด์ปค๋จธ์Šค ์ธ๋„ค์ผ์šฉ ๊ณ ํ’ˆ์งˆ ์ƒ์—… ์‚ฌ์ง„: #1 ์ด๋ฏธ์ง€์˜ {product_name} ์ƒํ’ˆ์„ ์™„๋ฒฝํ•˜๊ฒŒ ๋ณด์กดํ•˜๋ฉด์„œ ๋ฐฐ๊ฒฝ๋งŒ {background_type} ์ค‘ {background_style}์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
# ์ถ”๊ฐ€ ์„ธ๋ถ€ ์‚ฌํ•ญ ํฌํ•จ
if lighting:
prompt += f"{lighting}์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํ’ˆ์„ ๋‹๋ณด์ด๊ฒŒ ํ•˜๊ณ , "
if texture and texture != "์„ ํƒ ์•ˆํ•จ":
prompt += f"{texture} ์งˆ๊ฐ์˜ ๋ฐฐ๊ฒฝ์œผ๋กœ ์ƒํ’ˆ์˜ ๊ณ ๊ธ‰์Šค๋Ÿฌ์›€์„ ๊ฐ•์กฐํ•˜๋ฉฐ, "
if color_tone and color_tone != "์„ ํƒ ์•ˆํ•จ":
prompt += f"์ „์ฒด์ ์œผ๋กœ {color_tone}์œผ๋กœ ์กฐํ™”๋กญ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ณ , "
if mood and mood != "์„ ํƒ ์•ˆํ•จ":
prompt += f"{mood}ํ•œ ๋ถ„์œ„๊ธฐ๋กœ "
prompt += f"์—ฐ์ถœํ•ด์ฃผ์„ธ์š”. {product_category} ์ƒํ’ˆ์˜ ๋””ํ…Œ์ผ, ์ƒ‰์ƒ, ์งˆ๊ฐ, ํ˜•ํƒœ, ๊ทธ๋ฆผ์ž๋Š” ์›๋ณธ #1 ์ด๋ฏธ์ง€์™€ 100% ๋™์ผํ•˜๊ฒŒ ์œ ์ง€ํ•˜๊ณ , ๋ฐฐ๊ฒฝ๋งŒ ๋ณ€๊ฒฝํ•˜์—ฌ ์ „๋ฌธ์ ์ด๊ณ  ์ƒ์—…์„ฑ ๋†’์€ ์ด์ปค๋จธ์Šค ์ œํ’ˆ ์‚ฌ์ง„์œผ๋กœ ์ œ์ž‘ํ•ด์ฃผ์„ธ์š”."
else:
prompt = "์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์˜ ๋ฐฐ๊ฒฝ์„ ๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์˜ ๋ฐฐ๊ฒฝ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. ์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์˜ ์ฃผ์š” ํ”ผ์‚ฌ์ฒด๋Š” ์œ ์ง€ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ด๋ฏธ์ง€์˜ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ™”๋กญ๊ฒŒ ํ•ฉ์„ฑํ•ด์ฃผ์„ธ์š”."
elif "์ƒํ’ˆํ•ฉ์„ฑ" in prompt:
prompt = "#1 ์ƒํ’ˆ ์ด๋ฏธ์ง€์— #2 ์ด๋ฏธ์ง€ ๋˜๋Š” #3 ์ด๋ฏธ์ง€๋ฅผ [๋ชจ๋“  ์ด๋ฏธ์ง€์˜ ์ฃผ์š” ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ , ํŠนํžˆ ์ƒํ’ˆ์ด ๋‹๋ณด์ด๋„๋ก ์กฐํ™”๋กญ๊ฒŒ ํ†ตํ•ฉํ•˜์—ฌ]์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ•ฉ์„ฑํ•˜๋ผ."
prompt += " ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”."
return prompt
def generate_with_images(prompt, images):
"""
API ํ˜ธ์ถœ์„ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
"""
try:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
return None, "API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
client = genai.Client(api_key=api_key)
logger.info(f"Gemini API ์š”์ฒญ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: {prompt}")
contents = [prompt]
for idx, img in enumerate(images, 1):
if img is not None:
contents.append(img)
logger.info(f"์ด๋ฏธ์ง€ #{idx} ์ถ”๊ฐ€๋จ")
response = client.models.generate_content(
model="gemini-2.0-flash-exp-image-generation",
contents=contents,
config=types.GenerateContentConfig(
response_modalities=['Text', 'Image'],
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192
)
)
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
temp_path = tmp.name
result_text = ""
image_found = False
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
result_text += part.text
logger.info(f"์‘๋‹ต ํ…์ŠคํŠธ: {part.text}")
elif hasattr(part, 'inline_data') and part.inline_data:
save_binary_file(temp_path, part.inline_data.data)
image_found = True
logger.info("์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€ ์ถ”์ถœ ์„ฑ๊ณต")
if not image_found:
return None, f"API์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์‘๋‹ต ํ…์ŠคํŠธ: {result_text}"
result_img = Image.open(temp_path)
if result_img.mode == "RGBA":
result_img = result_img.convert("RGB")
return result_img, f"์ด๋ฏธ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. {result_text}"
except Exception as e:
logger.exception("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
return None, f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
def process_images_with_prompt(image1, image2, image3, prompt, bg_options=None):
"""
3๊ฐœ์˜ ์ด๋ฏธ์ง€์™€ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ตœ์ข… ์˜์–ด ํ”„๋กฌํ”„ํŠธ(final_prompt)๋ฅผ ์ƒ์„ฑํ•œ ํ›„,
API๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
"""
try:
images = [image1, image2, image3]
valid_images = [img for img in images if img is not None]
if not valid_images:
return None, "์ ์–ด๋„ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”.", ""
if prompt and prompt.strip():
processed_prompt = preprocess_prompt(prompt, image1, image2, image3, bg_options)
if re.search("[๊ฐ€-ํžฃ]", processed_prompt):
final_prompt = translate_prompt_to_english(processed_prompt)
else:
final_prompt = processed_prompt
else:
if len(valid_images) == 1:
final_prompt = "Please creatively transform this image into a more vivid and artistic version."
logger.info("Default prompt generated for single image")
elif len(valid_images) == 2:
final_prompt = "Please seamlessly composite these two images, integrating their key elements harmoniously into a single image."
logger.info("Default prompt generated for two images")
else:
final_prompt = "Please creatively composite these three images, combining their main elements into a cohesive and natural scene."
logger.info("Default prompt generated for three images")
result_img, status = generate_with_images(final_prompt, valid_images)
return result_img, status, final_prompt
except Exception as e:
logger.exception("์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
return None, f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}", prompt
def ask_llm_for_final_prompt(kr_prompt):
"""
LLM์— 2์ฐจ ์งˆ๋ฌธํ•˜์—ฌ ๋” ์ž์—ฐ์Šค๋Ÿฌ์šด ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์–ป๋Š” ํ•จ์ˆ˜
์‹ค์ œ ๊ตฌํ˜„์—์„œ๋Š” LLM API๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•จ
"""
try:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
logger.error("Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
return f"#1 {kr_prompt}"
client = genai.Client(api_key=api_key)
llm_prompt = f"""
๋‹ค์Œ์€ ์ด์ปค๋จธ์Šค ์ƒํ’ˆ ์ด๋ฏธ์ง€ ๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ์ž…๋‹ˆ๋‹ค. ์ด ํ”„๋กฌํ”„ํŠธ๋ฅผ ๋” ์ž์—ฐ์Šค๋Ÿฝ๊ณ  ๋งค๋ ฅ์ ์ธ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ”„๋กฌํ”„ํŠธ๋กœ
๋‹ค์‹œ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
์ค‘์š” ์š”๊ตฌ์‚ฌํ•ญ:
1. ๋ฐ˜๋“œ์‹œ "#1"์œผ๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค - ์ด๊ฒƒ์€ ์ฐธ์กฐํ•  ์ด๋ฏธ์ง€๋ฅผ ์ง€์ •ํ•˜๋Š” ํƒœ๊ทธ์ž…๋‹ˆ๋‹ค.
2. ์›๋ณธ ์ œํ’ˆ์˜ ํ’ˆ์งˆ๊ณผ ๋””ํ…Œ์ผ์ด 100% ์œ ์ง€๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
3. ๋ฐฐ๊ฒฝ์ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์–ด์šฐ๋Ÿฌ์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค - ํ•ฉ์„ฑ์ด ์•„๋‹Œ ์‹ค์ œ ์ดฌ์˜ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•ด์ฃผ์„ธ์š”.
4. ๋‹ค์Œ ์š”์†Œ๋“ค์„ ํฌํ•จํ•ด์ฃผ์„ธ์š”:
- ์ฃผ๋ณ€ ํ™˜๊ฒฝ๊ณผ์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ์ƒํ˜ธ์ž‘์šฉ (๊ทธ๋ฆผ์ž, ๋ฐ˜์‚ฌ, ํ™˜๊ฒฝ๊ด‘ ๋“ฑ)
- ์ œํ’ˆ๊ณผ ๋ฐฐ๊ฒฝ ๊ฐ„์˜ ์ž์—ฐ์Šค๋Ÿฌ์šด ๋น› ์ƒํ˜ธ์ž‘์šฉ
- ์‹ค์ œ ์‚ฌ์ง„ ์ดฌ์˜ ์šฉ์–ด (์ŠคํŠœ๋””์˜ค ์ดฌ์˜, ์ž์—ฐ๊ด‘, ๊นŠ์ด๊ฐ ๋“ฑ)
- ์‹ค์ œ ๊ณ ๊ธ‰ ์ƒ์—… ์‚ฌ์ง„ ํ’ˆ์งˆ (8K, ์„ ๋ช…๋„, ๋ฆฌ์–ผ๋ฆฌ์ฆ˜)
5. ์ธ์œ„์ ์ด๊ฑฐ๋‚˜ ํ•ฉ์„ฑ๋œ ๋А๋‚Œ์ด ์•„๋‹Œ, ์ „๋ฌธ ์ œํ’ˆ ์‚ฌ์ง„์ž‘๊ฐ€๊ฐ€ ์‹ค์ œ๋กœ ์ดฌ์˜ํ•œ ๊ฒƒ ๊ฐ™์€ ๋А๋‚Œ์ด ๋‚˜๋„๋ก ํ•ด์ฃผ์„ธ์š”.
์›๋ณธ ํ”„๋กฌํ”„ํŠธ:
{kr_prompt}
๊ฒฐ๊ณผ๋ฌผ์€ AI ์ด๋ฏธ์ง€ ์ƒ์„ฑ์— ์ตœ์ ํ™”๋œ ํ”„๋กฌํ”„ํŠธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋ฌผ๋งŒ ์ œ๊ณตํ•˜๊ณ  ์„ค๋ช…์ด๋‚˜ ์ถ”๊ฐ€ ํ…์ŠคํŠธ ์—†์ด ํ”„๋กฌํ”„ํŠธ ์ž์ฒด๋งŒ ์ถœ๋ ฅํ•ด์ฃผ์„ธ์š”.
"""
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[llm_prompt],
config=types.GenerateContentConfig(
response_modalities=['Text'],
temperature=0.7,
top_p=0.95,
top_k=40,
max_output_tokens=512
)
)
final_prompt = ""
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
final_prompt += part.text
# LLM ์‘๋‹ต์— #1์ด ์—†๋Š” ๊ฒฝ์šฐ ๊ฐ•์ œ๋กœ ์ถ”๊ฐ€
final_prompt = final_prompt.strip()
if not final_prompt.startswith("#1"):
final_prompt = f"#1 {final_prompt}"
if final_prompt.strip():
logger.info(f"LLM 2์ฐจ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๊ฒฐ๊ณผ: {final_prompt.strip()}")
return final_prompt.strip()
else:
logger.warning("LLM 2์ฐจ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์›๋ณธ ํ”„๋กฌํ”„ํŠธ ์‚ฌ์šฉ")
return f"#1 {kr_prompt}"
except Exception as e:
logger.exception("LLM 2์ฐจ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
return f"#1 {kr_prompt}"
# LLM ์Šคํƒ€์ผ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ํ•จ์ˆ˜
def generate_llm_prompt(product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood):
# ๊ธฐ๋ณธ ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ
prompt = f"#1 ์›๋ณธ ์ด๋ฏธ์ง€์˜ {product_name} ์ œํ’ˆ์„ 100% ๋™์ผํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฐฐ๊ฒฝ๋งŒ "
# ๋ฐฐ๊ฒฝ ์œ ํ˜• ๋ฐ ์Šคํƒ€์ผ ์ถ”๊ฐ€
if background_type == "์ž์—ฐ ๋ฐฐ๊ฒฝ":
if background_style == "์ˆฒ":
prompt += "์šธ์ฐฝํ•œ ์ˆฒ์†์˜ ์ž์—ฐ ํ™˜๊ฒฝ์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "ํ•ด๋ณ€":
prompt += "์ฒญ๋Ÿ‰ํ•œ ํ•ด๋ณ€๊ฐ€์˜ ๋ชจ๋ž˜ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "์‚ฐ":
prompt += "์›…์žฅํ•œ ์‚ฐ์˜ ์ •์ƒ์—์„œ ๋ง‘์€ ํ•˜๋Š˜์„ ๋ฐฐ๊ฒฝ์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๋“คํŒ":
prompt += "๋„“๊ฒŒ ํŽผ์ณ์ง„ ์ดˆ์› ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๊ณ„๊ณก":
prompt += "์‹œ์›ํ•œ ๊ณ„๊ณก๋ฌผ์ด ํ๋ฅด๋Š” ๋ฐ”์œ„ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "์—ด๋Œ€ ์ •์›":
prompt += "์ƒ๋™๊ฐ ๋„˜์น˜๋Š” ์—ด๋Œ€ ์‹๋ฌผ๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ๊ณต๊ฐ„์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "์‚ฌ๋ง‰":
prompt += "๋์—†์ด ํŽผ์ณ์ง„ ํ™ฉ๊ธˆ๋น› ์‚ฌ๋ง‰ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๋ˆˆ ํ’๊ฒฝ":
prompt += "ํ•˜์–€ ๋ˆˆ์œผ๋กœ ๋ฎ์ธ ์ˆœ๋ฐฑ์˜ ํ’๊ฒฝ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
else:
prompt += f"{background_style} ์ž์—ฐ ํ™˜๊ฒฝ์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_type == "์‹ค๋‚ด ๋ฐฐ๊ฒฝ":
if background_style == "๊ฑฐ์‹ค":
prompt += "๋ชจ๋˜ํ•˜๊ณ  ์„ธ๋ จ๋œ ๊ฑฐ์‹ค ๊ณต๊ฐ„์˜ ํ…Œ์ด๋ธ” ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "์ฃผ๋ฐฉ":
prompt += "๊น”๋”ํ•œ ์ฃผ๋ฐฉ ์นด์šดํ„ฐ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "์„œ์žฌ":
prompt += "์ง€์ ์ธ ๋ถ„์œ„๊ธฐ์˜ ์„œ์žฌ ์ฑ…์ƒ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "ํ™”์žฅ๋Œ€":
prompt += "์„ธ๋ จ๋œ ํ™”์žฅ๋Œ€ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "ํ˜ธํ…” ๋กœ๋น„":
prompt += "๊ณ ๊ธ‰์Šค๋Ÿฌ์šด ํ˜ธํ…” ๋กœ๋น„์˜ ๋Œ€๋ฆฌ์„ ํ…Œ์ด๋ธ” ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "์นดํŽ˜":
prompt += "๊ฐ์„ฑ์ ์ธ ์นดํŽ˜ ํ…Œ์ด๋ธ” ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๋ฏธ๋‹ˆ๋ฉ€ ๊ณต๊ฐ„":
prompt += "๋ฏธ๋‹ˆ๋ฉ€ํ•œ ๋””์ž์ธ์˜ ๊น”๋”ํ•œ ๊ณต๊ฐ„์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
else:
prompt += f"{background_style} ์‹ค๋‚ด ๊ณต๊ฐ„์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_type == "ํŠน์ˆ˜ ๋ฐฐ๊ฒฝ":
if background_style == "๋„์‹œ ํ’๊ฒฝ":
prompt += "ํ˜„๋Œ€์ ์ธ ๋„์‹œ ์Šค์นด์ด๋ผ์ธ์„ ๋ฐฐ๊ฒฝ์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๋ฃจํ”„ํƒ‘":
prompt += "๋„์‹œ ์ „๊ฒฝ์ด ๋ณด์ด๋Š” ๋ฃจํ”„ํƒ‘ ํ…Œ์ด๋ธ” ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "ํ•ด์•ˆ๊ฐ€":
prompt += "์•„๋ฆ„๋‹ค์šด ํ•ด์•ˆ๊ฐ€์˜ ์ ˆ๋ฒฝ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๋ชจ๋˜ ์ŠคํŠœ๋””์˜ค":
prompt += "ํ”„๋กœํŽ˜์…”๋„ํ•œ ๋ชจ๋˜ ์ŠคํŠœ๋””์˜ค ์„ธํŠธ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๋นˆํ‹ฐ์ง€ ๊ณต๊ฐ„":
prompt += "ํด๋ž˜์‹ํ•œ ๋ถ„์œ„๊ธฐ์˜ ๋นˆํ‹ฐ์ง€ ๊ณต๊ฐ„์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
elif background_style == "๋ฏธ๋‹ˆ์–ด์ฒ˜ ๊ณต๊ฐ„":
prompt += "์ •๊ตํ•˜๊ฒŒ ๊พธ๋ฉฐ์ง„ ๋ฏธ๋‹ˆ์–ด์ฒ˜ ์„ธํŠธ ์œ„๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
else:
prompt += f"{background_style} ํŠน์ˆ˜ ํ™˜๊ฒฝ์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”. "
# ์กฐ๋ช…, ์งˆ๊ฐ, ์ƒ‰๊ฐ, ๋ถ„์œ„๊ธฐ ์ถ”๊ฐ€
if lighting:
if lighting == "์ž์—ฐ๊ด‘":
prompt += "๋ถ€๋“œ๋Ÿฌ์šด ์ž์—ฐ๊ด‘์ด ๋น„์น˜๋Š” ํ™˜๊ฒฝ์—์„œ "
elif lighting == "๋”ฐ๋œปํ•œ ์กฐ๋ช…":
prompt += "๋”ฐ๋œปํ•œ ํ™ฉ๊ธˆ๋น› ์กฐ๋ช…์ด ๊ฐ์‹ธ๋Š” ํ™˜๊ฒฝ์—์„œ "
elif lighting == "๊ทธ๋ฆผ์ž ์žˆ๋Š” ์กฐ๋ช…":
prompt += "๋“œ๋ผ๋งˆํ‹ฑํ•œ ๊ทธ๋ฆผ์ž๋ฅผ ๋งŒ๋“œ๋Š” ์กฐ๋ช… ์•„๋ž˜์—์„œ "
elif lighting == "๋ฏธ๋‹ˆ๋ฉ€ ์กฐ๋ช…":
prompt += "๊น”๋”ํ•˜๊ณ  ๋ฏธ๋‹ˆ๋ฉ€ํ•œ ์กฐ๋ช… ์•„๋ž˜์—์„œ "
# ์งˆ๊ฐ ์ถ”๊ฐ€
if texture and texture != "์„ ํƒ ์•ˆํ•จ":
if texture == "ํฐ ๋ฒฝ":
prompt += "๊น”๋”ํ•œ ํฐ ๋ฒฝ์„ ๋ฐฐ๊ฒฝ์œผ๋กœ ํ•˜์—ฌ "
elif texture == "๋Œ€๋ฆฌ์„":
prompt += "๊ณ ๊ธ‰์Šค๋Ÿฌ์šด ๋Œ€๋ฆฌ์„ ์งˆ๊ฐ ์œ„์— "
elif texture == "๋‚˜๋ฌด":
prompt += "๋”ฐ๋œปํ•œ ๋А๋‚Œ์˜ ์›๋ชฉ ์งˆ๊ฐ ์œ„์— "
elif texture == "๋ฒฝ๋Œ":
prompt += "ํด๋ž˜์‹ํ•œ ๋ฒฝ๋Œ ์งˆ๊ฐ์„ ๋ฐฐ๊ฒฝ์œผ๋กœ "
elif texture == "ํฐ ์ฒœ":
prompt += "๋ถ€๋“œ๋Ÿฌ์šด ํฐ ์ฒœ ์œ„์— "
elif texture == "์‹œ๋ฉ˜ํŠธ":
prompt += "๋ชจ๋˜ํ•œ ์‹œ๋ฉ˜ํŠธ ์งˆ๊ฐ ์œ„์— "
# ์ƒ‰๊ฐ ์ถ”๊ฐ€
if color_tone and color_tone != "์„ ํƒ ์•ˆํ•จ":
if color_tone == "ํ™”์ดํŠธ ํ†ค":
prompt += "์ˆœ๋ฐฑ์˜ ๊น”๋”ํ•œ ์ƒ‰๊ฐ์œผ๋กœ "
elif color_tone == "๋ธ”๋ฃจ ํ†ค":
prompt += "์‹œ์›ํ•œ ๋ธ”๋ฃจ ํ†ค์˜ ์ƒ‰๊ฐ์œผ๋กœ "
elif color_tone == "๋”ฐ๋œปํ•œ ์ƒ‰์ƒ":
prompt += "๋”ฐ๋œปํ•œ ํŒŒ์Šคํ…” ์ƒ‰๊ฐ์œผ๋กœ "
elif color_tone == "๋ชจ๋…ธํ†ค":
prompt += "์„ธ๋ จ๋œ ๋ชจ๋…ธํ†ค ์ƒ‰๊ฐ์œผ๋กœ "
elif color_tone == "ํŒŒ์Šคํ…”":
prompt += "๋ถ€๋“œ๋Ÿฌ์šด ํŒŒ์Šคํ…” ์ƒ‰๊ฐ์œผ๋กœ "
# ๋ถ„์œ„๊ธฐ ์ถ”๊ฐ€
if mood and mood != "์„ ํƒ ์•ˆํ•จ":
if mood == "ํฌ๊ทผํ•œ":
prompt += "ํฌ๊ทผํ•˜๊ณ  ์•„๋Š‘ํ•œ ๋ถ„์œ„๊ธฐ ์†์—์„œ "
elif mood == "์‹ฌํ”Œํ•œ":
prompt += "์‹ฌํ”Œํ•˜๊ณ  ๊น”๋”ํ•œ ๋ถ„์œ„๊ธฐ ์†์—์„œ "
elif mood == "๋ชจ๋˜ํ•œ":
prompt += "๋ชจ๋˜ํ•˜๊ณ  ์„ธ๋ จ๋œ ๋ถ„์œ„๊ธฐ ์†์—์„œ "
elif mood == "๋‚ด์ถ”๋Ÿด":
prompt += "์ž์—ฐ์Šค๋Ÿฝ๊ณ  ํŽธ์•ˆํ•œ ๋ถ„์œ„๊ธฐ ์†์—์„œ "
elif mood == "๋ฏธ๋‹ˆ๋ฉ€":
prompt += "๋ฏธ๋‹ˆ๋ฉ€๋ฆฌ์ฆ˜์˜ ์ •์ œ๋œ ๋ถ„์œ„๊ธฐ ์†์—์„œ "
# ๋งˆ๋ฌด๋ฆฌ
if product_category == "ํ™”์žฅํ’ˆ":
prompt += "์™„๋ฒฝํžˆ ๋””ํ…Œ์ผ์ด ์‚ด์•„์žˆ๋Š” ํ™”์žฅํ’ˆ ์ œํ’ˆ์„ ๊ณ ๊ธ‰์Šค๋Ÿฝ๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "๊ฐ€๋ฐฉ":
prompt += "์งˆ๊ฐ๊ณผ ๋””์ž์ธ์ด ๋‹๋ณด์ด๋Š” ๊ฐ€๋ฐฉ ์ œํ’ˆ์„ ๊ณ ๊ธ‰์Šค๋Ÿฝ๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "์•ก์„ธ์„œ๋ฆฌ":
prompt += "์„ฌ์„ธํ•œ ๋””ํ…Œ์ผ์ด ๋น›๋‚˜๋Š” ์•ก์„ธ์„œ๋ฆฌ๋ฅผ ๊ณ ๊ธ‰์Šค๋Ÿฝ๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "๋ณด์„๋ฅ˜":
prompt += "ํ™”๋ คํ•œ ๊ด‘์ฑ„๊ฐ€ ๋‹๋ณด์ด๋Š” ๋ณด์„ ์ œํ’ˆ์„ ๊ณ ๊ธ‰์Šค๋Ÿฝ๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "ํ™ˆ ๋ฐ์ฝ”":
prompt += "๊ณต๊ฐ„์— ์–ด์šธ๋ฆฌ๋Š” ํ™ˆ ๋ฐ์ฝ” ์ œํ’ˆ์„ ์„ธ๋ จ๋˜๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "์‹ ๋ฐœ":
prompt += "๋””์ž์ธ๊ณผ ์งˆ๊ฐ์ด ๋‹๋ณด์ด๋Š” ์‹ ๋ฐœ์„ ์„ธ๋ จ๋˜๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "์˜๋ฅ˜":
prompt += "์†Œ์žฌ์™€ ์‹ค๋ฃจ์—ฃ์ด ๋‹๋ณด์ด๋Š” ์˜๋ฅ˜๋ฅผ ์„ธ๋ จ๋˜๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "์ฃผ๋ฐฉ์šฉํ’ˆ":
prompt += "์‹ค์šฉ์„ฑ๊ณผ ๋””์ž์ธ์ด ๋‹๋ณด์ด๋Š” ์ฃผ๋ฐฉ์šฉํ’ˆ์„ ์„ธ๋ จ๋˜๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
elif product_category == "์ „์ž๊ธฐ๊ธฐ":
prompt += "์ฒจ๋‹จ ๊ธฐ์ˆ ๊ณผ ๋””์ž์ธ์ด ๋‹๋ณด์ด๋Š” ์ „์ž๊ธฐ๊ธฐ๋ฅผ ์„ธ๋ จ๋˜๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
else:
prompt += f"๋””ํ…Œ์ผ๊ณผ ํ’ˆ์งˆ์ด ๋‹๋ณด์ด๋Š” {product_category} ์ œํ’ˆ์„ ์„ธ๋ จ๋˜๊ฒŒ ์—ฐ์ถœํ•œ ์ƒ์—…์šฉ ์ œํ’ˆ ์‚ฌ์ง„"
return prompt
# ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๋ฐ LLM์— 2์ฐจ ์งˆ๋ฌธ ํ•จ์ˆ˜
def generate_prompt_and_ask_llm(product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood):
# 1์ฐจ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
kr_prompt = generate_llm_prompt(product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood)
# LLM์— 2์ฐจ ์งˆ๋ฌธํ•˜์—ฌ ์ตœ์ข… ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
final_prompt = ask_llm_for_final_prompt(kr_prompt)
# ์„ ํƒ ์˜ต์…˜ ์š”์•ฝ
options_summary = f"์ƒํ’ˆ๋ช…: {product_name if product_name else '์ง€์ •๋˜์ง€ ์•Š์Œ'}\n"
options_summary += f"์ œํ’ˆ๊ตฐ: {product_category}\n"
options_summary += f"๋ฐฐ๊ฒฝ ์œ ํ˜•: {background_type}\n"
options_summary += f"์„ธ๋ถ€ ์Šคํƒ€์ผ: {background_style}\n"
if lighting:
options_summary += f"์กฐ๋ช…: {lighting}\n"
if texture and texture != "์„ ํƒ ์•ˆํ•จ":
options_summary += f"๋ฐฐ๊ฒฝ ์งˆ๊ฐ: {texture}\n"
if color_tone and color_tone != "์„ ํƒ ์•ˆํ•จ":
options_summary += f"์ƒ‰๊ฐ: {color_tone}\n"
if mood and mood != "์„ ํƒ ์•ˆํ•จ":
options_summary += f"๋ถ„์œ„๊ธฐ: {mood}\n"
return kr_prompt, final_prompt, options_summary
# ๋ฐฐ๊ฒฝ ์œ ํ˜•์— ๋”ฐ๋ฅธ ์„ธ๋ถ€ ์Šคํƒ€์ผ ์˜ต์…˜ ์—…๋ฐ์ดํŠธ
def update_background_style_options(background_type):
if background_type == "์ž์—ฐ ๋ฐฐ๊ฒฝ":
return gr.update(choices=["์ˆฒ", "ํ•ด๋ณ€", "์‚ฐ", "๋“คํŒ", "๊ณ„๊ณก", "์—ด๋Œ€ ์ •์›", "์‚ฌ๋ง‰", "๋ˆˆ ํ’๊ฒฝ"], value="์ˆฒ")
elif background_type == "์‹ค๋‚ด ๋ฐฐ๊ฒฝ":
return gr.update(choices=["๊ฑฐ์‹ค", "์ฃผ๋ฐฉ", "์„œ์žฌ", "ํ™”์žฅ๋Œ€", "ํ˜ธํ…” ๋กœ๋น„", "์นดํŽ˜", "๋ฏธ๋‹ˆ๋ฉ€ ๊ณต๊ฐ„"], value="๊ฑฐ์‹ค")
elif background_type == "ํŠน์ˆ˜ ๋ฐฐ๊ฒฝ":
return gr.update(choices=["๋„์‹œ ํ’๊ฒฝ", "๋ฃจํ”„ํƒ‘", "ํ•ด์•ˆ๊ฐ€", "๋ชจ๋˜ ์ŠคํŠœ๋””์˜ค", "๋นˆํ‹ฐ์ง€ ๊ณต๊ฐ„", "๋ฏธ๋‹ˆ์–ด์ฒ˜ ๊ณต๊ฐ„"], value="๋„์‹œ ํ’๊ฒฝ")
else:
return gr.update(choices=["์„ ํƒํ•˜์„ธ์š”"], value="์„ ํƒํ•˜์„ธ์š”")
# ์ธํ„ฐํŽ˜์ด์Šค ๋ถ€๋ถ„ ์ˆ˜์ •
with gr.Blocks() as demo:
gr.HTML(
"""
<div style="text-align: center; margin-bottom: 1rem;">
<h1>์ด์ปค๋จธ์Šค ์ƒํ’ˆ ์ด๋ฏธ์ง€ ๋ฐฐ๊ฒฝ ์ƒ์„ฑ๊ธฐ</h1>
<p>์ƒํ’ˆ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ์›ํ•˜๋Š” ๋ฐฐ๊ฒฝ ์˜ต์…˜์„ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์„ธ์š”.</p>
</div>
"""
)
current_selection = gr.State("")
with gr.Row():
with gr.Column(scale=1):
with gr.Row():
image1_input = gr.Image(type="pil", label="#1 ์ƒํ’ˆ ์ด๋ฏธ์ง€", image_mode="RGB")
image2_input = gr.Image(type="pil", label="#2 (์„ ํƒ์‚ฌํ•ญ)", image_mode="RGB")
image3_input = gr.Image(type="pil", label="#3 (์„ ํƒ์‚ฌํ•ญ)", image_mode="RGB")
prompt_input = gr.Textbox(
lines=3,
placeholder="ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ๋น„์›Œ๋‘๋ฉด ์ž๋™ ํ•ฉ์„ฑ๋ฉ๋‹ˆ๋‹ค.",
label="ํ”„๋กฌํ”„ํŠธ (์„ ํƒ ์‚ฌํ•ญ)"
)
with gr.Row():
image_change_btn = gr.Button("์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ", variant="secondary")
text_remove_btn = gr.Button("๊ธ€์ž์ง€์šฐ๊ธฐ", variant="secondary")
clothes_change_btn = gr.Button("์˜ท๋ฐ”๊พธ๊ธฐ", variant="secondary")
background_change_btn = gr.Button("๋ฐฐ๊ฒฝ๋ฐ”๊พธ๊ธฐ", variant="secondary")
composite_product_btn = gr.Button("์ƒํ’ˆํ•ฉ์„ฑ", variant="secondary")
# ๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝ ๊ด€๋ จ ์„น์…˜ (๊ธฐ๋ณธ์ ์œผ๋กœ ์ˆจ๊น€ ์ƒํƒœ)
with gr.Row(visible=False) as background_header:
gr.HTML("<h3 style='margin-top: 20px; margin-bottom: 10px; width: 100%; text-align: center;'>๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝ ์˜ต์…˜</h3>")
# ์ƒํ’ˆ๋ช… ์ž…๋ ฅ
product_name = gr.Textbox(
label="์ƒํ’ˆ๋ช… ์ž…๋ ฅ",
placeholder="์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”",
visible=False
)
# ์ œํ’ˆ๊ตฐ ์นดํ…Œ๊ณ ๋ฆฌ ๋“œ๋กญ๋‹ค์šด
product_category = gr.Dropdown(
choices=["ํ™”์žฅํ’ˆ", "๊ฐ€๋ฐฉ", "์•ก์„ธ์„œ๋ฆฌ", "๋ณด์„๋ฅ˜", "ํ™ˆ ๋ฐ์ฝ”", "์‹ ๋ฐœ", "์˜๋ฅ˜", "์ฃผ๋ฐฉ์šฉํ’ˆ", "์ „์ž๊ธฐ๊ธฐ", "๊ธฐํƒ€"],
label="์ œํ’ˆ๊ตฐ ์„ ํƒ",
value="๊ธฐํƒ€",
visible=False
)
# ๋ฐฐ๊ฒฝ ์œ ํ˜• ๋“œ๋กญ๋‹ค์šด
background_type = gr.Dropdown(
choices=["์ž์—ฐ ๋ฐฐ๊ฒฝ", "์‹ค๋‚ด ๋ฐฐ๊ฒฝ", "ํŠน์ˆ˜ ๋ฐฐ๊ฒฝ"],
label="๋ฐฐ๊ฒฝ ์œ ํ˜• ์„ ํƒ",
value="์ž์—ฐ ๋ฐฐ๊ฒฝ",
visible=False
)
# ๋ฐฐ๊ฒฝ ์„ธ๋ถ€ ์Šคํƒ€์ผ ๋“œ๋กญ๋‹ค์šด (๋ฐฐ๊ฒฝ ์œ ํ˜•์— ๋”ฐ๋ผ ๋™์  ๋ณ€๊ฒฝ)
background_style = gr.Dropdown(
choices=["์ˆฒ", "ํ•ด๋ณ€", "์‚ฐ", "๋“คํŒ", "๊ณ„๊ณก", "์—ด๋Œ€ ์ •์›", "์‚ฌ๋ง‰", "๋ˆˆ ํ’๊ฒฝ"],
label="๋ฐฐ๊ฒฝ ์„ธ๋ถ€ ์Šคํƒ€์ผ ์„ ํƒ",
value="์ˆฒ",
visible=False
)
# ์ถ”๊ฐ€ ์„ ํƒ ์˜ต์…˜ ํ—ค๋”
with gr.Row(visible=False) as additional_header:
gr.HTML("<h4 style='margin-top: 15px; margin-bottom: 10px; width: 100%; text-align: center;'>์ถ”๊ฐ€ ์„ ํƒ ์˜ต์…˜</h4>")
# ์กฐ๋ช… ๋“œ๋กญ๋‹ค์šด
lighting = gr.Dropdown(
choices=["์ž์—ฐ๊ด‘", "๋”ฐ๋œปํ•œ ์กฐ๋ช…", "๊ทธ๋ฆผ์ž ์žˆ๋Š” ์กฐ๋ช…", "๋ฏธ๋‹ˆ๋ฉ€ ์กฐ๋ช…"],
label="์กฐ๋ช… ์„ ํƒ",
value="์ž์—ฐ๊ด‘",
visible=False
)
# ๋ฐฐ๊ฒฝ ์งˆ๊ฐ ๋“œ๋กญ๋‹ค์šด
texture = gr.Dropdown(
choices=["์„ ํƒ ์•ˆํ•จ", "ํฐ ๋ฒฝ", "๋Œ€๋ฆฌ์„", "๋‚˜๋ฌด", "๋ฒฝ๋Œ", "ํฐ ์ฒœ", "์‹œ๋ฉ˜ํŠธ"],
label="๋ฐฐ๊ฒฝ ์งˆ๊ฐ ์„ ํƒ",
value="์„ ํƒ ์•ˆํ•จ",
visible=False
)
# ์ƒ‰๊ฐ ๋“œ๋กญ๋‹ค์šด
color_tone = gr.Dropdown(
choices=["์„ ํƒ ์•ˆํ•จ", "ํ™”์ดํŠธ ํ†ค", "๋ธ”๋ฃจ ํ†ค", "๋”ฐ๋œปํ•œ ์ƒ‰์ƒ", "๋ชจ๋…ธํ†ค", "ํŒŒ์Šคํ…”"],
label="์ƒ‰๊ฐ ์„ ํƒ",
value="์„ ํƒ ์•ˆํ•จ",
visible=False
)
# ๋ถ„์œ„๊ธฐ ๋“œ๋กญ๋‹ค์šด
mood = gr.Dropdown(
choices=["์„ ํƒ ์•ˆํ•จ", "ํฌ๊ทผํ•œ", "์‹ฌํ”Œํ•œ", "๋ชจ๋˜ํ•œ", "๋‚ด์ถ”๋Ÿด", "๋ฏธ๋‹ˆ๋ฉ€"],
label="๋ถ„์œ„๊ธฐ ์„ ํƒ",
value="์„ ํƒ ์•ˆํ•จ",
visible=False
)
# ์„ ํƒ ๋‚ด์šฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
selection_preview = gr.Textbox(
label="์„ ํƒ ๋‚ด์šฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ",
lines=8,
visible=False
)
# ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๋ฒ„ํŠผ (๋ฐฐ๊ฒฝ๋ฐ”๊พธ๊ธฐ ์„ ํƒ์‹œ์—๋งŒ ๋‚˜ํƒ€๋‚จ)
prompt_generate_btn = gr.Button("ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ", variant="secondary", visible=False)
# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฒ„ํŠผ (ํ•ญ์ƒ ํ‘œ์‹œ)
submit_btn = gr.Button("์ด๋ฏธ์ง€ ์ƒ์„ฑ", variant="primary")
with gr.Column(scale=1):
output_image = gr.Image(label="์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€")
output_text = gr.Textbox(label="์ƒํƒœ ๋ฉ”์‹œ์ง€")
kr_prompt_display = gr.Textbox(label="์‚ฌ์šฉ๋œ ํ”„๋กฌํ”„ํŠธ (ํ•œ๊ตญ์–ด)", visible=True, lines=4)
prompt_display = gr.Textbox(label="์‚ฌ์šฉ๋œ ํ”„๋กฌํ”„ํŠธ (์˜์–ด)", visible=True, lines=4)
options_summary = gr.Textbox(label="์„ ํƒ๋œ ์˜ต์…˜ ์š”์•ฝ", lines=8, visible=True)
# ๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ˆ˜์ •
def show_background_options():
return ("๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝ", "๋ฐฐ๊ฒฝ๋ฐ”๊พธ๊ธฐ",
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), "", gr.update(visible=True)) # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๋ฒ„ํŠผ ํ‘œ์‹œ
background_change_btn.click(
fn=show_background_options,
inputs=[],
outputs=[prompt_input, current_selection, background_header, product_name, product_category,
background_type, background_style, additional_header, lighting, texture,
color_tone, mood, selection_preview, prompt_generate_btn]
)
# ๋‹ค๋ฅธ ๊ธฐ๋Šฅ ๋ฒ„ํŠผ๋“ค๋„ ๊ฐ„๋‹จํ•œ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •
def hide_background_options(prompt_value):
return (prompt_value, "๊ธฐํƒ€",
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), "", gr.update(visible=False)) # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๋ฒ„ํŠผ ์ˆจ๊น€
image_change_btn.click(
fn=lambda: hide_background_options("#1 ์ด๋ฏธ์ง€์˜ [๋‹ค๋ฅธ ๋ชจ์Šต]์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ผ."),
inputs=[],
outputs=[prompt_input, current_selection, background_header, product_name, product_category,
background_type, background_style, additional_header, lighting, texture,
color_tone, mood, selection_preview, prompt_generate_btn]
)
text_remove_btn.click(
fn=lambda: hide_background_options("#1 ์ด๋ฏธ์ง€์˜ [์ค‘๊ตญ์–ด๋ฅผ ๋ชจ๋‘]๋ฅผ ์ œ๊ฑฐํ•˜๋ผ."),
inputs=[],
outputs=[prompt_input, current_selection, background_header, product_name, product_category,
background_type, background_style, additional_header, lighting, texture,
color_tone, mood, selection_preview, prompt_generate_btn]
)
clothes_change_btn.click(
fn=lambda: hide_background_options("#1์ด๋ฏธ์ง€์—์„œ [์‹ ์ฒด ๋น„์œจ๊ณผ ํฌ์ฆˆ๋Š” ์œ ์ง€ํ•œ ์ฒด] ์˜์ƒ[์Šคํƒ€์ผ๊ณผ ์ƒ‰์ƒ์„]#2, #3์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”."),
inputs=[],
outputs=[prompt_input, current_selection, background_header, product_name, product_category,
background_type, background_style, additional_header, lighting, texture,
color_tone, mood, selection_preview, prompt_generate_btn]
)
composite_product_btn.click(
fn=lambda: hide_background_options("์ด๋ฏธ์ง€ #1์˜ ๋ชจ๋ธ์ด ํ•„์š”์— ๋”ฐ๋ผ ์ž์„ธ๋‚˜ ํ‘œ์ •์„ ์œ ๋™์ ์œผ๋กœ ๋ณ€ํ˜•ํ•œ ํ›„, ์ด๋ฏธ์ง€ #2(๋˜๋Š” #3)์˜ ์ƒํ’ˆ์„ ์ตœ๋Œ€ํ•œ ์›๋ž˜ ํ˜•ํƒœ์™€ ๋””ํ…Œ์ผ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ฐฉ์šฉํ•œ ๋ชจ์Šต์„ ํ•ฉ์„ฑํ•˜๋ผ."),
inputs=[],
outputs=[prompt_input, current_selection, background_header, product_name, product_category,
background_type, background_style, additional_header, lighting, texture,
color_tone, mood, selection_preview, prompt_generate_btn]
)
# ์„ ํƒ ์˜ต์…˜ ๋ณ€๊ฒฝ ์‹œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์™€ LLM ํ”„๋กฌํ”„ํŠธ ์—…๋ฐ์ดํŠธ
def update_all_previews(product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood):
llm_prompt = generate_llm_prompt(product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood)
preview = f"์ƒํ’ˆ๋ช…: {product_name}\n"
preview += f"์ œํ’ˆ๊ตฐ: {product_category}\n"
preview += f"๋ฐฐ๊ฒฝ ์œ ํ˜•: {background_type}\n"
preview += f"์„ธ๋ถ€ ์Šคํƒ€์ผ: {background_style}\n"
if lighting:
preview += f"์กฐ๋ช…: {lighting}\n"
if texture and texture != "์„ ํƒ ์•ˆํ•จ":
preview += f"๋ฐฐ๊ฒฝ ์งˆ๊ฐ: {texture}\n"
if color_tone and color_tone != "์„ ํƒ ์•ˆํ•จ":
preview += f"์ƒ‰๊ฐ: {color_tone}\n"
if mood and mood != "์„ ํƒ ์•ˆํ•จ":
preview += f"๋ถ„์œ„๊ธฐ: {mood}\n"
preview += f"\n์ƒ์„ฑ๋  ํ”„๋กฌํ”„ํŠธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n{llm_prompt}"
return preview
# ๋ฐฐ๊ฒฝ ์œ ํ˜•์— ๋”ฐ๋ผ ์„ธ๋ถ€ ์Šคํƒ€์ผ ์˜ต์…˜ ์—…๋ฐ์ดํŠธ (๋จผ์ € ์ˆ˜ํ–‰)
background_type.change(
fn=update_background_style_options,
inputs=[background_type],
outputs=[background_style]
).then(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
# ๋ชจ๋“  ์„ ํƒ ์ปดํฌ๋„ŒํŠธ์˜ ๋ณ€๊ฒฝ ์ด๋ฒคํŠธ์— ์—ฐ๊ฒฐ
product_name.change(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
product_category.change(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
background_style.change(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
lighting.change(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
texture.change(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
color_tone.change(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
mood.change(
fn=update_all_previews,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
# ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
prompt_generate_btn.click(
fn=generate_prompt_and_ask_llm,
inputs=[product_name, product_category, background_type, background_style,
lighting, texture, color_tone, mood],
outputs=[kr_prompt_display, prompt_input, options_summary]
)
# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ (์›๋ž˜ ๊ธฐ๋Šฅ ๋ณต์›)
submit_btn.click(
fn=process_images_with_prompt,
inputs=[image1_input, image2_input, image3_input, prompt_input],
outputs=[output_image, output_text, prompt_display],
)
gr.Markdown(
"""
### ์‚ฌ์šฉ ๋ฐฉ๋ฒ•:
1. ์ƒํ’ˆ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์„ธ์š” (#1 ์ด๋ฏธ์ง€).
2. "๋ฐฐ๊ฒฝ๋ฐ”๊พธ๊ธฐ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ฐฐ๊ฒฝ ๋ณ€๊ฒฝ ์˜ต์…˜์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.
3. ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•˜๊ณ  ์ œํ’ˆ๊ตฐ, ๋ฐฐ๊ฒฝ ์œ ํ˜•, ๋ฐฐ๊ฒฝ ์„ธ๋ถ€ ์Šคํƒ€์ผ์„ ์„ ํƒํ•˜์„ธ์š”.
4. ์ถ”๊ฐ€ ์˜ต์…˜(์กฐ๋ช…, ์งˆ๊ฐ, ์ƒ‰๊ฐ, ๋ถ„์œ„๊ธฐ)์„ ์„ ํƒํ•˜๋ฉด ๋” ์„ธ๋ฐ€ํ•œ ๋ฐฐ๊ฒฝ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
5. "ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด LLM ์Šคํƒ€์ผ ํ”„๋กฌํ”„ํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ์šฉ ํ”„๋กฌํ”„ํŠธ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
6. "์ด๋ฏธ์ง€ ์ƒ์„ฑ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์ƒ์„ฑ๋œ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
7. ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ๋ž€์— ์ง์ ‘ ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
> **ํŒ**: ์„ ํƒ ๋‚ด์šฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์—์„œ ์ƒ์„ฑ๋  ํ”„๋กฌํ”„ํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
"""
)