jefffffff9 Claude Sonnet 4.6 commited on
Commit
e09e327
·
1 Parent(s): bcc9a12

Fix: use direct WhisperProcessor/WhisperForConditionalGeneration imports

Browse files

Auto classes (AutoProcessor, AutoModelForSpeechSeq2Seq) are not reliably
exported from transformers __init__.py across all 4.x/5.x versions.
Direct module paths (transformers.models.whisper) are stable in all versions.
Also allow _ensure_whisper_loaded to retry after an import error.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -82,35 +82,30 @@ if HF_TOKEN:
82
  def _do_load_whisper():
83
  global _whisper_model, _whisper_processor, _adapter_manager, _model_status
84
  import torch
85
- from transformers import AutoProcessor
86
- # AutoModelForSpeechSeq2Seq is not always exported at the transformers top level
87
- # depending on the version; fall back through known-good alternatives.
 
88
  try:
89
- from transformers import AutoModelForSpeechSeq2Seq as _ModelCls
90
  except ImportError:
91
- try:
92
- from transformers import AutoModelForConditionalGeneration as _ModelCls
93
- except ImportError:
94
- from transformers.models.whisper.modeling_whisper import WhisperForConditionalGeneration as _ModelCls
95
- from src.engine.adapter_manager import AdapterManager
96
 
97
  _model_status = "loading…"
98
  try:
99
- _whisper_processor = AutoProcessor.from_pretrained(
100
  WHISPER_MODEL_ID, token=HF_TOKEN
101
  )
102
- # `dtype` is the transformers 5.x name; `torch_dtype` works on 4.x.
103
- # Try the new name first, fall back if the version doesn't accept it.
104
  try:
105
- _whisper_model = _ModelCls.from_pretrained(
106
  WHISPER_MODEL_ID,
107
- dtype=torch.float32,
108
  token=HF_TOKEN,
109
  )
110
  except TypeError:
111
- _whisper_model = _ModelCls.from_pretrained(
112
  WHISPER_MODEL_ID,
113
- torch_dtype=torch.float32,
114
  token=HF_TOKEN,
115
  )
116
  _whisper_model.eval()
@@ -160,7 +155,9 @@ def _ensure_whisper_loaded():
160
  """Load Whisper to CPU in a background thread on first call. Non-blocking."""
161
  global _model_status
162
  with _model_lock:
163
- if _whisper_model is None and "loading" not in _model_status and "error" not in _model_status:
 
 
164
  t = threading.Thread(target=_do_load_whisper, daemon=True)
165
  t.start()
166
  return _model_status
 
82
  def _do_load_whisper():
83
  global _whisper_model, _whisper_processor, _adapter_manager, _model_status
84
  import torch
85
+ from src.engine.adapter_manager import AdapterManager
86
+
87
+ # Import concrete Whisper classes directly — bypasses transformers __init__.py
88
+ # Auto-class exports differ between transformers 4.x and 5.x; direct paths are stable.
89
  try:
90
+ from transformers.models.whisper import WhisperProcessor, WhisperForConditionalGeneration
91
  except ImportError:
92
+ from transformers.models.whisper.processing_whisper import WhisperProcessor
93
+ from transformers.models.whisper.modeling_whisper import WhisperForConditionalGeneration
 
 
 
94
 
95
  _model_status = "loading…"
96
  try:
97
+ _whisper_processor = WhisperProcessor.from_pretrained(
98
  WHISPER_MODEL_ID, token=HF_TOKEN
99
  )
 
 
100
  try:
101
+ _whisper_model = WhisperForConditionalGeneration.from_pretrained(
102
  WHISPER_MODEL_ID,
103
+ torch_dtype=torch.float32,
104
  token=HF_TOKEN,
105
  )
106
  except TypeError:
107
+ _whisper_model = WhisperForConditionalGeneration.from_pretrained(
108
  WHISPER_MODEL_ID,
 
109
  token=HF_TOKEN,
110
  )
111
  _whisper_model.eval()
 
155
  """Load Whisper to CPU in a background thread on first call. Non-blocking."""
156
  global _model_status
157
  with _model_lock:
158
+ # Retry if previous attempt errored (e.g. import failed on first try)
159
+ if _whisper_model is None and "loading" not in _model_status:
160
+ _model_status = "loading…"
161
  t = threading.Thread(target=_do_load_whisper, daemon=True)
162
  t.start()
163
  return _model_status