huggingface-hub 0.27.0rc1__py3-none-any.whl → 0.28.0__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.

Files changed (40) hide show
  1. huggingface_hub/__init__.py +418 -12
  2. huggingface_hub/_commit_api.py +33 -4
  3. huggingface_hub/_inference_endpoints.py +8 -2
  4. huggingface_hub/_local_folder.py +14 -3
  5. huggingface_hub/commands/scan_cache.py +1 -1
  6. huggingface_hub/commands/upload_large_folder.py +1 -1
  7. huggingface_hub/constants.py +7 -2
  8. huggingface_hub/file_download.py +1 -2
  9. huggingface_hub/hf_api.py +65 -84
  10. huggingface_hub/hub_mixin.py +12 -9
  11. huggingface_hub/inference/_client.py +706 -450
  12. huggingface_hub/inference/_common.py +32 -64
  13. huggingface_hub/inference/_generated/_async_client.py +722 -470
  14. huggingface_hub/inference/_generated/types/__init__.py +1 -0
  15. huggingface_hub/inference/_generated/types/image_to_image.py +3 -3
  16. huggingface_hub/inference/_generated/types/text_to_audio.py +1 -2
  17. huggingface_hub/inference/_generated/types/text_to_image.py +3 -3
  18. huggingface_hub/inference/_generated/types/text_to_speech.py +3 -6
  19. huggingface_hub/inference/_generated/types/text_to_video.py +47 -0
  20. huggingface_hub/inference/_generated/types/visual_question_answering.py +1 -1
  21. huggingface_hub/inference/_providers/__init__.py +89 -0
  22. huggingface_hub/inference/_providers/fal_ai.py +159 -0
  23. huggingface_hub/inference/_providers/hf_inference.py +202 -0
  24. huggingface_hub/inference/_providers/replicate.py +148 -0
  25. huggingface_hub/inference/_providers/sambanova.py +89 -0
  26. huggingface_hub/inference/_providers/together.py +153 -0
  27. huggingface_hub/py.typed +0 -0
  28. huggingface_hub/repocard.py +1 -1
  29. huggingface_hub/repocard_data.py +2 -1
  30. huggingface_hub/serialization/_base.py +1 -1
  31. huggingface_hub/serialization/_torch.py +1 -1
  32. huggingface_hub/utils/_fixes.py +25 -13
  33. huggingface_hub/utils/_http.py +3 -3
  34. huggingface_hub/utils/logging.py +1 -1
  35. {huggingface_hub-0.27.0rc1.dist-info → huggingface_hub-0.28.0.dist-info}/METADATA +4 -4
  36. {huggingface_hub-0.27.0rc1.dist-info → huggingface_hub-0.28.0.dist-info}/RECORD +40 -32
  37. {huggingface_hub-0.27.0rc1.dist-info → huggingface_hub-0.28.0.dist-info}/LICENSE +0 -0
  38. {huggingface_hub-0.27.0rc1.dist-info → huggingface_hub-0.28.0.dist-info}/WHEEL +0 -0
  39. {huggingface_hub-0.27.0rc1.dist-info → huggingface_hub-0.28.0.dist-info}/entry_points.txt +0 -0
  40. {huggingface_hub-0.27.0rc1.dist-info → huggingface_hub-0.28.0.dist-info}/top_level.txt +0 -0
@@ -18,6 +18,7 @@ import base64
18
18
  import io
19
19
  import json
20
20
  import logging
21
+ from abc import ABC, abstractmethod
21
22
  from contextlib import contextmanager
22
23
  from dataclasses import dataclass
23
24
  from pathlib import Path
@@ -49,11 +50,8 @@ from huggingface_hub.errors import (
49
50
  ValidationError,
50
51
  )
51
52
 
