Spaces:
Runtime error
Runtime error
Jeongsoo1975
commited on
Commit
·
22c5981
1
Parent(s):
82ee6ff
fix: TypeError 및 Hugging Face Spaces 호환성 개선
Browse files- app.py +27 -18
- stt_processor.py +11 -3
app.py
CHANGED
|
@@ -42,7 +42,8 @@ def initialize_models():
|
|
| 42 |
# 환경 변수 또는 Hugging Face Secrets에서 API 키 읽기
|
| 43 |
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
return False, "❌ Google API 키가 설정되지 않았습니다. Hugging Face Spaces의 Settings에서 GOOGLE_API_KEY를 설정해주세요."
|
| 47 |
|
| 48 |
# Whisper 모델 로드 시도
|
|
@@ -59,7 +60,7 @@ def initialize_models():
|
|
| 59 |
if TextProcessor is None:
|
| 60 |
return False, f"❌ TextProcessor 로딩 실패: {processor_error}"
|
| 61 |
|
| 62 |
-
text_processor = TextProcessor(google_api_key)
|
| 63 |
return True, "✅ 모든 모델이 초기화되었습니다."
|
| 64 |
|
| 65 |
except Exception as e:
|
|
@@ -384,27 +385,35 @@ if __name__ == "__main__":
|
|
| 384 |
# 인터페이스 생성
|
| 385 |
app = create_interface()
|
| 386 |
|
| 387 |
-
#
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
except Exception as e:
|
| 395 |
logger.error(f"앱 시작 실패: {e}")
|
| 396 |
-
#
|
| 397 |
try:
|
|
|
|
| 398 |
with gr.Blocks() as fallback_app:
|
| 399 |
gr.HTML("<h1>🔧 시스템 점검 중</h1>")
|
| 400 |
-
gr.Markdown(f"**오류 정보:** {str(e)}")
|
| 401 |
gr.Markdown("잠시 후 다시 시도해주세요.")
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
| 408 |
except Exception as fallback_error:
|
| 409 |
logger.error(f"Fallback 앱도 실패: {fallback_error}")
|
| 410 |
-
print("시스템 점검 중입니다.
|
|
|
|
| 42 |
# 환경 변수 또는 Hugging Face Secrets에서 API 키 읽기
|
| 43 |
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 44 |
|
| 45 |
+
# API 키 안전하게 검증
|
| 46 |
+
if not google_api_key or not isinstance(google_api_key, str) or len(google_api_key.strip()) == 0:
|
| 47 |
return False, "❌ Google API 키가 설정되지 않았습니다. Hugging Face Spaces의 Settings에서 GOOGLE_API_KEY를 설정해주세요."
|
| 48 |
|
| 49 |
# Whisper 모델 로드 시도
|
|
|
|
| 60 |
if TextProcessor is None:
|
| 61 |
return False, f"❌ TextProcessor 로딩 실패: {processor_error}"
|
| 62 |
|
| 63 |
+
text_processor = TextProcessor(google_api_key.strip())
|
| 64 |
return True, "✅ 모든 모델이 초기화되었습니다."
|
| 65 |
|
| 66 |
except Exception as e:
|
|
|
|
| 385 |
# 인터페이스 생성
|
| 386 |
app = create_interface()
|
| 387 |
|
| 388 |
+
# Hugging Face Spaces 환경 감지
|
| 389 |
+
is_spaces = os.getenv('SPACE_ID') is not None
|
| 390 |
+
|
| 391 |
+
if is_spaces:
|
| 392 |
+
# Hugging Face Spaces용 설정
|
| 393 |
+
app.launch()
|
| 394 |
+
else:
|
| 395 |
+
# 로컬 개발용 설정
|
| 396 |
+
app.launch(
|
| 397 |
+
server_name="0.0.0.0",
|
| 398 |
+
server_port=7860,
|
| 399 |
+
share=False
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
except Exception as e:
|
| 403 |
logger.error(f"앱 시작 실패: {e}")
|
| 404 |
+
# 최소한의 fallback
|
| 405 |
try:
|
| 406 |
+
import gradio as gr
|
| 407 |
with gr.Blocks() as fallback_app:
|
| 408 |
gr.HTML("<h1>🔧 시스템 점검 중</h1>")
|
|
|
|
| 409 |
gr.Markdown("잠시 후 다시 시도해주세요.")
|
| 410 |
+
gr.Markdown(f"**디버그 정보:** {str(e)[:200]}...")
|
| 411 |
+
|
| 412 |
+
if os.getenv('SPACE_ID') is not None:
|
| 413 |
+
fallback_app.launch()
|
| 414 |
+
else:
|
| 415 |
+
fallback_app.launch(server_name="0.0.0.0", server_port=7860)
|
| 416 |
+
|
| 417 |
except Exception as fallback_error:
|
| 418 |
logger.error(f"Fallback 앱도 실패: {fallback_error}")
|
| 419 |
+
print("🚨 시스템 점검 중입니다. 관리자에게 문의해주세요.")
|
stt_processor.py
CHANGED
|
@@ -24,12 +24,20 @@ class TextProcessor:
|
|
| 24 |
Args:
|
| 25 |
google_api_key (str): Google AI API 키. None인 경우 환경 변수에서 읽음
|
| 26 |
"""
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
self.gemini_model = None
|
| 29 |
self.models_loaded = False
|
| 30 |
|
| 31 |
-
# API 키 검증 - 안전한
|
| 32 |
-
if
|
|
|
|
|
|
|
|
|
|
| 33 |
raise ValueError("Google AI API 키가 설정되지 않았습니다. 환경 변수 GOOGLE_API_KEY를 설정하거나 매개변수로 전달하세요.")
|
| 34 |
|
| 35 |
def load_models(self):
|
|
|
|
| 24 |
Args:
|
| 25 |
google_api_key (str): Google AI API 키. None인 경우 환경 변수에서 읽음
|
| 26 |
"""
|
| 27 |
+
# API 키 안전하게 가져오기
|
| 28 |
+
if google_api_key:
|
| 29 |
+
self.google_api_key = str(google_api_key)
|
| 30 |
+
else:
|
| 31 |
+
self.google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 32 |
+
|
| 33 |
self.gemini_model = None
|
| 34 |
self.models_loaded = False
|
| 35 |
|
| 36 |
+
# API 키 검증 - 더 안전한 체크
|
| 37 |
+
if (self.google_api_key is None or
|
| 38 |
+
not isinstance(self.google_api_key, str) or
|
| 39 |
+
len(self.google_api_key.strip()) == 0 or
|
| 40 |
+
self.google_api_key.strip() == "your_google_api_key_here"):
|
| 41 |
raise ValueError("Google AI API 키가 설정되지 않았습니다. 환경 변수 GOOGLE_API_KEY를 설정하거나 매개변수로 전달하세요.")
|
| 42 |
|
| 43 |
def load_models(self):
|