File size: 26,138 Bytes
8d852cf ac7fd78 a7e1681 8d852cf ddcfe75 8d852cf 1d396b2 e0e8bcb 1d396b2 77a62b5 8d852cf ddcfe75 a7ea647 ceef5c6 ac7fd78 4718cfc ac7fd78 4718cfc a7ea647 4718cfc a7ea647 4718cfc a7ea647 4718cfc a7ea647 4718cfc a7ea647 4718cfc a7ea647 4718cfc a7ea647 03b3471 ceef5c6 03b3471 ceef5c6 03b3471 a7ea647 8ebbfcd a7ea647 4718cfc 14ca9a3 ac7fd78 d53a3c8 efacf1e a7ea647 efacf1e d53a3c8 4718cfc a7ea647 d53a3c8 1d396b2 a7ea647 d53a3c8 4718cfc a7ea647 d53a3c8 a7ea647 d53a3c8 4718cfc d53a3c8 4718cfc d53a3c8 4718cfc d53a3c8 4718cfc d53a3c8 4718cfc ddcfe75 ceef5c6 3e624fe a7ea647 3e624fe ba3536d 451a0ee 4718cfc a7ea647 ceef5c6 a7ea647 4718cfc a7ea647 0fee2a2 ba3536d a7ea647 0fee2a2 3e624fe a7ea647 0fee2a2 a7ea647 3e624fe 4718cfc a7ea647 4718cfc ceef5c6 a7ea647 ceef5c6 03b3471 ceef5c6 03b3471 ceef5c6 03b3471 ceef5c6 a7ea647 ceef5c6 03b3471 ceef5c6 03b3471 ceef5c6 03b3471 3e624fe ceef5c6 d366591 ba3536d ceef5c6 ba3536d 03b3471 ba3536d ceef5c6 d366591 ceef5c6 ba3536d d366591 ba3536d 03b3471 d366591 ceef5c6 8ebbfcd 03b3471 ceef5c6 03b3471 ceef5c6 d366591 03b3471 ceef5c6 d366591 ceef5c6 d366591 03b3471 ceef5c6 d366591 03b3471 d366591 03b3471 ceef5c6 d366591 03b3471 d366591 0365d81 d366591 03b3471 ceef5c6 d366591 03b3471 d366591 03b3471 d366591 03b3471 ceef5c6 d366591 03b3471 d366591 d1db013 d366591 03b3471 ceef5c6 03b3471 d366591 ceef5c6 03b3471 d366591 ceef5c6 d366591 23db719 ceef5c6 d366591 5ff6346 03b3471 d366591 23db719 ac7fd78 d366591 03b3471 |
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 |
import os
import tempfile
from PIL import Image
import gradio as gr
import logging
import re
from io import BytesIO
from google import genai
from google.genai import types
# ํ๊ฒฝ๋ณ์ ๋ก๋
from dotenv import load_dotenv
load_dotenv()
# ๋ก๊น
์ค์
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def save_binary_file(file_name, data):
with open(file_name, "wb") as f:
f.write(data)
def translate_prompt_to_english(prompt):
"""
์
๋ ฅ๋ ํ๋กฌํํธ์ ํ๊ธ์ด ํฌํจ๋์ด ์์ผ๋ฉด Geminiโ2.0โflash ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์์ด๋ก ๋ฒ์ญํฉ๋๋ค.
ํ๊ธ์ด ์์ผ๋ฉด ์๋ณธ ํ๋กฌํํธ๋ฅผ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.
"""
if not re.search("[๊ฐ-ํฃ]", prompt):
return prompt
try:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
return prompt
client = genai.Client(api_key=api_key)
translation_prompt = f"Translate the following Korean text to English:\n\n{prompt}"
logger.info(f"Translation prompt: {translation_prompt}")
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=[translation_prompt],
config=types.GenerateContentConfig(
response_modalities=['Text'],
temperature=0.2,
top_p=0.95,
top_k=40,
max_output_tokens=512
)
)
translated_text = ""
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
translated_text += part.text
if translated_text.strip():
logger.info(f"Translated text: {translated_text.strip()}")
return translated_text.strip()
else:
logger.warning("๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ์๋ณธ ํ๋กฌํํธ ์ฌ์ฉ")
return prompt
except Exception as e:
logger.exception("๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์:")
return prompt
def preprocess_prompt(prompt, image1, image2, image3, bg_options=None):
"""
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
"""
has_img1 = image1 is not None
has_img2 = image2 is not None
has_img3 = image3 is not None
if "#1" in prompt and not has_img1:
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
else:
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
if "#2" in prompt and not has_img2:
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
else:
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง")
if "#3" in prompt and not has_img3:
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
else:
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง")
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
desc_match = re.search(r'#1์ "(.*?)"์ผ๋ก ๋ฐ๊ฟ๋ผ', prompt)
if desc_match:
description = desc_match.group(1)
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ {description}์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์๋ณธ ์ด๋ฏธ์ง์ ์ฃผ์ ๋ด์ฉ์ ์ ์งํ๋ ์๋ก์ด ์คํ์ผ๊ณผ ๋ถ์๊ธฐ๋ก ์ฌํด์ํด์ฃผ์ธ์."
else:
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
text_match = re.search(r'#1์์ "(.*?)"๋ฅผ ์ง์๋ผ', prompt)
if text_match:
text_to_remove = text_match.group(1)
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ '{text_to_remove}' ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ํ
์คํธ๊ฐ ์๋ ๋ถ๋ถ์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ์ฑ์์ฃผ์ธ์."
else:
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
elif "4. ์ท๋ฐ๊พธ๊ธฐ" in prompt:
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์์์ ์คํ์ผ๊ณผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
elif "5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ" in prompt or "๋ฐฐ๊ฒฝ ๋ณ๊ฒฝ" in prompt:
# ๋ฐฐ๊ฒฝ ์ต์
์ด ์์ผ๋ฉด ๊ทธ๊ฒ์ ๊ธฐ๋ฐ์ผ๋ก ํ๋กฌํํธ ์์ฑ
if bg_options and all([bg_options.get("product_name"), bg_options.get("background_type"), bg_options.get("background_style")]):
product_name = bg_options.get("product_name", "")
product_category = bg_options.get("product_category", "์ผ๋ฐ ์ํ")
background_type = bg_options.get("background_type", "์์ฐ ๋ฐฐ๊ฒฝ")
background_style = bg_options.get("background_style", "์ฒ")
lighting = bg_options.get("lighting", "์์ฐ๊ด")
texture = bg_options.get("texture", "")
color_tone = bg_options.get("color_tone", "")
mood = bg_options.get("mood", "")
# ์์ธ ํ๋กฌํํธ ์์ฑ
prompt = f"{product_name} ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ {background_type} ์ค {background_style}์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ณ๊ฒฝํด์ฃผ์ธ์. "
# ์ถ๊ฐ ์ธ๋ถ ์ฌํญ ํฌํจ
if lighting:
prompt += f"{lighting} ์กฐ๋ช
์ ์ฌ์ฉํ๊ณ , "
if texture:
prompt += f"{texture} ์ง๊ฐ์ ํํํ๋ฉฐ, "
if color_tone:
prompt += f"{color_tone} : ์ ์ฒด ์๊ฐ์ผ๋ก ์ฒ๋ฆฌํ๊ณ , "
if mood:
prompt += f"{mood}ํ ๋ถ์๊ธฐ๋ก "
prompt += f"์ฐ์ถํด์ฃผ์ธ์. {product_category} ์ํ์ ์์, ์ง๊ฐ, ํํ๋ ์๋ฒฝํ๊ฒ ๋ณด์กดํ๊ณ ๊ณ ํ์ง ์ํ ์ฌ์ง ์คํ์ผ๋ก ์ ์ํด์ฃผ์ธ์."
else:
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ฃผ์ ํผ์ฌ์ฒด๋ ์ ์งํ๊ณ , ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ํฉ์ฑํด์ฃผ์ธ์."
elif "์ํํฉ์ฑ" in prompt:
prompt = "#1 ์ํ ์ด๋ฏธ์ง์ #2 ์ด๋ฏธ์ง ๋๋ #3 ์ด๋ฏธ์ง๋ฅผ [๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ๋๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํ์ฌ]์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํ๋ผ."
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
return prompt
def generate_with_images(prompt, images):
"""
API ํธ์ถ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ณ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
"""
try:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
client = genai.Client(api_key=api_key)
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
contents = [prompt]
for idx, img in enumerate(images, 1):
if img is not None:
contents.append(img)
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
response = client.models.generate_content(
model="gemini-2.0-flash-exp-image-generation",
contents=contents,
config=types.GenerateContentConfig(
response_modalities=['Text', 'Image'],
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192
)
)
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
temp_path = tmp.name
result_text = ""
image_found = False
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
result_text += part.text
logger.info(f"์๋ต ํ
์คํธ: {part.text}")
elif hasattr(part, 'inline_data') and part.inline_data:
save_binary_file(temp_path, part.inline_data.data)
image_found = True
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
if not image_found:
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
result_img = Image.open(temp_path)
if result_img.mode == "RGBA":
result_img = result_img.convert("RGB")
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
except Exception as e:
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
def process_images_with_prompt(image1, image2, image3, prompt, bg_options=None):
"""
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ์ฌ ์ต์ข
์์ด ํ๋กฌํํธ(final_prompt)๋ฅผ ์์ฑํ ํ,
API๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
"""
try:
images = [image1, image2, image3]
valid_images = [img for img in images if img is not None]
if not valid_images:
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
if prompt and prompt.strip():
processed_prompt = preprocess_prompt(prompt, image1, image2, image3, bg_options)
if re.search("[๊ฐ-ํฃ]", processed_prompt):
final_prompt = translate_prompt_to_english(processed_prompt)
else:
final_prompt = processed_prompt
else:
if len(valid_images) == 1:
final_prompt = "Please creatively transform this image into a more vivid and artistic version."
logger.info("Default prompt generated for single image")
elif len(valid_images) == 2:
final_prompt = "Please seamlessly composite these two images, integrating their key elements harmoniously into a single image."
logger.info("Default prompt generated for two images")
else:
final_prompt = "Please creatively composite these three images, combining their main elements into a cohesive and natural scene."
logger.info("Default prompt generated for three images")
result_img, status = generate_with_images(final_prompt, valid_images)
return result_img, status, final_prompt
except Exception as e:
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
def process_and_show_prompt(image1, image2, image3, prompt, product_name, product_category,
background_type, background_style, lighting, texture, color_tone, mood):
try:
# ๋ฐฐ๊ฒฝ ์ต์
์ ๋์
๋๋ฆฌ๋ก ๊ตฌ์ฑ
bg_options = {
"product_name": product_name,
"product_category": product_category,
"background_type": background_type,
"background_style": background_style,
"lighting": lighting,
"texture": texture,
"color_tone": color_tone,
"mood": mood
}
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt, bg_options)
# ์ ํ๋ ์ต์
์ ๋ฌธ์์ด๋ก ๊ตฌ์ฑ
options_summary = f"์ํ๋ช
: {product_name}\n"
options_summary += f"์ ํ๊ตฐ: {product_category}\n"
options_summary += f"๋ฐฐ๊ฒฝ ์ ํ: {background_type}\n"
options_summary += f"์ธ๋ถ ์คํ์ผ: {background_style}\n"
if lighting:
options_summary += f"์กฐ๋ช
: {lighting}\n"
if texture:
options_summary += f"๋ฐฐ๊ฒฝ ์ง๊ฐ: {texture}\n"
if color_tone:
options_summary += f"์๊ฐ: {color_tone}\n"
if mood:
options_summary += f"๋ถ์๊ธฐ: {mood}\n"
# ํ๊ตญ์ด์ ์์ด ํ๋กฌํํธ ๋น๊ต๋ฅผ ์ํด ์๋ณธ ํ๋กฌํํธ ํ์ธ
kr_prompt = preprocess_prompt(prompt, image1, image2, image3, bg_options)
return result_img, status, final_prompt, options_summary, kr_prompt
except Exception as e:
logger.exception("์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt, "", ""
# ์ ํ ์ต์
ํ์/์จ๊น ์ฒ๋ฆฌ ํจ์
def toggle_background_options(selection):
if selection == "๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ":
return (gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True))
else:
return (gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False))
# ๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ฅธ ์ธ๋ถ ์คํ์ผ ์ต์
์
๋ฐ์ดํธ
def update_background_style_options(background_type):
if background_type == "์์ฐ ๋ฐฐ๊ฒฝ":
return gr.update(choices=["์ฒ", "ํด๋ณ", "์ฐ", "๋คํ", "๊ณ๊ณก", "์ด๋ ์ ์", "์ฌ๋ง", "๋ ํ๊ฒฝ"], value="์ฒ")
elif background_type == "์ค๋ด ๋ฐฐ๊ฒฝ":
return gr.update(choices=["๊ฑฐ์ค", "์ฃผ๋ฐฉ", "์์ฌ", "ํ์ฅ๋", "ํธํ
๋ก๋น", "์นดํ", "๋ฏธ๋๋ฉ ๊ณต๊ฐ"], value="๊ฑฐ์ค")
elif background_type == "ํน์ ๋ฐฐ๊ฒฝ":
return gr.update(choices=["๋์ ํ๊ฒฝ", "๋ฃจํํ", "ํด์๊ฐ", "๋ชจ๋ ์คํ๋์ค", "๋นํฐ์ง ๊ณต๊ฐ", "๋ฏธ๋์ด์ฒ ๊ณต๊ฐ"], value="๋์ ํ๊ฒฝ")
else:
return gr.update(choices=["์ ํํ์ธ์"], value="์ ํํ์ธ์")
# ์ ํ ๋ด์ฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์
๋ฐ์ดํธ ํจ์
def update_preview(product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood):
preview = f"์ํ๋ช
: {product_name}\n"
preview += f"์ ํ๊ตฐ: {product_category}\n"
preview += f"๋ฐฐ๊ฒฝ ์ ํ: {background_type}\n"
preview += f"์ธ๋ถ ์คํ์ผ: {background_style}\n"
if lighting:
preview += f"์กฐ๋ช
: {lighting}\n"
if texture:
preview += f"๋ฐฐ๊ฒฝ ์ง๊ฐ: {texture}\n"
if color_tone:
preview += f"์๊ฐ: {color_tone}\n"
if mood:
preview += f"๋ถ์๊ธฐ: {mood}\n"
return preview
# Gradio ์ธํฐํ์ด์ค
with gr.Blocks() as demo:
gr.HTML(
"""
<div style="text-align: center; margin-bottom: 1rem;">
<h1>์ด์ปค๋จธ์ค ์ํ ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ ์์ฑ๊ธฐ</h1>
<p>์ํ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ์ํ๋ ๋ฐฐ๊ฒฝ ์ต์
์ ์ ํํ์ฌ ์๋ก์ด ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ธ์.</p>
</div>
"""
)
current_selection = gr.State("")
with gr.Row():
with gr.Column(scale=1):
with gr.Row():
image1_input = gr.Image(type="pil", label="#1 ์ํ ์ด๋ฏธ์ง", image_mode="RGB")
image2_input = gr.Image(type="pil", label="#2 (์ ํ์ฌํญ)", image_mode="RGB")
image3_input = gr.Image(type="pil", label="#3 (์ ํ์ฌํญ)", image_mode="RGB")
prompt_input = gr.Textbox(
lines=3,
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค.",
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
)
with gr.Row():
image_change_btn = gr.Button("์ด๋ฏธ์ง ๋ณ๊ฒฝ")
text_remove_btn = gr.Button("๊ธ์์ง์ฐ๊ธฐ")
clothes_change_btn = gr.Button("์ท๋ฐ๊พธ๊ธฐ")
background_change_btn = gr.Button("๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ", variant="primary")
composite_product_btn = gr.Button("์ํํฉ์ฑ")
# ๋ฐฐ๊ฒฝ ๋ณ๊ฒฝ ๊ด๋ จ ์น์
(๊ธฐ๋ณธ์ ์ผ๋ก ์จ๊น ์ํ)
with gr.Row(visible=False) as background_header:
gr.HTML("<h3 style='margin-top: 20px; margin-bottom: 10px; width: 100%; text-align: center;'>๋ฐฐ๊ฒฝ ๋ณ๊ฒฝ ์ต์
</h3>")
# ์ํ๋ช
์
๋ ฅ
product_name = gr.Textbox(
label="์ํ๋ช
์
๋ ฅ",
placeholder="์ํ๋ช
์ ์
๋ ฅํด์ฃผ์ธ์",
visible=False
)
# ์ ํ๊ตฐ ์นดํ
๊ณ ๋ฆฌ ๋๋กญ๋ค์ด
product_category = gr.Dropdown(
choices=["ํ์ฅํ", "๊ฐ๋ฐฉ", "์ก์ธ์๋ฆฌ", "๋ณด์๋ฅ", "ํ ๋ฐ์ฝ", "์ ๋ฐ", "์๋ฅ", "์ฃผ๋ฐฉ์ฉํ", "์ ์๊ธฐ๊ธฐ", "๊ธฐํ"],
label="์ ํ๊ตฐ ์ ํ",
value="๊ธฐํ",
visible=False
)
# ๋ฐฐ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
background_type = gr.Dropdown(
choices=["์์ฐ ๋ฐฐ๊ฒฝ", "์ค๋ด ๋ฐฐ๊ฒฝ", "ํน์ ๋ฐฐ๊ฒฝ"],
label="๋ฐฐ๊ฒฝ ์ ํ ์ ํ",
value="์์ฐ ๋ฐฐ๊ฒฝ",
visible=False
)
# ๋ฐฐ๊ฒฝ ์ธ๋ถ ์คํ์ผ ๋๋กญ๋ค์ด (๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ผ ๋์ ๋ณ๊ฒฝ)
background_style = gr.Dropdown(
choices=["์ฒ", "ํด๋ณ", "์ฐ", "๋คํ", "๊ณ๊ณก", "์ด๋ ์ ์", "์ฌ๋ง", "๋ ํ๊ฒฝ"],
label="๋ฐฐ๊ฒฝ ์ธ๋ถ ์คํ์ผ ์ ํ",
value="์ฒ",
visible=False
)
# ์ถ๊ฐ ์ ํ ์ต์
ํค๋
with gr.Row(visible=False) as additional_header:
gr.HTML("<h4 style='margin-top: 15px; margin-bottom: 10px; width: 100%; text-align: center;'>์ถ๊ฐ ์ ํ ์ต์
</h4>")
# ์กฐ๋ช
๋๋กญ๋ค์ด
lighting = gr.Dropdown(
choices=["์์ฐ๊ด", "๋ฐ๋ปํ ์กฐ๋ช
", "๊ทธ๋ฆผ์ ์๋ ์กฐ๋ช
", "๋ฏธ๋๋ฉ ์กฐ๋ช
"],
label="์กฐ๋ช
์ ํ",
value="์์ฐ๊ด",
visible=False
)
# ๋ฐฐ๊ฒฝ ์ง๊ฐ ๋๋กญ๋ค์ด
texture = gr.Dropdown(
choices=["ํฐ ๋ฒฝ", "๋๋ฆฌ์", "๋๋ฌด", "๋ฒฝ๋", "ํฐ ์ฒ", "์๋ฉํธ"],
label="๋ฐฐ๊ฒฝ ์ง๊ฐ ์ ํ",
value="",
visible=False
)
# ์๊ฐ ๋๋กญ๋ค์ด
color_tone = gr.Dropdown(
choices=["ํ์ดํธ ํค", "๋ธ๋ฃจ ํค", "๋ฐ๋ปํ ์์", "๋ชจ๋
ธํค", "ํ์คํ
"],
label="์๊ฐ ์ ํ",
value="",
visible=False
)
# ๋ถ์๊ธฐ ๋๋กญ๋ค์ด
mood = gr.Dropdown(
choices=["ํฌ๊ทผํ", "์ฌํํ", "๋ชจ๋ํ", "๋ด์ถ๋ด", "๋ฏธ๋๋ฉ"],
label="๋ถ์๊ธฐ ์ ํ",
value="",
visible=False
)
# ์ ํ ๋ด์ฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
selection_preview = gr.Textbox(
label="์ ํ ๋ด์ฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ",
lines=8,
readonly=True,
visible=False
)
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ", variant="primary")
with gr.Column(scale=1):
output_image = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง")
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", visible=True)
kr_prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (ํ๊ตญ์ด)", visible=True)
options_summary = gr.Textbox(label="์ ํ๋ ์ต์
์์ฝ", lines=8, visible=True)
# ์ ํ ์ต์
๋ฒํผ ํด๋ฆญ ์ ํ๋กฌํํธ ์
๋ ฅ๋ ์
๋ฐ์ดํธ (ํ๊ตญ์ด ๋ฌธ๊ตฌ)
image_change_btn.click(
fn=lambda: "#1 ์ด๋ฏธ์ง์ [๋ค๋ฅธ ๋ชจ์ต]์ผ๋ก ๋ณ๊ฒฝํ๋ผ.",
inputs=[],
outputs=prompt_input
).then(
fn=lambda: "์ด๋ฏธ์ง ๋ณ๊ฒฝ",
inputs=[],
outputs=current_selection
).then(
fn=toggle_background_options,
inputs=[current_selection],
outputs=[background_header, product_name, product_category, background_type,
background_style, additional_header, lighting, texture, color_tone,
mood, selection_preview]
)
text_remove_btn.click(
fn=lambda: "#1 ์ด๋ฏธ์ง์ [์ค๊ตญ์ด๋ฅผ ๋ชจ๋]๋ฅผ ์ ๊ฑฐํ๋ผ.",
inputs=[],
outputs=prompt_input
).then(
fn=lambda: "๊ธ์์ง์ฐ๊ธฐ",
inputs=[],
outputs=current_selection
).then(
fn=toggle_background_options,
inputs=[current_selection],
outputs=[background_header, product_name, product_category, background_type,
background_style, additional_header, lighting, texture, color_tone,
mood, selection_preview]
)
clothes_change_btn.click(
fn=lambda: "#1์ด๋ฏธ์ง์์ [์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์งํ ์ฒด] ์์[์คํ์ผ๊ณผ ์์์]#2, #3์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์.",
inputs=[],
outputs=prompt_input
).then(
fn=lambda: "์ท๋ฐ๊พธ๊ธฐ",
inputs=[],
outputs=current_selection
).then(
fn=toggle_background_options,
inputs=[current_selection],
outputs=[background_header, product_name, product_category, background_type,
background_style, additional_header, lighting, texture, color_tone,
mood, selection_preview]
)
background_change_btn.click(
fn=lambda: "๋ฐฐ๊ฒฝ ๋ณ๊ฒฝ",
inputs=[],
outputs=prompt_input
).then(
fn=lambda: "๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ",
inputs=[],
outputs=current_selection
).then(
fn=toggle_background_options,
inputs=[current_selection],
outputs=[background_header, product_name, product_category, background_type,
background_style, additional_header, lighting, texture, color_tone,
mood, selection_preview]
)
composite_product_btn.click(
fn=lambda: "์ด๋ฏธ์ง #1์ ๋ชจ๋ธ์ด ํ์์ ๋ฐ๋ผ ์์ธ๋ ํ์ ์ ์ ๋์ ์ผ๋ก ๋ณํํ ํ, ์ด๋ฏธ์ง #2(๋๋ #3)์ ์ํ์ ์ต๋ํ ์๋ ํํ์ ๋ํ
์ผ์ ์ ์งํ๋ฉด์ ์์ฐ์ค๋ฝ๊ฒ ์ฐฉ์ฉํ ๋ชจ์ต์ ํฉ์ฑํ๋ผ.",
inputs=[],
outputs=prompt_input
).then(
fn=lambda: "์ํํฉ์ฑ",
inputs=[],
outputs=current_selection
).then(
fn=toggle_background_options,
inputs=[current_selection],
outputs=[background_header, product_name, product_category, background_type,
background_style, additional_header, lighting, texture, color_tone,
mood, selection_preview]
)
# ๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ผ ์ธ๋ถ ์คํ์ผ ์ต์
์
๋ฐ์ดํธ
background_type.change(
fn=update_background_style_options,
inputs=[background_type],
outputs=[background_style]
)
# ์ ํ ๋ด์ฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์
๋ฐ์ดํธ (๋ชจ๋ ์ ํ ์ปดํฌ๋ํธ์ ๋ณ๊ฒฝ ์ด๋ฒคํธ์ ์ฐ๊ฒฐ)
product_name.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
product_category.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
background_type.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
background_style.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
lighting.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
texture.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
color_tone.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
mood.change(
fn=update_preview,
inputs=[product_name, product_category, background_type, background_style, lighting, texture, color_tone, mood],
outputs=[selection_preview]
)
# ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
submit_btn.click(
fn=process_and_show_prompt,
inputs=[image1_input, image2_input, image3_input, prompt_input,
product_name, product_category, background_type, background_style,
lighting, texture, color_tone, mood],
outputs=[output_image, output_text, prompt_display, options_summary, kr_prompt_display],
)
gr.Markdown(
"""
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค.
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
3. **์ ํ ์ต์
**: ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ๋กฌํํธ ์
๋ ฅ๋์ ํ๊ตญ์ด ๋ฌธ๊ตฌ๋ก ์
๋ ฅํ์๋ฉด ๋ฉ๋๋ค.
4. **๋ฐฐ๊ฒฝ ๋ณ๊ฒฝ**: ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ถ๊ฐ ์ต์
์ด ๋ํ๋ฉ๋๋ค.
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
"""
)
if __name__ == "__main__":
demo.launch(share=True) |