diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index 0d5a18ebdd..963eceadb2 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -4446,36 +4446,28 @@ def preupload_lfs_files( "(ignored by gitignore file)." ) - # Check if the hf_xet library is installed - hf_xet_spec = importlib.util.find_spec("hf_xet") - if hf_xet_spec is None: - # Upload files as LFS files - _upload_lfs_files( - additions=new_lfs_additions_to_upload, - repo_type=repo_type, - repo_id=repo_id, - headers=headers, - endpoint=self.endpoint, - num_threads=num_threads, - # If `create_pr`, we don't want to check user permission on the revision as users with read permission - # should still be able to create PRs even if they don't have write permission on the target branch of the - # PR (i.e. `revision`). - revision=revision if not create_pr else None, - ) + # Prepare upload parameters + upload_kwargs = { + "additions": new_lfs_additions_to_upload, + "repo_type": repo_type, + "repo_id": repo_id, + "headers": headers, + "endpoint": self.endpoint, + "num_threads": num_threads, + # If `create_pr`, we don't want to check user permission on the revision as users with read permission + # should still be able to create PRs even if they don't have write permission on the target branch of the + # PR (i.e. `revision`). + "revision": revision if not create_pr else None, + } + + # Upload files using Xet protocol if the library is installed. + # Otherwise, default back to LFS. + if importlib.util.find_spec("hf_xet") is not None: + _upload_xet_files(**upload_kwargs) else: - # Upload files as XET files. - _upload_xet_files( - additions=new_lfs_additions_to_upload, - repo_type=repo_type, - repo_id=repo_id, - headers=headers, - endpoint=self.endpoint, - # If `create_pr`, we don't want to check user permission on the revision as users with read permission - # should still be able to create PRs even if they don't have write permission on the target branch of the - # PR (i.e. `revision`). - revision=revision if not create_pr else None, - ) + _upload_lfs_files(**upload_kwargs, num_threads=num_threads) + # Free memory if requested. for addition in new_lfs_additions_to_upload: addition._is_uploaded = True if free_memory: