Update app.py
Browse files
app.py
CHANGED
|
@@ -111,20 +111,68 @@ class GradioClientController:
|
|
| 111 |
logger.error(f"초기 드롭다운 데이터 로드 실패: {str(e)}")
|
| 112 |
return tuple([[] for _ in range(7)])
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
def get_initial_simple_choices(self) -> list:
|
| 115 |
"""심플 배경 초기 선택지만 반환"""
|
| 116 |
try:
|
|
|
|
|
|
|
| 117 |
if "심플 배경" in self.background_options:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
elif self.client:
|
|
|
|
| 121 |
result = self.client.predict("심플 배경", api_name="/update_dropdowns")
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
logger.error(f"심플 배경 선택지 로드 실패: {str(e)}")
|
|
|
|
|
|
|
| 128 |
return []
|
| 129 |
|
| 130 |
def _ensure_client(self) -> bool:
|
|
@@ -137,6 +185,8 @@ class GradioClientController:
|
|
| 137 |
def update_dropdowns(self, bg_type: str) -> Tuple:
|
| 138 |
"""배경 유형에 따른 드롭다운 업데이트"""
|
| 139 |
try:
|
|
|
|
|
|
|
| 140 |
if not self._ensure_client():
|
| 141 |
logger.error("클라이언트 연결 실패")
|
| 142 |
# 캐시된 데이터가 있으면 사용
|
|
@@ -148,7 +198,14 @@ class GradioClientController:
|
|
| 148 |
else:
|
| 149 |
# 실시간으로 API 호출
|
| 150 |
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
| 151 |
-
logger.info(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# 결과를 Gradio 업데이트 형식으로 변환
|
| 154 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
|
@@ -157,19 +214,24 @@ class GradioClientController:
|
|
| 157 |
|
| 158 |
for i, choices in enumerate(result[:7]):
|
| 159 |
is_visible = (bg_type == visibility_map[i])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
updates.append(gr.update(
|
| 161 |
visible=is_visible,
|
| 162 |
choices=choices if choices else [],
|
| 163 |
-
value=
|
| 164 |
))
|
| 165 |
|
| 166 |
return tuple(updates)
|
| 167 |
else:
|
| 168 |
-
logger.warning("API에서 예상과 다른 형식의 결과를
|
| 169 |
return tuple([gr.update() for _ in range(7)])
|
| 170 |
|
| 171 |
except Exception as e:
|
| 172 |
logger.error(f"드롭다운 업데이트 오류: {str(e)}")
|
|
|
|
| 173 |
# 캐시된 데이터 시도
|
| 174 |
if bg_type in self.background_options:
|
| 175 |
try:
|
|
@@ -345,10 +407,9 @@ def create_gradio_interface():
|
|
| 345 |
value="심플 배경"
|
| 346 |
)
|
| 347 |
|
| 348 |
-
# 드롭다운 컴포넌트들 -
|
| 349 |
simple_dropdown = gr.Dropdown(
|
| 350 |
-
choices=
|
| 351 |
-
value=None, # 첫 번째 값은 로드 후 설정
|
| 352 |
label="심플 배경 선택",
|
| 353 |
visible=True,
|
| 354 |
interactive=True
|
|
|
|
| 111 |
logger.error(f"초기 드롭다운 데이터 로드 실패: {str(e)}")
|
| 112 |
return tuple([[] for _ in range(7)])
|
| 113 |
|
| 114 |
+
def _parse_dropdown_result(self, result):
|
| 115 |
+
"""API 응답에서 실제 선택지 데이터만 추출"""
|
| 116 |
+
try:
|
| 117 |
+
if isinstance(result, (list, tuple)):
|
| 118 |
+
parsed_choices = []
|
| 119 |
+
for item in result:
|
| 120 |
+
if isinstance(item, dict):
|
| 121 |
+
# Gradio 응답에서 choices 추출
|
| 122 |
+
if 'choices' in item:
|
| 123 |
+
choices = item['choices']
|
| 124 |
+
elif '__type__' in item and 'choices' in str(item):
|
| 125 |
+
# __type__ 구조에서 choices 찾기
|
| 126 |
+
choices = item.get('choices', [])
|
| 127 |
+
else:
|
| 128 |
+
# 직접 리스트인 경우
|
| 129 |
+
choices = item if isinstance(item, list) else []
|
| 130 |
+
elif isinstance(item, list):
|
| 131 |
+
# 이미 리스트인 경우
|
| 132 |
+
choices = item
|
| 133 |
+
else:
|
| 134 |
+
# 문자열이거나 기타 타입
|
| 135 |
+
choices = [str(item)] if item else []
|
| 136 |
+
|
| 137 |
+
parsed_choices.append(choices)
|
| 138 |
+
|
| 139 |
+
return parsed_choices
|
| 140 |
+
else:
|
| 141 |
+
logger.warning(f"예상하지 못한 응답 형식: {type(result)}")
|
| 142 |
+
return [[] for _ in range(7)]
|
| 143 |
+
except Exception as e:
|
| 144 |
+
logger.error(f"드롭다운 결과 파싱 오류: {str(e)}")
|
| 145 |
+
return [[] for _ in range(7)]
|
| 146 |
+
|
| 147 |
def get_initial_simple_choices(self) -> list:
|
| 148 |
"""심플 배경 초기 선택지만 반환"""
|
| 149 |
try:
|
| 150 |
+
logger.info("심플 배경 선택지 로드 시작")
|
| 151 |
+
|
| 152 |
if "심플 배경" in self.background_options:
|
| 153 |
+
result = self.background_options["심플 배경"]
|
| 154 |
+
logger.info("캐시에서 심플 배경 선택지 로드")
|
| 155 |
elif self.client:
|
| 156 |
+
logger.info("API에서 심플 배경 선택지 로드")
|
| 157 |
result = self.client.predict("심플 배경", api_name="/update_dropdowns")
|
| 158 |
+
logger.info(f"API 응답: {result}")
|
| 159 |
+
|
| 160 |
+
# 응답 파싱
|
| 161 |
+
parsed_result = self._parse_dropdown_result(result)
|
| 162 |
+
self.background_options["심플 배경"] = parsed_result
|
| 163 |
+
|
| 164 |
+
if len(parsed_result) > 0:
|
| 165 |
+
choices = parsed_result[0]
|
| 166 |
+
logger.info(f"파싱된 심플 배경 선택지: {choices}")
|
| 167 |
+
return choices
|
| 168 |
+
else:
|
| 169 |
+
logger.error("클라이언트가 초기화되지 않음")
|
| 170 |
+
return []
|
| 171 |
+
|
| 172 |
except Exception as e:
|
| 173 |
logger.error(f"심플 배경 선택지 로드 실패: {str(e)}")
|
| 174 |
+
logger.error(traceback.format_exc())
|
| 175 |
+
|
| 176 |
return []
|
| 177 |
|
| 178 |
def _ensure_client(self) -> bool:
|
|
|
|
| 185 |
def update_dropdowns(self, bg_type: str) -> Tuple:
|
| 186 |
"""배경 유형에 따른 드롭다운 업데이트"""
|
| 187 |
try:
|
| 188 |
+
logger.info(f"드롭다운 업데이트 시작: {bg_type}")
|
| 189 |
+
|
| 190 |
if not self._ensure_client():
|
| 191 |
logger.error("클라이언트 연결 실패")
|
| 192 |
# 캐시된 데이터가 있으면 사용
|
|
|
|
| 198 |
else:
|
| 199 |
# 실시간으로 API 호출
|
| 200 |
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
| 201 |
+
logger.info(f"API 응답 원본: {result}")
|
| 202 |
+
|
| 203 |
+
# 응답 파싱
|
| 204 |
+
parsed_result = self._parse_dropdown_result(result)
|
| 205 |
+
self.background_options[bg_type] = parsed_result
|
| 206 |
+
result = parsed_result
|
| 207 |
+
|
| 208 |
+
logger.info(f"파싱된 결과: {result}")
|
| 209 |
|
| 210 |
# 결과를 Gradio 업데이트 형식으로 변환
|
| 211 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
|
|
|
| 214 |
|
| 215 |
for i, choices in enumerate(result[:7]):
|
| 216 |
is_visible = (bg_type == visibility_map[i])
|
| 217 |
+
first_choice = choices[0] if choices and len(choices) > 0 else None
|
| 218 |
+
|
| 219 |
+
logger.info(f"드롭다운 {i} ({visibility_map[i]}): visible={is_visible}, choices_count={len(choices) if choices else 0}, first_choice={first_choice}")
|
| 220 |
+
|
| 221 |
updates.append(gr.update(
|
| 222 |
visible=is_visible,
|
| 223 |
choices=choices if choices else [],
|
| 224 |
+
value=first_choice
|
| 225 |
))
|
| 226 |
|
| 227 |
return tuple(updates)
|
| 228 |
else:
|
| 229 |
+
logger.warning(f"API에서 예상과 다른 형식의 결과를 반환했습니다: {type(result)}, length={len(result) if hasattr(result, '__len__') else 'N/A'}")
|
| 230 |
return tuple([gr.update() for _ in range(7)])
|
| 231 |
|
| 232 |
except Exception as e:
|
| 233 |
logger.error(f"드롭다운 업데이트 오류: {str(e)}")
|
| 234 |
+
logger.error(traceback.format_exc())
|
| 235 |
# 캐시된 데이터 시도
|
| 236 |
if bg_type in self.background_options:
|
| 237 |
try:
|
|
|
|
| 407 |
value="심플 배경"
|
| 408 |
)
|
| 409 |
|
| 410 |
+
# 드롭다운 컴포넌트들 - 초기에는 빈 상태로 시작
|
| 411 |
simple_dropdown = gr.Dropdown(
|
| 412 |
+
choices=[],
|
|
|
|
| 413 |
label="심플 배경 선택",
|
| 414 |
visible=True,
|
| 415 |
interactive=True
|