Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import os
|
| 2 |
import sys
|
| 3 |
import base64
|
| 4 |
import io
|
|
@@ -6,6 +5,7 @@ import logging
|
|
| 6 |
import tempfile
|
| 7 |
import traceback
|
| 8 |
import requests
|
|
|
|
| 9 |
from PIL import Image
|
| 10 |
import gradio as gr
|
| 11 |
from openai import OpenAI
|
|
@@ -24,54 +24,6 @@ logging.basicConfig(
|
|
| 24 |
)
|
| 25 |
logger = logging.getLogger("image-enhancer-app")
|
| 26 |
|
| 27 |
-
# ํ๊ฒฝ๋ณ์๋ก๋ถํฐ ๋ฐฐ๊ฒฝ ์ค์ ๋ก๋
|
| 28 |
-
def load_backgrounds():
|
| 29 |
-
"""ํ๊ฒฝ๋ณ์ BACKGROUNDS_CONFIG๋ก๋ถํฐ ๋ฐฐ๊ฒฝ ์ค์ ์ ๋ก๋ํฉ๋๋ค."""
|
| 30 |
-
import json
|
| 31 |
-
|
| 32 |
-
backgrounds_json = os.environ.get("BACKGROUNDS_CONFIG", "")
|
| 33 |
-
|
| 34 |
-
if not backgrounds_json:
|
| 35 |
-
logger.warning("BACKGROUNDS_CONFIG ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
| 36 |
-
return {
|
| 37 |
-
"SIMPLE_BACKGROUNDS": {"๊ธฐ๋ณธ ํ์ดํธ": "clean white background"},
|
| 38 |
-
"STUDIO_BACKGROUNDS": {"๊ธฐ๋ณธ ์คํ๋์ค": "studio background"},
|
| 39 |
-
"NATURE_BACKGROUNDS": {"๊ธฐ๋ณธ ์์ฐ": "nature background"},
|
| 40 |
-
"INDOOR_BACKGROUNDS": {"๊ธฐ๋ณธ ์ค๋ด": "indoor background"},
|
| 41 |
-
"SPECIAL_BACKGROUNDS": {"๊ธฐ๋ณธ ํน์": "special background"},
|
| 42 |
-
"JEWELRY_BACKGROUNDS": {"๊ธฐ๋ณธ ์ฃผ์ผ๋ฆฌ": "jewelry background"},
|
| 43 |
-
"SPECIAL_EFFECTS_BACKGROUNDS": {"๊ธฐ๋ณธ ํจ๊ณผ": "special effects background"}
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
try:
|
| 47 |
-
backgrounds_data = json.loads(backgrounds_json)
|
| 48 |
-
logger.info("ํ๊ฒฝ๋ณ์๋ก๋ถํฐ ๋ฐฐ๊ฒฝ ์ค์ ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค.")
|
| 49 |
-
return backgrounds_data
|
| 50 |
-
except json.JSONDecodeError as e:
|
| 51 |
-
logger.error(f"๋ฐฐ๊ฒฝ ์ค์ JSON ํ์ฑ ์ค๋ฅ: {e}")
|
| 52 |
-
return {}
|
| 53 |
-
|
| 54 |
-
# ๋ฐฐ๊ฒฝ ์ค์ ๋ก๋
|
| 55 |
-
backgrounds_data = load_backgrounds()
|
| 56 |
-
SIMPLE_BACKGROUNDS = backgrounds_data.get("SIMPLE_BACKGROUNDS", {})
|
| 57 |
-
STUDIO_BACKGROUNDS = backgrounds_data.get("STUDIO_BACKGROUNDS", {})
|
| 58 |
-
NATURE_BACKGROUNDS = backgrounds_data.get("NATURE_BACKGROUNDS", {})
|
| 59 |
-
INDOOR_BACKGROUNDS = backgrounds_data.get("INDOOR_BACKGROUNDS", {})
|
| 60 |
-
SPECIAL_BACKGROUNDS = backgrounds_data.get("SPECIAL_BACKGROUNDS", {})
|
| 61 |
-
JEWELRY_BACKGROUNDS = backgrounds_data.get("JEWELRY_BACKGROUNDS", {})
|
| 62 |
-
SPECIAL_EFFECTS_BACKGROUNDS = backgrounds_data.get("SPECIAL_EFFECTS_BACKGROUNDS", {})
|
| 63 |
-
|
| 64 |
-
# ํ๊ฒฝ๋ณ์๋ก๋ถํฐ ๋ชจ๋ธ ์ค์ ๋ก๋
|
| 65 |
-
IMAGE_EDIT_MODEL_GPT = os.environ.get("IMAGE_EDIT_MODEL_GPT", "gpt-image-1")
|
| 66 |
-
IMAGE_EDIT_MODEL_FLUX = os.environ.get("IMAGE_EDIT_MODEL_FLUX", "black-forest-labs/flux-kontext-pro")
|
| 67 |
-
TRANSLATION_MODEL = os.environ.get("TRANSLATION_MODEL", "gemini-2.0-flash")
|
| 68 |
-
ENHANCEMENT_MODEL = os.environ.get("ENHANCEMENT_MODEL", "philz1337x/clarity-upscaler:dfad41707589d68ecdccd1dfa600d55a208f9310748e44bfe35b4a6291453d5e")
|
| 69 |
-
|
| 70 |
-
logger.info(f"์ด๋ฏธ์ง ํธ์ง ๋ชจ๋ธ (GPT): {IMAGE_EDIT_MODEL_GPT}")
|
| 71 |
-
logger.info(f"์ด๋ฏธ์ง ํธ์ง ๋ชจ๋ธ (Flux): {IMAGE_EDIT_MODEL_FLUX}")
|
| 72 |
-
logger.info(f"๋ฒ์ญ ๋ชจ๋ธ: {TRANSLATION_MODEL}")
|
| 73 |
-
logger.info(f"ํ์ง ๊ฐ์ ๋ชจ๋ธ: {ENHANCEMENT_MODEL}")
|
| 74 |
-
|
| 75 |
# API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (์์ ํ๊ฒ)
|
| 76 |
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))
|
| 77 |
|
|
@@ -88,6 +40,148 @@ else:
|
|
| 88 |
logger.warning("GEMINI_API_KEY not found or empty, Gemini client not initialized")
|
| 89 |
gemini_client = None
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
# ์์ ํ์ผ ์ ์ฅ ํจ์
|
| 92 |
def save_uploaded_file(uploaded_file, suffix='.png'):
|
| 93 |
try:
|
|
@@ -130,9 +224,9 @@ def save_uploaded_file(uploaded_file, suffix='.png'):
|
|
| 130 |
logger.error(traceback.format_exc())
|
| 131 |
return None
|
| 132 |
|
| 133 |
-
# ํ
์คํธ ๋ฒ์ญ ํจ์ (ํ๊ตญ์ด โ ์์ด) -
|
| 134 |
def translate_to_english(text):
|
| 135 |
-
"""ํ๊ตญ์ด ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ (
|
| 136 |
try:
|
| 137 |
if not text or not text.strip():
|
| 138 |
return ""
|
|
@@ -142,10 +236,10 @@ def translate_to_english(text):
|
|
| 142 |
logger.warning("Gemini client not available, returning original text")
|
| 143 |
return text
|
| 144 |
|
| 145 |
-
#
|
| 146 |
try:
|
| 147 |
response = gemini_client.models.generate_content(
|
| 148 |
-
model=
|
| 149 |
config=types.GenerateContentConfig(
|
| 150 |
system_instruction="You are a professional translator. Translate the given Korean text to English. Keep the translation natural and contextually appropriate for image generation prompts. If the text is already in English, return it as is. Only return the translated text without any additional explanation.",
|
| 151 |
max_output_tokens=200,
|
|
@@ -155,11 +249,11 @@ def translate_to_english(text):
|
|
| 155 |
)
|
| 156 |
|
| 157 |
translated = response.text.strip()
|
| 158 |
-
logger.info(f"Translated '{text}' to '{translated}' using
|
| 159 |
return translated
|
| 160 |
|
| 161 |
except Exception as e:
|
| 162 |
-
logger.error(f"
|
| 163 |
logger.warning("Translation failed, returning original text")
|
| 164 |
return text
|
| 165 |
|
|
@@ -200,7 +294,7 @@ def generate_prompt(background_type, background_name, user_request, aspect_ratio
|
|
| 200 |
|
| 201 |
# ์ฌ์ฉ์ ์์ฒญ์ฌํญ ์ฒ๋ฆฌ
|
| 202 |
if user_request and user_request.strip():
|
| 203 |
-
# ํ๊ตญ์ด ์์ฒญ์ฌํญ์ ์์ด๋ก ๋ฒ์ญ
|
| 204 |
translated_request = translate_to_english(user_request)
|
| 205 |
|
| 206 |
# ๋ฒ์ญ๋ ์์ฒญ์ฌํญ์ ๋ฐฐ๊ฒฝ ํ๋กฌํํธ์ ํตํฉ
|
|
@@ -289,13 +383,13 @@ def edit_and_enhance_image(
|
|
| 289 |
|
| 290 |
params = {
|
| 291 |
"prompt": prompt,
|
| 292 |
-
"model":
|
| 293 |
"n": 1,
|
| 294 |
"size": size,
|
| 295 |
"image": processed_image
|
| 296 |
}
|
| 297 |
|
| 298 |
-
logger.info(f"Calling OpenAI API for image editing
|
| 299 |
response = openai_client.images.edit(**params)
|
| 300 |
logger.info("OpenAI API call successful")
|
| 301 |
|
|
@@ -321,7 +415,7 @@ def edit_and_enhance_image(
|
|
| 321 |
|
| 322 |
edited_images.append(image)
|
| 323 |
|
| 324 |
-
usage_info =
|
| 325 |
|
| 326 |
else: # quality_level == "flux"
|
| 327 |
# Flux ๋ชจ๋ธ ์ฌ์ฉ (ํญ์ ๊ธฐ๋ณธ ํ์ง๊ฐ์ 1ํ ์ ์ฉ)
|
|
@@ -329,11 +423,11 @@ def edit_and_enhance_image(
|
|
| 329 |
logger.error("Replicate API token is not set")
|
| 330 |
return None, None, None, "Replicate API ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค. API ํ ํฐ์ ์ค์ ํด์ฃผ์ธ์."
|
| 331 |
|
| 332 |
-
logger.info(f"Using Flux model for image editing
|
| 333 |
|
| 334 |
# Flux ๋ชจ๋ธ๋ก ์ด๋ฏธ์ง ์์ฑ
|
| 335 |
output = replicate.run(
|
| 336 |
-
|
| 337 |
input={
|
| 338 |
"prompt": prompt,
|
| 339 |
"input_image": processed_image,
|
|
@@ -368,7 +462,7 @@ def edit_and_enhance_image(
|
|
| 368 |
|
| 369 |
# Flux ๋ชจ๋ธ์ ํญ์ ์ฒซ ๋ฒ์งธ ํ์ง ๊ฐ์ ์ ์๋ ์ ์ฉ
|
| 370 |
try:
|
| 371 |
-
logger.info(
|
| 372 |
|
| 373 |
# ์์ ํ์ผ๋ก ์ ์ฅ
|
| 374 |
temp_flux_path = tempfile.mktemp(suffix='.png')
|
|
@@ -377,7 +471,7 @@ def edit_and_enhance_image(
|
|
| 377 |
|
| 378 |
# ์ฒซ ๋ฒ์งธ ํ์ง ํฅ์ (Flux ๋ชจ๋ธ ๊ธฐ๋ณธ ์ ์ฉ)
|
| 379 |
first_enhanced_output = replicate.run(
|
| 380 |
-
|
| 381 |
input={
|
| 382 |
"image": open(temp_flux_path, "rb"),
|
| 383 |
"scale_factor": 2,
|
|
@@ -403,22 +497,22 @@ def edit_and_enhance_image(
|
|
| 403 |
first_enhanced_image = background
|
| 404 |
|
| 405 |
edited_images.append(first_enhanced_image)
|
| 406 |
-
usage_info =
|
| 407 |
logger.info("First enhancement completed for Flux model")
|
| 408 |
else:
|
| 409 |
# ์ฒซ ๋ฒ์งธ ํ์ง๊ฐ์ ์คํจ ์ ์๋ณธ ์ฌ์ฉ
|
| 410 |
edited_images.append(flux_image)
|
| 411 |
-
usage_info =
|
| 412 |
else:
|
| 413 |
# ์ฒซ ๋ฒ์งธ ํ์ง๊ฐ์ ์คํจ ์ ์๋ณธ ์ฌ์ฉ
|
| 414 |
edited_images.append(flux_image)
|
| 415 |
-
usage_info =
|
| 416 |
|
| 417 |
except Exception as e:
|
| 418 |
logger.error(f"Error in first enhancement for Flux: {e}")
|
| 419 |
# ์ฒซ ๋ฒ์งธ ํ์ง๊ฐ์ ์คํจ ์ ์๋ณธ ์ฌ์ฉ
|
| 420 |
edited_images.append(flux_image)
|
| 421 |
-
usage_info = f"์ด๋ฏธ์ง ํธ์ง ์๋ฃ (
|
| 422 |
|
| 423 |
else:
|
| 424 |
logger.error("No output from Flux API")
|
|
@@ -445,11 +539,11 @@ def edit_and_enhance_image(
|
|
| 445 |
try:
|
| 446 |
if quality_level == "gpt":
|
| 447 |
# GPT ๋ชจ๋ธ: ์ผ๋ฐ์ ์ธ ํ์ง ๊ฐ์
|
| 448 |
-
logger.info(f"Enhancing GPT image with
|
| 449 |
enhancement_info = "ํ์ง ๊ฐ์ "
|
| 450 |
else:
|
| 451 |
# Flux ๋ชจ๋ธ: 2์ฐจ ํ์ง ๊ฐ์ (์ด๋ฏธ 1์ฐจ๋ ์ ์ฉ๋จ)
|
| 452 |
-
logger.info(f"Applying second enhancement for Flux image
|
| 453 |
enhancement_info = "2์ฐจ ํ์ง ๊ฐ์ "
|
| 454 |
|
| 455 |
if not os.environ.get("REPLICATE_API_TOKEN"):
|
|
@@ -463,7 +557,7 @@ def edit_and_enhance_image(
|
|
| 463 |
|
| 464 |
# Replicate API๋ก ํ์ง ํฅ์
|
| 465 |
output = replicate.run(
|
| 466 |
-
|
| 467 |
input={
|
| 468 |
"image": open(temp_image_path, "rb"),
|
| 469 |
"scale_factor": enhancement_level,
|
|
@@ -475,7 +569,7 @@ def edit_and_enhance_image(
|
|
| 475 |
}
|
| 476 |
)
|
| 477 |
|
| 478 |
-
logger.info(f"
|
| 479 |
|
| 480 |
if output and isinstance(output, list) and len(output) > 0:
|
| 481 |
enhanced_url = output[0]
|
|
@@ -489,13 +583,13 @@ def edit_and_enhance_image(
|
|
| 489 |
enhanced_image = background
|
| 490 |
|
| 491 |
if quality_level == "gpt":
|
| 492 |
-
usage_info += f" | {enhancement_info} ์๋ฃ:
|
| 493 |
else:
|
| 494 |
usage_info += f" | {enhancement_info} ์๋ฃ: ์ด 2ํ ํ์ง๊ฐ์ ์ ์ฉ"
|
| 495 |
else:
|
| 496 |
usage_info += f" | {enhancement_info} ์คํจ: ์ด๋ฏธ์ง ๋ค์ด๋ก๋ ์ค๋ฅ"
|
| 497 |
else:
|
| 498 |
-
usage_info += f" | {enhancement_info} ์คํจ:
|
| 499 |
|
| 500 |
except Exception as e:
|
| 501 |
logger.error(f"Error enhancing image: {e}")
|
|
@@ -558,58 +652,70 @@ def create_gradio_interface():
|
|
| 558 |
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
| 559 |
)
|
| 560 |
|
| 561 |
-
# ๋๋กญ๋ค์ด ์ปดํฌ๋ํธ๋ค
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
simple_dropdown = gr.Dropdown(
|
| 563 |
-
choices=
|
| 564 |
-
value=
|
| 565 |
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
|
| 566 |
visible=True,
|
| 567 |
interactive=True
|
| 568 |
)
|
| 569 |
|
| 570 |
studio_dropdown = gr.Dropdown(
|
| 571 |
-
choices=
|
| 572 |
-
value=
|
| 573 |
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
| 574 |
visible=False,
|
| 575 |
interactive=True
|
| 576 |
)
|
| 577 |
|
| 578 |
nature_dropdown = gr.Dropdown(
|
| 579 |
-
choices=
|
| 580 |
-
value=
|
| 581 |
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
| 582 |
visible=False,
|
| 583 |
interactive=True
|
| 584 |
)
|
| 585 |
|
| 586 |
indoor_dropdown = gr.Dropdown(
|
| 587 |
-
choices=
|
| 588 |
-
value=
|
| 589 |
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
| 590 |
visible=False,
|
| 591 |
interactive=True
|
| 592 |
)
|
| 593 |
|
| 594 |
special_dropdown = gr.Dropdown(
|
| 595 |
-
choices=
|
| 596 |
-
value=
|
| 597 |
label="ํน์๋ฐฐ๊ฒฝ ์ ํ",
|
| 598 |
visible=False,
|
| 599 |
interactive=True
|
| 600 |
)
|
| 601 |
|
| 602 |
jewelry_dropdown = gr.Dropdown(
|
| 603 |
-
choices=
|
| 604 |
-
value=
|
| 605 |
label="์ฃผ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ํ",
|
| 606 |
visible=False,
|
| 607 |
interactive=True
|
| 608 |
)
|
| 609 |
|
| 610 |
special_effects_dropdown = gr.Dropdown(
|
| 611 |
-
choices=
|
| 612 |
-
value=
|
| 613 |
label="ํน์ํจ๊ณผ ๋ฐฐ๊ฒฝ ์ ํ",
|
| 614 |
visible=False,
|
| 615 |
interactive=True
|
|
@@ -617,14 +723,58 @@ def create_gradio_interface():
|
|
| 617 |
|
| 618 |
# ๋๋กญ๋ค์ด ๋ณ๊ฒฝ ํจ์
|
| 619 |
def update_dropdowns(bg_type):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
return {
|
| 621 |
-
simple_dropdown: gr.update(
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
}
|
| 629 |
|
| 630 |
background_type.change(
|
|
@@ -645,7 +795,7 @@ def create_gradio_interface():
|
|
| 645 |
label="ํ์ง ๋ ๋ฒจ",
|
| 646 |
choices=["gpt", "flux"],
|
| 647 |
value="flux",
|
| 648 |
-
info="GPT: GPT ๋ชจ๋ธ (๊ณ ํ์ง),
|
| 649 |
)
|
| 650 |
|
| 651 |
aspect_ratio = gr.Dropdown(
|
|
@@ -693,8 +843,8 @@ def create_gradio_interface():
|
|
| 693 |
|
| 694 |
# ํ๋กฌํํธ๋ง ์์ฑํ๋ ํจ์ (๋น๋ฐ๋ฒํธ ์ฒดํฌ ํฌํจ)
|
| 695 |
def generate_prompt_with_password_check(password, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects, request_text, aspect_ratio):
|
| 696 |
-
# ๋น๋ฐ๋ฒํธ ํ์ธ
|
| 697 |
-
if password !=
|
| 698 |
return "๋น๋ฐ๋ฒํธ๊ฐ ํ๋ ธ์ต๋๋ค. ์ฌ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 699 |
|
| 700 |
# ๋ฐฐ๊ฒฝ ์ ํ
|
|
@@ -720,7 +870,7 @@ def create_gradio_interface():
|
|
| 720 |
|
| 721 |
# ๋น๋ฐ๋ฒํธ ํ์ธ ํจ์
|
| 722 |
def check_password(password, *args):
|
| 723 |
-
if password !=
|
| 724 |
return (
|
| 725 |
[], # original_output
|
| 726 |
None, # original_download
|
|
@@ -844,15 +994,30 @@ def create_gradio_interface():
|
|
| 844 |
# ์ฑ ์คํ
|
| 845 |
if __name__ == "__main__":
|
| 846 |
try:
|
| 847 |
-
logger.info("
|
| 848 |
|
| 849 |
# imgs ๋๋ ํ ๋ฆฌ ํ์ธ/์์ฑ
|
| 850 |
os.makedirs("imgs", exist_ok=True)
|
| 851 |
logger.info("์ด๋ฏธ์ง ๋๋ ํ ๋ฆฌ ์ค๋น ์๋ฃ")
|
| 852 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 853 |
app = create_gradio_interface()
|
| 854 |
-
logger.info("
|
|
|
|
| 855 |
app.launch(share=True)
|
|
|
|
| 856 |
except Exception as e:
|
| 857 |
-
logger.error(f"
|
| 858 |
logger.error(traceback.format_exc())
|
|
|
|
|
|
|
| 1 |
import sys
|
| 2 |
import base64
|
| 3 |
import io
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import traceback
|
| 7 |
import requests
|
| 8 |
+
import json
|
| 9 |
from PIL import Image
|
| 10 |
import gradio as gr
|
| 11 |
from openai import OpenAI
|
|
|
|
| 24 |
)
|
| 25 |
logger = logging.getLogger("image-enhancer-app")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# API ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (์์ ํ๊ฒ)
|
| 28 |
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))
|
| 29 |
|
|
|
|
| 40 |
logger.warning("GEMINI_API_KEY not found or empty, Gemini client not initialized")
|
| 41 |
gemini_client = None
|
| 42 |
|
| 43 |
+
# ์ค์ ๊ฐ๋ค์ ํ๊ฒฝ๋ณ์์์ ๋ก๋ํ๋ ํจ์๋ค
|
| 44 |
+
def get_app_password():
|
| 45 |
+
"""ํ๊ฒฝ๋ณ์์์ ์ฑ ๋น๋ฐ๋ฒํธ๋ฅผ ๊ฐ์ ธ์ต๋๋ค."""
|
| 46 |
+
password = os.environ.get("APP_PASSWORD")
|
| 47 |
+
if password:
|
| 48 |
+
logger.info("App password loaded from environment variable")
|
| 49 |
+
return password
|
| 50 |
+
else:
|
| 51 |
+
logger.warning("APP_PASSWORD environment variable not found, using default password")
|
| 52 |
+
return "1089" # ๊ธฐ๋ณธ ๋น๋ฐ๋ฒํธ
|
| 53 |
+
|
| 54 |
+
# ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ๋ฅผ ํ๊ฒฝ๋ณ์์์ ๋ก๋ํ๋ ํจ์
|
| 55 |
+
def load_backgrounds_from_env():
|
| 56 |
+
"""ํ๊ฒฝ๋ณ์์์ ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํฉ๋๋ค."""
|
| 57 |
+
try:
|
| 58 |
+
# ํ๊ฒฝ๋ณ์์์ JSON ๋ฌธ์์ด๋ก ์ ์ฅ๋ ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ๋ก๋
|
| 59 |
+
backgrounds_data = os.environ.get("BACKGROUNDS_DATA")
|
| 60 |
+
if backgrounds_data:
|
| 61 |
+
logger.info("Loading backgrounds data from environment variable")
|
| 62 |
+
return json.loads(backgrounds_data)
|
| 63 |
+
else:
|
| 64 |
+
logger.warning("BACKGROUNDS_DATA environment variable not found, using default backgrounds")
|
| 65 |
+
return get_default_backgrounds()
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logger.error(f"Error loading backgrounds from environment: {e}")
|
| 68 |
+
logger.info("Falling back to default backgrounds")
|
| 69 |
+
return get_default_backgrounds()
|
| 70 |
+
|
| 71 |
+
def get_default_backgrounds():
|
| 72 |
+
"""๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํฉ๋๋ค."""
|
| 73 |
+
return {
|
| 74 |
+
"SIMPLE_BACKGROUNDS": {
|
| 75 |
+
"ํ์ดํธ ๊ธฐ๋ณธ": "A clean, minimalistic digital background scene featuring a seamless pure white backdrop and a smooth white surface. The lighting is bright and evenly diffused from above, eliminating harsh shadows and creating a crisp, studio-lit environment. There are no products or objects present โ only the empty background remains. The composition is ultra-clean and modern, ideal for overlaying lifestyle or hydration-related products. The mood is fresh, light, and professional. Ultra-sharp, studio-quality, high resolution, photo-realistic",
|
| 76 |
+
"ํ์ ํฌํค": "A clean and minimal product photography background featuring a seamless, near-white light gray tone across both the backdrop and surface. The entire scene is evenly lit with bright, diffused lighting from above, eliminating harsh shadows and producing a calm, sophisticated atmosphere. The composition remains empty โ with no products or objects present โ offering a soft and airy visual ideal for modern lifestyle, wellness, or eco-friendly branding. The lighting gently enhances spatial depth without creating reflections. Ultra-clear, studio-quality, photo-realistic"
|
| 77 |
+
},
|
| 78 |
+
"STUDIO_BACKGROUNDS": {
|
| 79 |
+
"์ฐ๋
น์ ์ฅ๋ฏธ ์ ์": "A high-resolution, commercial-style digital photograph of a sophisticated background setup designed for a luxury product shoot. The scene features soft green tones with lush fern leaves and creamy pastel roses (white, peach, blush pink) artfully arranged. The flowers are placed on a white polished marble platform with soft veining, surrounded by elegant foliage. The lighting is natural and diffused from the top left, producing soft shadows and a serene mood. There is no product in the scene โ only the floral arrangement and surface remain, creating a refined, editorial-quality backdrop suitable for overlaying product photography. No object should be centered; the composition should remain balanced and inviting. ultra-detailed, 8K quality, photo-realistic studio setting."
|
| 80 |
+
},
|
| 81 |
+
"NATURE_BACKGROUNDS": {
|
| 82 |
+
"์์ ํ๋๊ฐ ์๋ ํด๋ณ": "A serene and natural beach-themed product photography scene. The background features warm golden sand and gentle turquoise waves softly rolling onto the shore, with white foam catching the golden light of a late afternoon sun. Around the edges of the frame โ but not overlapping the center foreground โ small natural elements like seashells, conch shells, and bits of coral are randomly scattered with varied sizes, positions, and quantities to evoke a natural, organic arrangement. The product area remains clean and visually unobstructed to maintain focus. Lighting is soft and ambient with subtle natural shadows to highlight the uploaded product. The overall atmosphere is peaceful, sunlit, and summer-inspired. Ultra photo-realistic, studio-quality"
|
| 83 |
+
},
|
| 84 |
+
"INDOOR_BACKGROUNDS": {
|
| 85 |
+
"๊ธฐ๋ณธ ์ฑ
์": "A bright, Scandinavian-inspired home office featuring a matte white desk with crisp edges placed against a soft sage green wall. Natural daylight gently enters from the side, casting diffused shadows across the surface and enhancing the calm, focused atmosphere. On the desk are neatly arranged everyday essentials like a few colorful pens, a potted plant, and an open notebook. A neutral-toned ergonomic chair is tucked under the desk, and the surrounding area is intentionally minimal to emphasize clarity and productivity. The entire setting evokes a modern, clutter-free work environment., photo-realistic, high-resolution."
|
| 86 |
+
},
|
| 87 |
+
"SPECIAL_BACKGROUNDS": {
|
| 88 |
+
"๋ค์ด๋น ๋นํฐ์ง ํ๋ก๋ด ๋ฒฝ์ง": "A richly detailed studio photography background featuring an ornate, vintage-inspired floral wallpaper design. The backdrop showcases vibrant red, pink, yellow, and green botanical motifs intricately woven across a deep navy blue base. The floral pattern is symmetrical and bold, giving the composition an artistic, maximalist aesthetic. The floor is a clean, solid blue surface, providing visual contrast and modern balance. Lighting is bright and evenly diffused, emphasizing both the product and the intricate details of the wallpaper. Ideal for showcasing creative, lifestyle, or design-forward products. Ultra-detailed, high-resolution, ."
|
| 89 |
+
},
|
| 90 |
+
"JEWELRY_BACKGROUNDS": {
|
| 91 |
+
"ํ์ดํธ ๋ฏธ๋ฌ ์คํ ๋ผ์ดํธ": "A luxury jewelry product photoshoot featuring a soft white backdrop and a polished white mirrored surface. A single focused overhead light beam softly illuminates the center of the scene, fading smoothly toward the edges with a clean gradient. The glossy white surface reflects the jewelry item with crystal clarity โ capturing the shape, facets, and symmetry of the design in perfect mirror-like detail. The reflection appears sharp, clean, and vertically aligned beneath the product, enhancing the sense of luxury and balance. The atmosphere is premium, minimal, and luminous โ ideal for high-end diamond earrings or bridal jewelry. Ultra-detailed, studio-quality, photo-realistic"
|
| 92 |
+
},
|
| 93 |
+
"SPECIAL_EFFECTS_BACKGROUNDS": {
|
| 94 |
+
"๋ธ๋ฃจ๋ธ๋ ํฐ ๋ฌผ๋ฐฉ์ธ ํจ๊ณผ": "A deep black and vivid cobalt blue gradient backdrop with a reflective surface splashed by crystalline water droplets frozen mid-air. Backlighting enhances shimmer and motion, creating a sense of waterproof resilience. High-impact, ultra-detailed."
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
# ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ๋ก๋
|
| 99 |
+
backgrounds_data = load_backgrounds_from_env()
|
| 100 |
+
SIMPLE_BACKGROUNDS = backgrounds_data.get("SIMPLE_BACKGROUNDS", {})
|
| 101 |
+
STUDIO_BACKGROUNDS = backgrounds_data.get("STUDIO_BACKGROUNDS", {})
|
| 102 |
+
NATURE_BACKGROUNDS = backgrounds_data.get("NATURE_BACKGROUNDS", {})
|
| 103 |
+
INDOOR_BACKGROUNDS = backgrounds_data.get("INDOOR_BACKGROUNDS", {})
|
| 104 |
+
SPECIAL_BACKGROUNDS = backgrounds_data.get("SPECIAL_BACKGROUNDS", {})
|
| 105 |
+
JEWELRY_BACKGROUNDS = backgrounds_data.get("JEWELRY_BACKGROUNDS", {})
|
| 106 |
+
SPECIAL_EFFECTS_BACKGROUNDS = backgrounds_data.get("SPECIAL_EFFECTS_BACKGROUNDS", {})
|
| 107 |
+
|
| 108 |
+
# ์ฑ ๋น๋ฐ๋ฒํธ ๋ก๋
|
| 109 |
+
APP_PASSWORD = get_app_password()
|
| 110 |
+
|
| 111 |
+
# ํ๊ฒฝ๋ณ์ ๊ฒ์ฆ ํจ์
|
| 112 |
+
def validate_environment_variables():
|
| 113 |
+
"""ํ์ ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์๋์ง ํ์ธํฉ๋๋ค."""
|
| 114 |
+
logger.info("=== ํ๊ฒฝ๋ณ์ ๊ฒ์ฆ ์์ ===")
|
| 115 |
+
|
| 116 |
+
# ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ
|
| 117 |
+
logger.info(f"APP_PASSWORD: {'์ค์ ๋จ' if os.environ.get('APP_PASSWORD') else '๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ (1089)'}")
|
| 118 |
+
|
| 119 |
+
# API ํค ๊ฒ์ฆ
|
| 120 |
+
openai_key = os.environ.get("OPENAI_API_KEY")
|
| 121 |
+
replicate_token = os.environ.get("REPLICATE_API_TOKEN")
|
| 122 |
+
gemini_key = os.environ.get("GEMINI_API_KEY")
|
| 123 |
+
|
| 124 |
+
logger.info(f"OPENAI_API_KEY: {'์ค์ ๋จ' if openai_key else '์ค์ ๋์ง ์์'}")
|
| 125 |
+
logger.info(f"REPLICATE_API_TOKEN: {'์ค์ ๋จ' if replicate_token else '์ค์ ๋์ง ์์'}")
|
| 126 |
+
logger.info(f"GEMINI_API_KEY: {'์ค์ ๋จ' if gemini_key else '์ค์ ๋์ง ์์'}")
|
| 127 |
+
|
| 128 |
+
# ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ๊ฒ์ฆ
|
| 129 |
+
backgrounds_data_env = os.environ.get("BACKGROUNDS_DATA")
|
| 130 |
+
if backgrounds_data_env:
|
| 131 |
+
logger.info("BACKGROUNDS_DATA: ํ๊ฒฝ๋ณ์์์ ์ค์ ๋จ")
|
| 132 |
+
try:
|
| 133 |
+
parsed_data = json.loads(backgrounds_data_env)
|
| 134 |
+
bg_counts = {
|
| 135 |
+
category: len(backgrounds)
|
| 136 |
+
for category, backgrounds in parsed_data.items()
|
| 137 |
+
}
|
| 138 |
+
logger.info(f"๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ๋ก๋ ์ฑ๊ณต: {bg_counts}")
|
| 139 |
+
|
| 140 |
+
# ๊ฐ ์นดํ
๊ณ ๋ฆฌ๋ณ ์์ธ ์ ๋ณด
|
| 141 |
+
for category, backgrounds in parsed_data.items():
|
| 142 |
+
if backgrounds:
|
| 143 |
+
sample_keys = list(backgrounds.keys())[:3] # ์ฒ์ 3๊ฐ๋ง ํ์
|
| 144 |
+
logger.info(f" {category}: {len(backgrounds)}๊ฐ - ์์: {sample_keys}")
|
| 145 |
+
else:
|
| 146 |
+
logger.warning(f" {category}: ๋น์ด์์")
|
| 147 |
+
|
| 148 |
+
except Exception as e:
|
| 149 |
+
logger.error(f"BACKGROUNDS_DATA JSON ํ์ฑ ์ค๋ฅ: {e}")
|
| 150 |
+
logger.error("๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ํ์์ด ์ฌ๋ฐ๋ฅธ์ง ํ์ธํด์ฃผ์ธ์.")
|
| 151 |
+
else:
|
| 152 |
+
logger.info("BACKGROUNDS_DATA: ํ๊ฒฝ๋ณ์ ์์ (๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ)")
|
| 153 |
+
|
| 154 |
+
# ์ค์ ๋ก๋๋ ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ํ์ธ
|
| 155 |
+
actual_bg_counts = {
|
| 156 |
+
"SIMPLE_BACKGROUNDS": len(SIMPLE_BACKGROUNDS),
|
| 157 |
+
"STUDIO_BACKGROUNDS": len(STUDIO_BACKGROUNDS),
|
| 158 |
+
"NATURE_BACKGROUNDS": len(NATURE_BACKGROUNDS),
|
| 159 |
+
"INDOOR_BACKGROUNDS": len(INDOOR_BACKGROUNDS),
|
| 160 |
+
"SPECIAL_BACKGROUNDS": len(SPECIAL_BACKGROUNDS),
|
| 161 |
+
"JEWELRY_BACKGROUNDS": len(JEWELRY_BACKGROUNDS),
|
| 162 |
+
"SPECIAL_EFFECTS_BACKGROUNDS": len(SPECIAL_EFFECTS_BACKGROUNDS)
|
| 163 |
+
}
|
| 164 |
+
logger.info(f"์ค์ ๋ก๋๋ ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ: {actual_bg_counts}")
|
| 165 |
+
|
| 166 |
+
# ๊ฒฝ๊ณ ๋ฉ์์ง
|
| 167 |
+
missing_apis = []
|
| 168 |
+
if not openai_key:
|
| 169 |
+
missing_apis.append("OpenAI (GPT ๋ชจ๋ธ ์ฌ์ฉ ๋ถ๊ฐ)")
|
| 170 |
+
if not replicate_token:
|
| 171 |
+
missing_apis.append("Replicate (Flux ๋ชจ๋ธ ๋ฐ ํ์ง๊ฐ์ ์ฌ์ฉ ๋ถ๊ฐ)")
|
| 172 |
+
if not gemini_key:
|
| 173 |
+
missing_apis.append("Gemini (๋ฒ์ญ ๊ธฐ๋ฅ ์ฌ์ฉ ๋ถ๊ฐ)")
|
| 174 |
+
|
| 175 |
+
if missing_apis:
|
| 176 |
+
logger.warning(f"๋๋ฝ๋ API ํค๋ก ์ธํด ๋ค์ ๊ธฐ๋ฅ์ด ์ ํ๋ฉ๋๋ค: {', '.join(missing_apis)}")
|
| 177 |
+
else:
|
| 178 |
+
logger.info("๋ชจ๋ API ํค๊ฐ ์ค์ ๋์ด ์ ์ฒด ๊ธฐ๋ฅ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.")
|
| 179 |
+
|
| 180 |
+
logger.info("=== ํ๊ฒฝ๋ณ์ ๊ฒ์ฆ ์๋ฃ ===")
|
| 181 |
+
|
| 182 |
+
# ํ๊ฒฝ๋ณ์ ๊ฒ์ฆ ์คํ
|
| 183 |
+
validate_environment_variables()
|
| 184 |
+
|
| 185 |
# ์์ ํ์ผ ์ ์ฅ ํจ์
|
| 186 |
def save_uploaded_file(uploaded_file, suffix='.png'):
|
| 187 |
try:
|
|
|
|
| 224 |
logger.error(traceback.format_exc())
|
| 225 |
return None
|
| 226 |
|
| 227 |
+
# ํ
์คํธ ๋ฒ์ญ ํจ์ (ํ๊ตญ์ด โ ์์ด) - Gemini 2.0 Flash ์ ์ฉ
|
| 228 |
def translate_to_english(text):
|
| 229 |
+
"""ํ๊ตญ์ด ํ
์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ (Gemini 2.0 Flash ์ฌ์ฉ)"""
|
| 230 |
try:
|
| 231 |
if not text or not text.strip():
|
| 232 |
return ""
|
|
|
|
| 236 |
logger.warning("Gemini client not available, returning original text")
|
| 237 |
return text
|
| 238 |
|
| 239 |
+
# Gemini 2.0 Flash๋ฅผ ์ฌ์ฉํ ๋ฒ์ญ
|
| 240 |
try:
|
| 241 |
response = gemini_client.models.generate_content(
|
| 242 |
+
model="gemini-2.0-flash",
|
| 243 |
config=types.GenerateContentConfig(
|
| 244 |
system_instruction="You are a professional translator. Translate the given Korean text to English. Keep the translation natural and contextually appropriate for image generation prompts. If the text is already in English, return it as is. Only return the translated text without any additional explanation.",
|
| 245 |
max_output_tokens=200,
|
|
|
|
| 249 |
)
|
| 250 |
|
| 251 |
translated = response.text.strip()
|
| 252 |
+
logger.info(f"Translated '{text}' to '{translated}' using Gemini 2.0 Flash")
|
| 253 |
return translated
|
| 254 |
|
| 255 |
except Exception as e:
|
| 256 |
+
logger.error(f"Gemini translation error: {e}")
|
| 257 |
logger.warning("Translation failed, returning original text")
|
| 258 |
return text
|
| 259 |
|
|
|
|
| 294 |
|
| 295 |
# ์ฌ์ฉ์ ์์ฒญ์ฌํญ ์ฒ๋ฆฌ
|
| 296 |
if user_request and user_request.strip():
|
| 297 |
+
# ํ๊ตญ์ด ์์ฒญ์ฌํญ์ ์์ด๋ก ๋ฒ์ญ (Gemini 2.0 Flash ์ฌ์ฉ)
|
| 298 |
translated_request = translate_to_english(user_request)
|
| 299 |
|
| 300 |
# ๋ฒ์ญ๋ ์์ฒญ์ฌํญ์ ๋ฐฐ๊ฒฝ ํ๋กฌํํธ์ ํตํฉ
|
|
|
|
| 383 |
|
| 384 |
params = {
|
| 385 |
"prompt": prompt,
|
| 386 |
+
"model": "gpt-image-1",
|
| 387 |
"n": 1,
|
| 388 |
"size": size,
|
| 389 |
"image": processed_image
|
| 390 |
}
|
| 391 |
|
| 392 |
+
logger.info(f"Calling OpenAI API for image editing")
|
| 393 |
response = openai_client.images.edit(**params)
|
| 394 |
logger.info("OpenAI API call successful")
|
| 395 |
|
|
|
|
| 415 |
|
| 416 |
edited_images.append(image)
|
| 417 |
|
| 418 |
+
usage_info = "์ด๋ฏธ์ง ํธ์ง ์๋ฃ (GPT ๋ชจ๋ธ ์ฌ์ฉ)"
|
| 419 |
|
| 420 |
else: # quality_level == "flux"
|
| 421 |
# Flux ๋ชจ๋ธ ์ฌ์ฉ (ํญ์ ๊ธฐ๋ณธ ํ์ง๊ฐ์ 1ํ ์ ์ฉ)
|
|
|
|
| 423 |
logger.error("Replicate API token is not set")
|
| 424 |
return None, None, None, "Replicate API ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค. API ํ ํฐ์ ์ค์ ํด์ฃผ์ธ์."
|
| 425 |
|
| 426 |
+
logger.info(f"Using Flux model for image editing")
|
| 427 |
|
| 428 |
# Flux ๋ชจ๋ธ๋ก ์ด๋ฏธ์ง ์์ฑ
|
| 429 |
output = replicate.run(
|
| 430 |
+
"black-forest-labs/flux-kontext-pro",
|
| 431 |
input={
|
| 432 |
"prompt": prompt,
|
| 433 |
"input_image": processed_image,
|
|
|
|
| 462 |
|
| 463 |
# Flux ๋ชจ๋ธ์ ํญ์ ์ฒซ ๋ฒ์งธ ํ์ง ๊ฐ์ ์ ์๋ ์ ์ฉ
|
| 464 |
try:
|
| 465 |
+
logger.info("Applying automatic first enhancement for Flux model")
|
| 466 |
|
| 467 |
# ์์ ํ์ผ๋ก ์ ์ฅ
|
| 468 |
temp_flux_path = tempfile.mktemp(suffix='.png')
|
|
|
|
| 471 |
|
| 472 |
# ์ฒซ ๋ฒ์งธ ํ์ง ํฅ์ (Flux ๋ชจ๋ธ ๊ธฐ๋ณธ ์ ์ฉ)
|
| 473 |
first_enhanced_output = replicate.run(
|
| 474 |
+
"philz1337x/clarity-upscaler:dfad41707589d68ecdccd1dfa600d55a208f9310748e44bfe35b4a6291453d5e",
|
| 475 |
input={
|
| 476 |
"image": open(temp_flux_path, "rb"),
|
| 477 |
"scale_factor": 2,
|
|
|
|
| 497 |
first_enhanced_image = background
|
| 498 |
|
| 499 |
edited_images.append(first_enhanced_image)
|
| 500 |
+
usage_info = "์ด๋ฏธ์ง ํธ์ง ์๋ฃ (Flux ๋ชจ๋ธ + ๊ธฐ๋ณธ ํ์ง๊ฐ์ ์ ์ฉ)"
|
| 501 |
logger.info("First enhancement completed for Flux model")
|
| 502 |
else:
|
| 503 |
# ์ฒซ ๋ฒ์งธ ํ์ง๊ฐ์ ์คํจ ์ ์๋ณธ ์ฌ์ฉ
|
| 504 |
edited_images.append(flux_image)
|
| 505 |
+
usage_info = "์ด๋ฏธ์ง ํธ์ง ์๋ฃ (Flux ๋ชจ๋ธ ์ฌ์ฉ, ๊ธฐ๋ณธ ํ์ง๊ฐ์ ์คํจ)"
|
| 506 |
else:
|
| 507 |
# ์ฒซ ๋ฒ์งธ ํ์ง๊ฐ์ ์คํจ ์ ์๋ณธ ์ฌ์ฉ
|
| 508 |
edited_images.append(flux_image)
|
| 509 |
+
usage_info = "์ด๋ฏธ์ง ํธ์ง ์๋ฃ (Flux ๋ชจ๋ธ ์ฌ์ฉ, ๊ธฐ๋ณธ ํ์ง๊ฐ์ ์คํจ)"
|
| 510 |
|
| 511 |
except Exception as e:
|
| 512 |
logger.error(f"Error in first enhancement for Flux: {e}")
|
| 513 |
# ์ฒซ ๋ฒ์งธ ํ์ง๊ฐ์ ์คํจ ์ ์๋ณธ ์ฌ์ฉ
|
| 514 |
edited_images.append(flux_image)
|
| 515 |
+
usage_info = f"์ด๋ฏธ์ง ํธ์ง ์๋ฃ (Flux ๋ชจ๋ธ ์ฌ์ฉ, ๊ธฐ๋ณธ ํ์ง๊ฐ์ ์ค๋ฅ: {str(e)})"
|
| 516 |
|
| 517 |
else:
|
| 518 |
logger.error("No output from Flux API")
|
|
|
|
| 539 |
try:
|
| 540 |
if quality_level == "gpt":
|
| 541 |
# GPT ๋ชจ๋ธ: ์ผ๋ฐ์ ์ธ ํ์ง ๊ฐ์
|
| 542 |
+
logger.info(f"Enhancing GPT image with Replicate API, enhancement level: {enhancement_level}")
|
| 543 |
enhancement_info = "ํ์ง ๊ฐ์ "
|
| 544 |
else:
|
| 545 |
# Flux ๋ชจ๋ธ: 2์ฐจ ํ์ง ๊ฐ์ (์ด๋ฏธ 1์ฐจ๋ ์ ์ฉ๋จ)
|
| 546 |
+
logger.info(f"Applying second enhancement for Flux image, enhancement level: {enhancement_level}")
|
| 547 |
enhancement_info = "2์ฐจ ํ์ง ๊ฐ์ "
|
| 548 |
|
| 549 |
if not os.environ.get("REPLICATE_API_TOKEN"):
|
|
|
|
| 557 |
|
| 558 |
# Replicate API๋ก ํ์ง ํฅ์
|
| 559 |
output = replicate.run(
|
| 560 |
+
"philz1337x/clarity-upscaler:dfad41707589d68ecdccd1dfa600d55a208f9310748e44bfe35b4a6291453d5e",
|
| 561 |
input={
|
| 562 |
"image": open(temp_image_path, "rb"),
|
| 563 |
"scale_factor": enhancement_level,
|
|
|
|
| 569 |
}
|
| 570 |
)
|
| 571 |
|
| 572 |
+
logger.info(f"Replicate API response: {output}")
|
| 573 |
|
| 574 |
if output and isinstance(output, list) and len(output) > 0:
|
| 575 |
enhanced_url = output[0]
|
|
|
|
| 583 |
enhanced_image = background
|
| 584 |
|
| 585 |
if quality_level == "gpt":
|
| 586 |
+
usage_info += f" | {enhancement_info} ์๋ฃ: Replicate Clarity Upscaler ์ฌ์ฉ"
|
| 587 |
else:
|
| 588 |
usage_info += f" | {enhancement_info} ์๋ฃ: ์ด 2ํ ํ์ง๊ฐ์ ์ ์ฉ"
|
| 589 |
else:
|
| 590 |
usage_info += f" | {enhancement_info} ์คํจ: ์ด๋ฏธ์ง ๋ค์ด๋ก๋ ์ค๋ฅ"
|
| 591 |
else:
|
| 592 |
+
usage_info += f" | {enhancement_info} ์คํจ: Replicate API ์๋ต ์์"
|
| 593 |
|
| 594 |
except Exception as e:
|
| 595 |
logger.error(f"Error enhancing image: {e}")
|
|
|
|
| 652 |
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
| 653 |
)
|
| 654 |
|
| 655 |
+
# ๋๋กญ๋ค์ด ์ปดํฌ๋ํธ๋ค - ํ๊ฒฝ๋ณ์์์ ๋ก๋๋ ๋ฐ์ดํฐ ์ฌ์ฉ
|
| 656 |
+
simple_choices = list(SIMPLE_BACKGROUNDS.keys()) if SIMPLE_BACKGROUNDS else []
|
| 657 |
+
studio_choices = list(STUDIO_BACKGROUNDS.keys()) if STUDIO_BACKGROUNDS else []
|
| 658 |
+
nature_choices = list(NATURE_BACKGROUNDS.keys()) if NATURE_BACKGROUNDS else []
|
| 659 |
+
indoor_choices = list(INDOOR_BACKGROUNDS.keys()) if INDOOR_BACKGROUNDS else []
|
| 660 |
+
special_choices = list(SPECIAL_BACKGROUNDS.keys()) if SPECIAL_BACKGROUNDS else []
|
| 661 |
+
jewelry_choices = list(JEWELRY_BACKGROUNDS.keys()) if JEWELRY_BACKGROUNDS else []
|
| 662 |
+
special_effects_choices = list(SPECIAL_EFFECTS_BACKGROUNDS.keys()) if SPECIAL_EFFECTS_BACKGROUNDS else []
|
| 663 |
+
|
| 664 |
+
# ํ๊ฒฝ๋ณ์๊ฐ ์์ด์ ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ๊ฐ ๋น์ด์๋ ๊ฒฝ์ฐ ๊ฒฝ๊ณ
|
| 665 |
+
if not any([simple_choices, studio_choices, nature_choices, indoor_choices, special_choices, jewelry_choices, special_effects_choices]):
|
| 666 |
+
logger.error("๋ชจ๋ ๋ฐฐ๊ฒฝ ์นดํ
๊ณ ๋ฆฌ๊ฐ ๋น์ด์์ต๋๋ค. BACKGROUNDS_DATA ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ ํด์ฃผ์ธ์.")
|
| 667 |
+
|
| 668 |
simple_dropdown = gr.Dropdown(
|
| 669 |
+
choices=simple_choices,
|
| 670 |
+
value=simple_choices[0] if simple_choices else None,
|
| 671 |
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
|
| 672 |
visible=True,
|
| 673 |
interactive=True
|
| 674 |
)
|
| 675 |
|
| 676 |
studio_dropdown = gr.Dropdown(
|
| 677 |
+
choices=studio_choices,
|
| 678 |
+
value=studio_choices[0] if studio_choices else None,
|
| 679 |
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
| 680 |
visible=False,
|
| 681 |
interactive=True
|
| 682 |
)
|
| 683 |
|
| 684 |
nature_dropdown = gr.Dropdown(
|
| 685 |
+
choices=nature_choices,
|
| 686 |
+
value=nature_choices[0] if nature_choices else None,
|
| 687 |
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
| 688 |
visible=False,
|
| 689 |
interactive=True
|
| 690 |
)
|
| 691 |
|
| 692 |
indoor_dropdown = gr.Dropdown(
|
| 693 |
+
choices=indoor_choices,
|
| 694 |
+
value=indoor_choices[0] if indoor_choices else None,
|
| 695 |
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
| 696 |
visible=False,
|
| 697 |
interactive=True
|
| 698 |
)
|
| 699 |
|
| 700 |
special_dropdown = gr.Dropdown(
|
| 701 |
+
choices=special_choices,
|
| 702 |
+
value=special_choices[0] if special_choices else None,
|
| 703 |
label="ํน์๋ฐฐ๊ฒฝ ์ ํ",
|
| 704 |
visible=False,
|
| 705 |
interactive=True
|
| 706 |
)
|
| 707 |
|
| 708 |
jewelry_dropdown = gr.Dropdown(
|
| 709 |
+
choices=jewelry_choices,
|
| 710 |
+
value=jewelry_choices[0] if jewelry_choices else None,
|
| 711 |
label="์ฃผ์ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์ ํ",
|
| 712 |
visible=False,
|
| 713 |
interactive=True
|
| 714 |
)
|
| 715 |
|
| 716 |
special_effects_dropdown = gr.Dropdown(
|
| 717 |
+
choices=special_effects_choices,
|
| 718 |
+
value=special_effects_choices[0] if special_effects_choices else None,
|
| 719 |
label="ํน์ํจ๊ณผ ๋ฐฐ๊ฒฝ ์ ํ",
|
| 720 |
visible=False,
|
| 721 |
interactive=True
|
|
|
|
| 723 |
|
| 724 |
# ๋๋กญ๋ค์ด ๋ณ๊ฒฝ ํจ์
|
| 725 |
def update_dropdowns(bg_type):
|
| 726 |
+
"""๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ผ ๋๋กญ๋ค์ด์ ์
๋ฐ์ดํธํฉ๋๋ค."""
|
| 727 |
+
logger.info(f"Updating dropdowns for background type: {bg_type}")
|
| 728 |
+
|
| 729 |
+
# ๊ฐ ๋ฐฐ๊ฒฝ ์นดํ
๊ณ ๋ฆฌ์ ํค ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
| 730 |
+
simple_choices = list(SIMPLE_BACKGROUNDS.keys()) if SIMPLE_BACKGROUNDS else []
|
| 731 |
+
studio_choices = list(STUDIO_BACKGROUNDS.keys()) if STUDIO_BACKGROUNDS else []
|
| 732 |
+
nature_choices = list(NATURE_BACKGROUNDS.keys()) if NATURE_BACKGROUNDS else []
|
| 733 |
+
indoor_choices = list(INDOOR_BACKGROUNDS.keys()) if INDOOR_BACKGROUNDS else []
|
| 734 |
+
special_choices = list(SPECIAL_BACKGROUNDS.keys()) if SPECIAL_BACKGROUNDS else []
|
| 735 |
+
jewelry_choices = list(JEWELRY_BACKGROUNDS.keys()) if JEWELRY_BACKGROUNDS else []
|
| 736 |
+
special_effects_choices = list(SPECIAL_EFFECTS_BACKGROUNDS.keys()) if SPECIAL_EFFECTS_BACKGROUNDS else []
|
| 737 |
+
|
| 738 |
+
# ๋๋ฒ๊น
์ ๋ณด ๋ก๊น
|
| 739 |
+
logger.info(f"Available choices - Simple: {len(simple_choices)}, Studio: {len(studio_choices)}, Nature: {len(nature_choices)}")
|
| 740 |
+
logger.info(f"Indoor: {len(indoor_choices)}, Special: {len(special_choices)}, Jewelry: {len(jewelry_choices)}, Effects: {len(special_effects_choices)}")
|
| 741 |
+
|
| 742 |
return {
|
| 743 |
+
simple_dropdown: gr.update(
|
| 744 |
+
visible=(bg_type == "์ฌํ ๋ฐฐ๊ฒฝ"),
|
| 745 |
+
choices=simple_choices,
|
| 746 |
+
value=simple_choices[0] if simple_choices else None
|
| 747 |
+
),
|
| 748 |
+
studio_dropdown: gr.update(
|
| 749 |
+
visible=(bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ"),
|
| 750 |
+
choices=studio_choices,
|
| 751 |
+
value=studio_choices[0] if studio_choices else None
|
| 752 |
+
),
|
| 753 |
+
nature_dropdown: gr.update(
|
| 754 |
+
visible=(bg_type == "์์ฐ ํ๊ฒฝ"),
|
| 755 |
+
choices=nature_choices,
|
| 756 |
+
value=nature_choices[0] if nature_choices else None
|
| 757 |
+
),
|
| 758 |
+
indoor_dropdown: gr.update(
|
| 759 |
+
visible=(bg_type == "์ค๋ด ํ๊ฒฝ"),
|
| 760 |
+
choices=indoor_choices,
|
| 761 |
+
value=indoor_choices[0] if indoor_choices else None
|
| 762 |
+
),
|
| 763 |
+
special_dropdown: gr.update(
|
| 764 |
+
visible=(bg_type == "ํน์๋ฐฐ๊ฒฝ"),
|
| 765 |
+
choices=special_choices,
|
| 766 |
+
value=special_choices[0] if special_choices else None
|
| 767 |
+
),
|
| 768 |
+
jewelry_dropdown: gr.update(
|
| 769 |
+
visible=(bg_type == "์ฃผ์ผ๋ฆฌ"),
|
| 770 |
+
choices=jewelry_choices,
|
| 771 |
+
value=jewelry_choices[0] if jewelry_choices else None
|
| 772 |
+
),
|
| 773 |
+
special_effects_dropdown: gr.update(
|
| 774 |
+
visible=(bg_type == "ํน์ํจ๊ณผ"),
|
| 775 |
+
choices=special_effects_choices,
|
| 776 |
+
value=special_effects_choices[0] if special_effects_choices else None
|
| 777 |
+
)
|
| 778 |
}
|
| 779 |
|
| 780 |
background_type.change(
|
|
|
|
| 795 |
label="ํ์ง ๋ ๋ฒจ",
|
| 796 |
choices=["gpt", "flux"],
|
| 797 |
value="flux",
|
| 798 |
+
info="GPT: GPT ๋ชจ๋ธ (๊ณ ํ์ง), ์ผ๋ฐ: Flux ๋ชจ๋ธ (๋น ๋ฅธ ์ฒ๋ฆฌ + ๊ธฐ๋ณธ ํ์ง๊ฐ์ )"
|
| 799 |
)
|
| 800 |
|
| 801 |
aspect_ratio = gr.Dropdown(
|
|
|
|
| 843 |
|
| 844 |
# ํ๋กฌํํธ๋ง ์์ฑํ๋ ํจ์ (๋น๋ฐ๋ฒํธ ์ฒดํฌ ํฌํจ)
|
| 845 |
def generate_prompt_with_password_check(password, bg_type, simple, studio, nature, indoor, special, jewelry, special_effects, request_text, aspect_ratio):
|
| 846 |
+
# ๋น๋ฐ๋ฒํธ ํ์ธ - ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์จ ๋น๋ฐ๋ฒํธ์ ๋น๊ต
|
| 847 |
+
if password != APP_PASSWORD:
|
| 848 |
return "๋น๋ฐ๋ฒํธ๊ฐ ํ๋ ธ์ต๋๋ค. ์ฌ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 849 |
|
| 850 |
# ๋ฐฐ๊ฒฝ ์ ํ
|
|
|
|
| 870 |
|
| 871 |
# ๋น๋ฐ๋ฒํธ ํ์ธ ํจ์
|
| 872 |
def check_password(password, *args):
|
| 873 |
+
if password != APP_PASSWORD:
|
| 874 |
return (
|
| 875 |
[], # original_output
|
| 876 |
None, # original_download
|
|
|
|
| 994 |
# ์ฑ ์คํ
|
| 995 |
if __name__ == "__main__":
|
| 996 |
try:
|
| 997 |
+
logger.info("=== AI ์ด๋ฏธ์ง ํธ์ง ๋ฐ ํ์ง ๊ฐ์ ์ ํ๋ฆฌ์ผ์ด์
์์ ===")
|
| 998 |
|
| 999 |
# imgs ๋๋ ํ ๋ฆฌ ํ์ธ/์์ฑ
|
| 1000 |
os.makedirs("imgs", exist_ok=True)
|
| 1001 |
logger.info("์ด๋ฏธ์ง ๋๋ ํ ๋ฆฌ ์ค๋น ์๋ฃ")
|
| 1002 |
|
| 1003 |
+
# ํ๊ฒฝ๋ณ์ ์ฌ๊ฒ์ฆ (์ฑ ์์ ์)
|
| 1004 |
+
logger.info("์ฑ ์์ ์ ํ๊ฒฝ๋ณ์ ์ต์ข
ํ์ธ")
|
| 1005 |
+
if not APP_PASSWORD:
|
| 1006 |
+
logger.warning("๋น๋ฐ๋ฒํธ๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ๊ธฐ๋ณธ ๋น๋ฐ๋ฒํธ(1089)๋ฅผ ์ฌ์ฉํฉ๋๋ค.")
|
| 1007 |
+
|
| 1008 |
+
if not any([
|
| 1009 |
+
os.environ.get("OPENAI_API_KEY"),
|
| 1010 |
+
os.environ.get("REPLICATE_API_TOKEN"),
|
| 1011 |
+
os.environ.get("GEMINI_API_KEY")
|
| 1012 |
+
]):
|
| 1013 |
+
logger.warning("API ํค๊ฐ ํ๋๋ ์ค์ ๋์ง ์์์ต๋๋ค. ๊ธฐ๋ฅ์ด ์ ํ๋ ์ ์์ต๋๋ค.")
|
| 1014 |
+
|
| 1015 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ ๋ฐ ์คํ
|
| 1016 |
app = create_gradio_interface()
|
| 1017 |
+
logger.info("Gradio ์ธํฐํ์ด์ค ์์ฑ ์๋ฃ")
|
| 1018 |
+
logger.info("์ ํ๋ฆฌ์ผ์ด์
์ ์์ํฉ๋๋ค...")
|
| 1019 |
app.launch(share=True)
|
| 1020 |
+
|
| 1021 |
except Exception as e:
|
| 1022 |
+
logger.error(f"์ ํ๋ฆฌ์ผ์ด์
์คํ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 1023 |
logger.error(traceback.format_exc())
|