Jeongsoo1975 commited on
Commit
f824b37
·
1 Parent(s): d76d4c0

fix: Internal Server Error 해결

Browse files
Files changed (2) hide show
  1. app.py +55 -20
  2. requirements.txt +3 -1
app.py CHANGED
@@ -17,6 +17,24 @@ logger = logging.getLogger(__name__)
17
  text_processor = None
18
  whisper_model = None
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def initialize_models():
21
  """모델들을 초기화합니다."""
22
  global text_processor, whisper_model
@@ -27,14 +45,20 @@ def initialize_models():
27
  if not google_api_key:
28
  return False, "❌ Google API 키가 설정되지 않았습니다. Hugging Face Spaces의 Settings에서 GOOGLE_API_KEY를 설정해주세요."
29
 
30
- # Whisper 모델 로드 (지연 로딩)
31
- import whisper
 
 
 
32
  logger.info("Whisper 모델을 로딩합니다...")
33
- whisper_model = whisper.load_model("base")
34
  logger.info("Whisper 모델 로딩 완료")
35
 
36
  # 텍스트 프로세서 초기화
37
- from stt_processor import TextProcessor
 
 
 
38
  text_processor = TextProcessor(google_api_key)
39
  return True, "✅ 모든 모델이 초기화되었습니다."
40
 
@@ -43,9 +67,7 @@ def initialize_models():
43
  return False, f"❌ 초기화 실패: {str(e)}"
44
 
45
  def process_audio_file(audio_file, progress=gr.Progress()):
46
- """
47
- 업로드된 오디오 파일을 처리합니다.
48
- """
49
  global text_processor, whisper_model
50
 
51
  if audio_file is None:
@@ -60,7 +82,7 @@ def process_audio_file(audio_file, progress=gr.Progress()):
60
  return message, "", "", "", "", ""
61
 
62
  # 오디오 파일 경로 확인
63
- audio_path = audio_file.name if hasattr(audio_file, 'name') else audio_file
64
  logger.info(f"오디오 파일 처리 시작: {audio_path}")
65
 
66
  # 1단계: Whisper로 음성 인식
@@ -123,21 +145,26 @@ def process_audio_file(audio_file, progress=gr.Progress()):
123
  return f"❌ 처리 중 오류가 발생했습니다: {str(e)}", "", "", "", "", ""
124
 
125
  def process_text_input(input_text, progress=gr.Progress()):
126
- """
127
- 입력된 텍스트를 처리합니다.
128
- """
129
  global text_processor
130
 
131
  if not input_text or not input_text.strip():
132
  return "❌ 처리할 텍스트를 입력해주세요.", "", "", "", "", ""
133
 
134
  try:
135
- # 모델 초기화 (필요한 경우)
136
  if text_processor is None:
137
  progress(0.1, desc="텍스트 프로세서 초기화 중...")
138
- success, message = initialize_models()
139
- if not success:
140
- return message, "", "", "", "", ""
 
 
 
 
 
 
 
141
 
142
  # 모델 로딩
143
  progress(0.2, desc="AI 모델 로딩 중...")
@@ -353,8 +380,16 @@ def create_interface():
353
  if __name__ == "__main__":
354
  logger.info("Gradio 앱을 시작합니다...")
355
 
356
- # 인터페이스 생성
357
- app = create_interface()
358
-
359
- # 앱 실행 (Hugging Face Spaces용)
360
- app.launch()
 
 
 
 
 
 
 
 
 
17
  text_processor = None
18
  whisper_model = None
19
 
20
+ def safe_import_whisper():
21
+ """Whisper를 안전하게 import합니다."""
22
+ try:
23
+ import whisper
24
+ return whisper, None
25
+ except Exception as e:
26
+ logger.error(f"Whisper import 실패: {e}")
27
+ return None, str(e)
28
+
29
+ def safe_import_processor():
30
+ """TextProcessor를 안전하게 import합니다."""
31
+ try:
32
+ from stt_processor import TextProcessor
33
+ return TextProcessor, None
34
+ except Exception as e:
35
+ logger.error(f"TextProcessor import 실패: {e}")
36
+ return None, str(e)
37
+
38
  def initialize_models():
