2626from huggingface_hub .constants import ENDPOINT , REPO_TYPES_URL_PREFIXES
2727from requests .auth import HTTPBasicAuth
2828
29- from .utils import hf_raise_for_status , http_backoff , validate_hf_hub_args
29+ from .utils import (
30+ get_token_to_send ,
31+ hf_raise_for_status ,
32+ http_backoff ,
33+ validate_hf_hub_args ,
34+ )
3035from .utils .sha import sha256 , sha_fileobj
3136
3237
@@ -132,7 +137,7 @@ def _validate_batch_error(lfs_batch_error: dict):
132137@validate_hf_hub_args
133138def post_lfs_batch_info (
134139 upload_infos : Iterable [UploadInfo ],
135- token : str ,
140+ token : Optional [ str ] ,
136141 repo_type : str ,
137142 repo_id : str ,
138143 endpoint : Optional [str ] = None ,
@@ -151,7 +156,7 @@ def post_lfs_batch_info(
151156 repo_id (`str`):
152157 A namespace (user or an organization) and a repo name separated
153158 by a `/`.
154- token (`str`):
159+ token (`str`, *optional* ):
155160 An authentication token ( See https://huggingface.co/settings/tokens )
156161
157162 Returns:
@@ -187,7 +192,10 @@ def post_lfs_batch_info(
187192 ],
188193 "hash_algo" : "sha256" ,
189194 },
190- auth = HTTPBasicAuth ("access_token" , token ),
195+ auth = HTTPBasicAuth (
196+ "access_token" ,
197+ get_token_to_send (token or True ), # Token must be provided or retrieved
198+ ),
191199 )
192200 hf_raise_for_status (resp )
193201 batch_info = resp .json ()
@@ -207,7 +215,7 @@ def lfs_upload(
207215 upload_info : UploadInfo ,
208216 upload_action : dict ,
209217 verify_action : Optional [dict ],
210- token : str ,
218+ token : Optional [ str ] ,
211219):
212220 """
213221 Uploads a file using the git lfs protocol and determines automatically whether or not
@@ -225,7 +233,7 @@ def lfs_upload(
225233 The `verify` action from the LFS Batch endpoint. Must contain
226234 a `href` field, and optionally a `header` field. The `href` URL will
227235 be called after a successful upload.
228- token (`str`):
236+ token (`str`, *optional* ):
229237 A [user access token](https://hf.co/settings/tokens) to authenticate requests
230238 against the Hub.
231239
@@ -268,7 +276,11 @@ def lfs_upload(
268276 if verify_action is not None :
269277 verify_resp = requests .post (
270278 verify_action ["href" ],
271- auth = HTTPBasicAuth (username = "USER" , password = token ),
279+ auth = HTTPBasicAuth (
280+ username = "USER" ,
281+ # Token must be provided or retrieved
282+ password = get_token_to_send (token or True ),
283+ ),
272284 json = {"oid" : upload_info .sha256 .hex (), "size" : upload_info .size },
273285 )
274286 hf_raise_for_status (verify_resp )
0 commit comments