fix(stt): tolerate a missing huggingface_hub when checking the whisper cache
The cache-first loader imported LocalEntryNotFoundError unconditionally; in environments without the optional huggingface_hub dependency every local transcription raised ModuleNotFoundError. Resolve the cache-miss exception lazily and fall back to its OSError base class when the package is absent.
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user