File size: 32,850 Bytes
395173a 42d38fe fd41e09 ad645a2 f9d2a46 ad645a2 42d38fe 649e6f6 42d38fe 649e6f6 ad645a2 42d38fe 395173a da0375c f7b8c2a da0375c a6f73ef da0375c a6f73ef da0375c a6f73ef da0375c 45ee188 da0375c 8750c78 da0375c aedf707 da0375c 6a47ab4 da0375c a6f73ef da0375c a6f73ef da0375c a6f73ef cfded4c da0375c a6f73ef da0375c aedf707 7b8b65a 45ee188 7b8b65a 7e555af aedf707 7b8b65a a6f73ef 7bb926b fd41e09 f9d2a46 ad645a2 f9d2a46 7bb926b f9d2a46 7bb926b f9d2a46 ad645a2 f9d2a46 ad645a2 f9d2a46 ad645a2 f9d2a46 395173a f9d2a46 395173a ad645a2 a6f73ef 42d38fe ad645a2 42d38fe ad645a2 42d38fe da0375c 42d38fe da0375c 42d38fe da0375c 42d38fe da0375c f9d2a46 42d38fe f9d2a46 da0375c ad645a2 f9d2a46 ad645a2 aedf707 f9d2a46 da0375c 42d38fe f9d2a46 42d38fe f9d2a46 42d38fe ad645a2 42d38fe 649e6f6 42d38fe 649e6f6 42d38fe b32830a 42d38fe a6f73ef da0375c af9a6de da0375c ad645a2 7e555af da0375c f9d2a46 da0375c f9d2a46 45ee188 f9d2a46 da0375c aedf707 da0375c aedf707 da0375c aedf707 da0375c e52aef4 a9320c9 f7b8c2a a9320c9 f7b8c2a a9320c9 56b9002 a9320c9 f7b8c2a ad645a2 e8f6871 ad645a2 f7b8c2a b32830a 56b9002 ad645a2 e8f6871 e52aef4 e8f6871 a9320c9 e8f6871 aedf707 e8f6871 ad645a2 b32830a 7b8b65a a9320c9 aedf707 56b9002 a9320c9 47a38fd 56b9002 47a38fd aedf707 ad645a2 da0375c dcc09a6 b32830a 45ee188 b32830a ad645a2 7b8b65a ad645a2 7b8b65a ad645a2 7b8b65a ad645a2 7b8b65a ad645a2 da0375c 7b8b65a da0375c 7b8b65a da0375c ad645a2 7b8b65a ad645a2 501b534 45ee188 ad645a2 b32830a ad645a2 aedf707 b32830a da0375c 7e555af 47a38fd b32830a 7e555af b32830a dcc09a6 a9320c9 dcc09a6 ad645a2 aedf707 dcc09a6 a9320c9 dcc09a6 aedf707 dcc09a6 a9320c9 dcc09a6 aedf707 a9320c9 dcc09a6 aedf707 a9320c9 aedf707 ad645a2 dcc09a6 b32830a ad645a2 da0375c ad645a2 da0375c b32830a aedf707 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
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 = {} # ์ฅฌ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ์ญ ๋ณ์ ์ถ๊ฐ
# ------------------- ๋ฐฐ๊ฒฝ 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 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
# ------------------- 4์ฅ ์ด๋ฏธ์ง ์์ฑ ํจ์ -------------------
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
# ------------------- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ -------------------
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():
# API ํค ์
๋ ฅ ํ๋๋ง ํ์
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):
# ํ๋กฌํํธ ์ถ๋ ฅ UI ์ ๊ฑฐ, ์ด๋ฏธ์ง 4์ฅ๋ง ์ถ๋ ฅ
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)
# ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํจ์ (API ํค ์
๋ ฅ๊ฐ ํ์ฉ)
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]
)
# 4์ฅ ์ด๋ฏธ์ง ์์ฑ์ ์ํ ํจ์ (API ํค ์
๋ ฅ๊ฐ ํ์ฉ)
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()
|