diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index b8e793c65d..23e5558933 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -4237,6 +4237,7 @@ def hf_hub_download( proxies: Optional[Dict] = None, etag_timeout: float = 10, resume_download: bool = False, + token: Optional[Union[str, bool]] = None, local_files_only: bool = False, legacy_cache_layout: bool = False, ) -> str: @@ -4322,6 +4323,11 @@ def hf_hub_download( data before giving up which is passed to `requests.request`. resume_download (`bool`, *optional*, defaults to `False`): If `True`, resume a previously interrupted download. + token (`bool` or `str`, *optional*): + A valid authentication token (see https://huggingface.co/settings/token). + If `None` or `True` and machine is logged in (through `huggingface-cli login` + or [`~huggingface_hub.login`]), token will be retrieved from the cache. + If `False`, token is not sent in the request header. local_files_only (`bool`, *optional*, defaults to `False`): If `True`, avoid downloading the file and return the path to the local cached file if it exists. @@ -4358,6 +4364,10 @@ def hf_hub_download( """ from .file_download import hf_hub_download + if token is None: + # Cannot do `token = token or self.token` as token can be `False`. + token = self.token + return hf_hub_download( repo_id=repo_id, filename=filename, @@ -4376,7 +4386,7 @@ def hf_hub_download( proxies=proxies, etag_timeout=etag_timeout, resume_download=resume_download, - token=self.token, + token=token, local_files_only=local_files_only, legacy_cache_layout=legacy_cache_layout, ) @@ -4395,6 +4405,7 @@ def snapshot_download( etag_timeout: float = 10, resume_download: bool = False, force_download: bool = False, + token: Optional[Union[str, bool]] = None, local_files_only: bool = False, allow_patterns: Optional[Union[List[str], str]] = None, ignore_patterns: Optional[Union[List[str], str]] = None, @@ -4455,6 +4466,11 @@ def snapshot_download( If `True`, resume a previously interrupted download. force_download (`bool`, *optional*, defaults to `False`): Whether the file should be downloaded even if it already exists in the local cache. + token (`bool` or `str`, *optional*): + A valid authentication token (see https://huggingface.co/settings/token). + If `None` or `True` and machine is logged in (through `huggingface-cli login` + or [`~huggingface_hub.login`]), token will be retrieved from the cache. + If `False`, token is not sent in the request header. local_files_only (`bool`, *optional*, defaults to `False`): If `True`, avoid downloading the file and return the path to the local cached file if it exists. @@ -4490,6 +4506,10 @@ def snapshot_download( """ from ._snapshot_download import snapshot_download + if token is None: + # Cannot do `token = token or self.token` as token can be `False`. + token = self.token + return snapshot_download( repo_id=repo_id, repo_type=repo_type, @@ -4505,7 +4525,7 @@ def snapshot_download( etag_timeout=etag_timeout, resume_download=resume_download, force_download=force_download, - token=self.token, + token=token, local_files_only=local_files_only, allow_patterns=allow_patterns, ignore_patterns=ignore_patterns,