ssboost commited on
Commit
f34d9b8
ยท
verified ยท
1 Parent(s): 1413c13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +267 -102
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=TRANSLATION_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 {TRANSLATION_MODEL}")
159
  return translated
160
 
161
  except Exception as e:
162
- logger.error(f"Translation error with {TRANSLATION_MODEL}: {e}")
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": IMAGE_EDIT_MODEL_GPT,
293
  "n": 1,
294
  "size": size,
295
  "image": processed_image
296
  }
297
 
298
- logger.info(f"Calling OpenAI API for image editing with model: {IMAGE_EDIT_MODEL_GPT}")
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 = f"์ด๋ฏธ์ง€ ํŽธ์ง‘ ์™„๋ฃŒ ({IMAGE_EDIT_MODEL_GPT} ๋ชจ๋ธ ์‚ฌ์šฉ)"
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: {IMAGE_EDIT_MODEL_FLUX}")
333
 
334
  # Flux ๋ชจ๋ธ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
335
  output = replicate.run(
336
- IMAGE_EDIT_MODEL_FLUX,
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(f"Applying automatic first enhancement for Flux model using: {ENHANCEMENT_MODEL}")
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
- ENHANCEMENT_MODEL,
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 = f"์ด๋ฏธ์ง€ ํŽธ์ง‘ ์™„๋ฃŒ ({IMAGE_EDIT_MODEL_FLUX} + ๊ธฐ๋ณธ ํ™”์งˆ๊ฐœ์„  ์ ์šฉ)"
407
  logger.info("First enhancement completed for Flux model")
408
  else:
409
  # ์ฒซ ๋ฒˆ์งธ ํ™”์งˆ๊ฐœ์„  ์‹คํŒจ ์‹œ ์›๋ณธ ์‚ฌ์šฉ
410
  edited_images.append(flux_image)
411
- usage_info = f"์ด๋ฏธ์ง€ ํŽธ์ง‘ ์™„๋ฃŒ ({IMAGE_EDIT_MODEL_FLUX} ์‚ฌ์šฉ, ๊ธฐ๋ณธ ํ™”์งˆ๊ฐœ์„  ์‹คํŒจ)"
412
  else:
413
  # ์ฒซ ๋ฒˆ์งธ ํ™”์งˆ๊ฐœ์„  ์‹คํŒจ ์‹œ ์›๋ณธ ์‚ฌ์šฉ
414
  edited_images.append(flux_image)
415
- usage_info = f"์ด๋ฏธ์ง€ ํŽธ์ง‘ ์™„๋ฃŒ ({IMAGE_EDIT_MODEL_FLUX} ์‚ฌ์šฉ, ๊ธฐ๋ณธ ํ™”์งˆ๊ฐœ์„  ์‹คํŒจ)"
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"์ด๋ฏธ์ง€ ํŽธ์ง‘ ์™„๋ฃŒ ({IMAGE_EDIT_MODEL_FLUX} ์‚ฌ์šฉ, ๊ธฐ๋ณธ ํ™”์งˆ๊ฐœ์„  ์˜ค๋ฅ˜: {str(e)})"
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 {ENHANCEMENT_MODEL}, enhancement level: {enhancement_level}")
449
  enhancement_info = "ํ™”์งˆ ๊ฐœ์„ "
450
  else:
451
  # Flux ๋ชจ๋ธ: 2์ฐจ ํ™”์งˆ ๊ฐœ์„  (์ด๋ฏธ 1์ฐจ๋Š” ์ ์šฉ๋จ)
452
- logger.info(f"Applying second enhancement for Flux image with {ENHANCEMENT_MODEL}, enhancement level: {enhancement_level}")
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
- ENHANCEMENT_MODEL,
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"Enhancement API response: {output}")
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} ์™„๋ฃŒ: {ENHANCEMENT_MODEL} ์‚ฌ์šฉ"
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} ์‹คํŒจ: Enhancement API ์‘๋‹ต ์—†์Œ"
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=list(SIMPLE_BACKGROUNDS.keys()),
564
- value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else None,
565
  label="์‹ฌํ”Œ ๋ฐฐ๊ฒฝ ์„ ํƒ",
566
  visible=True,
567
  interactive=True
568
  )
569
 
