From 135d66f698749ef22be05981bf747cf64004eeaf Mon Sep 17 00:00:00 2001 From: mainpart Date: Wed, 22 Jul 2026 00:33:08 +0300 Subject: [PATCH] fix: keep the real cause when align model loading fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every non-network failure in load_align_model is reported as ValueError: The chosen align_model "jonatasgrosman/wav2vec2-large-xlsr-53-russian" could not be found in huggingface (...) or torchaudio (...) regardless of what actually went wrong. Hit this on an interrupted download: the underlying error was OSError: Can't load feature extractor for ''. ... make sure '' is the correct path to a directory containing a preprocessor_config.json file but the message said the model does not exist. It does — the very next run loaded it from cache without touching the network. A gated repo or a cache permission problem lands in the same branch and reads the same way. The original exception was already chained with `from e`, but the chained traceback is not what a CLI user sees; they see the message. - put the cause into the raised message and the log line - swap `print` for `logger.error`, matching the rest of the module - soften the wording: a load failure is not proof the model is missing - add a test covering the non-network branch (the existing test only exercises the network path) --- tests/test_alignment.py | 26 ++++++++++++++++++++++++++ whispermlx/alignment.py | 14 ++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/tests/test_alignment.py b/tests/test_alignment.py index 82ba04f88..de0611c3f 100644 --- a/tests/test_alignment.py +++ b/tests/test_alignment.py @@ -293,6 +293,32 @@ def test_network_error_warns_about_cache_only_flag(self, mocker): warning_text = " ".join(str(a) for a in mock_logger.warning.call_args[0]) assert "--model_cache_only" in warning_text + def test_non_network_error_is_surfaced(self, mocker): + """A non-network failure must keep its own cause in the raised message. + + A partially downloaded cache raises OSError about a missing + preprocessor_config.json. Reporting that as "model could not be found" + sends people looking for a model that exists and loads fine on retry. + """ + import torchaudio + + import whispermlx.alignment as alignment_module + from whispermlx.alignment import load_align_model + + mocker.patch.object(torchaudio.pipelines, "__all__", []) + mocker.patch( + "whispermlx.alignment.Wav2Vec2Processor.from_pretrained", + side_effect=OSError("Can't load feature extractor for 'some/model'"), + ) + mock_logger = mocker.patch.object(alignment_module, "logger") + + with pytest.raises(ValueError, match="Can't load feature extractor"): + load_align_model("ru", "cpu") + + error_text = " ".join(str(a) for a in mock_logger.error.call_args[0]) + assert "OSError" in error_text + assert "not necessarily a missing model" in error_text + def test_explicit_model_name_skips_default_lookup(self, mocker): """Explicit model_name should bypass default language mapping.""" import torchaudio diff --git a/whispermlx/alignment.py b/whispermlx/alignment.py index b847f8af1..d39131120 100644 --- a/whispermlx/alignment.py +++ b/whispermlx/alignment.py @@ -132,12 +132,18 @@ def load_align_model( "re-run with --model_cache_only to load from local cache." ) else: - print(e) - print( - "Error loading model from huggingface, check https://huggingface.co/models for finetuned wav2vec2.0 models" + logger.error( + f"Failed to load align_model {model_name!r}: {type(e).__name__}: {e}\n" + "If the repo name is correct, this is not necessarily a missing model — " + "an interrupted download, a gated repo or a cache permission problem " + "raises here too. See https://huggingface.co/models for finetuned " + "wav2vec2.0 models." ) raise ValueError( - f'The chosen align_model "{model_name}" could not be found in huggingface (https://huggingface.co/models) or torchaudio (https://pytorch.org/audio/stable/pipelines.html#id14)' + f'Could not load align_model "{model_name}" ' + f"({type(e).__name__}: {e}). If the name is correct, check that the model is " + "reachable on huggingface (https://huggingface.co/models) or is a torchaudio " + "pipeline (https://pytorch.org/audio/stable/pipelines.html#id14)." ) from e pipeline_type = "huggingface" align_model = align_model.to(device)