Kims12 commited on
Commit
a6f73ef
·
verified ·
1 Parent(s): 6571908

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -34,6 +34,7 @@ ABSTRACT_BACKGROUNDS = {}
34
 
35
  # ------------------- 배경 JSON 파일 로드 함수 -------------------
36
  def load_background_json(filename):
 
37
  file_path = os.path.join(BACKGROUNDS_DIR, filename)
38
  try:
39
  with open(file_path, 'r', encoding='utf-8') as f:
@@ -47,11 +48,13 @@ def load_background_json(filename):
47
  logger.warning(f"{filename} 파일 로드 중 오류 발생: {str(e)}.")
48
  return {}
49
 
50
- # ------------------- 배경 초기화 함수 -------------------
51
  def initialize_backgrounds():
 
52
  global SIMPLE_BACKGROUNDS, STUDIO_BACKGROUNDS, NATURE_BACKGROUNDS, INDOOR_BACKGROUNDS
53
  global TECHNOLOGY_BACKGROUNDS, COLORFUL_PATTERN_BACKGROUNDS, ABSTRACT_BACKGROUNDS
54
 
 
55
  SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json")
56
  STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json")
57
  NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json")
@@ -60,9 +63,9 @@ def initialize_backgrounds():
60
  COLORFUL_PATTERN_BACKGROUNDS = load_background_json("colorful_pattern_backgrounds.json")
61
  ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json")
62
 
63
- # 기본값 설정 (JSON 파일이 없거나 비어있는 경우)
64
  if not SIMPLE_BACKGROUNDS:
65
- SIMPLE_BACKGROUNDS = {"화이트 배경": "white background"}
66
  if not STUDIO_BACKGROUNDS:
67
  STUDIO_BACKGROUNDS = {"미니멀 플랫레이": "minimalist flat lay with clean white background"}
68
  if not NATURE_BACKGROUNDS:
@@ -74,11 +77,11 @@ def initialize_backgrounds():
74
  if not COLORFUL_PATTERN_BACKGROUNDS:
75
  COLORFUL_PATTERN_BACKGROUNDS = {"화려한 꽃 패턴": "vibrant floral pattern backdrop"}
76
  if not ABSTRACT_BACKGROUNDS:
77
- ABSTRACT_BACKGROUNDS = {"네온 조명": "neon lights abstract background"}
78
 
79
- logger.info("모든 배경 옵션 로드 완료")
80
 
81
- # ------------------- 기본 이미지 생성 관련 함수 -------------------
82
  def save_binary_file(file_name, data):
83
  with open(file_name, "wb") as f:
84
  f.write(data)
@@ -140,6 +143,7 @@ def preprocess_prompt(prompt, image1):
140
  prompt += " 이미지를 생성해주세요. 이미지에 텍스트나 글자를 포함하지 마세요."
141
  return prompt
142
 
 
143
  def generate_with_images(prompt, images, variation_index=0):
144
  try:
145
  api_key = os.environ.get("GEMINI_API_KEY")
@@ -228,7 +232,7 @@ def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3)
228
  time.sleep(1)
229
  return None, f"최대 재시도 횟수({max_retries}회) 초과 후 실패: {last_error}", prompt
230
 
231
- # ------------------- 프롬프트에서 오직 Midjourney 프롬프트 텍스트만 추출하는 함수 -------------------
232
  def filter_prompt_only(prompt):
233
  idx = prompt.find("Highly detailed commercial photography")
234
  if idx != -1:
@@ -240,7 +244,6 @@ def filter_prompt_only(prompt):
240
  return prompt_text
241
  return prompt.strip()
242
 
243
- # ------------------- 선택된 배경 정보 가져오기 함수 -------------------
244
  def get_selected_background_info(bg_type, simple, studio, nature, indoor, tech, colorful, abstract):
245
  """선택된 배경 정보를 가져오는 함수"""
246
  if bg_type == "심플 배경":
@@ -292,7 +295,7 @@ def get_selected_background_info(bg_type, simple, studio, nature, indoor, tech,
292
  "english": "white background"
293
  }
294
 
295
- # ------------------- 향상된 시스템 인스트럭션 생성 함수 -------------------
296
  def generate_enhanced_system_instruction():
297
  """향상된 시스템 인스트럭션 생성 함수"""
298
  return """당신은 상품 이미지의 배경을 변경하기 위한 최고 품질의 프롬프트를 생성하는 전문가입니다.
@@ -332,7 +335,6 @@ def generate_enhanced_system_instruction():
332
  11. 프롬프트 끝에 "--ar 1:1 --s 750 --q 2 --v 5.2" 파라미터를 추가하여 미드저니에서 고품질 정사각형 비율을 강제합니다.
333
  """
334
 
335
- # ------------------- 프롬프트 생성 함수 -------------------
336
  def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