570
  studio_dropdown = gr.Dropdown(
571
- choices=list(STUDIO_BACKGROUNDS.keys()),
572
- value=list(STUDIO_BACKGROUNDS.keys())[0] if STUDIO_BACKGROUNDS else None,
573
  label="์ŠคํŠœ๋””์˜ค ๋ฐฐ๊ฒฝ ์„ ํƒ",
574
  visible=False,
575
  interactive=True
576
  )
577
 
578
  nature_dropdown = gr.Dropdown(
579
- choices=list(NATURE_BACKGROUNDS.keys()),
580
- value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else None,
581
  label="์ž์—ฐ ํ™˜๊ฒฝ ์„ ํƒ",
582
  visible=False,
583
  interactive=True
584
  )
585
 
586
  indoor_dropdown = gr.Dropdown(
587
- choices=list(INDOOR_BACKGROUNDS.keys()),
588
- value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None,
589
  label="์‹ค๋‚ด ํ™˜๊ฒฝ ์„ ํƒ",
590
  visible=False,
591
  interactive=True
592
  )
593
 
594
  special_dropdown = gr.Dropdown(
595
- choices=list(SPECIAL_BACKGROUNDS.keys()),
596
- value=list(SPECIAL_BACKGROUNDS.keys())[0] if SPECIAL_BACKGROUNDS else None,
597
  label="ํŠน์ˆ˜๋ฐฐ๊ฒฝ ์„ ํƒ",
598
  visible=False,
599
  interactive=True
600
  )
601
 
602
  jewelry_dropdown = gr.Dropdown(
603
- choices=list(JEWELRY_BACKGROUNDS.keys()),
604
- value=list(JEWELRY_BACKGROUNDS.keys())[0] if JEWELRY_BACKGROUNDS else None,
605
  label="์ฃผ์–ผ๋ฆฌ ๋ฐฐ๊ฒฝ ์„ ํƒ",
606
  visible=False,
607
  interactive=True
608
  )
609
 
610
  special_effects_dropdown = gr.Dropdown(
611
- choices=list(SPECIAL_EFFECTS_BACKGROUNDS.keys()),
612
- value=list(SPECIAL_EFFECTS_BACKGROUNDS.keys())[0] if SPECIAL_EFFECTS_BACKGROUNDS else None,
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(visible=(bg_type == "์‹ฌํ”Œ ๋ฐฐ๊ฒฝ")),
622
- studio_dropdown: gr.update(visible=(bg_type == "์ŠคํŠœ๋””์˜ค ๋ฐฐ๊ฒฝ")),
623
- nature_dropdown: gr.update(visible=(bg_type == "์ž์—ฐ ํ™˜๊ฒฝ")),
624
- indoor_dropdown: gr.update(visible=(bg_type == "์‹ค๋‚ด ํ™˜๊ฒฝ")),
625
- special_dropdown: gr.update(visible=(bg_type == "ํŠน์ˆ˜๋ฐฐ๊ฒฝ")),
626
- jewelry_dropdown: gr.update(visible=(bg_type == "์ฃผ์–ผ๋ฆฌ")),
627
- special_effects_dropdown: gr.update(visible=(bg_type == "ํŠน์ˆ˜ํšจ๊ณผ"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ๋ชจ๋ธ (๊ณ ํ’ˆ์งˆ), Flux: Flux ๋ชจ๋ธ (๋น ๋ฅธ ์ฒ˜๋ฆฌ + ๊ธฐ๋ณธ ํ™”์งˆ๊ฐœ์„ )"
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 != "1089":
698
  return "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
699
 
700
  # ๋ฐฐ๊ฒฝ ์„ ํƒ
@@ -720,7 +870,7 @@ def create_gradio_interface():
720
 
721
  # ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ํ•จ์ˆ˜
722
  def check_password(password, *args):
723
- if password != "1089":
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("Starting application")
848
 
849
  # imgs ๋””๋ ‰ํ† ๋ฆฌ ํ™•์ธ/์ƒ์„ฑ
850
  os.makedirs("imgs", exist_ok=True)
851
  logger.info("์ด๋ฏธ์ง€ ๋””๋ ‰ํ† ๋ฆฌ ์ค€๋น„ ์™„๋ฃŒ")
852
 
 
 
 
 
 
 
 
 
 
 
 
 
 
853
  app = create_gradio_interface()
854
- logger.info("Launching Gradio app")
 
855
  app.launch(share=True)
 
856
  except Exception as e:
857
- logger.error(f"Error running app: {e}")
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())