|
import os |
|
import tempfile |
|
import logging |
|
import re |
|
import time |
|
import json |
|
from PIL import Image |
|
import gradio as gr |
|
from google import genai |
|
from google.genai import types |
|
import google.generativeai as genai_generative |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
BACKGROUNDS_DIR = "./background" |
|
if not os.path.exists(BACKGROUNDS_DIR): |
|
os.makedirs(BACKGROUNDS_DIR) |
|
logger.info(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ต๋๋ค: {BACKGROUNDS_DIR}") |
|
|
|
|
|
SIMPLE_BACKGROUNDS = {} |
|
STUDIO_BACKGROUNDS = {} |
|
NATURE_BACKGROUNDS = {} |
|
INDOOR_BACKGROUNDS = {} |
|
TECHNOLOGY_BACKGROUNDS = {} |
|
COLORFUL_PATTERN_BACKGROUNDS = {} |
|
ABSTRACT_BACKGROUNDS = {} |
|
JEWELRY_BACKGROUNDS = {} |
|
|
|
|
|
def load_background_json(filename): |
|
"""๋ฐฐ๊ฒฝ JSON ํ์ผ ๋ก๋ ํจ์""" |
|
file_path = os.path.join(BACKGROUNDS_DIR, filename) |
|
try: |
|
with open(file_path, 'r', encoding='utf-8') as f: |
|
data = json.load(f) |
|
logger.info(f"{filename} ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค. {len(data)} ํญ๋ชฉ ํฌํจ.") |
|
return data |
|
except FileNotFoundError: |
|
logger.info(f"{filename} ํ์ผ์ด ์์ต๋๋ค.") |
|
return {} |
|
except Exception as e: |
|
logger.warning(f"{filename} ํ์ผ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}.") |
|
return {} |
|
|
|
|
|
def initialize_backgrounds(): |
|
"""๋ชจ๋ ๋ฐฐ๊ฒฝ ์ต์
์ด๊ธฐํ ํจ์""" |
|
global SIMPLE_BACKGROUNDS, STUDIO_BACKGROUNDS, NATURE_BACKGROUNDS, INDOOR_BACKGROUNDS |
|
global TECHNOLOGY_BACKGROUNDS, COLORFUL_PATTERN_BACKGROUNDS, ABSTRACT_BACKGROUNDS |
|
global JEWELRY_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") |
|
TECHNOLOGY_BACKGROUNDS = load_background_json("tech-backgrounds-final.json") |
|
COLORFUL_PATTERN_BACKGROUNDS = load_background_json("colorful-pattern-backgrounds.json") |
|
ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json") |
|
JEWELRY_BACKGROUNDS = load_background_json("jewelry_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 TECHNOLOGY_BACKGROUNDS: |
|
TECHNOLOGY_BACKGROUNDS = {"๋ค์ด๋๋ฏน ์คํ๋์": "dynamic water splash interaction with product"} |
|
if not COLORFUL_PATTERN_BACKGROUNDS: |
|
COLORFUL_PATTERN_BACKGROUNDS = {"ํ๋ คํ ๊ฝ ํจํด": "vibrant floral pattern backdrop"} |
|
if not ABSTRACT_BACKGROUNDS: |
|
ABSTRACT_BACKGROUNDS = {"๋ค์จ ๋ผ์ดํธ": "neon light abstract background with vibrant glowing elements"} |
|
if not JEWELRY_BACKGROUNDS: |
|
JEWELRY_BACKGROUNDS = {"ํด๋์ ํ์ดํธ ์คํฌ": "pristine white silk fabric backdrop"} |
|
|
|
logger.info("๋ชจ๋ ๋ฐฐ๊ฒฝ ์ต์
์ด๊ธฐํ ์๋ฃ") |
|
|
|
|
|
def initialize_dropdowns(): |
|
"""๋๋กญ๋ค์ด ๋ฉ๋ด ์ด๊ธฐํ ํจ์""" |
|
simple_choices = list(SIMPLE_BACKGROUNDS.keys()) |
|
studio_choices = list(STUDIO_BACKGROUNDS.keys()) |
|
nature_choices = list(NATURE_BACKGROUNDS.keys()) |
|
indoor_choices = list(INDOOR_BACKGROUNDS.keys()) |
|
tech_choices = list(TECHNOLOGY_BACKGROUNDS.keys()) |
|
colorful_choices = list(COLORFUL_PATTERN_BACKGROUNDS.keys()) |
|
abstract_choices = list(ABSTRACT_BACKGROUNDS.keys()) |
|
jewelry_choices = list(JEWELRY_BACKGROUNDS.keys()) |
|
|
|
return { |
|
"simple": simple_choices, |
|
"studio": studio_choices, |
|
"nature": nature_choices, |
|
"indoor": indoor_choices, |
|
"tech": tech_choices, |
|
"colorful": colorful_choices, |
|
"abstract": abstract_choices, |
|
"jewelry": jewelry_choices, |
|
} |
|
|
|
|
|
def save_binary_file(file_name, data): |
|
with open(file_name, "wb") as f: |
|
f.write(data) |
|
|
|
def translate_prompt_to_english(prompt): |
|
if not re.search("[๊ฐ-ํฃ]", prompt): |
|
return prompt |
|
prompt = prompt.replace("#1", "IMAGE_TAG_ONE") |
|
try: |
|
api_key = os.environ.get("GEMINI_API_KEY") |
|
if not api_key: |
|
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.") |
|
prompt = prompt.replace("IMAGE_TAG_ONE", "#1") |
|
return prompt |
|
client = genai.Client(api_key=api_key) |
|
translation_prompt = f""" |
|
Translate the following Korean text to English: |
|
|
|
{prompt} |
|
|
|
IMPORTANT: The token IMAGE_TAG_ONE is a special tag |
|
and must be preserved exactly as is in your translation. Do not translate this token. |
|
""" |
|
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(): |
|
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1") |
|
logger.info(f"Translated text: {translated_text.strip()}") |
|
return translated_text.strip() |
|
else: |
|
logger.warning("๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ์๋ณธ ํ๋กฌํํธ ์ฌ์ฉ") |
|
prompt = prompt.replace("IMAGE_TAG_ONE", "#1") |
|
return prompt |
|
except Exception as e: |
|
logger.exception("๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์:") |
|
prompt = prompt.replace("IMAGE_TAG_ONE", "#1") |
|
return prompt |
|
|
|
def preprocess_prompt(prompt, image1): |
|
has_img1 = image1 is not None |
|
if "#1" in prompt and not has_img1: |
|
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)") |
|
else: |
|
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง") |
|
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์." |
|
return prompt |
|
|
|
|
|
def generate_with_images(prompt, images, variation_index=0): |
|
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}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}") |
|
|
|
variation_suffixes = [ |
|
" Create this as a professional studio product shot with precise focus on the product details. Do not add any text, watermarks, or labels to the image.", |
|
" Create this as a high-contrast artistic studio shot with dramatic lighting and shadows. Do not add any text, watermarks, or labels to the image.", |
|
" Create this as a soft-lit elegantly styled product shot with complementary elements. Do not add any text, watermarks, or labels to the image.", |
|
" Create this as a high-definition product photography with perfect color accuracy and detail preservation. Do not add any text, watermarks, or labels to the image." |
|
] |
|
|
|
if variation_index < len(variation_suffixes): |
|
prompt = prompt + variation_suffixes[variation_index] |
|
else: |
|
prompt = prompt + " Create as high-end commercial product photography. Do not add any text, watermarks, or labels to the image." |
|
|
|
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.05, |
|
top_p=0.97, |
|
top_k=50, |
|
max_output_tokens=10240 |
|
) |
|
) |
|
|
|
with tempfile.NamedTemporaryFile(suffix=".jpg", 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") |
|
result_img.save(temp_path, format="JPEG", quality=95) |
|
return temp_path, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}" |
|
except Exception as e: |
|
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:") |
|
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}" |
|
|
|
def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3): |
|
retry_count = 0 |
|
last_error = None |
|
while retry_count < max_retries: |
|
try: |
|
images = [image1] |
|
valid_images = [img for img in images if img is not None] |
|
if not valid_images: |
|
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", "" |
|
final_prompt = prompt.strip() |
|
result_img, status = generate_with_images(final_prompt, valid_images, variation_index) |
|
if result_img is not None: |
|
return result_img, status, final_prompt |
|
else: |
|
last_error = status |
|
retry_count += 1 |
|
logger.warning(f"์ด๋ฏธ์ง ์์ฑ ์คํจ, ์ฌ์๋ {retry_count}/{max_retries}: {status}") |
|
time.sleep(1) |
|
except Exception as e: |
|
last_error = str(e) |
|
retry_count += 1 |
|
logger.exception(f"์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์, ์ฌ์๋ {retry_count}/{max_retries}:") |
|
time.sleep(1) |
|
return None, f"์ต๋ ์ฌ์๋ ํ์({max_retries}ํ) ์ด๊ณผ ํ ์คํจ: {last_error}", prompt |
|
|
|
|
|
def filter_prompt_only(prompt): |
|
"""Gemini์ ์ค๋ช
๋ฐ ๋ถํ์ํ ๋ฉ์์ง๋ฅผ ์ ๊ฑฐํ๊ณ ์ค์ ํ๋กฌํํธ๋ง ์ถ์ถํ๋ ํจ์""" |
|
code_block_pattern = r"```\s*(.*?)```" |
|
code_match = re.search(code_block_pattern, prompt, re.DOTALL) |
|
if code_match: |
|
return code_match.group(1).strip() |
|
|
|
if "--ar 1:1" in prompt: |
|
lines = prompt.split('\n') |
|
prompt_lines = [] |
|
in_prompt = False |
|
for line in lines: |
|
if (not in_prompt and |
|
("product" in line.lower() or |
|
"magazine" in line.lower() or |
|
"commercial" in line.lower() or |
|
"photography" in line.lower())): |
|
in_prompt = True |
|
prompt_lines.append(line) |
|
elif in_prompt: |
|
if "explanation" in line.lower() or "let me know" in line.lower(): |
|
break |
|
prompt_lines.append(line) |
|
if prompt_lines: |
|
return '\n'.join(prompt_lines).strip() |
|
|
|
return prompt.strip() |
|
|
|
def get_selected_background_info(bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry): |
|
"""์ ํ๋ ๋ฐฐ๊ฒฝ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์""" |
|
if bg_type == "์ฌํ ๋ฐฐ๊ฒฝ": |
|
return { |
|
"category": "์ฌํ ๋ฐฐ๊ฒฝ", |
|
"name": simple, |
|
"english": SIMPLE_BACKGROUNDS.get(simple, "white background") |
|
} |
|
elif bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ": |
|
return { |
|
"category": "์คํ๋์ค ๋ฐฐ๊ฒฝ", |
|
"name": studio, |
|
"english": STUDIO_BACKGROUNDS.get(studio, "product photography studio") |
|
} |
|
elif bg_type == "์์ฐ ํ๊ฒฝ": |
|
return { |
|
"category": "์์ฐ ํ๊ฒฝ", |
|
"name": nature, |
|
"english": NATURE_BACKGROUNDS.get(nature, "natural environment") |
|
} |
|
elif bg_type == "์ค๋ด ํ๊ฒฝ": |
|
return { |
|
"category": "์ค๋ด ํ๊ฒฝ", |
|
"name": indoor, |
|
"english": INDOOR_BACKGROUNDS.get(indoor, "indoor environment") |
|
} |
|
elif bg_type == "ํ
ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ": |
|
return { |
|
"category": "ํ
ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ", |
|
"name": tech, |
|
"english": TECHNOLOGY_BACKGROUNDS.get(tech, "technology environment") |
|
} |
|
elif bg_type == "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ": |
|
return { |
|
"category": "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ", |
|
"name": colorful, |
|
"english": COLORFUL_PATTERN_BACKGROUNDS.get(colorful, "colorful pattern background") |
|
} |
|
elif bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ": |
|
return { |
|
"category": "์ถ์/ํน์ ๋ฐฐ๊ฒฝ", |
|
"name": abstract, |
|
"english": ABSTRACT_BACKGROUNDS.get(abstract, "abstract background") |
|
} |
|
elif bg_type == "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ": |
|
return { |
|
"category": "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ", |
|
"name": jewelry, |
|
"english": JEWELRY_BACKGROUNDS.get(jewelry, "jewelry backdrop") |
|
} |
|
else: |
|
return { |
|
"category": "๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ", |
|
"name": "ํ์ดํธ ๋ฐฐ๊ฒฝ", |
|
"english": "white background" |
|
} |
|
|
|
def generate_enhanced_system_instruction(): |
|
"""ํฅ์๋ ์์คํ
์ธ์คํธ๋ญ์
์์ฑ ํจ์""" |
|
return """๋น์ ์ ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝํ๊ธฐ ์ํ ์ต๊ณ ํ์ง์ ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก ๋ฏธ๋์ ๋(Midjourney)์ ์ฌ์ฉํ ์ ์๋ ์์ธํ๊ณ ์ ๋ฌธ์ ์ธ ํ๋กฌํํธ๋ฅผ ์์ด๋ก ์์ฑํด์ฃผ์ธ์. |
|
๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ์ค์ํด์ผ ํฉ๋๋ค: |
|
1. ์ํ์ "#1"๋ก ์ง์ ํ์ฌ ์ฐธ์กฐํฉ๋๋ค. (์: "skincare tube (#1)") |
|
2. *** ๋งค์ฐ ์ค์: ์ํ์ ์๋ ํน์ฑ(๋์์ธ, ์์, ํํ, ๋ก๊ณ , ํจํค์ง ๋ฑ)์ ์ด๋ค ์ํฉ์์๋ ์ ๋ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. *** |
|
3. *** ์ํ์ ๋ณธ์ง์ ํน์ฑ์ ์ ์งํ๋, ์ํ์ ํฌ์ปค์ค๋ฅผ ๋ง์ถฐ ๋ชจ๋ ์ธ๋ถ ์ฌํญ์ด ์ ๋ช
ํ๊ฒ ๋๋ฌ๋๋๋ก ํ๋ฉฐ, |
|
8K ํด์๋(8K resolution), ์ค๋ฒ์คํ๋ ์๋ ์ด๊ณ ํ์ง(ultra high definition without oversharpening)๋ก ๋ ๋๋ง๋์ด์ผ ํฉ๋๋ค. *** |
|
4. ์ด๋ฏธ์ง ๋น์จ์ ์ ํํ 1:1(์ ์ฌ๊ฐํ) ํ์์ผ๋ก ์ง์ ํฉ๋๋ค. ํ๋กฌํํธ์ "square format", "1:1 ratio" ๋๋ "aspect ratio 1:1"์ ๋ช
์์ ์ผ๋ก ํฌํจํฉ๋๋ค. |
|
5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํ๋ฉฐ, ์ ์ ํ ํฌ๊ธฐ๋ก ํํํ์ฌ ๋ํ
์ผ์ด ์๋ฒฝํ๊ฒ ๋ณด์ด๋๋ก ํฉ๋๋ค. |
|
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ 60-70% ์ด์ ์ฐจ์งํ๋๋ก ํฉ๋๋ค. |
|
7. ์กฐ๋ช
์ค๋ช
์ ๋งค์ฐ ๊ตฌ์ฒด์ ์ผ๋ก ํด์ฃผ์ธ์. ์: "soft directional lighting from left side", "dramatic rim lighting", "diffused natural light through windows" |
|
8. ๋ฐฐ๊ฒฝ์ ์ฌ์ง๊ณผ ์ง๊ฐ์ ์์ธํ ์ค๋ช
ํด์ฃผ์ธ์. ์: "polished marble surface", "rustic wooden table with visible grain", "matte concrete wall with subtle texture" |
|
9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช
์์ ์ผ๋ก ํฌํจํ๋, ์ฌ์ฉ ๋งฅ๋ฝ์ ์ ์ ํ๊ฒ ๋ณํํ์ธ์: |
|
- "award-winning product photography" |
|
- "magazine-worthy commercial product shot" |
|
- "professional advertising imagery with perfect exposure" |
|
- "studio lighting with color-accurate rendering" |
|
- "8K ultra high definition product showcase" |
|
- "commercial product photography with precise detail rendering" |
|
- "ultra high definition" |
|
- "crystal clear details" |
|
10. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ํ๋กฌํํธ์ ์ ํํ ๋ฐ์ํ๊ณ ํ์ฅํฉ๋๋ค. |
|
11. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2 --v 5.2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค. |
|
12. ๋งค์ฐ ์ค์: ํ๋กฌํํธ ์ธ์ ๋ค๋ฅธ ์ค๋ช
์ด๋ ๋ฉํ ํ
์คํธ๋ฅผ ํฌํจํ์ง ๋ง์ธ์. ์ค์ง ํ๋กฌํํธ ์์ฒด๋ง ์ ๊ณตํ์ธ์. |
|
""" |
|
|
|
def generate_prompt_with_gemini(product_name, background_info, additional_info=""): |
|
"""ํฅ์๋ ํ๋กฌํํธ ์์ฑ ํจ์""" |
|
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "") |
|
if not GEMINI_API_KEY: |
|
return "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์ GEMINI_API_KEY๋ฅผ ์ค์ ํ๊ฑฐ๋ ์ฝ๋์ ์ง์ ์
๋ ฅํ์ธ์." |
|
|
|
try: |
|
genai_generative.configure(api_key=GEMINI_API_KEY) |
|
|
|
prompt_request = f""" |
|
์ํ๋ช
: {product_name} |
|
๋ฐฐ๊ฒฝ ์ ํ: {background_info.get('english', 'studio')} |
|
๋ฐฐ๊ฒฝ ์นดํ
๊ณ ๋ฆฌ: {background_info.get('category', '')} |
|
๋ฐฐ๊ฒฝ ์ด๋ฆ: {background_info.get('name', '')} |
|
์ถ๊ฐ ์์ฒญ์ฌํญ: {additional_info} |
|
์ค์ ์๊ตฌ์ฌํญ: |
|
1. ์ํ(#1)์ด ์ด๋ฏธ์ง ๊ตฌ๋์์ ์ค์ฌ์ ์ธ ์์น๋ฅผ ์ฐจ์งํ๋ฉฐ ์ ์ ํ ํฌ๊ธฐ(์ด๋ฏธ์ง์ 60-70%)๋ก ํํ๋๋๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. |
|
2. ์ด๋ฏธ์ง๋ ์ ํํ 1:1 ๋น์จ(์ ์ฌ๊ฐํ)์ด์ด์ผ ํฉ๋๋ค. |
|
3. ์ํ์ ๋์์ธ, ์์, ํํ, ๋ก๊ณ ๋ฑ ๋ณธ์ง์ ํน์ฑ์ ์ ๋ ์์ ํ์ง ๋ง์ธ์. |
|
4. ๊ตฌ์ฒด์ ์ธ ์กฐ๋ช
๊ธฐ๋ฒ์ ์์ธํ ๋ช
์ํด์ฃผ์ธ์: |
|
- ์ ํํ ์กฐ๋ช
์์น (์: "45-degree key light from upper left") |
|
- ์กฐ๋ช
ํ์ง (์: "soft diffused light", "hard directional light") |
|
- ์กฐ๋ช
๊ฐ๋์ ์์จ๋ (์: "warm tungsten key light with cool blue fill") |
|
- ๋ฐ์ฌ์ ๊ทธ๋ฆผ์ ์ฒ๋ฆฌ ๋ฐฉ์ (์: "controlled specular highlights with soft shadow transitions") |
|
5. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ณด์กฐ ์์(props)๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํ์ฉํ๋, ์ํ์ด ํญ์ ์ฃผ์ธ๊ณต์ด์ด์ผ ํฉ๋๋ค. |
|
6. ๋ฐฐ๊ฒฝ ์ฌ์ง๊ณผ ํ๋ฉด ์ง๊ฐ์ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช
ํ๊ณ , ์ํ๊ณผ์ ์ํธ์์ฉ ๋ฐฉ์์ ๋ช
์ํด์ฃผ์ธ์. |
|
7. ์์ ๊ตฌ์ฑ(color palette, color harmonies)์ ๋ช
ํํ ํด์ฃผ์ธ์. |
|
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
๊ด๊ณ ํ์ง์ ์ด๋ฏธ์ง๊ฐ ๋๋๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. |
|
9. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2 --v 5.2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์. |
|
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์ ๋ฌธ์ ์ธ ์์ด๋ก ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์. |
|
""" |
|
model = genai_generative.GenerativeModel( |
|
'gemini-2.0-flash', |
|
system_instruction=generate_enhanced_system_instruction() |
|
) |
|
|
|
response = model.generate_content( |
|
prompt_request, |
|
generation_config=genai_generative.types.GenerationConfig( |
|
temperature=0.8, |
|
top_p=0.97, |
|
top_k=64, |
|
max_output_tokens=1600, |
|
) |
|
) |
|
|
|
response_text = response.text.strip() |
|
|
|
if "--ar 1:1" not in response_text: |
|
response_text = response_text.rstrip(".") + ". --ar 1:1 --s 750 --q 2 --v 5.2" |
|
|
|
return response_text |
|
except Exception as e: |
|
return f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
|
|
|
|
def generate_product_image(image, bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry, product_name, additional_info): |
|
if image is None: |
|
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ ํ ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์." |
|
product_name = product_name.strip() or "์ ํ" |
|
background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry) |
|
generated_prompt = generate_prompt_with_gemini(product_name, background_info, additional_info) |
|
if "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค" in generated_prompt: |
|
warning_msg = ( |
|
"[Gemini API ํค ๋๋ฝ]\n" |
|
"API ํค ์ค์ ๋ฐฉ๋ฒ:\n" |
|
"1. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY=\"your-api-key\"\n" |
|
"2. ์ฝ๋ ๋ด ์ง์ ์
๋ ฅ: GEMINI_API_KEY = \"your-api-key\"\n" |
|
"ํค ๋ฐ๊ธ: https://aistudio.google.com/apikey" |
|
) |
|
return None, warning_msg, warning_msg |
|
final_prompt = filter_prompt_only(generated_prompt) |
|
result_image, status, _ = process_images_with_prompt(image, final_prompt, 0) |
|
return result_image, status, final_prompt |
|
|
|
|
|
def generate_product_images(image, bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry, product_name, additional_info): |
|
if image is None: |
|
return None, None, None, None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ ํ ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์." |
|
product_name = product_name.strip() or "์ ํ" |
|
background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry) |
|
generated_prompt = generate_prompt_with_gemini(product_name, background_info, additional_info) |
|
if "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค" in generated_prompt: |
|
warning_msg = ( |
|
"[Gemini API ํค ๋๋ฝ]\n" |
|
"API ํค ์ค์ ๋ฐฉ๋ฒ:\n" |
|
"1. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY=\"your-api-key\"\n" |
|
"2. ์ฝ๋ ๋ด ์ง์ ์
๋ ฅ: GEMINI_API_KEY = \"your-api-key\"\n" |
|
"ํค ๋ฐ๊ธ: https://aistudio.google.com/apikey" |
|
) |
|
return None, None, None, None, warning_msg, warning_msg |
|
final_prompt = filter_prompt_only(generated_prompt) |
|
images_list = [] |
|
statuses = [] |
|
for i in range(4): |
|
result_img, status, _ = process_images_with_prompt(image, final_prompt, variation_index=i) |
|
images_list.append(result_img) |
|
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}") |
|
time.sleep(1) |
|
combined_status = "\n".join(statuses) |
|
return images_list[0], images_list[1], images_list[2], images_list[3], combined_status, final_prompt |
|
|
|
|
|
def create_app(): |
|
dropdown_options = initialize_dropdowns() |
|
|
|
with gr.Blocks(title="๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ๋ฐ ์ด๋ฏธ์ง ์์ฑ") as demo: |
|
gr.Markdown("# ๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ๋ฐ ์ด๋ฏธ์ง ์์ฑ") |
|
gr.Markdown( |
|
"์ํ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ , ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ต์
, ์ถ๊ฐ ์์ฒญ์ฌํญ ๋ฐ Gemini API ํค๋ฅผ ์
๋ ฅํ๋ฉด Gemini API๋ฅผ ํตํด ์์ด ํ๋กฌํํธ๋ฅผ ์์ฑํ๊ณ , ํด๋น ํ๋กฌํํธ๋ก ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.\n\n" |
|
"๋จ์ผ ์ด๋ฏธ์ง ์์ฑ๊ณผ 4์ฅ ์ด๋ฏธ์ง ์์ฑ ๋ชจ๋ ๊ฐ๋ฅํฉ๋๋ค.\n\n" |
|
"[Gemini API ํค ๋ฐ๊ธฐ](https://aistudio.google.com/apikey)" |
|
) |
|
|
|
with gr.Row(): |
|
|
|
gemini_api_key = gr.Textbox( |
|
label="Gemini API ํค", |
|
type="password", |
|
placeholder="API ํค๋ฅผ ์
๋ ฅํ์ธ์", |
|
interactive=True |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
product_name = gr.Textbox(label="์ํ๋ช
(ํ๊ตญ์ด ์
๋ ฅ)", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ์ค๋งํธ์์น, ํฅ์, ์ด๋ํ ๋ฑ", interactive=True) |
|
image_input = gr.Image(label="์ํ ์ด๋ฏธ์ง ์
๋ก๋", type="pil") |
|
background_type = gr.Radio( |
|
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "ํ
ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ", "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ", "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ"], |
|
label="๋ฐฐ๊ฒฝ ์ ํ", |
|
value="์ฌํ ๋ฐฐ๊ฒฝ" |
|
) |
|
simple_dropdown = gr.Dropdown( |
|
choices=dropdown_options["simple"], |
|
value=dropdown_options["simple"][0] if dropdown_options["simple"] else None, |
|
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=True, |
|
interactive=True |
|
) |
|
studio_dropdown = gr.Dropdown( |
|
choices=dropdown_options["studio"], |
|
value=dropdown_options["studio"][0] if dropdown_options["studio"] else None, |
|
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
nature_dropdown = gr.Dropdown( |
|
choices=dropdown_options["nature"], |
|
value=dropdown_options["nature"][0] if dropdown_options["nature"] else None, |
|
label="์์ฐ ํ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
indoor_dropdown = gr.Dropdown( |
|
choices=dropdown_options["indoor"], |
|
value=dropdown_options["indoor"][0] if dropdown_options["indoor"] else None, |
|
label="์ค๋ด ํ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
tech_dropdown = gr.Dropdown( |
|
choices=dropdown_options["tech"], |
|
value=dropdown_options["tech"][0] if dropdown_options["tech"] else None, |
|
label="ํ
ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
colorful_dropdown = gr.Dropdown( |
|
choices=dropdown_options["colorful"], |
|
value=dropdown_options["colorful"][0] if dropdown_options["colorful"] else None, |
|
label="์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
abstract_dropdown = gr.Dropdown( |
|
choices=dropdown_options["abstract"], |
|
value=dropdown_options["abstract"][0] if dropdown_options["abstract"] else None, |
|
label="์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
jewelry_dropdown = gr.Dropdown( |
|
choices=dropdown_options["jewelry"], |
|
value=dropdown_options["jewelry"][0] if dropdown_options["jewelry"] else None, |
|
label="์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
additional_info = gr.Textbox( |
|
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)", |
|
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ ๊ฐ์ฒด ๋ฑ", |
|
lines=3, |
|
interactive=True |
|
) |
|
|
|
def update_dropdowns(bg_type): |
|
return { |
|
simple_dropdown: gr.update(visible=(bg_type == "์ฌํ ๋ฐฐ๊ฒฝ")), |
|
studio_dropdown: gr.update(visible=(bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ")), |
|
nature_dropdown: gr.update(visible=(bg_type == "์์ฐ ํ๊ฒฝ")), |
|
indoor_dropdown: gr.update(visible=(bg_type == "์ค๋ด ํ๊ฒฝ")), |
|
tech_dropdown: gr.update(visible=(bg_type == "ํ
ํฌ๋๋ก์ง ๋ฐฐ๊ฒฝ")), |
|
colorful_dropdown: gr.update(visible=(bg_type == "์ปฌ๋ฌํ ํจํด ๋ฐฐ๊ฒฝ")), |
|
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ")), |
|
jewelry_dropdown: gr.update(visible=(bg_type == "์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ")) |
|
} |
|
background_type.change( |
|
fn=update_dropdowns, |
|
inputs=[background_type], |
|
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, tech_dropdown, colorful_dropdown, abstract_dropdown, jewelry_dropdown] |
|
) |
|
|
|
with gr.Row(): |
|
single_btn = gr.Button("ํ๋กฌํํธ ๋ฐ ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ", variant="primary") |
|
multi_btn = gr.Button("ํ๋กฌํํธ ๋ฐ 4์ฅ ์ด๋ฏธ์ง ์์ฑ", variant="primary") |
|
|
|
with gr.Column(scale=1): |
|
|
|
with gr.Row(): |
|
image_output1 = gr.Image(label="์ด๋ฏธ์ง #1", type="filepath") |
|
image_output2 = gr.Image(label="์ด๋ฏธ์ง #2", type="filepath") |
|
with gr.Row(): |
|
image_output3 = gr.Image(label="์ด๋ฏธ์ง #3", type="filepath") |
|
image_output4 = gr.Image(label="์ด๋ฏธ์ง #4", type="filepath") |
|
status_output = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=3) |
|
|
|
|
|
def modified_single_image_gen(api_key, image, bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry, product_name, additional_info): |
|
os.environ["GEMINI_API_KEY"] = api_key.strip() |
|
result_img, status, _ = generate_product_image(image, bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry, product_name, additional_info) |
|
return result_img, None, None, None, status |
|
|
|
single_btn.click( |
|
fn=modified_single_image_gen, |
|
inputs=[gemini_api_key, image_input, background_type, simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, tech_dropdown, colorful_dropdown, abstract_dropdown, jewelry_dropdown, product_name, additional_info], |
|
outputs=[image_output1, image_output2, image_output3, image_output4, status_output] |
|
) |
|
|
|
|
|
def modified_multi_image_gen(api_key, image, bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry, product_name, additional_info): |
|
os.environ["GEMINI_API_KEY"] = api_key.strip() |
|
img1, img2, img3, img4, status, _ = generate_product_images(image, bg_type, simple, studio, nature, indoor, tech, colorful, abstract, jewelry, product_name, additional_info) |
|
return img1, img2, img3, img4, status |
|
|
|
multi_btn.click( |
|
fn=modified_multi_image_gen, |
|
inputs=[gemini_api_key, image_input, background_type, simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, tech_dropdown, colorful_dropdown, abstract_dropdown, jewelry_dropdown, product_name, additional_info], |
|
outputs=[image_output1, image_output2, image_output3, image_output4, status_output] |
|
) |
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
initialize_backgrounds() |
|
app = create_app() |
|
app.queue() |
|
app.launch() |
|
|