huggingface-hub 0.31.2__py3-none-any.whl → 0.31.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of huggingface-hub might be problematic. Click here for more details.
- huggingface_hub/__init__.py +1 -1
- huggingface_hub/constants.py +11 -0
- huggingface_hub/dataclasses.py +481 -0
- huggingface_hub/errors.py +29 -0
- huggingface_hub/hf_api.py +9 -1
- huggingface_hub/hf_file_system.py +4 -2
- huggingface_hub/serialization/_torch.py +18 -0
- huggingface_hub/utils/__init__.py +0 -1
- {huggingface_hub-0.31.2.dist-info → huggingface_hub-0.31.3.dist-info}/METADATA +1 -1
- {huggingface_hub-0.31.2.dist-info → huggingface_hub-0.31.3.dist-info}/RECORD +14 -13
- {huggingface_hub-0.31.2.dist-info → huggingface_hub-0.31.3.dist-info}/LICENSE +0 -0
- {huggingface_hub-0.31.2.dist-info → huggingface_hub-0.31.3.dist-info}/WHEEL +0 -0
- {huggingface_hub-0.31.2.dist-info → huggingface_hub-0.31.3.dist-info}/entry_points.txt +0 -0
- {huggingface_hub-0.31.2.dist-info → huggingface_hub-0.31.3.dist-info}/top_level.txt +0 -0
huggingface_hub/__init__.py
CHANGED
huggingface_hub/constants.py
CHANGED
|
@@ -82,6 +82,17 @@ INFERENCE_ENDPOINT = os.environ.get("HF_INFERENCE_ENDPOINT", "https://api-infere
|
|
|
82
82
|
INFERENCE_ENDPOINTS_ENDPOINT = "https://api.endpoints.huggingface.cloud/v2"
|
|
83
83
|
INFERENCE_CATALOG_ENDPOINT = "https://endpoints.huggingface.co/api/catalog"
|
|
84
84
|
|
|
85
|
+
# See https://api.endpoints.huggingface.cloud/#post-/v2/endpoint/-namespace-
|
|
86
|
+
INFERENCE_ENDPOINT_IMAGE_KEYS = [
|
|
87
|
+
"custom",
|
|
88
|
+
"huggingface",
|
|
89
|
+
"huggingfaceNeuron",
|
|
90
|
+
"llamacpp",
|
|
91
|
+
"tei",
|
|
92
|
+
"tgi",
|
|
93
|
+
"tgiNeuron",
|
|
94
|
+
]
|
|
95
|
+
|
|
85
96
|
# Proxy for third-party providers
|
|
86
97
|
INFERENCE_PROXY_TEMPLATE = "https://router.huggingface.co/{provider}"
|
|
87
98
|
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from dataclasses import _MISSING_TYPE, MISSING, Field, field, fields
|
|
3
|
+
from functools import wraps
|
|
4
|
+
from typing import (
|
|
5
|
+
Any,
|
|
6
|
+
Callable,
|
|
7
|
+
Dict,
|
|
8
|
+
List,
|
|
9
|
+
Literal,
|
|
10
|
+
Optional,
|
|
11
|
+
Tuple,
|
|
12
|
+
Type,
|
|
13
|
+
TypeVar,
|
|
14
|
+
Union,
|
|
15
|
+
get_args,
|
|
16
|
+
get_origin,
|
|
17
|
+
overload,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
from .errors import (
|
|
21
|
+
StrictDataclassClassValidationError,
|
|
22
|
+
StrictDataclassDefinitionError,
|
|
23
|
+
StrictDataclassFieldValidationError,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
Validator_T = Callable[[Any], None]
|
|
28
|
+
T = TypeVar("T")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# The overload decorator helps type checkers understand the different return types
|
|
32
|
+
@overload
|
|
33
|
+
def strict(cls: Type[T]) -> Type[T]: ...
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@overload
|
|
37
|
+
def strict(*, accept_kwargs: bool = False) -> Callable[[Type[T]], Type[T]]: ...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def strict(
|
|
41
|
+
cls: Optional[Type[T]] = None, *, accept_kwargs: bool = False
|
|
42
|
+
) -> Union[Type[T], Callable[[Type[T]], Type[T]]]:
|
|
43
|
+
"""
|
|
44
|
+
Decorator to add strict validation to a dataclass.
|
|
45
|
+
|
|
46
|
+
This decorator must be used on top of `@dataclass` to ensure IDEs and static typing tools
|
|
47
|
+
recognize the class as a dataclass.
|
|
48
|
+
|
|
49
|
+
Can be used with or without arguments:
|
|
50
|
+
- `@strict`
|
|
51
|
+
- `@strict(accept_kwargs=True)`
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
cls:
|
|
55
|
+
The class to convert to a strict dataclass.
|
|
56
|
+
accept_kwargs (`bool`, *optional*):
|
|
57
|
+
If True, allows arbitrary keyword arguments in `__init__`. Defaults to False.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
The enhanced dataclass with strict validation on field assignment.
|
|
61
|
+
|
|
62
|
+
Example:
|
|
63
|
+
```py
|
|
64
|
+
>>> from dataclasses import dataclass
|
|
65
|
+
>>> from huggingface_hub.dataclasses import as_validated_field, strict, validated_field
|
|
66
|
+
|
|
67
|
+
>>> @as_validated_field
|
|
68
|
+
>>> def positive_int(value: int):
|
|
69
|
+
... if not value >= 0:
|
|
70
|
+
... raise ValueError(f"Value must be positive, got {value}")
|
|
71
|
+
|
|
72
|
+
>>> @strict(accept_kwargs=True)
|
|
73
|
+
... @dataclass
|
|
74
|
+
... class User:
|
|
75
|
+
... name: str
|
|
76
|
+
... age: int = positive_int(default=10)
|
|
77
|
+
|
|
78
|
+
# Initialize
|
|
79
|
+
>>> User(name="John")
|
|
80
|
+
User(name='John', age=10)
|
|
81
|
+
|
|
82
|
+
# Extra kwargs are accepted
|
|
83
|
+
>>> User(name="John", age=30, lastname="Doe")
|
|
84
|
+
User(name='John', age=30, *lastname='Doe')
|
|
85
|
+
|
|
86
|
+
# Invalid type => raises
|
|
87
|
+
>>> User(name="John", age="30")
|
|
88
|
+
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
|
|
89
|
+
TypeError: Field 'age' expected int, got str (value: '30')
|
|
90
|
+
|
|
91
|
+
# Invalid value => raises
|
|
92
|
+
>>> User(name="John", age=-1)
|
|
93
|
+
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
|
|
94
|
+
ValueError: Value must be positive, got -1
|
|
95
|
+
```
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
def wrap(cls: Type[T]) -> Type[T]:
|
|
99
|
+
if not hasattr(cls, "__dataclass_fields__"):
|
|
100
|
+
raise StrictDataclassDefinitionError(
|
|
101
|
+
f"Class '{cls.__name__}' must be a dataclass before applying @strict."
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# List and store validators
|
|
105
|
+
field_validators: Dict[str, List[Validator_T]] = {}
|
|
106
|
+
for f in fields(cls): # type: ignore [arg-type]
|
|
107
|
+
validators = []
|
|
108
|
+
validators.append(_create_type_validator(f))
|
|
109
|
+
custom_validator = f.metadata.get("validator")
|
|
110
|
+
if custom_validator is not None:
|
|
111
|
+
if not isinstance(custom_validator, list):
|
|
112
|
+
custom_validator = [custom_validator]
|
|
113
|
+
for validator in custom_validator:
|
|
114
|
+
if not _is_validator(validator):
|
|
115
|
+
raise StrictDataclassDefinitionError(
|
|
116
|
+
f"Invalid validator for field '{f.name}': {validator}. Must be a callable taking a single argument."
|
|
117
|
+
)
|
|
118
|
+
validators.extend(custom_validator)
|
|
119
|
+
field_validators[f.name] = validators
|
|
120
|
+
cls.__validators__ = field_validators # type: ignore
|
|
121
|
+
|
|
122
|
+
# Override __setattr__ to validate fields on assignment
|
|
123
|
+
original_setattr = cls.__setattr__
|
|
124
|
+
|
|
125
|
+
def __strict_setattr__(self: Any, name: str, value: Any) -> None:
|
|
126
|
+
"""Custom __setattr__ method for strict dataclasses."""
|
|
127
|
+
# Run all validators
|
|
128
|
+
for validator in self.__validators__.get(name, []):
|
|
129
|
+
try:
|
|
130
|
+
validator(value)
|
|
131
|
+
except (ValueError, TypeError) as e:
|
|
132
|
+
raise StrictDataclassFieldValidationError(field=name, cause=e) from e
|
|
133
|
+
|
|
134
|
+
# If validation passed, set the attribute
|
|
135
|
+
original_setattr(self, name, value)
|
|
136
|
+
|
|
137
|
+
cls.__setattr__ = __strict_setattr__ # type: ignore[method-assign]
|
|
138
|
+
|
|
139
|
+
if accept_kwargs:
|
|
140
|
+
# (optional) Override __init__ to accept arbitrary keyword arguments
|
|
141
|
+
original_init = cls.__init__
|
|
142
|
+
|
|
143
|
+
@wraps(original_init)
|
|
144
|
+
def __init__(self, **kwargs: Any) -> None:
|
|
145
|
+
# Extract only the fields that are part of the dataclass
|
|
146
|
+
dataclass_fields = {f.name for f in fields(cls)} # type: ignore [arg-type]
|
|
147
|
+
standard_kwargs = {k: v for k, v in kwargs.items() if k in dataclass_fields}
|
|
148
|
+
|
|
149
|
+
# Call the original __init__ with standard fields
|
|
150
|
+
original_init(self, **standard_kwargs)
|
|
151
|
+
|
|
152
|
+
# Add any additional kwargs as attributes
|
|
153
|
+
for name, value in kwargs.items():
|
|
154
|
+
if name not in dataclass_fields:
|
|
155
|
+
self.__setattr__(name, value)
|
|
156
|
+
|
|
157
|
+
cls.__init__ = __init__ # type: ignore[method-assign]
|
|
158
|
+
|
|
159
|
+
# (optional) Override __repr__ to include additional kwargs
|
|
160
|
+
original_repr = cls.__repr__
|
|
161
|
+
|
|
162
|
+
@wraps(original_repr)
|
|
163
|
+
def __repr__(self) -> str:
|
|
164
|
+
# Call the original __repr__ to get the standard fields
|
|
165
|
+
standard_repr = original_repr(self)
|
|
166
|
+
|
|
167
|
+
# Get additional kwargs
|
|
168
|
+
additional_kwargs = [
|
|
169
|
+
# add a '*' in front of additional kwargs to let the user know they are not part of the dataclass
|
|
170
|
+
f"*{k}={v!r}"
|
|
171
|
+
for k, v in self.__dict__.items()
|
|
172
|
+
if k not in cls.__dataclass_fields__ # type: ignore [attr-defined]
|
|
173
|
+
]
|
|
174
|
+
additional_repr = ", ".join(additional_kwargs)
|
|
175
|
+
|
|
176
|
+
# Combine both representations
|
|
177
|
+
return f"{standard_repr[:-1]}, {additional_repr})" if additional_kwargs else standard_repr
|
|
178
|
+
|
|
179
|
+
cls.__repr__ = __repr__ # type: ignore [method-assign]
|
|
180
|
+
|
|
181
|
+
# List all public methods starting with `validate_` => class validators.
|
|
182
|
+
class_validators = []
|
|
183
|
+
|
|
184
|
+
for name in dir(cls):
|
|
185
|
+
if not name.startswith("validate_"):
|
|
186
|
+
continue
|
|
187
|
+
method = getattr(cls, name)
|
|
188
|
+
if not callable(method):
|
|
189
|
+
continue
|
|
190
|
+
if len(inspect.signature(method).parameters) != 1:
|
|
191
|
+
raise StrictDataclassDefinitionError(
|
|
192
|
+
f"Class '{cls.__name__}' has a class validator '{name}' that takes more than one argument."
|
|
193
|
+
" Class validators must take only 'self' as an argument. Methods starting with 'validate_'"
|
|
194
|
+
" are considered to be class validators."
|
|
195
|
+
)
|
|
196
|
+
class_validators.append(method)
|
|
197
|
+
|
|
198
|
+
cls.__class_validators__ = class_validators # type: ignore [attr-defined]
|
|
199
|
+
|
|
200
|
+
# Add `validate` method to the class, but first check if it already exists
|
|
201
|
+
def validate(self: T) -> None:
|
|
202
|
+
"""Run class validators on the instance."""
|
|
203
|
+
for validator in cls.__class_validators__: # type: ignore [attr-defined]
|
|
204
|
+
try:
|
|
205
|
+
validator(self)
|
|
206
|
+
except (ValueError, TypeError) as e:
|
|
207
|
+
raise StrictDataclassClassValidationError(validator=validator.__name__, cause=e) from e
|
|
208
|
+
|
|
209
|
+
# Hack to be able to raise if `.validate()` already exists except if it was created by this decorator on a parent class
|
|
210
|
+
# (in which case we just override it)
|
|
211
|
+
validate.__is_defined_by_strict_decorator__ = True # type: ignore [attr-defined]
|
|
212
|
+
|
|
213
|
+
if hasattr(cls, "validate"):
|
|
214
|
+
if not getattr(cls.validate, "__is_defined_by_strict_decorator__", False): # type: ignore [attr-defined]
|
|
215
|
+
raise StrictDataclassDefinitionError(
|
|
216
|
+
f"Class '{cls.__name__}' already implements a method called 'validate'."
|
|
217
|
+
" This method name is reserved when using the @strict decorator on a dataclass."
|
|
218
|
+
" If you want to keep your own method, please rename it."
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
cls.validate = validate # type: ignore
|
|
222
|
+
|
|
223
|
+
# Run class validators after initialization
|
|
224
|
+
initial_init = cls.__init__
|
|
225
|
+
|
|
226
|
+
@wraps(initial_init)
|
|
227
|
+
def init_with_validate(self, *args, **kwargs) -> None:
|
|
228
|
+
"""Run class validators after initialization."""
|
|
229
|
+
initial_init(self, *args, **kwargs) # type: ignore [call-arg]
|
|
230
|
+
cls.validate(self) # type: ignore [attr-defined]
|
|
231
|
+
|
|
232
|
+
setattr(cls, "__init__", init_with_validate)
|
|
233
|
+
|
|
234
|
+
return cls
|
|
235
|
+
|
|
236
|
+
# Return wrapped class or the decorator itself
|
|
237
|
+
return wrap(cls) if cls is not None else wrap
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def validated_field(
|
|
241
|
+
validator: Union[List[Validator_T], Validator_T],
|
|
242
|
+
default: Union[Any, _MISSING_TYPE] = MISSING,
|
|
243
|
+
default_factory: Union[Callable[[], Any], _MISSING_TYPE] = MISSING,
|
|
244
|
+
init: bool = True,
|
|
245
|
+
repr: bool = True,
|
|
246
|
+
hash: Optional[bool] = None,
|
|
247
|
+
compare: bool = True,
|
|
248
|
+
metadata: Optional[Dict] = None,
|
|
249
|
+
**kwargs: Any,
|
|
250
|
+
) -> Any:
|
|
251
|
+
"""
|
|
252
|
+
Create a dataclass field with a custom validator.
|
|
253
|
+
|
|
254
|
+
Useful to apply several checks to a field. If only applying one rule, check out the [`as_validated_field`] decorator.
|
|
255
|
+
|
|
256
|
+
Args:
|
|
257
|
+
validator (`Callable` or `List[Callable]`):
|
|
258
|
+
A method that takes a value as input and raises ValueError/TypeError if the value is invalid.
|
|
259
|
+
Can be a list of validators to apply multiple checks.
|
|
260
|
+
**kwargs:
|
|
261
|
+
Additional arguments to pass to `dataclasses.field()`.
|
|
262
|
+
|
|
263
|
+
Returns:
|
|
264
|
+
A field with the validator attached in metadata
|
|
265
|
+
"""
|
|
266
|
+
if not isinstance(validator, list):
|
|
267
|
+
validator = [validator]
|
|
268
|
+
if metadata is None:
|
|
269
|
+
metadata = {}
|
|
270
|
+
metadata["validator"] = validator
|
|
271
|
+
return field( # type: ignore
|
|
272
|
+
default=default,
|
|
273
|
+
default_factory=default_factory,
|
|
274
|
+
init=init,
|
|
275
|
+
repr=repr,
|
|
276
|
+
hash=hash,
|
|
277
|
+
compare=compare,
|
|
278
|
+
metadata=metadata,
|
|
279
|
+
**kwargs,
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def as_validated_field(validator: Validator_T):
|
|
284
|
+
"""
|
|
285
|
+
Decorates a validator function as a [`validated_field`] (i.e. a dataclass field with a custom validator).
|
|
286
|
+
|
|
287
|
+
Args:
|
|
288
|
+
validator (`Callable`):
|
|
289
|
+
A method that takes a value as input and raises ValueError/TypeError if the value is invalid.
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
def _inner(
|
|
293
|
+
default: Union[Any, _MISSING_TYPE] = MISSING,
|
|
294
|
+
default_factory: Union[Callable[[], Any], _MISSING_TYPE] = MISSING,
|
|
295
|
+
init: bool = True,
|
|
296
|
+
repr: bool = True,
|
|
297
|
+
hash: Optional[bool] = None,
|
|
298
|
+
compare: bool = True,
|
|
299
|
+
metadata: Optional[Dict] = None,
|
|
300
|
+
**kwargs: Any,
|
|
301
|
+
):
|
|
302
|
+
return validated_field(
|
|
303
|
+
validator,
|
|
304
|
+
default=default,
|
|
305
|
+
default_factory=default_factory,
|
|
306
|
+
init=init,
|
|
307
|
+
repr=repr,
|
|
308
|
+
hash=hash,
|
|
309
|
+
compare=compare,
|
|
310
|
+
metadata=metadata,
|
|
311
|
+
**kwargs,
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
return _inner
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def type_validator(name: str, value: Any, expected_type: Any) -> None:
|
|
318
|
+
"""Validate that 'value' matches 'expected_type'."""
|
|
319
|
+
origin = get_origin(expected_type)
|
|
320
|
+
args = get_args(expected_type)
|
|
321
|
+
|
|
322
|
+
if expected_type is Any:
|
|
323
|
+
return
|
|
324
|
+
elif validator := _BASIC_TYPE_VALIDATORS.get(origin):
|
|
325
|
+
validator(name, value, args)
|
|
326
|
+
elif isinstance(expected_type, type): # simple types
|
|
327
|
+
_validate_simple_type(name, value, expected_type)
|
|
328
|
+
else:
|
|
329
|
+
raise TypeError(f"Unsupported type for field '{name}': {expected_type}")
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _validate_union(name: str, value: Any, args: Tuple[Any, ...]) -> None:
|
|
333
|
+
"""Validate that value matches one of the types in a Union."""
|
|
334
|
+
errors = []
|
|
335
|
+
for t in args:
|
|
336
|
+
try:
|
|
337
|
+
type_validator(name, value, t)
|
|
338
|
+
return # Valid if any type matches
|
|
339
|
+
except TypeError as e:
|
|
340
|
+
errors.append(str(e))
|
|
341
|
+
|
|
342
|
+
raise TypeError(
|
|
343
|
+
f"Field '{name}' with value {repr(value)} doesn't match any type in {args}. Errors: {'; '.join(errors)}"
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _validate_literal(name: str, value: Any, args: Tuple[Any, ...]) -> None:
|
|
348
|
+
"""Validate Literal type."""
|
|
349
|
+
if value not in args:
|
|
350
|
+
raise TypeError(f"Field '{name}' expected one of {args}, got {value}")
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _validate_list(name: str, value: Any, args: Tuple[Any, ...]) -> None:
|
|
354
|
+
"""Validate List[T] type."""
|
|
355
|
+
if not isinstance(value, list):
|
|
356
|
+
raise TypeError(f"Field '{name}' expected a list, got {type(value).__name__}")
|
|
357
|
+
|
|
358
|
+
# Validate each item in the list
|
|
359
|
+
item_type = args[0]
|
|
360
|
+
for i, item in enumerate(value):
|
|
361
|
+
try:
|
|
362
|
+
type_validator(f"{name}[{i}]", item, item_type)
|
|
363
|
+
except TypeError as e:
|
|
364
|
+
raise TypeError(f"Invalid item at index {i} in list '{name}'") from e
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _validate_dict(name: str, value: Any, args: Tuple[Any, ...]) -> None:
|
|
368
|
+
"""Validate Dict[K, V] type."""
|
|
369
|
+
if not isinstance(value, dict):
|
|
370
|
+
raise TypeError(f"Field '{name}' expected a dict, got {type(value).__name__}")
|
|
371
|
+
|
|
372
|
+
# Validate keys and values
|
|
373
|
+
key_type, value_type = args
|
|
374
|
+
for k, v in value.items():
|
|
375
|
+
try:
|
|
376
|
+
type_validator(f"{name}.key", k, key_type)
|
|
377
|
+
type_validator(f"{name}[{k!r}]", v, value_type)
|
|
378
|
+
except TypeError as e:
|
|
379
|
+
raise TypeError(f"Invalid key or value in dict '{name}'") from e
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def _validate_tuple(name: str, value: Any, args: Tuple[Any, ...]) -> None:
|
|
383
|
+
"""Validate Tuple type."""
|
|
384
|
+
if not isinstance(value, tuple):
|
|
385
|
+
raise TypeError(f"Field '{name}' expected a tuple, got {type(value).__name__}")
|
|
386
|
+
|
|
387
|
+
# Handle variable-length tuples: Tuple[T, ...]
|
|
388
|
+
if len(args) == 2 and args[1] is Ellipsis:
|
|
389
|
+
for i, item in enumerate(value):
|
|
390
|
+
try:
|
|
391
|
+
type_validator(f"{name}[{i}]", item, args[0])
|
|
392
|
+
except TypeError as e:
|
|
393
|
+
raise TypeError(f"Invalid item at index {i} in tuple '{name}'") from e
|
|
394
|
+
# Handle fixed-length tuples: Tuple[T1, T2, ...]
|
|
395
|
+
elif len(args) != len(value):
|
|
396
|
+
raise TypeError(f"Field '{name}' expected a tuple of length {len(args)}, got {len(value)}")
|
|
397
|
+
else:
|
|
398
|
+
for i, (item, expected) in enumerate(zip(value, args)):
|
|
399
|
+
try:
|
|
400
|
+
type_validator(f"{name}[{i}]", item, expected)
|
|
401
|
+
except TypeError as e:
|
|
402
|
+
raise TypeError(f"Invalid item at index {i} in tuple '{name}'") from e
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def _validate_set(name: str, value: Any, args: Tuple[Any, ...]) -> None:
|
|
406
|
+
"""Validate Set[T] type."""
|
|
407
|
+
if not isinstance(value, set):
|
|
408
|
+
raise TypeError(f"Field '{name}' expected a set, got {type(value).__name__}")
|
|
409
|
+
|
|
410
|
+
# Validate each item in the set
|
|
411
|
+
item_type = args[0]
|
|
412
|
+
for i, item in enumerate(value):
|
|
413
|
+
try:
|
|
414
|
+
type_validator(f"{name} item", item, item_type)
|
|
415
|
+
except TypeError as e:
|
|
416
|
+
raise TypeError(f"Invalid item in set '{name}'") from e
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def _validate_simple_type(name: str, value: Any, expected_type: type) -> None:
|
|
420
|
+
"""Validate simple type (int, str, etc.)."""
|
|
421
|
+
if not isinstance(value, expected_type):
|
|
422
|
+
raise TypeError(
|
|
423
|
+
f"Field '{name}' expected {expected_type.__name__}, got {type(value).__name__} (value: {repr(value)})"
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def _create_type_validator(field: Field) -> Validator_T:
|
|
428
|
+
"""Create a type validator function for a field."""
|
|
429
|
+
# Hacky: we cannot use a lambda here because of reference issues
|
|
430
|
+
|
|
431
|
+
def validator(value: Any) -> None:
|
|
432
|
+
type_validator(field.name, value, field.type)
|
|
433
|
+
|
|
434
|
+
return validator
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def _is_validator(validator: Any) -> bool:
|
|
438
|
+
"""Check if a function is a validator.
|
|
439
|
+
|
|
440
|
+
A validator is a Callable that can be called with a single positional argument.
|
|
441
|
+
The validator can have more arguments with default values.
|
|
442
|
+
|
|
443
|
+
Basically, returns True if `validator(value)` is possible.
|
|
444
|
+
"""
|
|
445
|
+
if not callable(validator):
|
|
446
|
+
return False
|
|
447
|
+
|
|
448
|
+
signature = inspect.signature(validator)
|
|
449
|
+
parameters = list(signature.parameters.values())
|
|
450
|
+
if len(parameters) == 0:
|
|
451
|
+
return False
|
|
452
|
+
if parameters[0].kind not in (
|
|
453
|
+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
|
454
|
+
inspect.Parameter.POSITIONAL_ONLY,
|
|
455
|
+
inspect.Parameter.VAR_POSITIONAL,
|
|
456
|
+
):
|
|
457
|
+
return False
|
|
458
|
+
for parameter in parameters[1:]:
|
|
459
|
+
if parameter.default == inspect.Parameter.empty:
|
|
460
|
+
return False
|
|
461
|
+
return True
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
_BASIC_TYPE_VALIDATORS = {
|
|
465
|
+
Union: _validate_union,
|
|
466
|
+
Literal: _validate_literal,
|
|
467
|
+
list: _validate_list,
|
|
468
|
+
dict: _validate_dict,
|
|
469
|
+
tuple: _validate_tuple,
|
|
470
|
+
set: _validate_set,
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
__all__ = [
|
|
475
|
+
"strict",
|
|
476
|
+
"validated_field",
|
|
477
|
+
"Validator_T",
|
|
478
|
+
"StrictDataclassClassValidationError",
|
|
479
|
+
"StrictDataclassDefinitionError",
|
|
480
|
+
"StrictDataclassFieldValidationError",
|
|
481
|
+
]
|
huggingface_hub/errors.py
CHANGED
|
@@ -329,6 +329,35 @@ class DDUFInvalidEntryNameError(DDUFExportError):
|
|
|
329
329
|
"""Exception thrown when the entry name is invalid."""
|
|
330
330
|
|
|
331
331
|
|
|
332
|
+
# STRICT DATACLASSES ERRORS
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
class StrictDataclassError(Exception):
|
|
336
|
+
"""Base exception for strict dataclasses."""
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
class StrictDataclassDefinitionError(StrictDataclassError):
|
|
340
|
+
"""Exception thrown when a strict dataclass is defined incorrectly."""
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
class StrictDataclassFieldValidationError(StrictDataclassError):
|
|
344
|
+
"""Exception thrown when a strict dataclass fails validation for a given field."""
|
|
345
|
+
|
|
346
|
+
def __init__(self, field: str, cause: Exception):
|
|
347
|
+
error_message = f"Validation error for field '{field}':"
|
|
348
|
+
error_message += f"\n {cause.__class__.__name__}: {cause}"
|
|
349
|
+
super().__init__(error_message)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
class StrictDataclassClassValidationError(StrictDataclassError):
|
|
353
|
+
"""Exception thrown when a strict dataclass fails validation on a class validator."""
|
|
354
|
+
|
|
355
|
+
def __init__(self, validator: str, cause: Exception):
|
|
356
|
+
error_message = f"Class validation error for validator '{validator}':"
|
|
357
|
+
error_message += f"\n {cause.__class__.__name__}: {cause}"
|
|
358
|
+
super().__init__(error_message)
|
|
359
|
+
|
|
360
|
+
|
|
332
361
|
# XET ERRORS
|
|
333
362
|
|
|
334
363
|
|
huggingface_hub/hf_api.py
CHANGED
|
@@ -7698,7 +7698,15 @@ class HfApi:
|
|
|
7698
7698
|
"""
|
|
7699
7699
|
namespace = namespace or self._get_namespace(token=token)
|
|
7700
7700
|
|
|
7701
|
-
|
|
7701
|
+
if custom_image is not None:
|
|
7702
|
+
image = (
|
|
7703
|
+
custom_image
|
|
7704
|
+
if next(iter(custom_image)) in constants.INFERENCE_ENDPOINT_IMAGE_KEYS
|
|
7705
|
+
else {"custom": custom_image}
|
|
7706
|
+
)
|
|
7707
|
+
else:
|
|
7708
|
+
image = {"huggingface": {}}
|
|
7709
|
+
|
|
7702
7710
|
payload: Dict = {
|
|
7703
7711
|
"accountId": account_id,
|
|
7704
7712
|
"compute": {
|
|
@@ -1009,7 +1009,9 @@ class HfFileSystemFile(fsspec.spec.AbstractBufferedFile):
|
|
|
1009
1009
|
"""
|
|
1010
1010
|
if self.mode == "rb" and (length is None or length == -1) and self.loc == 0:
|
|
1011
1011
|
with self.fs.open(self.path, "rb", block_size=0) as f: # block_size=0 enables fast streaming
|
|
1012
|
-
|
|
1012
|
+
out = f.read()
|
|
1013
|
+
self.loc += len(out)
|
|
1014
|
+
return out
|
|
1013
1015
|
return super().read(length)
|
|
1014
1016
|
|
|
1015
1017
|
def url(self) -> str:
|
|
@@ -1057,7 +1059,7 @@ class HfFileSystemStreamFile(fsspec.spec.AbstractBufferedFile):
|
|
|
1057
1059
|
|
|
1058
1060
|
def read(self, length: int = -1):
|
|
1059
1061
|
read_args = (length,) if length >= 0 else ()
|
|
1060
|
-
if self.response is None
|
|
1062
|
+
if self.response is None:
|
|
1061
1063
|
url = hf_hub_url(
|
|
1062
1064
|
repo_id=self.resolved_path.repo_id,
|
|
1063
1065
|
revision=self.resolved_path.revision,
|
|
@@ -706,6 +706,15 @@ def _get_unique_id(tensor: "torch.Tensor") -> Union[int, Tuple[Any, ...]]:
|
|
|
706
706
|
if the input is a wrapper tensor subclass Tensor
|
|
707
707
|
"""
|
|
708
708
|
|
|
709
|
+
try:
|
|
710
|
+
from torch.distributed.tensor import DTensor
|
|
711
|
+
|
|
712
|
+
if isinstance(tensor, DTensor):
|
|
713
|
+
local_tensor = tensor.to_local()
|
|
714
|
+
return local_tensor.storage().data_ptr()
|
|
715
|
+
except ImportError:
|
|
716
|
+
pass
|
|
717
|
+
|
|
709
718
|
try:
|
|
710
719
|
# for torch 2.1 and above we can also handle tensor subclasses
|
|
711
720
|
from torch.utils._python_dispatch import is_traceable_wrapper_subclass
|
|
@@ -753,6 +762,15 @@ def get_torch_storage_size(tensor: "torch.Tensor") -> int:
|
|
|
753
762
|
"""
|
|
754
763
|
Taken from https://github.com/huggingface/safetensors/blob/08db34094e9e59e2f9218f2df133b7b4aaff5a99/bindings/python/py_src/safetensors/torch.py#L31C1-L41C59
|
|
755
764
|
"""
|
|
765
|
+
try:
|
|
766
|
+
from torch.distributed.tensor import DTensor
|
|
767
|
+
|
|
768
|
+
if isinstance(tensor, DTensor):
|
|
769
|
+
# this returns the size of the FULL tensor in bytes
|
|
770
|
+
return tensor.nbytes
|
|
771
|
+
except ImportError:
|
|
772
|
+
pass
|
|
773
|
+
|
|
756
774
|
try:
|
|
757
775
|
# for torch 2.1 and above we can also handle tensor subclasses
|
|
758
776
|
from torch.utils._python_dispatch import is_traceable_wrapper_subclass
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: huggingface-hub
|
|
3
|
-
Version: 0.31.
|
|
3
|
+
Version: 0.31.3
|
|
4
4
|
Summary: Client library to download and publish models, datasets and other repos on the huggingface.co hub
|
|
5
5
|
Home-page: https://github.com/huggingface/huggingface_hub
|
|
6
6
|
Author: Hugging Face, Inc.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
huggingface_hub/__init__.py,sha256=
|
|
1
|
+
huggingface_hub/__init__.py,sha256=q8bSLV5UKfmlItPZqhAmzAZXHcSX1mXQEq2v8IdF1GY,49368
|
|
2
2
|
huggingface_hub/_commit_api.py,sha256=ZbmuIhFdF8B3F_cvGtxorka7MmIQOk8oBkCtYltnCvI,39456
|
|
3
3
|
huggingface_hub/_commit_scheduler.py,sha256=tfIoO1xWHjTJ6qy6VS6HIoymDycFPg0d6pBSZprrU2U,14679
|
|
4
4
|
huggingface_hub/_inference_endpoints.py,sha256=qXR0utAYRaEWTI8EXzAsDpVDcYpp8bJPEBbcOxRS52E,17413
|
|
@@ -11,12 +11,13 @@ huggingface_hub/_upload_large_folder.py,sha256=mDKZv7MIieKlTCbTv0jccHIM4smCqK-Y0
|
|
|
11
11
|
huggingface_hub/_webhooks_payload.py,sha256=Xm3KaK7tCOGBlXkuZvbym6zjHXrT1XCrbUFWuXiBmNY,3617
|
|
12
12
|
huggingface_hub/_webhooks_server.py,sha256=5J63wk9MUGKBNJVsOD9i60mJ-VMp0YYmlf87vQsl-L8,15767
|
|
13
13
|
huggingface_hub/community.py,sha256=4MtcoxEI9_0lmmilBEnvUEi8_O1Ivfa8p6eKxYU5-ts,12198
|
|
14
|
-
huggingface_hub/constants.py,sha256=
|
|
15
|
-
huggingface_hub/
|
|
14
|
+
huggingface_hub/constants.py,sha256=ZYu0fEhPhrs12BVCZ_ygCdVvil6Sz4DLq9oMFsDkzNg,9766
|
|
15
|
+
huggingface_hub/dataclasses.py,sha256=gegl8I9N8SbGoCUAVbQejQ6CNImwzYbbDwlua5m-PH8,17170
|
|
16
|
+
huggingface_hub/errors.py,sha256=D7Lw0Jjrf8vfmD0B26LEvg-JWkU8Zq0KDPJOzFY4QLw,11201
|
|
16
17
|
huggingface_hub/fastai_utils.py,sha256=DpeH9d-6ut2k_nCAAwglM51XmRmgfbRe2SPifpVL5Yk,16745
|
|
17
18
|
huggingface_hub/file_download.py,sha256=Kh7Lg7C-Zn2U-zhF_mLw75ifiM28b9lSIODPvO4hGlA,78453
|
|
18
|
-
huggingface_hub/hf_api.py,sha256=
|
|
19
|
-
huggingface_hub/hf_file_system.py,sha256=
|
|
19
|
+
huggingface_hub/hf_api.py,sha256=RB9LjZjncC9Ox1TZPuYUL_X8-YnDhzWdg3gP0oNenb4,441435
|
|
20
|
+
huggingface_hub/hf_file_system.py,sha256=U6IY_QLNzZfvpsbvKEiakOBS2U6cduZw5t0x8wBPUn4,47531
|
|
20
21
|
huggingface_hub/hub_mixin.py,sha256=fdAhdDujpUBZPUB6AfzzMRBeQ_Ua9tgQkhHE_ao5n2k,38062
|
|
21
22
|
huggingface_hub/inference_api.py,sha256=b4-NhPSn9b44nYKV8tDKXodmE4JVdEymMWL4CVGkzlE,8323
|
|
22
23
|
huggingface_hub/keras_mixin.py,sha256=3d2oW35SALXHq-WHoLD_tbq0UrcabGKj3HidtPRx51U,19574
|
|
@@ -96,10 +97,10 @@ huggingface_hub/serialization/__init__.py,sha256=kn-Fa-m4FzMnN8lNsF-SwFcfzug4Cuc
|
|
|
96
97
|
huggingface_hub/serialization/_base.py,sha256=Df3GwGR9NzeK_SD75prXLucJAzPiNPgHbgXSw-_LTk8,8126
|
|
97
98
|
huggingface_hub/serialization/_dduf.py,sha256=s42239rLiHwaJE36QDEmS5GH7DSmQ__BffiHJO5RjIg,15424
|
|
98
99
|
huggingface_hub/serialization/_tensorflow.py,sha256=zHOvEMg-JHC55Fm4roDT3LUCDO5zB9qtXZffG065RAM,3625
|
|
99
|
-
huggingface_hub/serialization/_torch.py,sha256=
|
|
100
|
+
huggingface_hub/serialization/_torch.py,sha256=hJglq5F56s3k06GfLuYKQV4bSSjXQLuk1CC9l1M3Fmo,45191
|
|
100
101
|
huggingface_hub/templates/datasetcard_template.md,sha256=W-EMqR6wndbrnZorkVv56URWPG49l7MATGeI015kTvs,5503
|
|
101
102
|
huggingface_hub/templates/modelcard_template.md,sha256=4AqArS3cqdtbit5Bo-DhjcnDFR-pza5hErLLTPM4Yuc,6870
|
|
102
|
-
huggingface_hub/utils/__init__.py,sha256=
|
|
103
|
+
huggingface_hub/utils/__init__.py,sha256=ORfVkn5D0wuLIq12jjhTzn5_c4F8fRPxB7TG-iednuQ,3722
|
|
103
104
|
huggingface_hub/utils/_auth.py,sha256=-9p3SSOtWKMMCDKlsM_-ebsIGX0sSgKTSnC-_O4kTxg,8294
|
|
104
105
|
huggingface_hub/utils/_cache_assets.py,sha256=kai77HPQMfYpROouMBQCr_gdBCaeTm996Sqj0dExbNg,5728
|
|
105
106
|
huggingface_hub/utils/_cache_manager.py,sha256=GhiuVQsEkWU55uYkkgiGJV1_naeciyk8u4qb4WTIVyw,34531
|
|
@@ -127,9 +128,9 @@ huggingface_hub/utils/insecure_hashlib.py,sha256=OjxlvtSQHpbLp9PWSrXBDJ0wHjxCBU-
|
|
|
127
128
|
huggingface_hub/utils/logging.py,sha256=0A8fF1yh3L9Ka_bCDX2ml4U5Ht0tY8Dr3JcbRvWFuwo,4909
|
|
128
129
|
huggingface_hub/utils/sha.py,sha256=OFnNGCba0sNcT2gUwaVCJnldxlltrHHe0DS_PCpV3C4,2134
|
|
129
130
|
huggingface_hub/utils/tqdm.py,sha256=xAKcyfnNHsZ7L09WuEM5Ew5-MDhiahLACbbN2zMmcLs,10671
|
|
130
|
-
huggingface_hub-0.31.
|
|
131
|
-
huggingface_hub-0.31.
|
|
132
|
-
huggingface_hub-0.31.
|
|
133
|
-
huggingface_hub-0.31.
|
|
134
|
-
huggingface_hub-0.31.
|
|
135
|
-
huggingface_hub-0.31.
|
|
131
|
+
huggingface_hub-0.31.3.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
132
|
+
huggingface_hub-0.31.3.dist-info/METADATA,sha256=_lqS1pB3p0iLof7BjRmnaRyz08gijNYWywrl1lnZzc8,13558
|
|
133
|
+
huggingface_hub-0.31.3.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
134
|
+
huggingface_hub-0.31.3.dist-info/entry_points.txt,sha256=Y3Z2L02rBG7va_iE6RPXolIgwOdwUFONyRN3kXMxZ0g,131
|
|
135
|
+
huggingface_hub-0.31.3.dist-info/top_level.txt,sha256=8KzlQJAY4miUvjAssOAJodqKOw3harNzuiwGQ9qLSSk,16
|
|
136
|
+
huggingface_hub-0.31.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|