Skip to content

Commit

Permalink
Add token parameter to HfApi's snapshot_download and `hf_hub_do…
Browse files Browse the repository at this point in the history
…wnload` (#1717)

* Add `token` parameter to `HfApi`'s `snapshot_download` and `hf_hub_download`

* allow token=False

---------

Co-authored-by: Lucain Pouget <lucainp@gmail.com>
  • Loading branch information
mariosasko and Wauplin authored Oct 10, 2023
1 parent 6dd7ee8 commit 33b0405
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 33b0405

Please sign in to comment.