Update app.py
Browse files
app.py
CHANGED
|
@@ -112,83 +112,31 @@ class GradioClientController:
|
|
| 112 |
return tuple([[] for _ in range(7)])
|
| 113 |
|
| 114 |
def _parse_dropdown_result(self, result):
|
| 115 |
-
"""API 응답에서 실제 선택지 데이터만 추출"""
|
| 116 |
try:
|
| 117 |
-
logger.info(f"응답
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if isinstance(item, dict):
|
| 125 |
-
# Gradio 응답에서 choices 추출
|
| 126 |
-
if 'choices' in item:
|
| 127 |
-
choices = item['choices']
|
| 128 |
-
logger.info(f"dict에서 choices 추출: {choices}")
|
| 129 |
-
elif '__type__' in item:
|
| 130 |
-
# __type__ 구조 분석
|
| 131 |
-
logger.info(f"__type__ 구조 발견: {item}")
|
| 132 |
-
# 일반적으로 Gradio에서는 choices가 별도 필드에 있음
|
| 133 |
-
choices = []
|
| 134 |
-
else:
|
| 135 |
-
# dict의 다른 값들 확인
|
| 136 |
-
choices = []
|
| 137 |
-
for key, value in item.items():
|
| 138 |
-
if isinstance(value, list) and key != '__type__':
|
| 139 |
-
choices = value
|
| 140 |
-
break
|
| 141 |
-
elif isinstance(item, list):
|
| 142 |
-
# 이미 리스트인 경우
|
| 143 |
-
choices = item
|
| 144 |
-
logger.info(f"리스트 형태 choices: {choices}")
|
| 145 |
-
else:
|
| 146 |
-
# 문자열이거나 기타 타입
|
| 147 |
-
choices = [str(item)] if item else []
|
| 148 |
-
logger.info(f"기타 타입을 문자열로 변환: {choices}")
|
| 149 |
-
|
| 150 |
-
parsed_choices.append(choices)
|
| 151 |
-
|
| 152 |
-
logger.info(f"파싱 완료: {parsed_choices}")
|
| 153 |
-
return parsed_choices
|
| 154 |
else:
|
| 155 |
-
logger.warning(f"예상하지 못한 응답 형식: {type(result)}")
|
| 156 |
return [[] for _ in range(7)]
|
|
|
|
| 157 |
except Exception as e:
|
| 158 |
logger.error(f"드롭다운 결과 파싱 오류: {str(e)}")
|
| 159 |
-
logger.error(traceback.format_exc())
|
| 160 |
return [[] for _ in range(7)]
|
| 161 |
|
| 162 |
def get_initial_simple_choices(self) -> list:
|
| 163 |
-
"""심플 배경 초기
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
elif self.client:
|
| 171 |
-
logger.info("API에서 심플 배경 선택지 로드")
|
| 172 |
-
result = self.client.predict("심플 배경", api_name="/update_dropdowns")
|
| 173 |
-
logger.info(f"API 응답: {result}")
|
| 174 |
-
|
| 175 |
-
# 응답 파싱
|
| 176 |
-
parsed_result = self._parse_dropdown_result(result)
|
| 177 |
-
self.background_options["심플 배경"] = parsed_result
|
| 178 |
-
|
| 179 |
-
if len(parsed_result) > 0:
|
| 180 |
-
choices = parsed_result[0]
|
| 181 |
-
logger.info(f"파싱된 심플 배경 선택지: {choices}")
|
| 182 |
-
return choices
|
| 183 |
-
else:
|
| 184 |
-
logger.error("클라이언트가 초기화되지 않음")
|
| 185 |
-
return []
|
| 186 |
-
|
| 187 |
-
except Exception as e:
|
| 188 |
-
logger.error(f"심플 배경 선택지 로드 실패: {str(e)}")
|
| 189 |
-
logger.error(traceback.format_exc())
|
| 190 |
-
|
| 191 |
-
return []
|
| 192 |
|
| 193 |
def _ensure_client(self) -> bool:
|
| 194 |
"""클라이언트 연결 상태 확인 및 재연결"""
|
|
@@ -198,31 +146,19 @@ class GradioClientController:
|
|
| 198 |
return self.client is not None
|
| 199 |
|
| 200 |
def update_dropdowns(self, bg_type: str) -> Tuple:
|
| 201 |
-
"""배경 유형에 따른 드롭다운 업데이트"""
|
| 202 |
try:
|
| 203 |
-
logger.info(f"드롭다운
|
| 204 |
|
| 205 |
if not self._ensure_client():
|
| 206 |
logger.error("클라이언트 연결 실패")
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
return tuple([gr.update(visible=False) for _ in range(7)])
|
| 213 |
-
else:
|
| 214 |
-
# 실시간으로 API 호출
|
| 215 |
-
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
| 216 |
-
logger.info(f"API 응답 원본: {result}")
|
| 217 |
-
|
| 218 |
-
# 응답 파싱
|
| 219 |
-
parsed_result = self._parse_dropdown_result(result)
|
| 220 |
-
self.background_options[bg_type] = parsed_result
|
| 221 |
-
result = parsed_result
|
| 222 |
-
|
| 223 |
-
logger.info(f"파싱된 결과: {result}")
|
| 224 |
|
| 225 |
-
#
|
| 226 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
| 227 |
updates = []
|
| 228 |
visibility_map = ["심플 배경", "스튜디오 배경", "자연 환경", "실내 환경", "특수배경", "주얼리", "특수효과"]
|
|
@@ -230,33 +166,24 @@ class GradioClientController:
|
|
| 230 |
for i, choices in enumerate(result[:7]):
|
| 231 |
is_visible = (bg_type == visibility_map[i])
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
first_choice = choices[0]
|
| 236 |
-
logger.info(f"드롭다운 {i} ({visibility_map[i]}): visible=True, choices={len(choices)}개, default='{first_choice}'")
|
| 237 |
updates.append(gr.update(
|
| 238 |
visible=True,
|
| 239 |
choices=choices,
|
| 240 |
-
value=
|
| 241 |
interactive=True
|
| 242 |
))
|
| 243 |
else:
|
| 244 |
-
|
| 245 |
-
updates.append(gr.update(
|
| 246 |
-
visible=False,
|
| 247 |
-
choices=[],
|
| 248 |
-
value=None,
|
| 249 |
-
interactive=True
|
| 250 |
-
))
|
| 251 |
|
| 252 |
return tuple(updates)
|
| 253 |
else:
|
| 254 |
-
logger.
|
| 255 |
return tuple([gr.update(visible=False) for _ in range(7)])
|
| 256 |
|
| 257 |
except Exception as e:
|
| 258 |
logger.error(f"드롭다운 업데이트 오류: {str(e)}")
|
| 259 |
-
logger.error(traceback.format_exc())
|
| 260 |
return tuple([gr.update(visible=False) for _ in range(7)])
|
| 261 |
|
| 262 |
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
|
|
@@ -412,9 +339,10 @@ def create_gradio_interface():
|
|
| 412 |
value="심플 배경"
|
| 413 |
)
|
| 414 |
|
| 415 |
-
# 드롭다운 컴포넌트들 -
|
| 416 |
simple_dropdown = gr.Dropdown(
|
| 417 |
-
choices=
|
|
|
|
| 418 |
label="심플 배경 선택",
|
| 419 |
visible=True,
|
| 420 |
interactive=True
|
|
|
|
| 112 |
return tuple([[] for _ in range(7)])
|
| 113 |
|
| 114 |
def _parse_dropdown_result(self, result):
|
| 115 |
+
"""API 응답에서 실제 선택지 데이터만 추출 - 단순화"""
|
| 116 |
try:
|
| 117 |
+
logger.info(f"응답 파싱: {result}")
|
| 118 |
|
| 119 |
+
# API 문서에 따르면 tuple of 7 elements를 반환
|
| 120 |
+
# 각 요소는 Literal 타입의 리스트여야 함
|
| 121 |
+
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
| 122 |
+
# 결과를 그대로 반환 (API가 이미 올바른 형식으로 반환한다고 가정)
|
| 123 |
+
return result[:7]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
else:
|
| 125 |
+
logger.warning(f"예상하지 못한 응답 형식: {type(result)}, length={len(result) if hasattr(result, '__len__') else 'N/A'}")
|
| 126 |
return [[] for _ in range(7)]
|
| 127 |
+
|
| 128 |
except Exception as e:
|
| 129 |
logger.error(f"드롭다운 결과 파싱 오류: {str(e)}")
|
|
|
|
| 130 |
return [[] for _ in range(7)]
|
| 131 |
|
| 132 |
def get_initial_simple_choices(self) -> list:
|
| 133 |
+
"""심플 배경 초기 선택지 - 하드코딩으로 테스트"""
|
| 134 |
+
# API 문서에서 확인된 실제 선택지들
|
| 135 |
+
return [
|
| 136 |
+
'화이트 기본', '회색 투톤', '라이트 그레이', '그레이 그라데이션 스포트라이트',
|
| 137 |
+
'프리미엄 드라마틱 블랙', '딥블루 유리반사', '파스텔 그라데이션', '스카이블루 파스텔',
|
| 138 |
+
'버터옐로우 파스텔', '블루 원색', '레드 원색'
|
| 139 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
def _ensure_client(self) -> bool:
|
| 142 |
"""클라이언트 연결 상태 확인 및 재연결"""
|
|
|
|
| 146 |
return self.client is not None
|
| 147 |
|
| 148 |
def update_dropdowns(self, bg_type: str) -> Tuple:
|
| 149 |
+
"""배경 유형에 따른 드롭다운 업데이트 - 단순화"""
|
| 150 |
try:
|
| 151 |
+
logger.info(f"드롭다운 업데이트: {bg_type}")
|
| 152 |
|
| 153 |
if not self._ensure_client():
|
| 154 |
logger.error("클라이언트 연결 실패")
|
| 155 |
+
return tuple([gr.update(visible=False) for _ in range(7)])
|
| 156 |
+
|
| 157 |
+
# API 호출
|
| 158 |
+
result = self.client.predict(bg_type, api_name="/update_dropdowns")
|
| 159 |
+
logger.info(f"API 응답: {result}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
# API 응답을 그대로 사용
|
| 162 |
if isinstance(result, (list, tuple)) and len(result) >= 7:
|
| 163 |
updates = []
|
| 164 |
visibility_map = ["심플 배경", "스튜디오 배경", "자연 환경", "실내 환경", "특수배경", "주얼리", "특수효과"]
|
|
|
|
| 166 |
for i, choices in enumerate(result[:7]):
|
| 167 |
is_visible = (bg_type == visibility_map[i])
|
| 168 |
|
| 169 |
+
if is_visible and isinstance(choices, list) and len(choices) > 0:
|
| 170 |
+
logger.info(f"{visibility_map[i]} 드롭다운: {len(choices)}개 선택지")
|
|
|
|
|
|
|
| 171 |
updates.append(gr.update(
|
| 172 |
visible=True,
|
| 173 |
choices=choices,
|
| 174 |
+
value=choices[0], # 첫 번째 항목 선택
|
| 175 |
interactive=True
|
| 176 |
))
|
| 177 |
else:
|
| 178 |
+
updates.append(gr.update(visible=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
return tuple(updates)
|
| 181 |
else:
|
| 182 |
+
logger.error(f"잘못된 API 응답: {result}")
|
| 183 |
return tuple([gr.update(visible=False) for _ in range(7)])
|
| 184 |
|
| 185 |
except Exception as e:
|
| 186 |
logger.error(f"드롭다운 업데이트 오류: {str(e)}")
|
|
|
|
| 187 |
return tuple([gr.update(visible=False) for _ in range(7)])
|
| 188 |
|
| 189 |
def generate_prompt_only(self, password: str, bg_type: str, simple: str, studio: str,
|
|
|
|
| 339 |
value="심플 배경"
|
| 340 |
)
|
| 341 |
|
| 342 |
+
# 드롭다운 컴포넌트들 - 심플 배경은 하드코딩된 선택지로 시작
|
| 343 |
simple_dropdown = gr.Dropdown(
|
| 344 |
+
choices=controller.get_initial_simple_choices(),
|
| 345 |
+
value="화이트 기본", # 기본값 설정
|
| 346 |
label="심플 배경 선택",
|
| 347 |
visible=True,
|
| 348 |
interactive=True
|