From 358d373691c95205021bd4bbf28cde7ce4d10030 Mon Sep 17 00:00:00 2001 From: Jordi Mas Date: Thu, 20 Apr 2023 14:26:06 +0200 Subject: [PATCH] Allow specifying local_files_only to prevent checking the Internet everytime (#166) --- faster_whisper/transcribe.py | 7 ++++++- faster_whisper/utils.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/faster_whisper/transcribe.py b/faster_whisper/transcribe.py index 6d31271..2c544fc 100644 --- a/faster_whisper/transcribe.py +++ b/faster_whisper/transcribe.py @@ -73,6 +73,7 @@ class WhisperModel: cpu_threads: int = 0, num_workers: int = 1, download_root: Optional[str] = None, + local_files_only: Optional[bool] = False, ): """Initializes the Whisper model. @@ -96,13 +97,17 @@ class WhisperModel: This can improve the global throughput at the cost of increased memory usage. download_root: Directory where the model should be saved. If not set, the model is saved in the standard Hugging Face cache directory. + local_files_only: If True, avoid downloading the file and return the path to the + local cached file if it exists. """ self.logger = get_logger() if os.path.isdir(model_size_or_path): model_path = model_size_or_path else: - model_path = download_model(model_size_or_path, download_root) + model_path = download_model( + model_size_or_path, download_root, local_files_only + ) self.model = ctranslate2.models.Whisper( model_path, diff --git a/faster_whisper/utils.py b/faster_whisper/utils.py index 66c7161..649906a 100644 --- a/faster_whisper/utils.py +++ b/faster_whisper/utils.py @@ -31,7 +31,11 @@ def get_logger(): return logging.getLogger("faster_whisper") -def download_model(size: str, output_dir: Optional[str] = None): +def download_model( + size: str, + output_dir: Optional[str] = None, + local_files_only: Optional[bool] = False, +): """Downloads a CTranslate2 Whisper model from the Hugging Face Hub. The model is downloaded from https://huggingface.co/guillaumekln. @@ -41,6 +45,8 @@ def download_model(size: str, output_dir: Optional[str] = None): medium, medium.en, large-v1, or large-v2). output_dir: Directory where the model should be saved. If not set, the model is saved in the standard Hugging Face cache directory. + local_files_only: If True, avoid downloading the file and return the path to the local + cached file if it exists. Returns: The path to the downloaded model. @@ -55,7 +61,7 @@ def download_model(size: str, output_dir: Optional[str] = None): repo_id = "guillaumekln/faster-whisper-%s" % size kwargs = {} - + kwargs["local_files_only"] = local_files_only if output_dir is not None: kwargs["local_dir"] = output_dir kwargs["local_dir_use_symlinks"] = False