Skip to content

Commit 595721c

Browse files
Wauplinclaude
andauthored
Pass library_name/version to Hub calls via a shared HfApi (#46318)
* Route Hub calls through a shared library-tagged HfApi instance Introduce a `hf_api()` helper in `utils/hub.py` returning a shared `HfApi(library_name="transformers", library_version=__version__)` and route repo-management, metadata and lightweight download calls through it so the library info is attributed consistently on Hub requests. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * Fix mocked offline test to patch HfApi methods `list_repo_templates` now calls `list_repo_tree`/`snapshot_download` via the shared `hf_api()` instance, so the offline test must patch those methods on `HfApi` rather than the (now removed) module-level functions. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * address comment * fix * unreleasted change --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 0f0036c commit 595721c

21 files changed

Lines changed: 80 additions & 81 deletions

‎src/transformers/backbone_utils.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import functools
1919
import inspect
2020

21-
from huggingface_hub import repo_exists
22-
23-
from .utils import logging
21+
from .utils import hf_api, logging
2422
from .utils.output_capturing import maybe_install_capturing_hooks
2523

2624

@@ -334,7 +332,7 @@ def consolidate_backbone_kwargs_to_config(
334332
):
335333
backbone_config = CONFIG_MAPPING["timm_backbone"](backbone=backbone, **timm_default_kwargs)
336334
elif backbone is not None and backbone_config is None:
337-
if repo_exists(backbone):
335+
if hf_api().repo_exists(backbone):
338336
config_dict, _ = PreTrainedConfig.get_config_dict(backbone)
339337
config_class = CONFIG_MAPPING[config_dict["model_type"]]
340338
config_dict.update(backbone_kwargs)

‎src/transformers/configuration_utils.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from functools import wraps
2424
from typing import TYPE_CHECKING, Any, ClassVar, Literal, TypeVar, Union
2525

26-
from huggingface_hub import create_repo
2726
from huggingface_hub.dataclasses import strict
2827
from packaging import version
2928
from typing_extensions import dataclass_transform
@@ -39,6 +38,7 @@
3938
cached_file,
4039
copy_func,
4140
extract_commit_hash,
41+
hf_api,
4242
is_torch_available,
4343
logging,
4444
)
@@ -521,7 +521,7 @@ def save_pretrained(self, save_directory: str | os.PathLike, push_to_hub: bool =
521521
if push_to_hub:
522522
commit_message = kwargs.pop("commit_message", None)
523523
repo_id = kwargs.pop("repo_id", save_directory.split(os.path.sep)[-1])
524-
repo_id = create_repo(repo_id, exist_ok=True, **kwargs).repo_id
524+
repo_id = hf_api().create_repo(repo_id, exist_ok=True, **kwargs).repo_id
525525
files_timestamps = self._get_files_timestamps(save_directory)
526526

527527
# This attribute is important to know on load, but should not be serialized on save.

‎src/transformers/feature_extraction_utils.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from typing import TYPE_CHECKING, Any, TypeVar, Union
2323

2424
import numpy as np
25-
from huggingface_hub import create_repo, is_offline_mode
25+
from huggingface_hub import is_offline_mode
2626

2727
from .dynamic_module_utils import custom_object_save
2828
from .utils import (
@@ -40,7 +40,7 @@
4040
requires_backends,
4141
safe_load_json_file,
4242
)
43-
from .utils.hub import cached_file
43+
from .utils.hub import cached_file, hf_api
4444

4545

4646
if TYPE_CHECKING:
@@ -403,7 +403,7 @@ def save_pretrained(self, save_directory: str | os.PathLike, push_to_hub: bool =
403403
if push_to_hub:
404404
commit_message = kwargs.pop("commit_message", None)
405405
repo_id = kwargs.pop("repo_id", save_directory.split(os.path.sep)[-1])
406-
repo_id = create_repo(repo_id, exist_ok=True, **kwargs).repo_id
406+
repo_id = hf_api().create_repo(repo_id, exist_ok=True, **kwargs).repo_id
407407
files_timestamps = self._get_files_timestamps(save_directory)
408408

409409
# If we have a custom config, we copy the file defining it in the folder and set the attributes so it can be

‎src/transformers/generation/configuration_utils.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
from dataclasses import dataclass, is_dataclass
2222
from typing import TYPE_CHECKING, Any, Optional, Union
2323

24-
from huggingface_hub import create_repo
25-
2624
from .. import __version__
2725
from ..utils import (
2826
GENERATION_CONFIG_NAME,
2927
ExplicitEnum,
3028
PushToHubMixin,
3129
cached_file,
3230
extract_commit_hash,
31+
hf_api,
3332
is_torch_available,
3433
logging,
3534
)
@@ -866,7 +865,7 @@ def save_pretrained(
866865
if push_to_hub:
867866
commit_message = kwargs.pop("commit_message", None)
868867
repo_id = kwargs.pop("repo_id", str(save_directory).split(os.path.sep)[-1])
869-
repo_id = create_repo(repo_id, exist_ok=True, **kwargs).repo_id
868+
repo_id = hf_api().create_repo(repo_id, exist_ok=True, **kwargs).repo_id
870869
files_timestamps = self._get_files_timestamps(save_directory)
871870

872871
output_config_file = os.path.join(save_directory, config_file_name)

‎src/transformers/image_processing_base.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import Any, TypeVar
1919

2020
import numpy as np
21-
from huggingface_hub import create_repo, is_offline_mode
21+
from huggingface_hub import is_offline_mode
2222

2323
from .dynamic_module_utils import custom_object_save
2424
from .feature_extraction_utils import BatchFeature as BaseBatchFeature
@@ -31,7 +31,7 @@
3131
logging,
3232
safe_load_json_file,
3333
)
34-
from .utils.hub import cached_file
34+
from .utils.hub import cached_file, hf_api
3535

3636

3737
ImageProcessorType = TypeVar("ImageProcessorType", bound="ImageProcessingMixin")
@@ -203,7 +203,7 @@ def save_pretrained(self, save_directory: str | os.PathLike, push_to_hub: bool =
203203
if push_to_hub:
204204
commit_message = kwargs.pop("commit_message", None)
205205
repo_id = kwargs.pop("repo_id", save_directory.split(os.path.sep)[-1])
206-
repo_id = create_repo(repo_id, exist_ok=True, **kwargs).repo_id
206+
repo_id = hf_api().create_repo(repo_id, exist_ok=True, **kwargs).repo_id
207207
files_timestamps = self._get_files_timestamps(save_directory)
208208

209209
# If we have a custom config, we copy the file defining it in the folder and set the attributes so it can be

‎src/transformers/modelcard.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import httpx
2222
import yaml
23-
from huggingface_hub import is_offline_mode, model_info
23+
from huggingface_hub import is_offline_mode
2424
from huggingface_hub.errors import OfflineModeIsEnabled
2525
from huggingface_hub.utils import HFValidationError
2626

@@ -44,6 +44,7 @@
4444
)
4545
from .training_args import ParallelMode
4646
from .utils import (
47+
hf_api,
4748
is_datasets_available,
4849
is_tokenizers_available,
4950
is_torch_available,
@@ -194,7 +195,7 @@ def __post_init__(self):
194195
and len(self.finetuned_from) > 0
195196
):
196197
try:
197-
info = model_info(self.finetuned_from)
198+
info = hf_api().model_info(self.finetuned_from)
198199
for tag in info.tags:
199200
if tag.startswith("license:"):
200201
self.license = tag[8:]

‎src/transformers/modeling_utils.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from zipfile import is_zipfile
3434

3535
import torch
36-
from huggingface_hub import create_repo, is_offline_mode, split_torch_state_dict_into_shards
36+
from huggingface_hub import is_offline_mode, split_torch_state_dict_into_shards
3737
from packaging import version
3838
from safetensors import safe_open
3939
from safetensors.torch import load as _safe_load_bytes
@@ -122,7 +122,7 @@
122122
logging,
123123
)
124124
from .utils.generic import GeneralInterface, is_flash_attention_requested, split_attention_implementation
125-
from .utils.hub import DownloadKwargs, create_and_tag_model_card, get_checkpoint_shard_files
125+
from .utils.hub import DownloadKwargs, create_and_tag_model_card, get_checkpoint_shard_files, hf_api
126126
from .utils.import_utils import (
127127
is_flash_attn_greater_or_equal,
128128
is_huggingface_hub_greater_or_equal,
@@ -3397,7 +3397,7 @@ def save_pretrained(
33973397
commit_message = kwargs.pop("commit_message", None)
33983398
repo_id = kwargs.pop("repo_id", save_directory_path.split(os.path.sep)[-1])
33993399
create_pr = kwargs.pop("create_pr", False)
3400-
repo_id = create_repo(repo_id, exist_ok=True, **kwargs).repo_id
3400+
repo_id = hf_api().create_repo(repo_id, exist_ok=True, **kwargs).repo_id
34013401
files_timestamps = self._get_files_timestamps(save_directory)
34023402

34033403
metadata = {}

‎src/transformers/models/auto/auto_factory.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
from collections.abc import Iterator
2222
from typing import Any, TypeVar
2323

24-
from huggingface_hub import repo_exists
25-
2624
from ...configuration_utils import PreTrainedConfig
2725
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
2826
from ...utils import (
@@ -31,6 +29,7 @@
3129
copy_func,
3230
extract_commit_hash,
3331
find_adapter_config_file,
32+
hf_api,
3433
is_peft_available,
3534
is_torch_available,
3635
logging,
@@ -463,7 +462,7 @@ def _load_timm_backbone_from_pretrained(cls, pretrained_model_name_or_path, *mod
463462
@classmethod
464463
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
465464
kwargs.pop("use_timm_backbone", None)
466-
if not repo_exists(pretrained_model_name_or_path):
465+
if not hf_api().repo_exists(pretrained_model_name_or_path):
467466
return cls._load_timm_backbone_from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs)
468467

469468
return super().from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs)

‎src/transformers/models/oneformer/image_processing_oneformer.py‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Union
1919

2020
import torch
21+
from huggingface_hub.utils import RepositoryNotFoundError
2122
from torch import nn
2223
from torchvision.transforms.v2 import functional as tvF
2324

@@ -34,19 +35,7 @@
3435
get_max_height_width,
3536
)
3637
from ...processing_utils import ImagesKwargs, Unpack
37-
from ...utils import (
38-
TensorType,
39-
auto_docstring,
40-
logging,
41-
)
42-
43-
44-
try:
45-
from huggingface_hub import hf_hub_download
46-
from huggingface_hub.utils import RepositoryNotFoundError
47-
except ImportError:
48-
hf_hub_download = None
49-
RepositoryNotFoundError = None
38+
from ...utils import TensorType, auto_docstring, hf_api, logging
5039

5140

5241
logger = logging.get_logger(__name__)
@@ -96,15 +85,11 @@ def load_metadata(repo_id, class_info_file):
9685
if not os.path.exists(fname) or not os.path.isfile(fname):
9786
if repo_id is None:
9887
raise ValueError(f"Could not file {fname} locally. repo_id must be defined if loading from the hub")
99-
if hf_hub_download is None:
100-
raise ImportError(
101-
"huggingface_hub is required to download metadata files. Install it with `pip install huggingface_hub`"
102-
)
10388
# We try downloading from a dataset by default for backward compatibility
10489
try:
105-
fname = hf_hub_download(repo_id, class_info_file, repo_type="dataset")
90+
fname = hf_api().hf_hub_download(repo_id, class_info_file, repo_type="dataset")
10691
except RepositoryNotFoundError:
107-
fname = hf_hub_download(repo_id, class_info_file)
92+
fname = hf_api().hf_hub_download(repo_id, class_info_file)
10893

10994
with open(fname, "r") as f:
11095
class_info = json.load(f)

‎src/transformers/models/pix2struct/image_processing_pil_pix2struct.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import textwrap
1919

2020
import numpy as np
21-
from huggingface_hub import hf_hub_download
2221
from PIL import Image, ImageDraw, ImageFont
2322

2423
from ...image_processing_backends import PilBackend
@@ -27,6 +26,7 @@
2726
from ...image_utils import ChannelDimension, ImageInput, SizeDict
2827
from ...processing_utils import ImagesKwargs, Unpack
2928
from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends
29+
from ...utils.hub import hf_api
3030
from ...utils.import_utils import requires
3131

3232

@@ -109,7 +109,7 @@ def render_text(
109109
elif font_path is not None:
110110
font = font_path
111111
else:
112-
font = hf_hub_download(DEFAULT_FONT_PATH, "Arial.TTF")
112+
font = hf_api().hf_hub_download(DEFAULT_FONT_PATH, "Arial.TTF")
113113
font = ImageFont.truetype(font, encoding="UTF-8", size=text_size)
114114

115115
# Use a temporary canvas to determine the width and height in pixels when

0 commit comments

Comments
 (0)