52
- from ..constants import ENDPOINT
53
53
  from ..utils import (
54
- build_hf_headers,
55
54
  get_session,
56
- hf_raise_for_status,
57
55
  is_aiohttp_available,
58
56
  is_numpy_available,
59
57
  is_pillow_available,
@@ -77,6 +75,34 @@ TASKS_EXPECTING_IMAGES = {"text-to-image", "image-to-image"}
77
75
  logger = logging.getLogger(__name__)
78
76
 
79
77
 
78
+ @dataclass
79
+ class RequestParameters:
80
+ url: str
81
+ task: str
82
+ model: Optional[str]
83
+ json: Optional[Union[str, Dict, List]]
84
+ data: Optional[ContentT]
85
+ headers: Dict[str, Any]
86
+
87
+
88
+ class TaskProviderHelper(ABC):
89
+ """Protocol defining the interface for task-specific provider helpers."""
90
+
91
+ @abstractmethod
92
+ def prepare_request(
93
+ self,
94
+ *,
95
+ inputs: Any,
96
+ parameters: Dict[str, Any],
97
+ headers: Dict,
98
+ model: Optional[str],
99
+ api_key: Optional[str],
100
+ extra_payload: Optional[Dict[str, Any]] = None,
101
+ ) -> RequestParameters: ...
102
+ @abstractmethod
103
+ def get_response(self, response: Union[bytes, Dict]) -> Any: ...
104
+
105
+
80
106
  # Add dataclass for ModelStatus. We use this dataclass in get_model_status function.
81
107
  @dataclass
82
108
  class ModelStatus:
@@ -141,30 +167,6 @@ def _import_pil_image():
141
167
  return Image
142
168
 
143
169
 
144
- ## RECOMMENDED MODELS
145
-
146
- # Will be globally fetched only once (see '_fetch_recommended_models')
147
- _RECOMMENDED_MODELS: Optional[Dict[str, Optional[str]]] = None
148
-
149
-
150
- def _fetch_recommended_models() -> Dict[str, Optional[str]]:
151
- global _RECOMMENDED_MODELS
152
- if _RECOMMENDED_MODELS is None:
153
- response = get_session().get(f"{ENDPOINT}/api/tasks", headers=build_hf_headers())
154
- hf_raise_for_status(response)
155
- _RECOMMENDED_MODELS = {
156
- task: _first_or_none(details["widgetModels"]) for task, details in response.json().items()
157
- }
158
- return _RECOMMENDED_MODELS
159
-
160
-
161
- def _first_or_none(items: List[Any]) -> Optional[Any]:
162
- try:
163
- return items[0] or None
164
- except IndexError:
165
- return None
166
-
167
-
168
170
  ## ENCODING / DECODING UTILS
169
171
 
170
172
 
@@ -256,45 +258,11 @@ def _bytes_to_image(content: bytes) -> "Image":
256
258
  return Image.open(io.BytesIO(content))
257
259
 
258
260
 
259
- ## PAYLOAD UTILS
261
+ def _as_dict(response: Union[bytes, Dict]) -> Dict:
262
+ return json.loads(response) if isinstance(response, bytes) else response
260
263
 
261
264
 
262
- def _prepare_payload(
263
- inputs: Union[str, Dict[str, Any], ContentT],
264
- parameters: Optional[Dict[str, Any]],
265
- expect_binary: bool = False,
266
- ) -> Dict[str, Any]:
267
- """
268
- Used in `InferenceClient` and `AsyncInferenceClient` to prepare the payload for an API request, handling various input types and parameters.
269
- `expect_binary` is set to `True` when the inputs are a binary object or a local path or URL. This is the case for image and audio inputs.
270
- """
271
- if parameters is None:
272
- parameters = {}
273
- parameters = {k: v for k, v in parameters.items() if v is not None}
274
- has_parameters = len(parameters) > 0
275
-
276
- is_binary = isinstance(inputs, (bytes, Path))
277
- # If expect_binary is True, inputs must be a binary object or a local path or a URL.
278
- if expect_binary and not is_binary and not isinstance(inputs, str):
279
- raise ValueError(f"Expected binary inputs or a local path or a URL. Got {inputs}") # type: ignore
280
- # Send inputs as raw content when no parameters are provided
281
- if expect_binary and not has_parameters:
282
- return {"data": inputs}
283
- # If expect_binary is False, inputs must not be a binary object.
284
- if not expect_binary and is_binary:
285
- raise ValueError(f"Unexpected binary inputs. Got {inputs}") # type: ignore
286
-
287
- json: Dict[str, Any] = {}
288
- # If inputs is a bytes-like object, encode it to base64
289
- if expect_binary:
290
- json["inputs"] = _b64_encode(inputs) # type: ignore
291
- # Otherwise (string, dict, list) send it as is
292
- else:
293
- json["inputs"] = inputs
294
- # Add parameters to the json payload if any
295
- if has_parameters:
296
- json["parameters"] = parameters
297
- return {"json": json}
265
+ ## PAYLOAD UTILS
298
266
 
299
267
 
300
268
  ## STREAMING UTILS