Jerich commited on
Commit
1881cc0
·
verified ·
1 Parent(s): 4279e53

Optimize app for Hugging Face Spaces: Switch to whisper-tiny, add health check, and improve model loading

Browse files

- Switched STT model from openai/whisper-small to openai/whisper-tiny to reduce memory usage and prevent startup timeout
- Optimized update_languages method to only reload TTS model, not STT model
- Added debug logging in __init__ to confirm model loading
- Added /health endpoint for Hugging Face Spaces health check
- Fixed clean_up_tokenization_spaces warning in _initialize_mt_model by setting clean_up_tokenization_spaces=True

Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -58,29 +58,25 @@ class TalklasTranslator:
58
  self._initialize_stt_model()
59
  self._initialize_mt_model()
60
  self._initialize_tts_model()
 
61
 
62
  def _initialize_stt_model(self):
63
  try:
64
- print("Trying to load openai/whisper-small...")
65
- self.stt_processor = WhisperProcessor.from_pretrained("openai/whisper-small")
66
- self.stt_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
67
  self.stt_model.to(self.device)
68
- print("Loaded openai/whisper-small successfully")
69
  except Exception as e:
70
- print(f"Failed to load openai/whisper-small: {e}")
71
- print("Falling back to openai/whisper-base...")
72
- try:
73
- self.stt_processor = WhisperProcessor.from_pretrained("openai/whisper-base")
74
- self.stt_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
75
- self.stt_model.to(self.device)
76
- print("Loaded openai/whisper-base successfully")
77
- except Exception as e2:
78
- raise RuntimeError(f"STT model initialization failed: {e2}")
79
 
80
  def _initialize_mt_model(self):
81
  try:
82
  self.mt_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
83
- self.mt_tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
 
 
 
84
  self.mt_model.to(self.device)
85
  print("Loaded NLLB translation model successfully")
86
  except Exception as e:
@@ -106,10 +102,12 @@ class TalklasTranslator:
106
  print("Loaded fallback TTS model facebook/mms-tts-eng successfully")
107
 
108
  def update_languages(self, source_lang: str, target_lang: str):
 
109
  self.source_lang = source_lang
110
  self.target_lang = target_lang
111
- self._initialize_stt_model()
112
  self._initialize_tts_model()
 
113
  return f"Languages updated to {source_lang} → {target_lang}"
114
 
115
  def speech_to_text(self, audio_path: str) -> str:
@@ -167,6 +165,10 @@ class TalklasTranslator:
167
 
168
  translator = TalklasTranslator()
169
 
 
 
 
 
170
  @app.post("/update-languages")
171
  async def update_languages(source_lang: str = Form(...), target_lang: str = Form(...)):
172
  if source_lang not in TalklasTranslator.LANGUAGE_MAPPING or target_lang not in TalklasTranslator.LANGUAGE_MAPPING:
 
58
  self._initialize_stt_model()
59
  self._initialize_mt_model()
60
  self._initialize_tts_model()
61
+ print("All models loaded successfully, starting FastAPI app")
62
 
63
  def _initialize_stt_model(self):
64
  try:
65
+ print("Trying to load openai/whisper-tiny...")
66
+ self.stt_processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
67
+ self.stt_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
68
  self.stt_model.to(self.device)
69
+ print("Loaded openai/whisper-tiny successfully")
70
  except Exception as e:
71
+ raise RuntimeError(f"STT model initialization failed: {e}")
 
 
 
 
 
 
 
 
72
 
73
  def _initialize_mt_model(self):
74
  try:
75
  self.mt_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
76
+ self.mt_tokenizer = AutoTokenizer.from_pretrained(
77
+ "facebook/nllb-200-distilled-600M",
78
+ clean_up_tokenization_spaces=True
79
+ )
80
  self.mt_model.to(self.device)
81
  print("Loaded NLLB translation model successfully")
82
  except Exception as e:
 
102
  print("Loaded fallback TTS model facebook/mms-tts-eng successfully")
103
 
104
  def update_languages(self, source_lang: str, target_lang: str):
105
+ print(f"Updating languages: source_lang={source_lang}, target_lang={target_lang}")
106
  self.source_lang = source_lang
107
  self.target_lang = target_lang
108
+ print("Calling _initialize_tts_model...")
109
  self._initialize_tts_model()
110
+ print("Languages updated successfully")
111
  return f"Languages updated to {source_lang} → {target_lang}"
112
 
113
  def speech_to_text(self, audio_path: str) -> str:
 
165
 
166
  translator = TalklasTranslator()
167
 
168
+ @app.get("/health")
169
+ async def health_check():
170
+ return {"status": "healthy"}
171
+
172
  @app.post("/update-languages")
173
  async def update_languages(source_lang: str = Form(...), target_lang: str = Form(...)):
174
  if source_lang not in TalklasTranslator.LANGUAGE_MAPPING or target_lang not in TalklasTranslator.LANGUAGE_MAPPING: