|
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__) |
|
|
|
|
|
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 the first variation. Do not add any text, watermarks, or labels to the image.", |
|
" Create this as the second variation with more vivid colors. Do not add any text, watermarks, or labels to the image.", |
|
" Create this as the third variation with a more creative style. Do not add any text, watermarks, or labels to the image.", |
|
" Create this as the fourth variation with enhanced details. 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 + " 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, |
|
top_p=0.95, |
|
top_k=40, |
|
max_output_tokens=8192 |
|
) |
|
) |
|
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, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", "" |
|
if prompt and prompt.strip(): |
|
processed_prompt = preprocess_prompt(prompt, image1) |
|
if re.search("[๊ฐ-ํฃ]", processed_prompt): |
|
final_prompt = translate_prompt_to_english(processed_prompt) |
|
else: |
|
final_prompt = processed_prompt |
|
else: |
|
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." |
|
logger.info("Default prompt generated for single image") |
|
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 generate_multiple_images(image1, prompt, progress=gr.Progress()): |
|
results = [] |
|
statuses = [] |
|
prompts = [] |
|
num_images = 4 |
|
max_retries = 3 |
|
progress(0, desc="์ด๋ฏธ์ง ์์ฑ ์ค๋น ์ค...") |
|
for i in range(num_images): |
|
progress((i / num_images), desc=f"{i+1}/{num_images} ์ด๋ฏธ์ง ์์ฑ ์ค...") |
|
result_img, status, final_prompt = process_images_with_prompt(image1, prompt, i, max_retries) |
|
if result_img is not None: |
|
results.append(result_img) |
|
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}") |
|
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}") |
|
else: |
|
results.append(None) |
|
statuses.append(f"์ด๋ฏธ์ง #{i+1} ์์ฑ ์คํจ: {status}") |
|
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}") |
|
time.sleep(1) |
|
progress(1.0, desc="์ด๋ฏธ์ง ์์ฑ ์๋ฃ!") |
|
while len(results) < 4: |
|
results.append(None) |
|
combined_status = "\n".join(statuses) |
|
combined_prompts = "\n".join(prompts) |
|
return results[0], results[1], results[2], results[3], combined_status, combined_prompts |
|
|
|
|
|
|
|
|
|
BACKGROUNDS_DIR = "./background" |
|
if not os.path.exists(BACKGROUNDS_DIR): |
|
os.makedirs(BACKGROUNDS_DIR) |
|
logger.info(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ต๋๋ค: {BACKGROUNDS_DIR}") |
|
else: |
|
logger.info(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๊ฐ ์ด๋ฏธ ์กด์ฌํฉ๋๋ค: {BACKGROUNDS_DIR}") |
|
|
|
def load_background_json(filename): |
|
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 Exception as e: |
|
logger.warning(f"{filename} ํ์ผ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.") |
|
return {} |
|
|
|
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") |
|
ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json") |
|
|
|
|
|
if not SIMPLE_BACKGROUNDS: |
|
SIMPLE_BACKGROUNDS = {"ํ์ดํธ ๋ฐฐ๊ฒฝ": "white background"} |
|
if not STUDIO_BACKGROUNDS: |
|
STUDIO_BACKGROUNDS = {"์ ํ ์ฌ์ง ์คํ๋์ค": "product photography studio"} |
|
if not NATURE_BACKGROUNDS: |
|
NATURE_BACKGROUNDS = {"์ด๋ ํด๋ณ": "tropical beach"} |
|
if not INDOOR_BACKGROUNDS: |
|
INDOOR_BACKGROUNDS = {"๋ชจ๋ ๋ฆฌ๋น๋ฃธ": "modern living room"} |
|
if not ABSTRACT_BACKGROUNDS: |
|
ABSTRACT_BACKGROUNDS = {"๋ค์จ ์กฐ๋ช
": "neon lights"} |
|
|
|
def generate_system_instruction(): |
|
return """๋น์ ์ ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝํ๊ธฐ ์ํ ๊ณ ํ์ง ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค. |
|
์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก ๋ฏธ๋์ ๋(Midjourney)์ ์ฌ์ฉํ ์ ์๋ |
|
์์ธํ๊ณ ์ ๋ฌธ์ ์ธ ํ๋กฌํํธ๋ฅผ ์์ด๋ก ์์ฑํด์ฃผ์ธ์. |
|
๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ๋ฐ๋ผ์ผ ํฉ๋๋ค: |
|
1. ์ํ์ "#1"๋ก ์ง์ ํ์ฌ ์ฐธ์กฐํฉ๋๋ค. (์: "skincare tube (#1)") |
|
2. *** ๋งค์ฐ ์ค์: ์ํ์ ์๋ ํน์ฑ(๋์์ธ, ์์, ํํ, ๋ก๊ณ , ํจํค์ง ๋ฑ)์ ์ด๋ค ์ํฉ์์๋ ์ ๋ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. *** |
|
3. *** ์ํ์ ๋ณธ์ง์ ํน์ฑ์ ์ ์งํ๋, ์์ฐ์ค๋ฌ์ด ํ๊ฒฝ ํตํฉ์ ์ํ ์กฐ๋ช
๊ณผ ๊ทธ๋ฆผ์๋ ํ์ฉํฉ๋๋ค: *** |
|
- ์ํ ์์ฒด์ ์์, ๋์์ธ, ํํ, ํ
์ค์ฒ๋ ์ ๋ ์์ ํ์ง ์์ต๋๋ค. |
|
- ํ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ธ๋ฆฌ๋ ๊ทธ๋ฆผ์, ์ฃผ๋ณ ์กฐ๋ช
ํจ๊ณผ๋ ํ์ฉ๋ฉ๋๋ค. |
|
- ์ํ์ ๋ฌผ๋ฐฉ์ธ, ์์ถ, ๊ธ, ์๊ณผ ๊ฐ์ ์ถ๊ฐ ์์๋ ๋ฌผ๋ฆฌ์ ํจ๊ณผ๋ ์ ์ฉํ์ง ์์ต๋๋ค. |
|
- ํ๊ฒฝ์ ์ด์ธ๋ฆฌ๋ ์์ฐ์ค๋ฌ์ด ๋น ๋ฐ์ฌ, ์ฃผ๋ณ ์กฐ๋ช
, ๊ทธ๋ฆผ์๋ ์ฌ์ค์ ํตํฉ๊ฐ์ ์ํด ์ ์ฉํ ์ ์์ต๋๋ค. |
|
4. ์ด๋ฏธ์ง ๋น์จ์ ์ ํํ 1:1(์ ์ฌ๊ฐํ) ํ์์ผ๋ก ์ง์ ํฉ๋๋ค. ํ๋กฌํํธ์ "square format", "1:1 ratio" ๋๋ "aspect ratio 1:1"์ ๋ช
์์ ์ผ๋ก ํฌํจํฉ๋๋ค. |
|
5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํฉ๋๋ค. |
|
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค. |
|
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค. |
|
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๋ค์ ํ๊ฒฝ ์์๋ค์ ํฌํจํ์ธ์: |
|
- ์ํ๊ณผ ์ด์ธ๋ฆฌ๋ ์ฃผ๋ณ ํ๊ฒฝ/๋ฐฐ๊ฒฝ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ฅํ ์ฃผ๋ณ์ ๊ฝ์ด๋ ํ๋ธ, ์๋ฃ ์ ํ ์์ ๊ณผ์ผ, ์ ์์ ํ ๊ทผ์ฒ์ ํ๋์ ์ํ ๋ฑ. |
|
- ํ๊ฒฝ์ ์กฐ๋ช
ํจ๊ณผ(๋ฆผ ๋ผ์ดํธ, ๋ฐฑ๋ผ์ดํธ, ์ํํธ๋ฐ์ค ๋ฑ)๋ฅผ ์ค๋ช
ํฉ๋๋ค. |
|
- ์ํ์ด ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ์กด์ฌํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ ์ ํ ๊ทธ๋ฆผ์์ ๋น ํํ์ ํฌํจํฉ๋๋ค. |
|
- ์ํ์ ์ฉ๋๋ ์ฅ์ ์ ๊ฐ์ ์ ์ผ๋ก ์์ํ๋ ๋ฐฐ๊ฒฝ ์์๋ฅผ ํฌํจํฉ๋๋ค. |
|
- ํ๋กํ์
๋ํ ์์
์ฌ์ง ํจ๊ณผ(์ ํ์ ํผ์ฌ๊ณ ์ฌ๋, ์ํํธ ํฌ์ปค์ค, ์คํ๋์ค ์กฐ๋ช
๋ฑ)๋ฅผ ๋ช
์ํฉ๋๋ค. |
|
9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช
์์ ์ผ๋ก ํฌํจํ์ธ์: |
|
- "highly detailed commercial photography" |
|
- "award-winning product photography" |
|
- "professional advertising imagery" |
|
- "studio quality" |
|
- "magazine advertisement quality" |
|
10. ๋ฐฐ๊ฒฝ ํ๊ฒฝ ์์๋ฅผ ์ํ ์นดํ
๊ณ ๋ฆฌ์ ๋ง๊ฒ ์ ํํฉ๋๋ค: |
|
- ์คํจ์ผ์ด ์ ํ: ๊นจ๋ํ ์์ค ์ ๋ฐ, ์ฐ์ํ ํ์ฅ๋, ์คํ ๊ฐ์ ํ๊ฒฝ ๋ฑ |
|
- ์๋ฃ ์ ํ: ์ธ๋ จ๋ ํ
์ด๋ธ, ํํฐ ํ๊ฒฝ, ์ผ์ธ ํผํฌ๋ ์ฅ๋ฉด ๋ฑ |
|
- ์ ์ ์ ํ: ์ธ๋ จ๋ ์์
๊ณต๊ฐ, ํ๋์ ์ธ ๊ฑฐ์ค, ๋ฏธ๋๋ฉํ ์ฑ
์ ๋ฑ |
|
- ํจ์
/์๋ฅ: ์ธ๋ จ๋ ์ผ๋ฃธ, ๋์ ๊ฑฐ๋ฆฌ, ์๋ ๊ฐ์คํ ๋ผ์ดํ์คํ์ผ ํ๊ฒฝ ๋ฑ |
|
- ์ํ ์ ํ: ๊น๋ํ ์ฃผ๋ฐฉ, ์ํ, ์๋ฆฌ ํ๊ฒฝ ๋ฑ |
|
11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ํํ ๋ฐ์ํฉ๋๋ค. |
|
12. ํ๋กฌํํธ๋ ๋ฏธ๋์ ๋ AI์ ์ต์ ํ๋์ด์ผ ํฉ๋๋ค. |
|
13. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค. |
|
์ถ๋ ฅ ํ์์ ์์ด๋ก ๋ ๋จ์ผ ๋จ๋ฝ์ ์์ธํ ํ๋กฌํํธ์ฌ์ผ ํ๋ฉฐ, ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค. |
|
""" |
|
|
|
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. ์ํ์ด ํฌ๊ฒ ๋ถ๊ฐ๋๊ณ ์ด๋ฏธ์ง์์ ์ค์ฌ์ ์ธ ์์น๋ฅผ ์ฐจ์งํ๋๋ก ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์. |
|
2. ์ด๋ฏธ์ง๋ ์ ํํ 1:1 ๋น์จ(์ ์ฌ๊ฐํ)์ด์ด์ผ ํฉ๋๋ค. |
|
3. ์ํ์ ์ ์ฌ๊ฐํ ํ๋ ์์ ์ ์ค์์ ์์นํด์ผ ํฉ๋๋ค. |
|
4. ์ํ์ ๋์์ธ, ์์, ํํ, ๋ก๊ณ ๋ฑ ๋ณธ์ง์ ํน์ฑ์ ์ ๋ ์์ ํ์ง ๋ง์ธ์. |
|
5. ํ๊ฒฝ๊ณผ์ ์์ฐ์ค๋ฌ์ด ํตํฉ์ ์ํ ์กฐ๋ช
ํจ๊ณผ์ ๊ทธ๋ฆผ์๋ ํฌํจํด์ฃผ์ธ์. |
|
6. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ฐฐ๊ฒฝ ํ๊ฒฝ์ ์ค๋ช
ํด์ฃผ์ธ์. |
|
7. ๊ณ ๊ธ์ค๋ฌ์ด ์์
๊ด๊ณ ํ์ง์ ์ด๋ฏธ์ง๊ฐ ๋๋๋ก ํ๊ฒฝ ์ค๋ช
์ ํด์ฃผ์ธ์. |
|
8. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์. |
|
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์์ด๋ก ์ ์ ํ ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์. |
|
""" |
|
model = genai_generative.GenerativeModel( |
|
'gemini-2.0-flash', |
|
system_instruction=generate_system_instruction() |
|
) |
|
response = model.generate_content( |
|
prompt_request, |
|
generation_config=genai_generative.types.GenerationConfig( |
|
temperature=0.7, |
|
top_p=0.95, |
|
top_k=64, |
|
max_output_tokens=1024, |
|
) |
|
) |
|
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" |
|
return response_text |
|
except Exception as e: |
|
return f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
|
|
def get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract): |
|
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": abstract, |
|
"english": ABSTRACT_BACKGROUNDS.get(abstract, "abstract background") |
|
} |
|
else: |
|
return { |
|
"category": "๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ", |
|
"name": "ํ์ดํธ ๋ฐฐ๊ฒฝ", |
|
"english": "white background" |
|
} |
|
|
|
def generate_product_prompt_output(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_info): |
|
if image is None: |
|
gr.Warning("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.") |
|
return "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ ํ ํ๋กฌํํธ๋ฅผ ์์ฑํด์ฃผ์ธ์." |
|
product_name = product_name.strip() or "์ ํ" |
|
background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract) |
|
try: |
|
prompt = generate_prompt_with_gemini(product_name, background_info, additional_info) |
|
if "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค" in prompt: |
|
gr.Warning("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ ์ค์ ํด์ฃผ์ธ์.") |
|
prompt = """ |
|
[Gemini API ํค ๋๋ฝ] |
|
API ํค ์ค์ ๋ฐฉ๋ฒ: |
|
1. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY="your-api-key" |
|
2. ์ฝ๋ ๋ด ์ง์ ์
๋ ฅ: GEMINI_API_KEY = "your-api-key" |
|
ํค ๋ฐ๊ธ: https://makersuite.google.com/ |
|
""" |
|
return prompt, image, "API ํค๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค." |
|
preview = f""" |
|
<div style="padding:10px; border:1px solid #ddd; border-radius:8px; margin-top:10px;"> |
|
<h3>ํ๋กฌํํธ ์์ฝ</h3> |
|
<p><strong>์ด ๊ธธ์ด:</strong> {len(prompt)} ๊ธ์</p> |
|
<p><strong>๋ฐฐ๊ฒฝ:</strong> {background_info['category']} > {background_info['name']}</p> |
|
<p><strong>๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ:</strong> {" ".join([param for param in ["--ar 1:1", "--s 750", "--q 2"] if param in prompt])}</p> |
|
</div> |
|
""" |
|
return prompt, image, preview |
|
except Exception as e: |
|
error_msg = f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}" |
|
gr.Error(error_msg) |
|
return error_msg, image, "์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค." |
|
|
|
|
|
|
|
def create_basic_app(): |
|
with gr.Blocks() as basic_app: |
|
with gr.Column(): |
|
with gr.Group(): |
|
with gr.Column(): |
|
with gr.Row(): |
|
image1_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋", image_mode="RGB") |
|
prompt_input = gr.Textbox( |
|
lines=3, |
|
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์. '#1'์ผ๋ก ์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.", |
|
label="ํ๋กฌํํธ ์
๋ ฅ" |
|
) |
|
with gr.Row(): |
|
submit_single_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (1์ฅ)') |
|
submit_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (4์ฅ)') |
|
with gr.Column(): |
|
with gr.Row(): |
|
with gr.Column(): |
|
output_image1 = gr.Image(label="์ด๋ฏธ์ง #1", type="filepath") |
|
output_image3 = gr.Image(label="์ด๋ฏธ์ง #3", type="filepath") |
|
with gr.Column(): |
|
output_image2 = gr.Image(label="์ด๋ฏธ์ง #2", type="filepath") |
|
output_image4 = gr.Image(label="์ด๋ฏธ์ง #4", type="filepath") |
|
output_text = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=2) |
|
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", lines=2) |
|
submit_single_btn.click( |
|
fn=lambda image1, prompt: process_images_with_prompt(image1, prompt, 0), |
|
inputs=[image1_input, prompt_input], |
|
outputs=[output_image1, output_text, prompt_display], |
|
) |
|
submit_btn.click( |
|
fn=generate_multiple_images, |
|
inputs=[image1_input, prompt_input], |
|
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display], |
|
) |
|
return basic_app |
|
|
|
def create_product_prompt_app(): |
|
with gr.Blocks(title="๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ๊ธฐ") as product_app: |
|
gr.Markdown("# ๊ณ ๊ธ ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ํ๋กฌํํธ ์์ฑ๊ธฐ") |
|
gr.Markdown("์ํ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ์ต์
์ ์ ํํ๋ฉด ๊ณ ํ์ง ์์
์ฉ ํ๋กฌํํธ๊ฐ ์์ฑ๋ฉ๋๋ค.") |
|
with gr.Row(): |
|
product_name = gr.Textbox(label="์ํ๋ช
(ํ๊ตญ์ด ์
๋ ฅ)", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ํ
๋ธ๋ฌ ๋ฑ", interactive=True) |
|
background_type = gr.Radio( |
|
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"], |
|
label="๋ฐฐ๊ฒฝ ์ ํ", |
|
value="์ฌํ ๋ฐฐ๊ฒฝ" |
|
) |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
image_input = gr.Image(label="์ํ ์ด๋ฏธ์ง ์
๋ก๋", type="pil") |
|
simple_dropdown = gr.Dropdown( |
|
choices=list(SIMPLE_BACKGROUNDS.keys()), |
|
value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else None, |
|
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=True, |
|
interactive=True |
|
) |
|
studio_dropdown = gr.Dropdown( |
|
choices=list(STUDIO_BACKGROUNDS.keys()), |
|
value=list(STUDIO_BACKGROUNDS.keys())[0] if STUDIO_BACKGROUNDS else None, |
|
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
nature_dropdown = gr.Dropdown( |
|
choices=list(NATURE_BACKGROUNDS.keys()), |
|
value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else None, |
|
label="์์ฐ ํ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
indoor_dropdown = gr.Dropdown( |
|
choices=list(INDOOR_BACKGROUNDS.keys()), |
|
value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None, |
|
label="์ค๋ด ํ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
abstract_dropdown = gr.Dropdown( |
|
choices=list(ABSTRACT_BACKGROUNDS.keys()), |
|
value=list(ABSTRACT_BACKGROUNDS.keys())[0] if ABSTRACT_BACKGROUNDS else None, |
|
label="์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ", |
|
visible=False, |
|
interactive=True |
|
) |
|
additional_info = gr.Textbox( |
|
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)", |
|
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์ ๋ฑ", |
|
lines=3, |
|
interactive=True |
|
) |
|
submit_btn = gr.Button("ํ๋กฌํํธ ์์ฑ", variant="primary") |
|
with gr.Column(scale=1): |
|
prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ", lines=10) |
|
image_preview = gr.Image(label="์
๋ก๋๋ ์ด๋ฏธ์ง (#1)", type="pil") |
|
preview_html = gr.HTML("ํ๋กฌํํธ ์์ฑ ์ ์ฌ๊ธฐ์ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๊ฐ ํ์๋ฉ๋๋ค.") |
|
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 == "์ค๋ด ํ๊ฒฝ")), |
|
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ")) |
|
} |
|
background_type.change( |
|
fn=update_dropdowns, |
|
inputs=[background_type], |
|
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown] |
|
) |
|
def generate_output(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_text): |
|
return generate_product_prompt_output(image, bg_type, simple, studio, nature, indoor, abstract, product_name, additional_text) |
|
submit_btn.click( |
|
fn=generate_output, |
|
inputs=[ |
|
image_input, |
|
background_type, |
|
simple_dropdown, |
|
studio_dropdown, |
|
nature_dropdown, |
|
indoor_dropdown, |
|
abstract_dropdown, |
|
product_name, |
|
additional_info |
|
], |
|
outputs=[ |
|
prompt_output, |
|
image_preview, |
|
preview_html |
|
] |
|
) |
|
return product_app |
|
|
|
def main(): |
|
basic_app = create_basic_app() |
|
product_app = create_product_prompt_app() |
|
with gr.Blocks() as demo: |
|
gr.Markdown("# ํตํฉ ์ด๋ฏธ์ง ์์ฑ ๋ฐ ํ๋กฌํํธ ์์ฑ ์ฑ") |
|
with gr.Tabs(): |
|
with gr.TabItem("์ผ๋ฐ ์ด๋ฏธ์ง ์์ฑ"): |
|
basic_app.render() |
|
with gr.TabItem("์ํ ํ๋กฌํํธ ์์ฑ ๋ฐ ์ด๋ฏธ์ง ์์ฑ"): |
|
product_app.render() |
|
demo.queue() |
|
demo.launch() |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|