diff --git a/tests/tools/test_transcription_tools.py b/tests/tools/test_transcription_tools.py index 8494627d26..2a69c07433 100644 --- a/tests/tools/test_transcription_tools.py +++ b/tests/tools/test_transcription_tools.py @@ -366,9 +366,9 @@ class TestLocalModelLoading: @pytest.mark.parametrize("download_error", [None, "Got: ConnectTimeout: [Errno 110] Connection timed out"]) def test_cache_miss_falls_back_with_actionable_download_failure(self, download_error): - from huggingface_hub.errors import LocalEntryNotFoundError - from tools.transcription_local import _create_whisper_model + from tools.transcription_local import _create_whisper_model, _hub_cache_miss_error + LocalEntryNotFoundError = _hub_cache_miss_error() if download_error: download_error = LocalEntryNotFoundError(download_error) diff --git a/tools/transcription_local.py b/tools/transcription_local.py index 7fb966e4c9..db444f83bd 100644 --- a/tools/transcription_local.py +++ b/tools/transcription_local.py @@ -120,15 +120,27 @@ def _get_idle_unload_seconds(local_cfg: Dict[str, Any]) -> int: return max(_config_number(local_cfg, "unload_after_idle_seconds", 0, int), 0) +def _hub_cache_miss_error() -> type: + """Exception faster-whisper raises for a model missing from the local Hub cache. + + ``huggingface_hub`` is an optional dependency (it arrives with faster-whisper); when it is + absent, its ``LocalEntryNotFoundError`` base class ``OSError`` is the closest match. + """ + try: + from huggingface_hub.errors import LocalEntryNotFoundError + except ImportError: + return OSError + return LocalEntryNotFoundError + + def _create_whisper_model(model_name: str, *, device: str, compute_type: str): """Use a cached model without contacting the Hub, downloading only on a cache miss.""" from faster_whisper import WhisperModel - from huggingface_hub.errors import LocalEntryNotFoundError kwargs = {"device": device, "compute_type": compute_type} try: return WhisperModel(model_name, local_files_only=True, **kwargs) - except LocalEntryNotFoundError: + except _hub_cache_miss_error(): logger.info("faster-whisper model '%s' is not cached; downloading it from the Hugging Face Hub", model_name) # huggingface_hub surfaces every Hub/network failure as an OSError subclass