Update app.py
Browse files
app.py
CHANGED
@@ -41,6 +41,7 @@ class GradioClientController:
|
|
41 |
def __init__(self):
|
42 |
self.client = None
|
43 |
self.api_endpoint = None
|
|
|
44 |
self._initialize_client()
|
45 |
|
46 |
def _initialize_client(self):
|
@@ -57,6 +58,8 @@ class GradioClientController:
|
|
57 |
try:
|
58 |
self.client = Client(self.api_endpoint)
|
59 |
logger.info("API 클라이언트가 성공적으로 초기화되었습니다.")
|
|
|
|
|
60 |
return
|
61 |
except Exception as e:
|
62 |
if attempt < max_retries - 1:
|
@@ -69,6 +72,22 @@ class GradioClientController:
|
|
69 |
logger.error(f"클라이언트 초기화 실패: {str(e)}")
|
70 |
self.client = None
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
def _ensure_client(self) -> bool:
|
73 |
"""클라이언트 연결 상태 확인 및 재연결"""
|
74 |
if self.client is None:
|
@@ -81,14 +100,16 @@ class GradioClientController:
|
|
81 |
try:
|
82 |
if not self._ensure_client():
|
83 |
logger.error("클라이언트 연결 실패")
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
92 |
|
93 |
# 결과를 Gradio 업데이트 형식으로 변환
|
94 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
@@ -110,6 +131,26 @@ class GradioClientController:
|
|
110 |
|
111 |
except Exception as e:
|
112 |
logger.error(f"드롭다운 업데이트 오류: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
return tuple([gr.update() for _ in range(7)])
|
114 |
|
115 |
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
|
@@ -146,7 +187,7 @@ class GradioClientController:
|
|
146 |
nature: str, indoor: str, special: str, jewelry: str, special_effects: str,
|
147 |
request_text: str, quality_level: str, aspect_ratio: str,
|
148 |
output_format: str, enable_enhancement: bool) -> Tuple:
|
149 |
-
"""이미지 처리 (편집 및 화질 개선)"""
|
150 |
temp_path = None
|
151 |
try:
|
152 |
if not self._ensure_client():
|
@@ -165,39 +206,53 @@ class GradioClientController:
|
|
165 |
logger.error(f"이미지 저장 실패: {str(e)}")
|
166 |
return ([], None, [], None, "", "", "이미지 저장에 실패했습니다. 다른 이미지를 시도해보세요.")
|
167 |
|
168 |
-
# API 호출
|
169 |
logger.info("이미지 처리 API 호출 시작")
|
170 |
result = self.client.predict(
|
171 |
-
password,
|
172 |
-
handle_file(temp_path),
|
173 |
-
bg_type,
|
174 |
-
simple or "",
|
175 |
-
studio or "",
|
176 |
-
nature or "",
|
177 |
-
indoor or "",
|
178 |
-
special or "",
|
179 |
-
jewelry or "",
|
180 |
-
special_effects or "",
|
181 |
-
request_text or "",
|
182 |
-
quality_level,
|
183 |
-
aspect_ratio,
|
184 |
-
output_format,
|
185 |
-
enable_enhancement,
|
186 |
api_name="/check_password"
|
187 |
)
|
188 |
|
189 |
logger.info("이미지 처리 API 호출 완료")
|
190 |
|
191 |
-
# 결과 검증 및 반환
|
192 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
193 |
-
|
|
|
|
|
194 |
else:
|
195 |
-
logger.warning("API
|
196 |
return ([], None, [], None, "", "", "API에서 올바르지 않은 응답을 받았습니다.")
|
197 |
|
198 |
except Exception as e:
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
finally:
|
203 |
# 임시 파일 정리
|
@@ -251,7 +306,7 @@ def create_gradio_interface():
|
|
251 |
value="심플 배경"
|
252 |
)
|
253 |
|
254 |
-
# 드롭다운 컴포넌트들 (
|
255 |
simple_dropdown = gr.Dropdown(
|
256 |
choices=[],
|
257 |
label="심플 배경 선택",
|
|
|
41 |
def __init__(self):
|
42 |
self.client = None
|
43 |
self.api_endpoint = None
|
44 |
+
self.background_options = {} # 캐시된 배경 옵션
|
45 |
self._initialize_client()
|
46 |
|
47 |
def _initialize_client(self):
|
|
|
58 |
try:
|
59 |
self.client = Client(self.api_endpoint)
|
60 |
logger.info("API 클라이언트가 성공적으로 초기화되었습니다.")
|
61 |
+
# 초기화 성공 시 배경 옵션 로드
|
62 |
+
self._load_background_options()
|
63 |
return
|
64 |
except Exception as e:
|
65 |
if attempt < max_retries - 1:
|
|
|
72 |
logger.error(f"클라이언트 초기화 실패: {str(e)}")
|
73 |
self.client = None
|
74 |
|
75 |
+
def _load_background_options(self):
|
76 |
+
"""배경 옵션을 미리 로드하여 캐시"""
|
77 |
+
try:
|
78 |
+
if self.client:
|
79 |
+
# 각 배경 타입별로 옵션 로드
|
80 |
+
background_types = ["심플 배경", "스튜디오 배경", "자연 환경", "실내 환경", "특수배경", "주얼리", "특수효과"]
|
81 |
+
for bg_type in background_types:
|
82 |
+
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
83 |
+
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
84 |
+
self.background_options[bg_type] = result
|
85 |
+
logger.info(f"배경 옵션 로드 완료: {bg_type}")
|
86 |
+
time.sleep(0.1) # API 호출 간격
|
87 |
+
except Exception as e:
|
88 |
+
logger.warning(f"배경 옵션 미리 로드 실패: {str(e)}")
|
89 |
+
# 실패해도 계속 진행
|
90 |
+
|
91 |
def _ensure_client(self) -> bool:
|
92 |
"""클라이언트 연결 상태 확인 및 재연결"""
|
93 |
if self.client is None:
|
|
|
100 |
try:
|
101 |
if not self._ensure_client():
|
102 |
logger.error("클라이언트 연결 실패")
|
103 |
+
# 캐시된 데이터가 있으면 사용
|
104 |
+
if bg_type in self.background_options:
|
105 |
+
result = self.background_options[bg_type]
|
106 |
+
logger.info(f"캐시된 드롭다운 데이터 사용: {bg_type}")
|
107 |
+
else:
|
108 |
+
return tuple([gr.update() for _ in range(7)])
|
109 |
+
else:
|
110 |
+
# 실시간으로 API 호출
|
111 |
+
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
112 |
+
logger.info(f"실시간 드롭다운 업데이트 완료: {bg_type}")
|
113 |
|
114 |
# 결과를 Gradio 업데이트 형식으로 변환
|
115 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
|
|
131 |
|
132 |
except Exception as e:
|
133 |
logger.error(f"드롭다운 업데이트 오류: {str(e)}")
|
134 |
+
# 캐시된 데이터 시도
|
135 |
+
if bg_type in self.background_options:
|
136 |
+
try:
|
137 |
+
result = self.background_options[bg_type]
|
138 |
+
updates = []
|
139 |
+
visibility_map = ["심플 배경", "스튜디오 배경", "자연 환경", "실내 환경", "특수배경", "주얼리", "특수효과"]
|
140 |
+
|
141 |
+
for i, choices in enumerate(result[:7]):
|
142 |
+
is_visible = (bg_type == visibility_map[i])
|
143 |
+
updates.append(gr.update(
|
144 |
+
visible=is_visible,
|
145 |
+
choices=choices if choices else [],
|
146 |
+
value=choices[0] if choices and len(choices) > 0 else None
|
147 |
+
))
|
148 |
+
|
149 |
+
logger.info(f"캐시된 데이터로 드롭다운 업데이트: {bg_type}")
|
150 |
+
return tuple(updates)
|
151 |
+
except:
|
152 |
+
pass
|
153 |
+
|
154 |
return tuple([gr.update() for _ in range(7)])
|
155 |
|
156 |
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
|
|
|
187 |
nature: str, indoor: str, special: str, jewelry: str, special_effects: str,
|
188 |
request_text: str, quality_level: str, aspect_ratio: str,
|
189 |
output_format: str, enable_enhancement: bool) -> Tuple:
|
190 |
+
"""이미지 처리 (편집 및 화질 개선) - API 문서와 정확히 매칭"""
|
191 |
temp_path = None
|
192 |
try:
|
193 |
if not self._ensure_client():
|
|
|
206 |
logger.error(f"이미지 저장 실패: {str(e)}")
|
207 |
return ([], None, [], None, "", "", "이미지 저장에 실패했습니다. 다른 이미지를 시도해보세요.")
|
208 |
|
209 |
+
# API 호출 - 매개변수 순서를 API 문서와 정확히 맞춤
|
210 |
logger.info("이미지 처리 API 호출 시작")
|
211 |
result = self.client.predict(
|
212 |
+
password, # password (str)
|
213 |
+
handle_file(temp_path), # param_1 (image file)
|
214 |
+
bg_type, # param_2 (배경 유형)
|
215 |
+
simple or "", # param_3 (심플 배경)
|
216 |
+
studio or "", # param_4 (스튜디오 배경)
|
217 |
+
nature or "", # param_5 (자연 환경)
|
218 |
+
indoor or "", # param_6 (실내 환경)
|
219 |
+
special or "", # param_7 (특수배경)
|
220 |
+
jewelry or "", # param_8 (주얼리)
|
221 |
+
special_effects or "", # param_9 (특수효과)
|
222 |
+
request_text or "", # param_10 (요청사항)
|
223 |
+
quality_level, # param_11 (품질 레벨)
|
224 |
+
aspect_ratio, # param_12 (종횡비)
|
225 |
+
output_format, # param_13 (이미지 형식)
|
226 |
+
enable_enhancement, # param_14 (추가 화질 개선)
|
227 |
api_name="/check_password"
|
228 |
)
|
229 |
|
230 |
logger.info("이미지 처리 API 호출 완료")
|
231 |
|
232 |
+
# 결과 검증 및 반환 - API 문서에 따르면 tuple of 7 elements
|
233 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
234 |
+
# [0] original_gallery, [1] original_file, [2] enhanced_gallery,
|
235 |
+
# [3] enhanced_file, [4] prompt_text, [5] info_text, [6] error_text
|
236 |
+
return result[:7] # 정확히 7개 요소만 반환
|
237 |
else:
|
238 |
+
logger.warning(f"API 응답 형식 이상: type={type(result)}, length={len(result) if hasattr(result, '__len__') else 'N/A'}")
|
239 |
return ([], None, [], None, "", "", "API에서 올바르지 않은 응답을 받았습니다.")
|
240 |
|
241 |
except Exception as e:
|
242 |
+
error_msg = str(e)
|
243 |
+
logger.error(f"이미지 처리 오류: {error_msg}")
|
244 |
+
|
245 |
+
# 일반적인 오류 메시지 개선
|
246 |
+
if "timeout" in error_msg.lower():
|
247 |
+
user_error = "요청 처리 시간이 초과되었습니다. 잠시 후 다시 시도해주세요."
|
248 |
+
elif "connection" in error_msg.lower():
|
249 |
+
user_error = "서버 연결에 문제가 있습니다. 네트워크 상태를 확인해주세요."
|
250 |
+
elif "unauthorized" in error_msg.lower() or "password" in error_msg.lower():
|
251 |
+
user_error = "인증에 실패했습니다. 비밀번호를 확인해주세요."
|
252 |
+
else:
|
253 |
+
user_error = f"이미지 처리 중 오류가 발생했습니다: {error_msg}"
|
254 |
+
|
255 |
+
return ([], None, [], None, "", "", user_error)
|
256 |
|
257 |
finally:
|
258 |
# 임시 파일 정리
|
|
|
306 |
value="심플 배경"
|
307 |
)
|
308 |
|
309 |
+
# 드롭다운 컴포넌트들 (API에서 동적으로 로드)
|
310 |
simple_dropdown = gr.Dropdown(
|
311 |
choices=[],
|
312 |
label="심플 배경 선택",
|