Spaces:
Running
Running
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 | |
from db_examples import product_background_examples | |
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}") | |
# ------------------- API ํค ์ํ ์์คํ ------------------- | |
API_KEYS = [] # API ํค ๋ชฉ๋ก | |
current_key_index = 0 # ํ์ฌ ์ฌ์ฉ ์ค์ธ ํค ์ธ๋ฑ์ค | |
def initialize_api_keys(): | |
"""API ํค ๋ชฉ๋ก์ ์ด๊ธฐํํ๋ ํจ์""" | |
global API_KEYS | |
# ํ๊ฒฝ ๋ณ์์์ API ํค ๊ฐ์ ธ์ค๊ธฐ | |
key1 = os.environ.get("GEMINI_API_KEY_1", "") | |
key2 = os.environ.get("GEMINI_API_KEY_2", "") | |
key3 = os.environ.get("GEMINI_API_KEY_3", "") | |
# ๋น ๋ฌธ์์ด์ด ์๋ ํค๋ง ์ถ๊ฐ | |
if key1: | |
API_KEYS.append(key1) | |
if key2: | |
API_KEYS.append(key2) | |
if key3: | |
API_KEYS.append(key3) | |
# ๊ธฐ์กด GEMINI_API_KEY๊ฐ ์์ผ๋ฉด ์ถ๊ฐ | |
default_key = os.environ.get("GEMINI_API_KEY", "") | |
if default_key and default_key not in API_KEYS: | |
API_KEYS.append(default_key) | |
logger.info(f"API ํค {len(API_KEYS)}๊ฐ๊ฐ ๋ก๋๋์์ต๋๋ค.") | |
def get_next_api_key(): | |
"""๋ค์ API ํค๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์""" | |
global current_key_index | |
if not API_KEYS: | |
return None | |
# ํ์ฌ ํค ๊ฐ์ ธ์ค๊ธฐ | |
api_key = API_KEYS[current_key_index] | |
# ๋ค์ ํค ์ธ๋ฑ์ค๋ก ์ ๋ฐ์ดํธ | |
current_key_index = (current_key_index + 1) % len(API_KEYS) | |
return api_key | |
# ------------------- ์ ์ญ ๋ณ์ ์ค์ ------------------- | |
SIMPLE_BACKGROUNDS = {} | |
STUDIO_BACKGROUNDS = {} | |
NATURE_BACKGROUNDS = {} | |
INDOOR_BACKGROUNDS = {} | |
SPECIAL_BACKGROUNDS = {} # ํน์ ๋ฐฐ๊ฒฝ ์ถ๊ฐ | |
IMAGE_CACHE = {} # ์ด๋ฏธ์ง ์บ์ ์ถ๊ฐ | |
# ์ปค์คํ CSS ์คํ์ผ - ๊ธฐ์กด ์คํ์ผ ์ ์ง ๋ฐ ์์ ํญ์ฉ ์คํ์ผ ์ถ๊ฐ | |
custom_css = """ | |
:root { | |
--primary-color: #FB7F0D; | |
--secondary-color: #ff9a8b; | |
--accent-color: #FF6B6B; | |
--background-color: #FFF3E9; | |
--card-bg: #ffffff; | |
--text-color: #334155; | |
--border-radius: 18px; | |
--shadow: 0 8px 30px rgba(251, 127, 13, 0.08); | |
} | |
/* ์์ ๊ฐค๋ฌ๋ฆฌ ์คํ์ผ */ | |
.example-gallery { | |
display: grid; | |
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); | |
gap: 20px; | |
padding: 20px; | |
} | |
.example-item { | |
cursor: pointer; | |
border: 1px solid rgba(0, 0, 0, 0.1); | |
border-radius: var(--border-radius); | |
overflow: hidden; | |
transition: all 0.3s ease; | |
background: white; | |
} | |
.example-item:hover { | |
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); | |
transform: translateY(-2px); | |
} | |
.example-item img { | |
width: 100%; | |
height: 250px; | |
object-fit: cover; | |
} | |
.example-label { | |
padding: 10px; | |
text-align: center; | |
font-weight: bold; | |
background: rgba(251, 127, 13, 0.1); | |
} | |
.example-detail-view { | |
margin-bottom: 30px; | |
} | |
.example-params { | |
background: white; | |
padding: 20px; | |
border-radius: var(--border-radius); | |
box-shadow: var(--shadow); | |
} | |
.example-params p { | |
margin: 10px 0; | |
font-size: 16px; | |
} | |
.example-params strong { | |
color: var(--primary-color); | |
} | |
/* ์ ํ๋ ์์ ํ์ด๋ผ์ดํธ */ | |
.example-item.selected { | |
border: 3px solid var(--primary-color); | |
} | |
/* โโ ํญ ๋ด๋ถ ํจ๋ ๋ฐฐ๊ฒฝ ์ ๊ฑฐ โโ */ | |
.gr-tabs-panel { | |
background-color: var(--background-color) !important; | |
box-shadow: none !important; | |
} | |
.gr-tabs-panel::before, | |
.gr-tabs-panel::after { | |
display: none !important; | |
content: none !important; | |
} | |
/* โโ ๊ทธ๋ฃน ๋ํผ ๋ฐฐ๊ฒฝ ์์ ์ ๊ฑฐ โโ */ | |
.custom-section-group, | |
.gr-block.gr-group { | |
background-color: var(--background-color) !important; | |
box-shadow: none !important; | |
} | |
.custom-section-group::before, | |
.custom-section-group::after, | |
.gr-block.gr-group::before, | |
.gr-block.gr-group::after { | |
display: none !important; | |
content: none !important; | |
} | |
/* ๊ทธ๋ฃน ์ปจํ ์ด๋ ๋ฐฐ๊ฒฝ์ ์์ด๋ณด๋ฆฌ๋ก, ๊ทธ๋ฆผ์ ์ ๊ฑฐ */ | |
.custom-section-group { | |
background-color: var(--background-color) !important; | |
box-shadow: none !important; | |
} | |
/* ์๋จยทํ๋จ์ ๊ทธ๋ ค์ง๋ ํ์ ์บก(๋ฅ๊ทผ ๋ชจ์๋ฆฌ) ์ ๊ฑฐ */ | |
.custom-section-group::before, | |
.custom-section-group::after { | |
display: none !important; | |
content: none !important; | |
} | |
body { | |
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif; | |
background-color: var(--background-color); | |
color: var(--text-color); | |
line-height: 1.6; | |
margin: 0; | |
padding: 0; | |
} | |
.gradio-container { | |
width: 100%; /* ์ ์ฒด ๋๋น 100% ๊ณ ์ */ | |
margin: 0 auto; | |
padding: 20px; | |
background-color: var(--background-color); | |
} | |
/* ์ฝํ ์ธ ๋ฐ์ค (ํ๋ ์) ์คํ์ผ */ | |
.custom-frame { | |
background-color: var(--card-bg); | |
border: 1px solid rgba(0, 0, 0, 0.04); | |
border-radius: var(--border-radius); | |
padding: 20px; | |
margin: 10px 0; | |
box-shadow: var(--shadow); | |
} | |
/* ์น์ ๊ทธ๋ฃน ์คํ์ผ - ํ์ ๋ฐฐ๊ฒฝ ์์ ์ ๊ฑฐ */ | |
.custom-section-group { | |
margin-top: 20px; | |
padding: 0; | |
border: none; | |
border-radius: 0; | |
background-color: var(--background-color); /* ํ์ โ ์์ด๋ณด๋ฆฌ(์ ์ฒด ๋ฐฐ๊ฒฝ์) */ | |
box-shadow: none !important; /* ํน์ ๋จ์์๋ ๊ทธ๋ฆผ์๋ ๊ฐ์ด ์ ๊ฑฐ */ | |
} | |
/* ๋ฒํผ ์คํ์ผ - ๊ธ์ ํฌ๊ธฐ 18px */ | |
.custom-button { | |
border-radius: 30px !important; | |
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important; | |
color: white !important; | |
font-size: 18px !important; | |
padding: 10px 20px !important; | |
border: none; | |
box-shadow: 0 4px 8px rgba(251, 127, 13, 0.25); | |
transition: transform 0.3s ease; | |
} | |
.custom-button:hover { | |
transform: translateY(-2px); | |
box-shadow: 0 6px 12px rgba(251, 127, 13, 0.3); | |
} | |
/* ์ ๋ชฉ ์คํ์ผ (๋ชจ๋ ํญ๋ชฉ๋ช ์ด ๋์ผํ๊ฒ custom-title ํด๋์ค๋ก) */ | |
.custom-title { | |
font-size: 28px; | |
font-weight: bold; | |
margin-bottom: 10px; | |
color: var(--text-color); | |
border-bottom: 2px solid var(--primary-color); | |
padding-bottom: 5px; | |
} | |
/* ์ด๋ฏธ์ง ์ปจํ ์ด๋ */ | |
.image-container { | |
border-radius: var(--border-radius); | |
overflow: hidden; | |
border: 1px solid rgba(0, 0, 0, 0.08); | |
transition: all 0.3s ease; | |
background-color: white; | |
aspect-ratio: 1 / 1; /* ์ ์ฌ๊ฐํ ๋น์จ ๊ฐ์ */ | |
} | |
.image-container:hover { | |
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); | |
} | |
.image-container img { | |
width: 100%; | |
height: 100%; | |
object-fit: contain; /* ์ด๋ฏธ์ง ๋น์จ ์ ์งํ๋ฉด์ ์ปจํ ์ด๋์ ๋ง์ถค */ | |
} | |
/* ์ ๋ ฅ ํ๋ ์คํ์ผ */ | |
.gr-input, .gr-text-input, .gr-sample-inputs { | |
border-radius: var(--border-radius) !important; | |
border: 1px solid #dddddd !important; | |
padding: 12px !important; | |
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05) !important; | |
transition: all 0.3s ease !important; | |
} | |
.gr-input:focus, .gr-text-input:focus { | |
border-color: var(--primary-color) !important; | |
outline: none !important; | |
box-shadow: 0 0 0 2px rgba(251, 127, 13, 0.2) !important; | |
} | |
/* ๋ฉ์ธ ์ปจํ ์ธ ์คํฌ๋กค๋ฐ */ | |
::-webkit-scrollbar { | |
width: 8px; | |
height: 8px; | |
} | |
::-webkit-scrollbar-track { | |
background: rgba(0, 0, 0, 0.05); | |
border-radius: 10px; | |
} | |
::-webkit-scrollbar-thumb { | |
background: var(--primary-color); | |
border-radius: 10px; | |
} | |
/* ์ ๋๋ฉ์ด์ ์คํ์ผ */ | |
@keyframes fadeIn { | |
from { opacity: 0; transform: translateY(10px); } | |
to { opacity: 1; transform: translateY(0); } | |
} | |
.fade-in { | |
animation: fadeIn 0.5s ease-out; | |
} | |
/* ๋ฐ์ํ */ | |
@media (max-width: 768px) { | |
.button-grid { | |
grid-template-columns: repeat(2, 1fr); | |
} | |
} | |
/* ์น์ ์ ๋ชฉ ์คํ์ผ - ์ฐธ์กฐ์ฝ๋ ์คํ์ผ๊ณผ ๋์ผํ๊ฒ ์ ์ฉ */ | |
.section-title { | |
display: flex; | |
align-items: center; | |
font-size: 20px; | |
font-weight: 700; | |
color: #333333; | |
margin-bottom: 10px; | |
padding-bottom: 5px; | |
border-bottom: 2px solid #FB7F0D; | |
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif; | |
} | |
.section-title img { | |
margin-right: 10px; | |
width: 24px; | |
height: 24px; | |
} | |
""" | |
# FontAwesome ์์ด์ฝ ํฌํจ | |
fontawesome_link = """ | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" /> | |
""" | |
# ์ ๋ชฉ๊ณผ ์ฌ์ฉ ๊ฐ์ด๋ HTML ์ ๊ฑฐ | |
header_html = "" | |
guide_html = "" | |
# ------------------- ๋ฐฐ๊ฒฝ JSON ํ์ผ ๋ก๋ ํจ์ ------------------- | |
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 SPECIAL_BACKGROUNDS | |
logger.info(f"Backgrounds ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก: {BACKGROUNDS_DIR}") | |
logger.info(f"๋๋ ํ ๋ฆฌ ๋ด ํ์ผ ๋ชฉ๋ก: {os.listdir(BACKGROUNDS_DIR)}") | |
SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json") | |
STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json") | |
NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json") | |
INDOOR_BACKGROUNDS = load_background_json("indoor_backgrounds.json") | |
SPECIAL_BACKGROUNDS = load_background_json("special_backgrounds.json") | |
# ๊ธฐ๋ณธ๊ฐ ์ค์ (ํ์ผ์ด ์๊ฑฐ๋ ๋น์ด์๋ ๊ฒฝ์ฐ) | |
if not SIMPLE_BACKGROUNDS: | |
SIMPLE_BACKGROUNDS = {"ํด๋์ ํ์ดํธ": "clean white background with soft even lighting"} | |
if not STUDIO_BACKGROUNDS: | |
STUDIO_BACKGROUNDS = {"๋ฏธ๋๋ฉ ํ๋ซ๋ ์ด": "minimalist flat lay with clean white background"} | |
if not NATURE_BACKGROUNDS: | |
NATURE_BACKGROUNDS = {"์ด๋ ํด๋ณ": "tropical beach with crystal clear water"} | |
if not INDOOR_BACKGROUNDS: | |
INDOOR_BACKGROUNDS = {"๋ฏธ๋๋ฉ ์ค์นธ๋๋๋น์ ๊ฑฐ์ค": "minimalist Scandinavian living room"} | |
if not SPECIAL_BACKGROUNDS: | |
SPECIAL_BACKGROUNDS = {"๋ค์จ ๋ผ์ดํธ": "neon light background with vibrant glowing elements"} | |
logger.info("๋ชจ๋ ๋ฐฐ๊ฒฝ ์ต์ ์ด๊ธฐํ ์๋ฃ") | |
# ๋ฐฐ๊ฒฝ ๋๋กญ๋ค์ด ์ด๊ธฐํ๋ฅผ ์ํ ํจ์ | |
def 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()) | |
special_choices = list(SPECIAL_BACKGROUNDS.keys()) | |
return { | |
"simple": simple_choices, | |
"studio": studio_choices, | |
"nature": nature_choices, | |
"indoor": indoor_choices, | |
"special": special_choices, | |
} | |
# ------------------- ๊ธฐ๋ณธ ์ ํธ๋ฆฌํฐ ํจ์ ------------------- | |
def get_api_key(user_input_key=None): | |
"""API ํค๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์ (์ํ ์์คํ ์ ์ฉ)""" | |
return get_next_api_key() | |
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 = get_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 = get_api_key() | |
if not api_key: | |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์์ GEMINI_API_KEY_1, GEMINI_API_KEY_2, GEMINI_API_KEY_3 ์ค ํ๋ ์ด์์ ์ค์ ํด์ฃผ์ธ์." | |
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_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"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค." | |
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: | |
if "์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค" in status: | |
status = "์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค." | |
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, special): | |
"""์ ํ๋ ๋ฐฐ๊ฒฝ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ํจ์""" | |
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": special, | |
"english": SPECIAL_BACKGROUNDS.get(special, "special background") | |
} | |
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=""): | |
"""ํฅ์๋ ํ๋กฌํํธ ์์ฑ ํจ์""" | |
api_key = get_api_key() | |
if not api_key: | |
return "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค." | |
try: | |
genai_generative.configure(api_key=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, special, 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, special) | |
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_1=\"your-api-key-1\"\n" | |
"2. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY_2=\"your-api-key-2\"\n" | |
"3. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY_3=\"your-api-key-3\"\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 | |
# ------------------- 4์ฅ ์ด๋ฏธ์ง ์์ฑ ํจ์ ------------------- | |
def generate_product_images(image, bg_type, simple, studio, nature, indoor, special, 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, special) | |
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_1=\"your-api-key-1\"\n" | |
"2. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY_2=\"your-api-key-2\"\n" | |
"3. ํ๊ฒฝ ๋ณ์: export GEMINI_API_KEY_3=\"your-api-key-3\"\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 load_image_cached(image_path): | |
"""์ด๋ฏธ์ง๋ฅผ ์บ์ํ์ฌ ๋ก๋ํ๋ ํจ์""" | |
global IMAGE_CACHE | |
if image_path not in IMAGE_CACHE: | |
try: | |
img = Image.open(image_path) | |
# ํฐ ์ด๋ฏธ์ง๋ ๋ฏธ๋ฆฌ ๋ฆฌ์ฌ์ด์ฆํ์ฌ ์บ์ | |
if max(img.size) > 1000: | |
ratio = 1000 / max(img.size) | |
new_size = (int(img.size[0] * ratio), int(img.size[1] * ratio)) | |
img = img.resize(new_size, Image.Resampling.LANCZOS) | |
IMAGE_CACHE[image_path] = img | |
except Exception as e: | |
logger.error(f"์ด๋ฏธ์ง ๋ก๋ ์คํจ: {image_path}, ์๋ฌ: {e}") | |
return None | |
return IMAGE_CACHE[image_path] | |
def preload_example_images(): | |
"""์์ ์ด๋ฏธ์ง๋ค์ ๋ฏธ๋ฆฌ ๋ก๋ํ๋ ํจ์""" | |
for example in product_background_examples: | |
load_image_cached(example[0]) # ์ ๋ ฅ ์ด๋ฏธ์ง | |
load_image_cached(example[5]) # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง | |
def load_example(evt: gr.SelectData): | |
"""์ ํ๋ ์์์ ์ ๋ณด๋ฅผ ๋ก๋ํ๋ ํจ์""" | |
selected_example = product_background_examples[evt.index] | |
return ( | |
selected_example[0], # ์ ๋ ฅ ์ด๋ฏธ์ง | |
selected_example[1], # ๋ฐฐ๊ฒฝ ์ ํ | |
selected_example[2], # ๋ฐฐ๊ฒฝ ์ ํ | |
selected_example[3], # ์ํ๋ช | |
selected_example[4], # ์ถ๊ฐ ์์ฒญ์ฌํญ | |
selected_example[5] # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง | |
) | |
# ------------------- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ ------------------- | |
def create_app(): | |
dropdown_options = initialize_dropdowns() | |
with gr.Blocks(css=custom_css, theme=gr.themes.Default( | |
primary_hue="orange", | |
secondary_hue="orange", | |
font=[gr.themes.GoogleFont("Noto Sans KR"), "ui-sans-serif", "system-ui"] | |
)) as demo: | |
gr.HTML(fontawesome_link) | |
# ์ ๋ชฉ๊ณผ ์ฌ์ฉ ๊ฐ์ด๋ ์ ๊ฑฐ | |
# gr.HTML(header_html) | |
# gr.HTML(guide_html) | |
with gr.Tabs(): | |
# ์ฒซ ๋ฒ์งธ ํญ: ์ด๋ฏธ์ง ์์ฑ | |
with gr.TabItem("์ด๋ฏธ์ง ์์ฑ"): | |
with gr.Row(): | |
with gr.Column(scale=1): | |
# API ํค ์ ๋ ฅ ์น์ ์ ๊ฑฐ | |
# ์ด๋ฏธ์ง ์ ๋ก๋ ๋ฐ ์ค์ ์น์ | |
with gr.Column(elem_classes="custom-frame"): | |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/3097/3097412.png"> ์ด๋ฏธ์ง ์ ๋ก๋ ๋ฐ ์ค์ </div>') | |
product_name = gr.Textbox( | |
label="์ํ๋ช (ํ๊ตญ์ด ์ ๋ ฅ)", | |
placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ์ค๋งํธ์์น, ํฅ์, ์ด๋ํ ๋ฑ", | |
interactive=True | |
) | |
image_input = gr.Image( | |
label="์ํ ์ด๋ฏธ์ง ์ ๋ก๋", | |
type="pil", | |
elem_classes="image-container" | |
) | |
# ๋ฐฐ๊ฒฝ ์ต์ ์น์ | |
with gr.Column(elem_classes="custom-frame"): | |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/4297/4297825.png"> ๋ฐฐ๊ฒฝ ์ต์ </div>') | |
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 | |
) | |
special_dropdown = gr.Dropdown( | |
choices=dropdown_options["special"], | |
value=dropdown_options["special"][0] if dropdown_options["special"] 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 == "์ค๋ด ํ๊ฒฝ")), | |
special_dropdown: gr.update(visible=(bg_type == "ํน์๋ฐฐ๊ฒฝ")) | |
} | |
background_type.change( | |
fn=update_dropdowns, | |
inputs=[background_type], | |
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, special_dropdown] | |
) | |
# ์ด๋ฏธ์ง ์์ฑ ์น์ | |
with gr.Column(elem_classes="custom-frame"): | |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/1022/1022293.png"> ์ด๋ฏธ์ง ์์ฑ</div>') | |
with gr.Row(): | |
single_btn = gr.Button("ํ๋กฌํํธ ๋ฐ ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ", elem_classes="custom-button") | |
multi_btn = gr.Button("ํ๋กฌํํธ ๋ฐ 4์ฅ ์ด๋ฏธ์ง ์์ฑ", elem_classes="custom-button") | |
with gr.Column(scale=1): | |
# ์์ฑ๋ ์ด๋ฏธ์ง ์น์ | |
with gr.Column(elem_classes="custom-frame"): | |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/1375/1375106.png"> ์์ฑ๋ ์ด๋ฏธ์ง</div>') | |
with gr.Row(): | |
with gr.Column(): | |
image_output1 = gr.Image(label="์ด๋ฏธ์ง #1", elem_classes="image-container", height=400, width=400) | |
image_output3 = gr.Image(label="์ด๋ฏธ์ง #3", elem_classes="image-container", height=400, width=400) | |
with gr.Column(): | |
image_output2 = gr.Image(label="์ด๋ฏธ์ง #2", elem_classes="image-container", height=400, width=400) | |
image_output4 = gr.Image(label="์ด๋ฏธ์ง #4", elem_classes="image-container", height=400, width=400) | |
# ๊ฒฐ๊ณผ ์ ๋ณด ์น์ | |
with gr.Column(elem_classes="custom-frame"): | |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/3153/3153376.png"> ๊ฒฐ๊ณผ ์ ๋ณด</div>') | |
status_output = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=3) | |
# ๋ ๋ฒ์งธ ํญ: ์์ ๊ฒฐ๊ณผ ๋ณด๊ธฐ | |
with gr.TabItem("์์ ๊ฒฐ๊ณผ ๋ณด๊ธฐ"): | |
# ์ํ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์์ ๊ฐค๋ฌ๋ฆฌ ์น์ | |
with gr.Column(elem_classes="custom-frame"): | |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/681/681443.png"> ์ํ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์์ ๊ฐค๋ฌ๋ฆฌ</div>') | |
# ์๋จ ๋ฉ์ธ ๋ทฐ ์์ญ | |
with gr.Row(): | |
example_input_image = gr.Image( | |
label="์ ๋ ฅ ์ด๋ฏธ์ง", | |
height=400, | |
width=400, | |
elem_classes="image-container", | |
show_label=True, | |
show_download_button=True, | |
container=True, | |
scale=1 | |
) | |
with gr.Column(elem_classes="example-params"): | |
example_bg_type = gr.Textbox(label="๋ฐฐ๊ฒฝ ์ ํ", interactive=False) | |
example_bg_option = gr.Textbox(label="๋ฐฐ๊ฒฝ ์ ํ", interactive=False) | |
example_product_name = gr.Textbox(label="์ํ๋ช ", interactive=False) | |
example_additional_info = gr.Textbox(label="์ถ๊ฐ ์์ฒญ์ฌํญ", interactive=False) | |
example_output_image = gr.Image( | |
label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง", | |
height=400, | |
width=400, | |
elem_classes="image-container", | |
show_label=True, | |
show_download_button=True, | |
container=True, | |
scale=1 | |
) | |
# ๋ฐฐ๊ฒฝ ์ ํ๋ณ ๊ฐค๋ฌ๋ฆฌ ์น์ | |
with gr.Column(elem_classes="custom-frame"): | |
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/3068/3068327.png"> ๋ฐฐ๊ฒฝ ์ ํ๋ณ ์์</div>') | |
# ์์๋ค์ ๋ฐฐ๊ฒฝ ์ ํ๋ณ๋ก ๊ทธ๋ฃนํ | |
examples_by_type = {} | |
for example in product_background_examples: | |
bg_type = example[1] # ๋ฐฐ๊ฒฝ ์ ํ | |
if bg_type not in examples_by_type: | |
examples_by_type[bg_type] = [] | |
examples_by_type[bg_type].append(example) | |
# ๋ฐฐ๊ฒฝ ์ ํ๋ณ ๊ฐค๋ฌ๋ฆฌ ์น์ ์์ฑ | |
for bg_type, examples in examples_by_type.items(): | |
gr.Markdown(f"### {bg_type}") | |
# 10๊ฐ์ฉ ํ ์ค๋ก ํ์ | |
for i in range(0, len(examples), 10): | |
with gr.Row(): | |
for j in range(10): | |
if i + j < len(examples): | |
example = examples[i + j] | |
with gr.Column(scale=1, min_width=100): | |
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ง ํ์ | |
display_img = gr.Image( | |
value=example[5], | |
label=None, # ๋ ์ด๋ธ ์ ๊ฑฐ | |
elem_classes="example-item", | |
height=120, # ํฌ๊ธฐ ์ถ์ | |
width=120, # ํฌ๊ธฐ ์ถ์ | |
show_label=False, # ๋ ์ด๋ธ ์จ๊ธฐ๊ธฐ | |
show_download_button=False, # ๋ค์ด๋ก๋ ๋ฒํผ ์จ๊ธฐ๊ธฐ | |
show_share_button=False, # ๊ณต์ ๋ฒํผ ์จ๊ธฐ๊ธฐ | |
container=False # ์ปจํ ์ด๋ ํ ๋๋ฆฌ ์ ๊ฑฐ | |
) | |
def make_example_handler(ex): | |
def handler(): | |
# ๋ก๋ฉ ์ํ ํ์๋ฅผ ์ํ ์ค๊ฐ ์ ๋ฐ์ดํธ | |
yield ( | |
gr.update(value=None), # ์์๋ก ์ด๋ฏธ์ง ๋น์ฐ๊ธฐ | |
gr.update(value="๋ก๋ฉ ์ค..."), | |
gr.update(value="๋ก๋ฉ ์ค..."), | |
gr.update(value="๋ก๋ฉ ์ค..."), | |
gr.update(value="๋ก๋ฉ ์ค..."), | |
gr.update(value=None) | |
) | |
# ์ค์ ๋ฐ์ดํฐ ๋ก๋ | |
yield ( | |
ex[0], # ์ ๋ ฅ ์ด๋ฏธ์ง | |
ex[1], # ๋ฐฐ๊ฒฝ ์ ํ | |
ex[2], # ๋ฐฐ๊ฒฝ ์ ํ | |
ex[3], # ์ํ๋ช | |
ex[4] if ex[4] else "(์์)", # ์ถ๊ฐ ์์ฒญ์ฌํญ | |
ex[5] # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง | |
) | |
return handler | |
display_img.select( | |
fn=make_example_handler(example), | |
outputs=[ | |
example_input_image, | |
example_bg_type, | |
example_bg_option, | |
example_product_name, | |
example_additional_info, | |
example_output_image | |
], | |
queue=True # ์์ฒญ์ ํ์ ๋ฃ์ด ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌ | |
) | |
else: | |
# ๋น ๊ณต๊ฐ ์ ์ง | |
with gr.Column(scale=1, min_width=100): | |
pass | |
# ํ์ด์ง ๋ก๋ ์ ์ฒซ ๋ฒ์งธ ์์ ์๋ ํ์ | |
def load_first_example(): | |
if product_background_examples: | |
first_example = product_background_examples[0] | |
return ( | |
first_example[0], # ์ ๋ ฅ ์ด๋ฏธ์ง | |
first_example[1], # ๋ฐฐ๊ฒฝ ์ ํ | |
first_example[2], # ๋ฐฐ๊ฒฝ ์ ํ | |
first_example[3], # ์ํ๋ช | |
first_example[4] if first_example[4] else "(์์)", # ์ถ๊ฐ ์์ฒญ์ฌํญ | |
first_example[5] # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง | |
) | |
return None, "", "", "", "", None | |
demo.load( | |
fn=load_first_example, | |
outputs=[ | |
example_input_image, | |
example_bg_type, | |
example_bg_option, | |
example_product_name, | |
example_additional_info, | |
example_output_image | |
] | |
) | |
# ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ ํจ์ (API ํค ํ๋ผ๋ฏธํฐ ์ ๊ฑฐ) | |
def modified_single_image_gen(image, bg_type, simple, studio, nature, indoor, special, product_name, additional_info): | |
actual_api_key = get_api_key() | |
if actual_api_key: | |
os.environ["GEMINI_API_KEY"] = actual_api_key # ํ๊ฒฝ ๋ณ์ ์ ๋ฐ์ดํธ | |
else: | |
return None, None, None, None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์์ GEMINI_API_KEY_1, GEMINI_API_KEY_2, GEMINI_API_KEY_3 ์ค ํ๋ ์ด์์ ์ค์ ํด์ฃผ์ธ์." | |
result_img, status, _ = generate_product_image(image, bg_type, simple, studio, nature, indoor, special, product_name, additional_info) | |
return result_img, None, None, None, status | |
# ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ | |
single_btn.click( | |
fn=modified_single_image_gen, | |
inputs=[image_input, background_type, simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, special_dropdown, product_name, additional_info], | |
outputs=[image_output1, image_output2, image_output3, image_output4, status_output] | |
) | |
# 4์ฅ ์ด๋ฏธ์ง ์์ฑ ํจ์ (API ํค ํ๋ผ๋ฏธํฐ ์ ๊ฑฐ) | |
def modified_multi_image_gen(image, bg_type, simple, studio, nature, indoor, special, product_name, additional_info): | |
actual_api_key = get_api_key() | |
if actual_api_key: | |
os.environ["GEMINI_API_KEY"] = actual_api_key # ํ๊ฒฝ ๋ณ์ ์ ๋ฐ์ดํธ | |
else: | |
return None, None, None, None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋ณ์์ GEMINI_API_KEY_1, GEMINI_API_KEY_2, GEMINI_API_KEY_3 ์ค ํ๋ ์ด์์ ์ค์ ํด์ฃผ์ธ์." | |
img1, img2, img3, img4, combined_status, _ = generate_product_images(image, bg_type, simple, studio, nature, indoor, special, product_name, additional_info) | |
# ์ํ ์ ๋ณด์์ ํ๋กฌํํธ ์ ๋ณด ์ ๊ฑฐ | |
cleaned_statuses = [] | |
for status_line in combined_status.split('\n'): | |
if "์ด๋ฏธ์ง #" in status_line: | |
if "์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค" in status_line: | |
cleaned_statuses.append(status_line.split(":")[0] + ": ์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค.") | |
else: | |
cleaned_statuses.append(status_line) | |
cleaned_combined_status = "\n".join(cleaned_statuses) | |
return img1, img2, img3, img4, cleaned_combined_status | |
# 4์ฅ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ | |
multi_btn.click( | |
fn=modified_multi_image_gen, | |
inputs=[image_input, background_type, simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, special_dropdown, product_name, additional_info], | |
outputs=[image_output1, image_output2, image_output3, image_output4, status_output] | |
) | |
return demo | |
# ------------------- ๋ฉ์ธ ์คํ ํจ์ ------------------- | |
if __name__ == "__main__": | |
initialize_backgrounds() | |
initialize_api_keys() # API ํค ์ด๊ธฐํ ํจ์ ํธ์ถ ์ถ๊ฐ | |
preload_example_images() # ์์ ์ด๋ฏธ์ง ๋ฏธ๋ฆฌ ๋ก๋ | |
app = create_app() | |
app.queue(max_size=10) # ์์ฒญ์ ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌํ๋๋ก ํ ์ค์ | |
app.launch(share=False, inbrowser=True, width="100%") |