File size: 30,518 Bytes
16a6799 8d852cf ac7fd78 a7e1681 98fdfb4 8d852cf 16a6799 ddcfe75 8d852cf 1d396b2 e0e8bcb 1d396b2 77a62b5 8d852cf ddcfe75 98fdfb4 853fc8a ac7fd78 4718cfc ac7fd78 4718cfc 98fdfb4 4718cfc 98fdfb4 4718cfc 98fdfb4 4718cfc 98fdfb4 4718cfc 98fdfb4 4718cfc 98fdfb4 4718cfc 98fdfb4 853fc8a 98fdfb4 853fc8a 98fdfb4 d58b205 14ca9a3 ac7fd78 98fdfb4 efacf1e 98fdfb4 efacf1e d53a3c8 4718cfc 98fdfb4 d53a3c8 98fdfb4 d58b205 98fdfb4 af7449f 98fdfb4 d58b205 98fdfb4 d53a3c8 4718cfc 98fdfb4 d53a3c8 98fdfb4 d53a3c8 4718cfc d53a3c8 4718cfc d53a3c8 4718cfc d53a3c8 4718cfc d53a3c8 4718cfc ddcfe75 6989f5c 3e624fe 98fdfb4 6989f5c 3e624fe 6989f5c 98fdfb4 6989f5c 98fdfb4 6989f5c 98fdfb4 6989f5c 447a9c2 6989f5c 447a9c2 6989f5c 4718cfc 98fdfb4 6989f5c 98fdfb4 6989f5c 98fdfb4 af7449f 98fdfb4 3e624fe 16a6799 98fdfb4 e7c539f ccab539 e7c539f 16a6799 e7c539f 16a6799 33bb4a3 16a6799 e7c539f 16a6799 4d6058d 06c7417 16a6799 06c7417 16a6799 06c7417 4d6058d 16a6799 4d6058d 16a6799 4d6058d 16a6799 853fc8a 76f0c08 16a6799 |
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 |
import os
import sys
from torchvision.transforms import functional
sys.modules["torchvision.transforms.functional_tensor"] = functional
from basicsr.archs.srvgg_arch import SRVGGNetCompact
from gfpgan.utils import GFPGANer
from realesrgan.utils import RealESRGANer
import torch
import cv2
import gradio as gr
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋
def download_upscaler_models():
if not os.path.exists('realesr-general-x4v3.pth'):
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
if not os.path.exists('GFPGANv1.4.pth'):
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
# ์
์ค์ผ์ผ๋ฌ ๋ชจ๋ธ ์ด๊ธฐํ
def init_upscaler_model():
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
model_path = 'realesr-general-x4v3.pth'
half = True if torch.cuda.is_available() else False
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
return upsampler
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ง ํจ์
def upscale_image(img):
try:
upsampler = init_upscaler_model()
# ์ด๋ฏธ์ง ํ์ผ ์ฝ๊ธฐ
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
if len(img.shape) == 3 and img.shape[2] == 4:
img_mode = 'RGBA'
elif len(img.shape) == 2:
img_mode = None
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
else:
img_mode = None
# ์์ ์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์
h, w = img.shape[0:2]
if h < 300:
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
# ๊ณ ์ ๋ GFPGANv1.4 ๋ชจ๋ธ ์ฌ์ฉ
face_enhancer = GFPGANer(
model_path='GFPGANv1.4.pth',
upscale=2,
arch='clean',
channel_multiplier=2,
bg_upsampler=upsampler
)
# ์ด๋ฏธ์ง ํฅ์
try:
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
except RuntimeError as error:
print('์ค๋ฅ', error)
return None
# RGB๋ก ๋ณํํ์ฌ ๋ฐํ
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
return output
except Exception as error:
print('๊ธ๋ก๋ฒ ์์ธ', error)
return None
# ์
์ค์ผ์ผ๋ฌ ํญ UI ์์ฑ ํจ์
def create_upscaler_tab():
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋
download_upscaler_models()
with gr.Tab("์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ"):
gr.Markdown(
"""
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ ๊ณ ํด์๋๋ก ๋ณํํฉ๋๋ค. ํนํ ์ธ๋ฌผ ์ฌ์ง์ ์ผ๊ตด ๋ํ
์ผ์ ํฅ์์ํต๋๋ค.
"""
)
with gr.Row():
with gr.Column():
input_image = gr.Image(type="filepath", label="์
๋ ฅ ์ด๋ฏธ์ง")
upscale_btn = gr.Button("์ด๋ฏธ์ง ์
์ค์ผ์ผ", variant="primary")
with gr.Column():
output_image = gr.Image(type="numpy", label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
upscale_btn.click(
fn=upscale_image,
inputs=[input_image],
outputs=[output_image]
)
import os
import tempfile
from PIL import Image
import gradio as gr
import logging
import re
from io import BytesIO
import time
# ์
์ค์ผ์ผ๋ฌ ๋ชจ๋ ์ํฌํธ
import sys
from torchvision.transforms import functional
sys.modules["torchvision.transforms.functional_tensor"] = functional
from basicsr.archs.srvgg_arch import SRVGGNetCompact
from gfpgan.utils import GFPGANer
from realesrgan.utils import RealESRGANer
import torch
import cv2
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 ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์์ด๋ก ๋ฒ์ญํฉ๋๋ค.
ํ๊ธ์ด ์์ผ๋ฉด ์๋ณธ ํ๋กฌํํธ๋ฅผ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.
์ค์: #1, #2, #3 ํ๊ทธ๋ ๋ฒ์ญ ์ ํ์ ๋ฐ๋์ ๋ณด์กด๋์ด์ผ ํฉ๋๋ค.
"""
if not re.search("[๊ฐ-ํฃ]", prompt):
return prompt
# #1, #2, #3 ํ๊ทธ๋ฅผ ์์ ํ ํฐ์ผ๋ก ๋์ฒดํ์ฌ ๋ณด์กด
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
prompt = prompt.replace("#2", "IMAGE_TAG_TWO")
prompt = prompt.replace("#3", "IMAGE_TAG_THREE")
try:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
# ์์ ํ ํฐ์ ์๋ ํ๊ทธ๋ก ๋ณต์
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
return prompt
client = genai.Client(api_key=api_key)
translation_prompt = f"""
Translate the following Korean text to English:
{prompt}
IMPORTANT: The tokens IMAGE_TAG_ONE, IMAGE_TAG_TWO, and IMAGE_TAG_THREE are special tags
and must be preserved exactly as is in your translation. Do not translate these tokens.
"""
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")
translated_text = translated_text.replace("IMAGE_TAG_TWO", "#2")
translated_text = translated_text.replace("IMAGE_TAG_THREE", "#3")
logger.info(f"Translated text: {translated_text.strip()}")
return translated_text.strip()
else:
logger.warning("๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ์๋ณธ ํ๋กฌํํธ ์ฌ์ฉ")
# ์์ ํ ํฐ์ ์๋ ํ๊ทธ๋ก ๋ณต์
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
return prompt
except Exception as e:
logger.exception("๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์:")
# ์์ ํ ํฐ์ ์๋ ํ๊ทธ๋ก ๋ณต์
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
return prompt
def preprocess_prompt(prompt, image1, image2, image3):
"""
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
"""
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:
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ฃผ์ ํผ์ฌ์ฒด๋ ์ ์งํ๊ณ , ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ํฉ์ฑํด์ฃผ์ธ์."
elif "6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)" in prompt:
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง(๋๋ ์ธ ๋ฒ์งธ ์ด๋ฏธ์ง)๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ๋๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํด์ฃผ์ธ์."
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
return prompt
def generate_with_images(prompt, images, variation_index=0):
"""
API ํธ์ถ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ณ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
variation_index๋ก ๋ค์ํ ๋ณํ๋ฅผ ์ค๋๋ค.
"""
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=".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, variation_index=0, max_retries=3):
"""
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ์ฌ ์ต์ข
์์ด ํ๋กฌํํธ(final_prompt)๋ฅผ ์์ฑํ ํ,
API๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค. ์๋ฌ ๋ฐ์ ์ ์ต๋ max_retries ํ์๋งํผ ์ฌ์๋ํฉ๋๋ค.
"""
retry_count = 0
last_error = None
while retry_count < max_retries:
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)
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. Do not include any text or watermarks in the generated image."
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. Do not include any text or watermarks in the generated 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. Do not include any text or watermarks in the generated image."
logger.info("Default prompt generated for three images")
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, image2, image3, 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, image2, image3, 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
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ ๊ธฐ๋ฅ
def download_upscaler_models():
if not os.path.exists('realesr-general-x4v3.pth'):
os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
if not os.path.exists('GFPGANv1.4.pth'):
os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
def upscale_image(img):
try:
# ๋ชจ๋ธ ์ด๊ธฐํ
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
model_path = 'realesr-general-x4v3.pth'
half = True if torch.cuda.is_available() else False
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
# ์ด๋ฏธ์ง ํ์ผ ์ฝ๊ธฐ
img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
if len(img.shape) == 3 and img.shape[2] == 4:
img_mode = 'RGBA'
elif len(img.shape) == 2:
img_mode = None
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
else:
img_mode = None
# ์์ ์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์
h, w = img.shape[0:2]
if h < 300:
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
# GFPGANv1.4 ๋ชจ๋ธ ์ฌ์ฉ
face_enhancer = GFPGANer(
model_path='GFPGANv1.4.pth',
upscale=2,
arch='clean',
channel_multiplier=2,
bg_upsampler=upsampler
)
# ์ด๋ฏธ์ง ํฅ์
try:
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
except RuntimeError as error:
print('์ค๋ฅ', error)
return None
# RGB๋ก ๋ณํํ์ฌ ๋ฐํ
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
return output
except Exception as error:
print('๊ธ๋ก๋ฒ ์์ธ', error)
return None
# Gradio ์ธํฐํ์ด์ค
with gr.Blocks() as demo:
gr.HTML(
"""
<div style="text-align: center; margin-bottom: 1rem;">
<h1>์ด์ปค๋จธ์ค์ฉ ์ด๋ฏธ์ง ์์ฑ ๋ฐ ํธ์ง ๋๊ตฌ</h1>
<p>์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ์์ ๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ๋กฌํํธ๋ฅผ ์์ ํ๊ฑฐ๋ ์ด๋ฏธ์ง ์
์ค์ผ์ผ ๊ธฐ๋ฅ์ ์ด์ฉํ์ธ์.</p>
</div>
"""
)
with gr.Tabs():
# ์ฒซ ๋ฒ์งธ ํญ: ์ด๋ฏธ์ง ์์ฑ๊ธฐ
with gr.Tab("์ด๋ฏธ์ง ์์ฑ๊ธฐ"):
with gr.Row():
# ์ผ์ชฝ ์
๋ ฅ ์์ญ (์์ ์ด๋ฏธ์ง๋ฅผ ํฌํจํ์ฌ ์ฌ์ฉ ๋ฐฉ๋ฒ ์๋์ ๋ฐฐ์น)
with gr.Column(scale=1):
with gr.Row():
image1_input = gr.Image(type="pil", label="#1", image_mode="RGB", height=300, width=200)
image2_input = gr.Image(type="pil", label="#2", image_mode="RGB", height=300, width=200)
image3_input = gr.Image(type="pil", label="#3", image_mode="RGB", height=300, width=200)
prompt_input = gr.Textbox(
lines=3,
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค.",
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
)
with gr.Row():
image_change_btn1 = gr.Button("์ด๋ฏธ์ง ๋ณ๊ฒฝ-1")
image_change_btn2 = gr.Button("์ด๋ฏธ์ง ๋ณ๊ฒฝ-2")
text_remove_btn = gr.Button("๊ธ์ ์ง์ฐ๊ธฐ")
text_change_btn = gr.Button("๊ธ์ ๋ณ๊ฒฝํ๊ธฐ")
clothes_change_btn1 = gr.Button("๊ฐ์ ์ํ์ฐฉ์ฉ-1")
clothes_change_btn2 = gr.Button("๊ฐ์ ์ํ์ฐฉ์ฉ-2")
holding_product_btn = gr.Button("์ํ๋ค๊ณ ์๊ธฐ")
background_change_btn = gr.Button("๋ฐฐ๊ฒฝ ๋ฐ๊พธ๊ธฐ")
composite_product_btn = gr.Button("๋ถ๋ถ ์ง์ฐ๊ธฐ")
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ (4์ฅ)", variant="primary")
gr.Markdown(
"""
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค.
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
3. **์ ํ ์ต์
**: ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ๋กฌํํธ ์
๋ ฅ๋์ ํ๊ตญ์ด ๋ฌธ๊ตฌ๋ก ์
๋ ฅํ์๋ฉด ๋ฉ๋๋ค.
4. **๋ค์ํ ์ด๋ฏธ์ง**: "์ด๋ฏธ์ง ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฐจ๋ก๋ก 4์ฅ์ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.
5. **์์ ์ ํ**: ๋ค์ํ ์์ ๋ฅผ ํตํด ๋ฏธ๋ฆฌ ํ
์คํธํด๋ณด์ธ์.
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
"""
)
# ์์ ์ด๋ฏธ์ง: ์ฌ์ฉ ๋ฐฉ๋ฒ ์๋ ์ผ์ชฝ ์์ญ์ ๋ฐฐ์น
gr.Markdown("## ์์ ์ด๋ฏธ์ง")
examples = [
["down/๋ชจ๋ธ.jpg", None, None, "(#1์ ์ฌ์ฑ)์ด ์ด์ง ๋ค๋ก ๋์๋ณด๋ ๋ชจ์ต์ผ๋ก ์ต๋ํ ์ด์ seed๋ฅผ ์ ์งํํ
์์ฐ์ค๋ฝ๊ฒ ๋ณ๊ฒฝํ๋ผ."],
["down/์์ด๋ ๊ณ ๋ชจํ.png", None, None, "(#1 ๋ ๋ชจ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ง ๊ฒ์์ ๊ณ ๋๋ ๊ณ ๋ก ๋ณ๊ฒฝํ๊ณ ๋๋จธ์ง ๋ถ๋ถ์ seed๋ฅผ ๋ณ๊ฒฝ์ ํ์ง๋ง๋ผ."],
["down/์ค๊ตญ์ด.png", None, None, "(#1 ์ด๋ฏธ์ง)์ ์๋ ์ค๊ตญ์ด๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๋ผ."],
["down/ํ
์คํธ.webp", None, None, '(#1์ ํ
์คํธ)๋ฅผ ์คํ์ผ์ ์ ์งํ์ฒด ํ
์คํธ๋ง "Hello"๋ก ๋ฐ๊ฟ๋ผ'],
["down/๋ชจ๋ธ.jpg", "down/์ ๊ธ๋ผ์ค.png", "down/์ฒญ๋ฐ์ง.png", "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)์ (#3์ ์ฒญ๋ฐ์ง)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ๊ฒ ์ฒ๋ผ ์์ฐ์ค๋ฝ๊ฒ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ."],
["down/๋ชจ๋ธ.jpg", "down/์ ๊ธ๋ผ์ค.png", "down/์นดํ์ ๊ฒฝ.png", "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ ๊ฒ์ฒ๋ผ (#3์ ์ฅ์)์์ ์์์ ์์ ์๋ ์์ฐ์ค๋ฌ์ด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ."],
["down/๋ชจ๋ธ.jpg", "down/์์ธ์.png", None, "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์์ธ์)์ ์ฌ์ฑ๋ชจ๋ธ์ด ํ๋ณดํ ์์ธ์์ ๋๋ณด์ด๊ฒ ๋ค๊ณ ์๋ ์์ฐ์ค๋ฌ์ด ๋ชจ์ต์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ."],
["down/๋ชจ๋ธ.jpg", "down/์นดํ์ ๊ฒฝ.png", None, "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด (#2 ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ)์ ์ฃผ์ ํผ์ฌ์ฒด๋ ๊ทธ๋๋ก ์ ์งํ์ฌ ์ด๋ฏธ์ง์ ๋ถ์๊ธฐ๊ฐ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์์ฑํ๋ผ."],
["down/์์ด๋ ๊ณ ๋ชจํ.png", None, None, "(#1์ ๋ ๊ณ ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ฅผ ์ ๊ฑฐํ ํ, ๊ทธ ์๋ฆฌ๋ฅผ ์ฃผ๋ณ ๋ฐฐ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์ฑ์์ฃผ์ธ์. ๋จ, ์ด๋ฏธ์ง์ ๋ค๋ฅธ ๋ถ๋ถ์ ์ฃผ์ ์์๋ ๋์ผํ๊ฒ ์ ์งํด ํด์ผํ๋ค."]
]
gr.Examples(
examples=examples,
inputs=[image1_input, image2_input, image3_input, prompt_input],
elem_id="examples-grid"
)
# ์ค๋ฅธ์ชฝ ์ถ๋ ฅ ์์ญ
with gr.Column(scale=1):
with gr.Row():
with gr.Column():
output_image1 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #1", height=600, width=450)
output_image3 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #3", height=600, width=450)
with gr.Column():
output_image2 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #2", height=600, width=450)
output_image4 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #4", height=600, width=450)
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง", lines=4)
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", visible=True, lines=4)
# ์ ํ ์ต์
๋ฒํผ ํด๋ฆญ ์ ํ๋กฌํํธ ์
๋ ฅ๋ ์
๋ฐ์ดํธ (ํ๊ตญ์ด ๋ฌธ๊ตฌ)
image_change_btn1.click(
fn=lambda: "(#1์ ์ฌ์ฑ)์ด ์ด์ง ๋ค๋ก ๋์๋ณด๋ ๋ชจ์ต์ผ๋ก ์ต๋ํ ์ด์ seed๋ฅผ ์ ์งํํ
์์ฐ์ค๋ฝ๊ฒ ๋ณ๊ฒฝํ๋ผ.",
inputs=[],
outputs=prompt_input
)
image_change_btn2.click(
fn=lambda: "(#1 ๋ ๋ชจ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ง ๊ฒ์์ ๊ณ ๋๋ ๊ณ ๋ก ๋ณ๊ฒฝํ๊ณ ๋๋จธ์ง ๋ถ๋ถ์ seed๋ฅผ ๋ณ๊ฒฝ์ ํ์ง๋ง๋ผ.",
inputs=[],
outputs=prompt_input
)
text_remove_btn.click(
fn=lambda: "(#1 ์ด๋ฏธ์ง)์ ์๋ ์ค๊ตญ์ด๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๋ผ.",
inputs=[],
outputs=prompt_input
)
text_change_btn.click(
fn=lambda: "(#1์ ํ
์คํธ)๋ฅผ ์คํ์ผ์ ์ ์งํ์ฒด ํ
์คํธ๋ง \"Hello\"๋ก ๋ฐ๊ฟ๋ผ",
inputs=[],
outputs=prompt_input
)
clothes_change_btn1.click(
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๊ฒ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)์ (#3์ ์ฒญ๋ฐ์ง)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ๊ฒ ์ฒ๋ผ ์์ฐ์ค๋ฝ๊ฒ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ.",
inputs=[],
outputs=prompt_input
)
clothes_change_btn2.click(
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์ ๊ธ๋ผ์ค)๋ฅผ ์ง์ ๋ชจ๋ธ์ด ์ฐฉ์ฉํ ๊ฒ์ฒ๋ผ (#3์ ์ฅ์)์์ ์์์ ์์ ์๋ ์์ฐ์ค๋ฌ์ด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ.",
inputs=[],
outputs=prompt_input
)
holding_product_btn.click(
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์นํ ์ฒด (#2์ ์์ธ์)์ ์ฌ์ฑ๋ชจ๋ธ์ด ํ๋ณดํ ์์ธ์์ ๋๋ณด์ด๊ฒ ๋ค๊ณ ์๋ ์์ฐ์ค๋ฌ์ด ๋ชจ์ต์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ผ.",
inputs=[],
outputs=prompt_input
)
background_change_btn.click(
fn=lambda: "(#1์ ์ฌ์ฑ๋ชจ๋ธ)์ด (#2 ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ)์ ์ฃผ์ ํผ์ฌ์ฒด๋ ๊ทธ๋๋ก ์ ์งํ์ฌ ๋ ์ด๋ฏธ์ง์ ๋ถ์๊ธฐ๊ฐ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์์ฑํ๋ผ.",
inputs=[],
outputs=prompt_input
)
composite_product_btn.click(
fn=lambda: "(#1์ ๋ ๊ณ ๋ชจํ)์์ ์ฒญ์์์ด๋ ๊ณ ๋ฅผ ์ ๊ฑฐํ ํ, ๊ทธ ์๋ฆฌ๋ฅผ ์ฃผ๋ณ ๋ฐฐ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์ฑ์์ฃผ์ธ์. ๋จ, ์ด๋ฏธ์ง์ ๋ค๋ฅธ ๋ถ๋ถ์ ์ฃผ์ ์์๋ ๋์ผํ๊ฒ ์ ์งํด ํด์ผํ๋ค.",
inputs=[],
outputs=prompt_input
)
submit_btn.click(
fn=generate_multiple_images,
inputs=[image1_input, image2_input, image3_input, prompt_input],
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
)
# ๋ ๋ฒ์งธ ํญ: ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
with gr.Tab("์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ"):
# ํ์ํ ๋ชจ๋ธ ๋ค์ด๋ก๋ ํธ์ถ
download_upscaler_models()
gr.Markdown(
"""
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ๋ฌ
์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ ๊ณ ํด์๋๋ก ๋ณํํฉ๋๋ค. ํนํ ์ธ๋ฌผ ์ฌ์ง์ ์ผ๊ตด ๋ํ
์ผ์ ํฅ์์ํต๋๋ค.
"""
)
with gr.Row():
with gr.Column():
input_upscale_image = gr.Image(type="filepath", label="์
๋ ฅ ์ด๋ฏธ์ง")
upscale_btn = gr.Button("์ด๋ฏธ์ง ์
์ค์ผ์ผ", variant="primary")
with gr.Column():
output_upscale_image = gr.Image(type="numpy", label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
upscale_btn.click(
fn=upscale_image,
inputs=[input_upscale_image],
outputs=[output_upscale_image]
)
if __name__ == "__main__":
demo.launch(share=True) |