File size: 32,157 Bytes
265c64d 186470f 265c64d |
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 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 |
# app.py
import gradio as gr
import pillow_heif
import spaces
import torch
from PIL import Image, ImageEnhance, ImageFilter # ImageFilter ์ถ๊ฐ
from refiners.fluxion.utils import manual_seed, no_grad
from utils import load_ic_light, resize_modulo_8
import zipfile
from io import BytesIO
import tempfile
import cv2 # OpenCV ์ถ๊ฐ
import numpy as np # NumPy ์ถ๊ฐ
# HEIF/AVIF ์ด๋ฏธ์ง ์ง์ ๋ฑ๋ก
pillow_heif.register_heif_opener()
pillow_heif.register_avif_opener()
# GPU ์ค์
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DTYPE = torch.float16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else torch.float32
ic_light = load_ic_light(device=DEVICE, dtype=DTYPE)
# ๋ชจ๋ธ์ ์ง์ ๋ ๋๋ฐ์ด์ค๋ก ์ด๋
ic_light.to(device=DEVICE, dtype=DTYPE)
ic_light.device = DEVICE
ic_light.dtype = DTYPE
ic_light.solver = ic_light.solver.to(device=DEVICE, dtype=DTYPE)
# ๋ฒ์ญ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ์ค์
import os
from openai import OpenAI
import uuid
import time
import logging
import random
from datetime import datetime
# ๋ก๊น
์ค์
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(message)s'
)
logger = logging.getLogger(__name__)
# API ํค ์ค์
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
# API ํด๋ผ์ด์ธํธ ์ค์
openai_client = OpenAI(api_key=OPENAI_API_KEY)
def validate_input(text: str) -> bool:
"""์
๋ ฅ ํ
์คํธ ์ ํจ์ฑ ๊ฒ์ฌ"""
if not text.strip():
return False
if not any('\u3131' <= char <= '\u318F' or '\uAC00' <= char <= '\uD7A3' for char in text):
return False
return True
def translate_to_english(input_text: str):
"""ํ๊ตญ์ด๋ฅผ ์์ด๋ก ๋ฒ์ญํ๋ ํจ์ (GPT-4-mini ์ฌ์ฉ)"""
logger.info("GPT-4-mini ๋ฒ์ญ ์์")
if not validate_input(input_text):
logger.info("์ ํจํ์ง ์์ ์
๋ ฅ์
๋๋ค.")
return "์ ํจํ์ง ์์ ์
๋ ฅ์
๋๋ค. ํ๊ธ์ ํฌํจํ ํ
์คํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
try:
# ํ์ฌ ์๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ๋๋ค ์๋ ์ค์
current_time = int(time.time() * 1000)
random.seed(current_time)
# ์จ๋(temperature)์ top_p ๊ฐ์ ๋งค๋ฒ ๋๋คํ๊ฒ ์ค์
temperature = random.uniform(0.4, 0.85)
top_p = random.uniform(0.9, 0.98)
request_id = str(uuid.uuid4())[:8]
timestamp_micro = int(time.time() * 1000000) % 1000
system_msg = f"REQ-{request_id}-{timestamp_micro}"
# ์๊ฐ์ ๋ฐ๋ผ ๋ณํํ๋ ํ๋กฌํํธ ์์์ด ์ถ๊ฐ
current_hour = datetime.now().hour
time_context = f"Consider the current time is {current_hour}:00. "
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": system_msg
},
{
"role": "user",
"content": f"""
1. Translate the input Korean text into English with these rules:
2. Output ONLY the English translation.
3. Create a coherent, brief scene using provided words/phrases.
4. Stay strictly within the scope of input words.
5. Maintain flow and natural transitions.
6. Keep scene descriptions concise but complete.
7. Do not add external elements.
8. Focus on core scene elements.
9. Preserve original meaning and intent.
10. No explanations or meta-commentary.
11. No formatting beyond basic text.
12. Just the direct English translation.
Additional guidance:
13. Use input words harmoniously.
14. Create clear mental imagery.
15. Keep scene contained and focused.
16. Ensure logical connections.
17. Maintain narrative flow.
Input: {input_text} [Seed: {current_time}]
"""
}
],
max_tokens=100,
temperature=temperature,
top_p=top_p,
seed=current_time
)
translated = response.choices[0].message.content.strip()
logger.info("GPT-4-mini ๋ฒ์ญ ์๋ฃ")
return translated
except Exception as e:
logger.error(f"๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
return f"๋ฒ์ญ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
@spaces.GPU(duration=120) # GPU ์ฌ์ฉ์ ๋ช
์ํ๋ ๋ฐ์ฝ๋ ์ดํฐ
@no_grad()
def generate_images(
image: Image.Image,
prompt: str,
) -> tuple[Image.Image, Image.Image, Image.Image, Image.Image]:
assert image.mode == "RGBA", "์
๋ ฅ ์ด๋ฏธ์ง๋ RGBA ๋ชจ๋์ฌ์ผ ํฉ๋๋ค."
# ์๋ ๋๋ค ์ค์
seed = torch.seed() # ๋๋ค ์๋ ์์ฑ
manual_seed(seed)
# ๋ค๊ฑฐํฐ๋ธ ํ๋กฌํํธ ๊ณ ์
negative_prompt = "dirty, messy, worst quality, low quality, watermark, signature, jpeg artifacts, deformed, monochrome, black and white"
# ๊ธฐ๋ณธ๊ฐ ์ค์
condition_scale = 1.25
num_inference_steps = 25
strength_first_pass = 0.9
strength_second_pass = 0.5
# ์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์
image = resize_modulo_8(image, 768)
# RGB์ ์ํ ์ฑ๋ ๋ถ๋ฆฌ
mask = image.getchannel("A")
image_rgb = image.convert("RGB")
# ํ
์คํธ ์๋ฒ ๋ฉ ๊ณ์ฐ
clip_text_embedding = ic_light.compute_clip_text_embedding(text=prompt, negative_text=negative_prompt)
ic_light.set_ic_light_condition(image=image_rgb, mask=mask)
# ์กฐ๋ช
์ ํธ๋ ๊ธฐ๋ณธ๊ฐ ์ค์
light_pref_image = None # "None"์ผ๋ก ๊ณ ์
if light_pref_image is None:
x = torch.randn_like(ic_light._ic_light_condition) # pyright: ignore[reportPrivateUsage]
strength_first_pass = 1.0
else:
x = ic_light.lda.image_to_latents(light_pref_image)
x = ic_light.solver.add_noise(x, noise=torch.randn_like(x), step=0)
# ์ฒซ ๋ฒ์งธ ํจ์ค ์ค์
num_steps = int(round(num_inference_steps / strength_first_pass))
first_step = int(num_steps * (1 - strength_first_pass))
ic_light.set_inference_steps(num_steps, first_step)
# ์ฒซ ๋ฒ์งธ ํจ์ค
for step in ic_light.steps:
x = ic_light(
x,
step=step,
clip_text_embedding=clip_text_embedding,
condition_scale=condition_scale,
)
# ๋ ๋ฒ์งธ ํจ์ค ์ค์
num_steps = int(round(num_inference_steps / strength_second_pass))
first_step = int(num_steps * (1 - strength_second_pass))
ic_light.set_inference_steps(num_steps, first_step)
# ์ด๊ธฐํ๋ ๋ผํ
ํธ
x = ic_light.solver.add_noise(x, noise=torch.randn_like(x), step=first_step)
# ๋ ๋ฒ์งธ ํจ์ค
for step in ic_light.steps:
x = ic_light(
x,
step=step,
clip_text_embedding=clip_text_embedding,
condition_scale=condition_scale,
)
# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์์ฑ
bg_image = ic_light.lda.latents_to_image(x)
# ์ต์ข
์ด๋ฏธ์ง ํฉ์ฑ (์ ๊ฒฝ์ ์กฐ์ ์์ด ํฉ์ฑ)
result = Image.composite(image_rgb, bg_image, mask)
return image_rgb, bg_image, mask, result # image_rgb ์ถ๊ฐ
@no_grad()
def adjust_final_image(
bg_image: Image.Image,
mask: Image.Image,
bg_brightness: float,
bg_contrast: float,
bg_saturation: float,
bg_temperature: float, # ์์จ๋ ํ๋ผ๋ฏธํฐ
bg_vibrance: float,
bg_color_mixer_blues: float,
bg_shadows: float, # Shadows ํ๋ผ๋ฏธํฐ
) -> Image.Image:
"""๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์กฐ์ ํ๋ ํจ์"""
print("Adjusting Final Image with the following parameters:")
print(f"Background - Brightness: {bg_brightness}, Contrast: {bg_contrast}, "
f"Saturation: {bg_saturation}, Temperature: {bg_temperature}, "
f"Vibrance: {bg_vibrance}, Blues: {bg_color_mixer_blues}, Shadows: {bg_shadows}")
# ๋ฐ๊ธฐ ์กฐ์
enhancer = ImageEnhance.Brightness(bg_image)
bg_image = enhancer.enhance(bg_brightness)
# ๋๋น ์กฐ์
enhancer = ImageEnhance.Contrast(bg_image)
bg_image = enhancer.enhance(bg_contrast)
# ์ฑ๋ ์กฐ์
enhancer = ImageEnhance.Color(bg_image)
bg_image = enhancer.enhance(bg_saturation)
# ์์จ๋ ์กฐ์
if bg_temperature != 0.0:
# PIL ์ด๋ฏธ์ง๋ฅผ OpenCV ํ์์ผ๋ก ๋ณํ
cv_image = cv2.cvtColor(np.array(bg_image), cv2.COLOR_RGB2BGR).astype(np.float32)
# ์์จ๋ ์กฐ์ ๋ก์ง
value = float(bg_temperature) * 30 # ์์จ๋ ํจ๊ณผ ์ค์ผ์ผ ์กฐ์
if value > 0: # ๋ฐ๋ปํ๊ฒ
cv_image[:, :, 2] = cv_image[:, :, 2] + value # Red ์ฑ๋ ์ฆ๊ฐ
cv_image[:, :, 0] = cv_image[:, :, 0] - value # Blue ์ฑ๋ ๊ฐ์
else: # ์ฐจ๊ฐ๊ฒ
cv_image[:, :, 2] = cv_image[:, :, 2] + value # Red ์ฑ๋ ๊ฐ์
cv_image[:, :, 0] = cv_image[:, :, 0] - value # Blue ์ฑ๋ ์ฆ๊ฐ
# ์ฑ๋ ๊ฐ ํด๋ฆฌํ
cv_image[:, :, 2] = np.clip(cv_image[:, :, 2], 0, 255)
cv_image[:, :, 0] = np.clip(cv_image[:, :, 0], 0, 255)
# ๋ค์ PIL ์ด๋ฏธ์ง๋ก ๋ณํ
bg_image = Image.fromarray(cv2.cvtColor(cv_image.astype(np.uint8), cv2.COLOR_BGR2RGB))
# ํ๊ธฐ(Vibrance) ์กฐ์ : ์ฑ๋๋ฅผ ๋น๋ก์ ์ผ๋ก ์ฆ๊ฐ/๊ฐ์
if bg_vibrance != 0.0:
# Vibrance๋ ์ผ๋ฐ์ ์ธ ์ฑ๋๋ณด๋ค ๋ ๋ฏผ๊ฐํ๊ฒ ์ฑ๋๋ฅผ ์กฐ์
converter = ImageEnhance.Color(bg_image)
factor = 1 + (bg_vibrance / 100.0)
bg_image = converter.enhance(factor)
# ์ปฌ๋ฌ ๋ฏน์ ๋ธ๋ฃจ ์กฐ์ : ํ๋ ์ฑ๋์ ๊ฐ๋ณ์ ์ผ๋ก ์กฐ์
if bg_color_mixer_blues != 0.0:
r, g, b = bg_image.split()
b = b.point(lambda p: min(255, max(0, p + bg_color_mixer_blues)))
bg_image = Image.merge("RGB", (r, g, b))
# Shadows ์กฐ์ : ์ด๋์ด ์์ญ์ ์กฐ์ ํ๊ณ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ๋ถ๋๋ฝ๊ฒ ์ฒ๋ฆฌ
if bg_shadows != 0.0:
# ์ด๋์ด ์์ญ์ ์๋ณํ๊ธฐ ์ํด ๊ทธ๋ ์ด์ค์ผ์ผ ๋ณํ
grayscale = bg_image.convert("L")
# ์ด๋์ด ์์ญ ๋ง์คํฌ ์์ฑ (์๊ณ๊ฐ 128 ์ดํ)
mask_dark = grayscale.point(lambda p: p < 128 and 255)
mask_dark = mask_dark.convert("L") # ๊ทธ๋ ์ด์ค์ผ์ผ๋ก ๋ณํ
# ๋ง์คํฌ ๊ฐ์ฅ์๋ฆฌ ๋ถ๋๋ฝ๊ฒ ์ฒ๋ฆฌ (Gaussian Blur ์ ์ฉ)
mask_dark = mask_dark.filter(ImageFilter.GaussianBlur(radius=10)) # ๋ธ๋ฌ ๋ฐ๊ฒฝ ์กฐ์ ๊ฐ๋ฅ
# ๊ทธ๋ฆผ์ ์กฐ์ ์ ์ํ ๋ฐ๊ธฐ ์กฐ์
if bg_shadows < 0:
# ์ด๋์ด ์์ญ์ ๋ ์ด๋ก๊ฒ
factor = 1 + (bg_shadows / 100.0) # ์: -50 -> 0.5
enhancer = ImageEnhance.Brightness(bg_image)
dark_adjusted = enhancer.enhance(factor)
else:
# ์ด๋์ด ์์ญ์ ๋ฐ๊ฒ
factor = 1 + (bg_shadows / 100.0) # ์: 50 -> 1.5
enhancer = ImageEnhance.Brightness(bg_image)
dark_adjusted = enhancer.enhance(factor)
# ๋ง์คํฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋์ด ์์ญ๋ง ์กฐ์ ๋ ์ด๋ฏธ์ง์ ํฉ์ฑ
bg_image = Image.composite(dark_adjusted, bg_image, mask_dark)
# ์กฐ์ ๋ ๋ฐฐ๊ฒฝ ๋ฐํ
return bg_image
def download_all(original_image, bg_image, final_image):
"""
๋ชจ๋ ์ด๋ฏธ์ง๋ฅผ ZIP ํ์ผ์ผ๋ก ์์ถํ์ฌ ๋ค์ด๋ก๋ํ ์ ์๋๋ก ํฉ๋๋ค.
"""
if original_image is None or bg_image is None or final_image is None:
print("Download function received None input.")
return None
print("Preparing images for download...")
# Convert images to RGB if they are not
if original_image.mode != "RGB":
original_image = original_image.convert("RGB")
if bg_image.mode != "RGB":
bg_image = bg_image.convert("RGB")
if final_image.mode != "RGB":
final_image = final_image.convert("RGB")
# ZIP ํ์ผ ์์ฑ
with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp_file:
with zipfile.ZipFile(tmp_file, 'w') as zip_file:
# ์๋ณธ ์ด๋ฏธ์ง ์ ์ฅ
original_buffer = BytesIO()
original_image.save(original_buffer, format="JPEG")
zip_file.writestr("original_image.jpg", original_buffer.getvalue())
# ์์ฑ๋ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์ ์ฅ
bg_buffer = BytesIO()
bg_image.save(bg_buffer, format="JPEG")
zip_file.writestr("background_image.jpg", bg_buffer.getvalue())
# ์ต์ข
๊ฒฐ๊ณผ ์ด๋ฏธ์ง ์ ์ฅ
final_buffer = BytesIO()
final_image.save(final_buffer, format="JPEG")
zip_file.writestr("final_image.jpg", final_buffer.getvalue())
zip_path = tmp_file.name
print(f"ZIP file created at {zip_path}")
return zip_path
def reset_sliders(initial_result):
"""
์ฌ๋ผ์ด๋๋ฅผ "ํํฐ์์"์ ์ด๊ธฐ๊ฐ์ผ๋ก ์ด๊ธฐํํ๊ณ ์ต์ข
๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ์ด๊ธฐ ์์ฑ๋ ์ด๋ฏธ์ง๋ก ๋ณต์ํ๋ ํจ์.
"""
print("Resetting sliders to default values.")
return (
"ํํฐ์์", # filter_radio
gr.update(value=1.0), # ๋ฐ๊ธฐ
gr.update(value=1.0), # ๋๋น
gr.update(value=1.0), # ์ฑ๋
gr.update(value=0.0), # ์์จ๋
gr.update(value=0.0), # ํ๊ธฐ
gr.update(value=0.0), # ์ปฌ๋ฌ ๋ฏน์ (๋ธ๋ฃจ)
gr.update(value=0.0), # ๊ทธ๋ฆผ์
initial_result, # ํํฐ ์ ์ฉ ์ด๋ฏธ์ง
)
# Gradio ์ธํฐํ์ด์ค ์ค์
def create_interface():
css = """
/* ํ๋กฌํํธ ํ
์คํธ๋ฐ์ค ๋ผ๋ฒจ๊ณผ ์
๋ ฅ ํ
์คํธ๋ง ๊ฒ์์์ผ๋ก ์ค์ */
.gr-textbox > label {
color: #000 !important;
}
.gr-textbox input {
color: #000 !important;
}
/* ํํฐ ์ ํ ๋ผ๋์ค ๋ฒํผ ๋ผ๋ฒจ๋ง ๊ฒ์์์ผ๋ก ์ค์ */
.gradio-radio > label {
color: #000 !important;
}
/* ์ฌ๋ผ์ด๋ ๋ผ๋ฒจ๊ณผ ํ์ฌ ๊ฐ ํ
์คํธ๋ง ๊ฒ์์์ผ๋ก ์ค์ */
.gr-slider > label {
color: #000 !important;
}
.gr-slider .slider-value {
color: #000 !important;
}
/* ์ ํ๋ ๋ผ๋์ค ๋ฒํผ ํ
์คํธ๋ฅผ ๊ฒ์์์ผ๋ก ์ ์งํ๊ณ ๋ฐฐ๊ฒฝ์์ ํฐ์์ผ๋ก ์ค์ */
.gradio-radio label[aria-checked="true"] {
color: #000 !important;
background-color: #fff !important; /* ํฐ์ ๋ฐฐ๊ฒฝ */
padding: 4px 8px;
border-radius: 4px;
border: 1px solid #000 !important; /* ๊ฒ์์ ํ
๋๋ฆฌ ์ถ๊ฐ */
}
/* ์ ํ๋์ง ์์ ๋ผ๋์ค ๋ฒํผ ํ
์คํธ๋ ๊ฒ์์ ์ ์ง */
.gradio-radio label[aria-checked="false"] {
color: #000 !important;
background-color: #fff !important; /* ํฐ์ ๋ฐฐ๊ฒฝ ์ ์ง */
border: 1px solid #000 !important; /* ๊ฒ์์ ํ
๋๋ฆฌ ์ถ๊ฐ */
}
/* ๋ผ๋์ค ๋ฒํผ์ ์ ํ ํ์ ์ ์จ๊ธฐ๊ธฐ */
.gradio-radio input[type="radio"] + label::before {
display: none;
}
/* ๋ผ๋์ค ๋ฒํผ ์ ํ ์ ์ ํ ํ์ ์ ์ ์จ๊ธฐ๊ธฐ */
.gradio-radio input[type="radio"]:checked + label::after {
content: "";
display: none;
}
/* ๋ฒํผ ํ
์คํธ ์์๋ ๊ฒ์์์ผ๋ก ๋ณ๊ฒฝ */
.btn-primary, .btn-secondary, .gr-button {
color: #000 !important;
}
/* ๋ค์ด๋ก๋ ๋ฒํผ ํ
์คํธ ์์ ๊ฒ์์์ผ๋ก ๋ณ๊ฒฝ */
.gr-button.download-button {
color: #000 !important;
}
/* ๊ธฐํ ํ
์คํธ ์์์ ์ํฅ์ ์ฃผ์ง ์๋๋ก ์ฃผ์ */
/* ํ์ํ ๊ฒฝ์ฐ ์ถ๊ฐ์ ์ธ ์ ํ์๋ฅผ ์ฌ๊ธฐ์ ์์ฑ */
"""
# ํํฐ ํ๋ฆฌ์
์ ์
filters = [
{
"name": "ํํฐ์์",
"bg_brightness": 1.0,
"bg_contrast": 1.0,
"bg_saturation": 1.0,
"bg_temperature": 0.0,
"bg_vibrance": 0.0,
"bg_color_mixer_blues": 0.0,
"bg_shadows": 0.0
},
{
"name": "๊นจ๋ํ",
"bg_brightness": 1.3,
"bg_contrast": 1.2,
"bg_saturation": 1.0,
"bg_temperature": -0.2,
"bg_vibrance": -20.0,
"bg_color_mixer_blues": 5.0,
"bg_shadows": -10.0
},
{
"name": "๋ฐ์คํ",
"bg_brightness": 1.3,
"bg_contrast": 1.2,
"bg_saturation": 0.8,
"bg_temperature": 0.0,
"bg_vibrance": -10.0,
"bg_color_mixer_blues": 2.0,
"bg_shadows": -10.0
},
{
"name": "ํธ๋ฅธ๋น",
"bg_brightness": 1.3,
"bg_contrast": 1.0,
"bg_saturation": 1.0,
"bg_temperature": 0.0,
"bg_vibrance": 0.0,
"bg_color_mixer_blues": 10.0,
"bg_shadows": 5.0
}
]
# ๊ธฐ๋ณธ ํํฐ ์ค์ (๊นจ๋ํ)
default_filter = next(filter for filter in filters if filter["name"] == "๊นจ๋ํ")
with gr.Blocks(theme=gr.themes.Soft(
primary_hue=gr.themes.Color(
c50="#FFF7ED", # ๊ฐ์ฅ ๋ฐ์ ์ฃผํฉ
c100="#FFEDD5",
c200="#FED7AA",
c300="#FDBA74",
c400="#FB923C",
c500="#F97316", # ๊ธฐ๋ณธ ์ฃผํฉ
c600="#EA580C",
c700="#C2410C",
c800="#9A3412",
c900="#7C2D12", # ๊ฐ์ฅ ์ด๋์ด ์ฃผํฉ
c950="#431407",
),
secondary_hue="zinc", # ๋ชจ๋ํ ๋๋์ ํ์ ๊ณ์ด
neutral_hue="zinc",
font=("Pretendard", "sans-serif")
), css=css) as demo:
with gr.Row():
# ์ผ์ชฝ ์ด: ์
๋ ฅ๊ณผ ์ค์
with gr.Column(scale=1):
input_image = gr.Image(
label="์ด๋ฏธ์ง ์ถ๊ฐ",
image_mode="RGBA",
type="pil",
)
prompt = gr.Textbox(
label="ํ๋กฌํํธ (ํ๊ตญ์ด)",
placeholder="๋๋ฎ์ธ ํ๋ง๋ผ์ผ, ๋ญ๊ฒ๊ตฌ๋ฆ, ํฐ์ ๋ฐ๋ฅ์ ๋์ฌ์๋ ์๋ง",
)
run_button = gr.Button(
value="์ด๋ฏธ์ง ์์ฑ",
variant="primary",
size="lg"
)
reset_button = gr.Button(
value="ํํฐ ์ด๊ธฐํ",
variant="secondary",
size="lg"
)
# ํํฐ ์ ํ ๋ผ๋์ค ๋ฒํผ ์ถ๊ฐ
filter_radio = gr.Radio(
label="ํํฐ ์ ํ",
choices=["ํํฐ์์", "๊นจ๋ํ", "๋ฐ์คํ", "ํธ๋ฅธ๋น"],
value="๊นจ๋ํ", # ๊ธฐ๋ณธ ์ ํ์ ๊นจ๋ํ
type="value",
)
# ์์ ์ถ๊ฐ
examples = [
["img/1.png", "๋, ํ๋ง๋ผ์ผ", "ํธ๋ฅธ๋น"],
["img/2.png", "ํฐ์ ๋ฒฝ์ง์ ๊ธฐ๋์ด ์๋ ์ฌ๋", "๊นจ๋ํ"],
["img/3.png", "๋จ์ํ ์ฑ
์์์ ๋์ธ ๋ฏธ๋ ํ์ฐ์น", "๋ฐ์คํ"],
]
gr.Examples(
examples=examples,
inputs=[input_image, prompt, filter_radio],
fn=lambda img, prm, flt: on_generate(img, prm, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0) if flt == "ํํฐ์์" else on_generate(img, prm,
next(filter for filter in filters if filter["name"] == flt)["bg_brightness"],
next(filter for filter in filters if filter["name"] == flt)["bg_contrast"],
next(filter for filter in filters if filter["name"] == flt)["bg_saturation"],
next(filter for filter in filters if filter["name"] == flt)["bg_temperature"],
next(filter for filter in filters if filter["name"] == flt)["bg_vibrance"],
next(filter for filter in filters if filter["name"] == flt)["bg_color_mixer_blues"],
next(filter for filter in filters if filter["name"] == flt)["bg_shadows"],
),
outputs=[
"background_output",
"mask_state",
"final_output",
"image_rgb_state",
"initial_result",
],
label="์์ ์ ํ",
cache_examples=False,
)
# ๋ฐฐ๊ฒฝ ์กฐ์ ์ต์
์์ ๋ณ๊ฒฝ (๋ผ๋ฒจ์์ "๋ฐฐ๊ฒฝ" ์ ๊ฑฐ)
bg_brightness = gr.Slider(label="๋ฐ๊ธฐ", minimum=0.1, maximum=5.0, value=default_filter["bg_brightness"], step=0.1)
bg_contrast = gr.Slider(label="๋๋น", minimum=0.1, maximum=5.0, value=default_filter["bg_contrast"], step=0.1)
bg_saturation = gr.Slider(label="์ฑ๋", minimum=0.0, maximum=5.0, value=default_filter["bg_saturation"], step=0.1)
bg_temperature = gr.Slider( # ์์จ๋ ์ฌ๋ผ์ด๋
label="์์จ๋",
minimum=-1.0,
maximum=1.0,
value=default_filter["bg_temperature"],
step=0.1
)
bg_vibrance = gr.Slider(label="ํ๊ธฐ", minimum=-100.0, maximum=100.0, value=default_filter["bg_vibrance"], step=1.0)
bg_color_mixer_blues = gr.Slider(label="์ปฌ๋ฌ ๋ฏน์ (๋ธ๋ฃจ)", minimum=-100.0, maximum=100.0, value=default_filter["bg_color_mixer_blues"], step=1.0)
bg_shadows = gr.Slider(label="๊ทธ๋ฆผ์", minimum=-100.0, maximum=100.0, value=default_filter["bg_shadows"], step=1.0) # Shadows ์ฌ๋ผ์ด๋
# ์ค๋ฅธ์ชฝ ์ด: ์ถ๋ ฅ๊ณผ ๋ค์ด๋ก๋ ๋ฒํผ
with gr.Column(scale=2):
background_output = gr.Image(
label="์์ฑ๋ ์ด๋ฏธ์ง",
type="pil",
interactive=False # ์ถ๋ ฅ ์ ์ฉ์ผ๋ก ์ค์
)
final_output = gr.Image(
label="ํํฐ ์ ์ฉ ์ด๋ฏธ์ง",
type="pil",
interactive=False # ์ถ๋ ฅ ์ ์ฉ์ผ๋ก ์ค์
)
# "๋ชจ๋ ์ด๋ฏธ์ง ๋ค์ด๋ก๋" ๋ฒํผ๊ณผ ๋ค์ด๋ก๋ ์ถ๋ ฅ์ฐฝ์ ์ค์์ ๋ฐฐ์น
with gr.Row(elem_classes="download-container"):
download_button = gr.Button(
value="๋ชจ๋ ์ด๋ฏธ์ง ๋ค์ด๋ก๋",
elem_classes="download-button",
variant="primary",
size="lg"
)
with gr.Row(elem_classes="download-container"):
download_output = gr.File(label="๋ชจ๋ ์ด๋ฏธ์ง ๋ค์ด๋ก๋", elem_classes="download-output")
# ์ํ ์ปดํฌ๋ํธ
mask_state = gr.State()
image_rgb_state = gr.State() # image_rgb ์ํ ์ถ๊ฐ
initial_result = gr.State()
# ํํฐ ์ ํ ์ ์ฌ๋ผ์ด๋ ๊ฐ ๋ณ๊ฒฝ ํจ์
def apply_filter_preset(selected_filter):
for filter in filters:
if filter["name"] == selected_filter:
return (
gr.update(value=filter["bg_brightness"]),
gr.update(value=filter["bg_contrast"]),
gr.update(value=filter["bg_saturation"]),
gr.update(value=filter["bg_temperature"]),
gr.update(value=filter["bg_vibrance"]),
gr.update(value=filter["bg_color_mixer_blues"]),
gr.update(value=filter["bg_shadows"]),
)
# ๊ธฐ๋ณธ๊ฐ ๋ฐํ (ํํฐ์์)
return (
gr.update(value=1.0),
gr.update(value=1.0),
gr.update(value=1.0),
gr.update(value=0.0),
gr.update(value=0.0),
gr.update(value=0.0),
gr.update(value=0.0),
)
# ํํฐ ๋ผ๋์ค ๋ฒํผ ์ ํ ์ ์ฌ๋ผ์ด๋ ๊ฐ ์
๋ฐ์ดํธ
filter_radio.change(
fn=apply_filter_preset,
inputs=[filter_radio],
outputs=[
bg_brightness,
bg_contrast,
bg_saturation,
bg_temperature,
bg_vibrance,
bg_color_mixer_blues,
bg_shadows,
],
)
# ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ํด๋ฆญ ์ generate_images ํจ์ ํธ์ถ ๋ฐ ํ๋กฌํํธ ๋ฒ์ญ
def on_generate(image, prompt, bg_brightness, bg_contrast, bg_saturation, bg_temperature, bg_vibrance, bg_color_mixer_blues, bg_shadows):
# ํ๋กฌํํธ๊ฐ ๋น์ด์๋์ง ํ์ธ
if not prompt.strip():
logger.info("ํ๋กฌํํธ๊ฐ ๋น์ด ์์ต๋๋ค. ๊ธฐ๋ณธ ํ๋กฌํํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค.")
# ๊ธฐ๋ณธ ํ๋กฌํํธ ์ค์
final_prompt = "high-quality professional studio photography, Realistic soft white tone bright lighting, HEIC, CR2, NEF"
else:
# ํ๊ตญ์ด ํ๋กฌํํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ
translated_prompt = translate_to_english(prompt)
if translated_prompt.startswith("์ ํจํ์ง ์์ ์
๋ ฅ") or translated_prompt.startswith("๋ฒ์ญ ์ค ์ค๋ฅ"):
return None, None, None, None, None # ๋ฒ์ญ ์คํจ ์ ์ข
๋ฃ
# ๋ฒ์ญ๋ ํ๋กฌํํธ์ ์๋์ผ๋ก ๋ฌธ์์ด ์ถ๊ฐ
final_prompt = f"{translated_prompt}, high-quality professional studio photography, Realistic soft white tone bright lighting, HEIC, CR2, NEF"
image_rgb, bg_img, mask, result = generate_images(image, final_prompt)
print("Image generation completed.")
# ์ฌ๋ผ์ด๋ ๊ฐ ์ ์ฉ
if bg_img is not None and mask is not None and image_rgb is not None:
adjusted_bg = adjust_final_image(
bg_image=bg_img,
mask=mask,
bg_brightness=bg_brightness,
bg_contrast=bg_contrast,
bg_saturation=bg_saturation,
bg_temperature=bg_temperature,
bg_vibrance=bg_vibrance,
bg_color_mixer_blues=bg_color_mixer_blues,
bg_shadows=bg_shadows,
)
final_result = Image.composite(image_rgb, adjusted_bg, mask)
else:
final_result = result
# ์ด๊ธฐ ๊ฒฐ๊ณผ์ final_result ์ ์ฅ
initial_result.value = final_result
return bg_img, mask, final_result, image_rgb, final_result # initial_result ์ถ๊ฐ ๋ฐํ
run_button.click(
fn=on_generate,
inputs=[
input_image,
prompt,
bg_brightness,
bg_contrast,
bg_saturation,
bg_temperature,
bg_vibrance,
bg_color_mixer_blues,
bg_shadows,
],
outputs=[
background_output,
mask_state,
final_output,
image_rgb_state,
initial_result, # initial_result ์ถ๊ฐ ๋ฐํ
],
)
def on_adjust(
image_rgb, # image_rgb_state ์ฌ์ฉ
bg_image, mask,
bg_brightness, bg_contrast, bg_saturation, bg_temperature, bg_vibrance, bg_color_mixer_blues, bg_shadows,
):
if bg_image is None or mask is None or image_rgb is None:
print("Adjust function received None input.")
return None
print(f"Adjusting background image... Current slider values:")
print(f"Brightness: {bg_brightness}, Contrast: {bg_contrast}, Saturation: {bg_saturation}, "
f"Temperature: {bg_temperature}, Vibrance: {bg_vibrance}, Blues: {bg_color_mixer_blues}, Shadows: {bg_shadows}")
adjusted_bg = adjust_final_image(
bg_image=bg_image,
mask=mask, # mask๋ฅผ ๋ฐ๋์ ์ ๋ฌ
bg_brightness=bg_brightness,
bg_contrast=bg_contrast,
bg_saturation=bg_saturation,
bg_temperature=bg_temperature,
bg_vibrance=bg_vibrance,
bg_color_mixer_blues=bg_color_mixer_blues,
bg_shadows=bg_shadows, # Shadows ํ๋ผ๋ฏธํฐ ์ถ๊ฐ
)
# ์กฐ์ ๋ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ํ์ธ
print(f"adjusted_bg size: {adjusted_bg.size}, mode: {adjusted_bg.mode}")
adjusted_bg.save("adjusted_bg_debug.jpg") # ๋๋ฒ๊น
์ฉ ์์ ํ์ผ ์ ์ฅ
# image_rgb์ ์ด๋ฏธ resize๋ ์ํ์ด๋ฏ๋ก ์ถ๊ฐ์ ์ธ ๋ณํ ํ์ ์์
input_image_rgb = image_rgb
# ์ด๋ฏธ์ง ํฌ๊ธฐ์ ๋ชจ๋ ํ์ธ
print(f"input_image_rgb size: {input_image_rgb.size}, mode: {input_image_rgb.mode}")
print(f"adjusted_bg size: {adjusted_bg.size}, mode: {adjusted_bg.mode}")
print(f"mask size: {mask.size}, mode: {mask.mode}")
# ์ด๋ฏธ์ง ํฌ๊ธฐ๊ฐ ๋์ผํ์ง ํ์ธํ๊ณ , ๋ค๋ฅด๋ฉด ์กฐ์
if input_image_rgb.size != adjusted_bg.size:
print(f"Resizing input_image_rgb from {input_image_rgb.size} to {adjusted_bg.size}")
input_image_rgb = input_image_rgb.resize(adjusted_bg.size)
# mask๋ฅผ ๋จ์ผ ์ฑ๋๋ก ๋ณํ (ํ์์)
mask = mask.convert("L")
try:
final_result = Image.composite(input_image_rgb, adjusted_bg, mask)
except ValueError as e:
print(f"Composite error: {e}")
return None
# ์ต์ข
๊ฒฐ๊ณผ ํ์ธ
final_result.save("final_result_debug.jpg") # ๋๋ฒ๊น
์ฉ ์์ ํ์ผ ์ ์ฅ
print("Final image composite completed.")
# ์ด๊ธฐ ๊ฒฐ๊ณผ์ final_result ์ ์ฅ
initial_result.value = final_result
return final_result
bg_sliders = [
bg_brightness,
bg_contrast,
bg_saturation,
bg_temperature,
bg_vibrance,
bg_color_mixer_blues,
bg_shadows, # Shadows ์ฌ๋ผ์ด๋ ์ถ๊ฐ
]
for slider in bg_sliders:
slider.change(
fn=on_adjust,
inputs=[
image_rgb_state, # image_rgb_state ์ฌ์ฉ
background_output, # ์์ฑ๋ ์ด๋ฏธ์ง
mask_state, # ์ํ ์ฑ๋
bg_brightness,
bg_contrast,
bg_saturation,
bg_temperature,
bg_vibrance,
bg_color_mixer_blues,
bg_shadows, # Shadows ํ๋ผ๋ฏธํฐ ์ถ๊ฐ
],
outputs=final_output, # ์ต์ข
๊ฒฐ๊ณผ๋ ํฉ์ฑ๋ ์ด๋ฏธ์ง
)
# ๋ฆฌ์
๋ฒํผ ํด๋ฆญ ์ ๋ชจ๋ ์ฌ๋ผ์ด๋๋ฅผ "ํํฐ์์"์ ์ด๊ธฐ๊ฐ์ผ๋ก ์ด๊ธฐํํ๊ณ ํํฐ ์ ํ์ "ํํฐ์์"์ผ๋ก ๋ณ๊ฒฝ
reset_button.click(
fn=reset_sliders,
inputs=[final_output],
outputs=[
filter_radio, # ํํฐ ์ ํ ๋ผ๋์ค ๋ฒํผ
bg_brightness,
bg_contrast,
bg_saturation,
bg_temperature,
bg_vibrance,
bg_color_mixer_blues,
bg_shadows,
final_output,
],
)
# ๋ค์ด๋ก๋ ๋ฒํผ ํด๋ฆญ ์ ๋ชจ๋ ์ด๋ฏธ์ง ๋ค์ด๋ก๋
download_button.click(
fn=download_all,
inputs=[
input_image,
background_output,
final_output
],
outputs=download_output,
)
return demo
if __name__ == "__main__":
logger.info("์ ํ๋ฆฌ์ผ์ด์
์์")
demo = create_interface()
demo.queue()
demo.launch(server_name='0.0.0.0')
|