39
  """모델들을 초기화합니다."""
40
  global text_processor, whisper_model
 
45
  if not google_api_key:
46
  return False, "❌ Google API 키가 설정되지 않았습니다. Hugging Face Spaces의 Settings에서 GOOGLE_API_KEY를 설정해주세요."
47
 
48
+ # Whisper 모델 로드 시도
49
+ whisper_lib, whisper_error = safe_import_whisper()
50
+ if whisper_lib is None:
51
+ return False, f"❌ Whisper 라이브러리 로딩 실패: {whisper_error}"
52
+
53
  logger.info("Whisper 모델을 로딩합니다...")
54
+ whisper_model = whisper_lib.load_model("base")
55
  logger.info("Whisper 모델 로딩 완료")
56
 
57
  # 텍스트 프로세서 초기화
58
+ TextProcessor, processor_error = safe_import_processor()
59
+ if TextProcessor is None:
60
+ return False, f"❌ TextProcessor 로딩 실패: {processor_error}"
61
+
62
  text_processor = TextProcessor(google_api_key)
63
  return True, "✅ 모든 모델이 초기화되었습니다."
64
 
 
67
  return False, f"❌ 초기화 실패: {str(e)}"
68
 
69
  def process_audio_file(audio_file, progress=gr.Progress()):
70
+ """업로드된 오디오 파일을 처리합니다."""
 
 
71
  global text_processor, whisper_model
72
 
73
  if audio_file is None:
 
82
  return message, "", "", "", "", ""
83
 
84
  # 오디오 파일 경로 확인
85
+ audio_path = audio_file.name if hasattr(audio_file, 'name') else str(audio_file)
86
  logger.info(f"오디오 파일 처리 시작: {audio_path}")
87
 
88
  # 1단계: Whisper로 음성 인식
 
145
  return f"❌ 처리 중 오류가 발생했습니다: {str(e)}", "", "", "", "", ""
146
 
147
  def process_text_input(input_text, progress=gr.Progress()):
148
+ """입력된 텍스트를 처리합니다."""
 
 
149
  global text_processor
150
 
151
  if not input_text or not input_text.strip():
152
  return "❌ 처리할 텍스트를 입력해주세요.", "", "", "", "", ""
153
 
154
  try:
155
+ # 텍스트 프로세서만 초기화
156
  if text_processor is None:
157
  progress(0.1, desc="텍스트 프로세서 초기화 중...")
158
+
159
+ google_api_key = os.getenv("GOOGLE_API_KEY")
160
+ if not google_api_key:
161
+ return "❌ Google API 키가 설정되지 않았습니다.", "", "", "", "", ""
162
+
163
+ TextProcessor, processor_error = safe_import_processor()
164
+ if TextProcessor is None:
165
+ return f"❌ TextProcessor 로딩 실패: {processor_error}", "", "", "", "", ""
166
+
167
+ text_processor = TextProcessor(google_api_key)
168
 
169
  # 모델 로딩
170
  progress(0.2, desc="AI 모델 로딩 중...")
 
380
  if __name__ == "__main__":
381
  logger.info("Gradio 앱을 시작합니다...")
382
 
383
+ try:
384
+ # 인터페이스 생성
385
+ app = create_interface()
386
+
387
+ # 앱 실행 (Hugging Face Spaces용)
388
+ app.launch()
389
+ except Exception as e:
390
+ logger.error(f"앱 시작 실패: {e}")
391
+ # 기본 인터페이스라도 보여주기
392
+ with gr.Blocks() as fallback_app:
393
+ gr.HTML("<h1>앱 로딩 중 오류 발생</h1>")
394
+ gr.Markdown(f"오류: {str(e)}")
395
+ fallback_app.launch()
requirements.txt CHANGED
@@ -1,4 +1,6 @@
1
  python-dotenv==1.0.0
2
  google-generativeai==0.8.3
3
  gradio==4.44.0
4
- openai-whisper==20240930
 
 
 
1
  python-dotenv==1.0.0
2
  google-generativeai==0.8.3
3
  gradio==4.44.0
4
+ openai-whisper
5
+ torch
6
+ torchaudio