337
  """향상된 프롬프트 생성 함수"""
338
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
@@ -557,8 +559,9 @@ def create_app():
557
 
558
  # ------------------- 메인 실행 함수 -------------------
559
  if __name__ == "__main__":
560
- # 배경 옵션 초기화
561
  initialize_backgrounds()
 
562
  # 앱 생성 및 실행
563
  app = create_app()
564
  app.queue()
 
34
 
35
  # ------------------- 배경 JSON 파일 로드 함수 -------------------
36
  def load_background_json(filename):
37
+ """배경 JSON 파일 로드 함수"""
38
  file_path = os.path.join(BACKGROUNDS_DIR, filename)
39
  try:
40
  with open(file_path, 'r', encoding='utf-8') as f:
 
48
  logger.warning(f"{filename} 파일 로드 중 오류 발생: {str(e)}.")
49
  return {}
50
 
51
+ # ------------------- 배경 옵션 초기화 함수 -------------------
52
  def initialize_backgrounds():
53
+ """모든 배경 옵션 초기화 함수"""
54
  global SIMPLE_BACKGROUNDS, STUDIO_BACKGROUNDS, NATURE_BACKGROUNDS, INDOOR_BACKGROUNDS
55
  global TECHNOLOGY_BACKGROUNDS, COLORFUL_PATTERN_BACKGROUNDS, ABSTRACT_BACKGROUNDS
56
 
57
+ # 각 배경 파일 로드
58
  SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json")
59
  STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json")
60
  NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json")
 
63
  COLORFUL_PATTERN_BACKGROUNDS = load_background_json("colorful_pattern_backgrounds.json")
64
  ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json")
65
 
66
+ # 기본값 설정 (파일이 없거나 비어있는 경우)
67
  if not SIMPLE_BACKGROUNDS:
68
+ SIMPLE_BACKGROUNDS = {"클래식 화이트": "clean white background with soft even lighting"}
69
  if not STUDIO_BACKGROUNDS:
70
  STUDIO_BACKGROUNDS = {"미니멀 플랫레이": "minimalist flat lay with clean white background"}
71
  if not NATURE_BACKGROUNDS:
 
77
  if not COLORFUL_PATTERN_BACKGROUNDS:
78
  COLORFUL_PATTERN_BACKGROUNDS = {"화려한 꽃 패턴": "vibrant floral pattern backdrop"}
79
  if not ABSTRACT_BACKGROUNDS:
80
+ ABSTRACT_BACKGROUNDS = {"네온 라이트": "neon light abstract background with vibrant glowing elements"}
81
 
82
+ logger.info("모든 배경 옵션 초기화 완료")
83
 
84
+ # ------------------- 기본 유틸리티 함수 -------------------
85
  def save_binary_file(file_name, data):
86
  with open(file_name, "wb") as f:
87
  f.write(data)
 
143
  prompt += " 이미지를 생성해주세요. 이미지에 텍스트나 글자를 포함하지 마세요."
144
  return prompt
145
 
146
+ # ------------------- 이미지 생성 함수 -------------------
147
  def generate_with_images(prompt, images, variation_index=0):
148
  try:
149
  api_key = os.environ.get("GEMINI_API_KEY")
 
232
  time.sleep(1)
233
  return None, f"최대 재시도 횟수({max_retries}회) 초과 후 실패: {last_error}", prompt
234
 
235
+ # ------------------- 프롬프트 관련 함수 -------------------
236
  def filter_prompt_only(prompt):
237
  idx = prompt.find("Highly detailed commercial photography")
238
  if idx != -1:
 
244
  return prompt_text
245
  return prompt.strip()
246
 
 
247
  def get_selected_background_info(bg_type, simple, studio, nature, indoor, tech, colorful, abstract):
248
  """선택된 배경 정보를 가져오는 함수"""
249
  if bg_type == "심플 배경":
 
295
  "english": "white background"
296
  }
297
 
298
+ # ------------------- 프롬프트 생성 함수 -------------------
299
  def generate_enhanced_system_instruction():
300
  """향상된 시스템 인스트럭션 생성 함수"""
301
  return """당신은 상품 이미지의 배경을 변경하기 위한 최고 품질의 프롬프트를 생성하는 전문가입니다.
 
335
  11. 프롬프트 끝에 "--ar 1:1 --s 750 --q 2 --v 5.2" 파라미터를 추가하여 미드저니에서 고품질 정사각형 비율을 강제합니다.
336
  """
337
 
 
338
  def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
339
  """향상된 프롬프트 생성 함수"""
340
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
 
559
 
560
  # ------------------- 메인 실행 함수 -------------------
561
  if __name__ == "__main__":
562
+ # 배경 옵션 초기화 - JSON 파일에서 로드
563
  initialize_backgrounds()
564
+
565
  # 앱 생성 및 실행
566
  app = create_app()
567
  app.queue()