MCPcopy Create free account
hub / github.com/abetlen/llama-cpp-python / from_pretrained

Method from_pretrained

llama_cpp/llama_chat_format.py:3692–3771  ·  view source on GitHub ↗
(
        cls,
        repo_id: str,
        filename: Optional[str],
        local_dir: Optional[Union[str, os.PathLike[str]]] = None,
        local_dir_use_symlinks: Union[bool, Literal["auto"]] = "auto",
        cache_dir: Optional[Union[str, os.PathLike[str]]] = None,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

3690
3691 @classmethod
3692 def from_pretrained(
3693 cls,
3694 repo_id: str,
3695 filename: Optional[str],
3696 local_dir: Optional[Union[str, os.PathLike[str]]] = None,
3697 local_dir_use_symlinks: Union[bool, Literal["auto"]] = "auto",
3698 cache_dir: Optional[Union[str, os.PathLike[str]]] = None,
3699 **kwargs: Any,
3700 ) -> "MTMDChatHandler":
3701 import fnmatch
3702 from pathlib import Path
3703
3704 try:
3705 from huggingface_hub import hf_hub_download, HfFileSystem # type: ignore
3706 from huggingface_hub.utils import validate_repo_id # type: ignore
3707 except ImportError:
3708 raise ImportError(
3709 "Llama.from_pretrained requires the huggingface-hub package. "
3710 "You can install it with `pip install huggingface-hub`."
3711 )
3712
3713 validate_repo_id(repo_id)
3714
3715 hffs = HfFileSystem()
3716
3717 files = [
3718 file["name"] if isinstance(file, dict) else file
3719 for file in hffs.ls(repo_id) # type: ignore
3720 ]
3721
3722 file_list: List[str] = []
3723 for file in files:
3724 rel_path = Path(file).relative_to(repo_id)
3725 file_list.append(str(rel_path))
3726
3727 matching_files = [file for file in file_list if fnmatch.fnmatch(file, filename)] # type: ignore
3728
3729 if len(matching_files) == 0:
3730 raise ValueError(
3731 f"No file found in {repo_id} that match {filename}\n\n"
3732 f"Available Files:\n{json.dumps(file_list)}"
3733 )
3734
3735 if len(matching_files) > 1:
3736 raise ValueError(
3737 f"Multiple files found in {repo_id} matching {filename}\n\n"
3738 f"Available Files:\n{json.dumps(files)}"
3739 )
3740
3741 (matching_file,) = matching_files
3742
3743 subfolder = str(Path(matching_file).parent)
3744 filename = Path(matching_file).name
3745
3746 hf_hub_download(
3747 repo_id=repo_id,
3748 filename=filename,
3749 subfolder=subfolder,

Callers 1

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected