shiko217 commited on
Commit
80f2daf
Β·
unverified Β·
1 Parent(s): 51a445b
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -183,28 +183,26 @@ def load_model():
183
  return None, str(e)
184
 
185
 
186
- # ── Lazy model loading ─────────────────────────────────────────────────────
187
- # Loading the model at import time doubles peak memory (TF init + model
188
- # deserialization happen simultaneously). Deferring until the first request
189
- # keeps startup lightweight and avoids the OOM β†’ SIGSEGV on free-tier HF.
190
- _model = None
191
- _model_error = ""
192
-
193
- def _get_model():
194
- """Return the cached model, loading it on first call."""
195
- global _model, _model_error
196
- if _model is None and _model_error == "":
197
- print("Lazy-loading model on first request…")
198
- _model, _model_error = load_model()
199
- if _model is None:
200
- print(f"Warning: Could not load model. Error: {_model_error}")
201
- return _model, _model_error
202
 
203
  preprocessor = MachineListenerPreprocessor()
204
 
205
-
206
  def predict(audio_filepath):
207
- model, model_error = _get_model()
208
  if model is None:
209
  return f"Model not loaded properly. Error: {model_error}"
210
  if audio_filepath is None:
 
183
  return None, str(e)
184
 
185
 
186
+ # ── Model loading ──────────────────────────────────────────────────────────
187
+ model, model_error = load_model()
188
+ if model is None:
189
+ print(f"Warning: Could not load model. Error: {model_error}")
190
+ else:
191
+ # Warm-up prediction: forces TensorFlow to trace and compile the graph
192
+ # on the main thread. If the first prediction happens in a Gradio
193
+ # background thread, TF can deadlock and freeze the app.
194
+ print("Warming up model to prevent thread deadlock...")
195
+ dummy_spec = np.zeros((1, N_MELS, MAX_TIME_FRAMES, 1), dtype=np.float32)
196
+ dummy_stat = np.zeros((1, N_1D_FEATURES), dtype=np.float32)
197
+ try:
198
+ model.predict([dummy_spec, dummy_stat], verbose=0)
199
+ print("Model warm-up complete.")
200
+ except Exception as e:
201
+ print(f"Warm-up failed (this might be fine): {e}")
202
 
203
  preprocessor = MachineListenerPreprocessor()
204
 
 
205
  def predict(audio_filepath):
 
206
  if model is None:
207
  return f"Model not loaded properly. Error: {model_error}"
208
  if audio_filepath is None: