diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa7e917b..ebc2f1f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix: load cached projector files offline in the multimodal chat handlers' from_pretrained by @sohailshk in #XXXXX - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index 62e236415..5fcb71610 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -3195,7 +3195,12 @@ def from_pretrained( from pathlib import Path try: - from huggingface_hub import hf_hub_download, HfFileSystem # type: ignore + from huggingface_hub import ( # type: ignore + hf_hub_download, + HfFileSystem, + snapshot_download, + constants, + ) from huggingface_hub.utils import validate_repo_id # type: ignore except ImportError: raise ImportError( @@ -3205,12 +3210,32 @@ def from_pretrained( validate_repo_id(repo_id) - hffs = HfFileSystem() - - files = [ - file["name"] if isinstance(file, dict) else file - for file in hffs.ls(repo_id) # type: ignore - ] + if constants.HF_HUB_OFFLINE: + if ( + local_dir is not None + and Path(local_dir).is_dir() + and any(Path(local_dir).iterdir()) + ): + snapshot_path = Path(local_dir) + else: + snapshot_path = Path( + snapshot_download( + repo_id=repo_id, + cache_dir=cache_dir, + local_files_only=True, + ) + ) + files = [ + f"{repo_id}/{file.name}" + for file in snapshot_path.iterdir() + if file.is_file() + ] + else: + hffs = HfFileSystem() + files = [ + file["name"] if isinstance(file, dict) else file + for file in hffs.ls(repo_id) # type: ignore + ] # split each file into repo_id, subfolder, filename file_list: List[str] = [] @@ -3703,7 +3728,12 @@ def from_pretrained( from pathlib import Path try: - from huggingface_hub import hf_hub_download, HfFileSystem # type: ignore + from huggingface_hub import ( # type: ignore + hf_hub_download, + HfFileSystem, + snapshot_download, + constants, + ) from huggingface_hub.utils import validate_repo_id # type: ignore except ImportError: raise ImportError( @@ -3713,12 +3743,32 @@ def from_pretrained( validate_repo_id(repo_id) - hffs = HfFileSystem() - - files = [ - file["name"] if isinstance(file, dict) else file - for file in hffs.ls(repo_id) # type: ignore - ] + if constants.HF_HUB_OFFLINE: + if ( + local_dir is not None + and Path(local_dir).is_dir() + and any(Path(local_dir).iterdir()) + ): + snapshot_path = Path(local_dir) + else: + snapshot_path = Path( + snapshot_download( + repo_id=repo_id, + cache_dir=cache_dir, + local_files_only=True, + ) + ) + files = [ + f"{repo_id}/{file.name}" + for file in snapshot_path.iterdir() + if file.is_file() + ] + else: + hffs = HfFileSystem() + files = [ + file["name"] if isinstance(file, dict) else file + for file in hffs.ls(repo_id) # type: ignore + ] file_list: List[str] = [] for file in files: