huggingface-hub 0.31.4__py3-none-any.whl → 0.32.0rc0__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 (41) hide show
  1. huggingface_hub/__init__.py +42 -4
  2. huggingface_hub/_local_folder.py +8 -0
  3. huggingface_hub/_oauth.py +464 -0
  4. huggingface_hub/_snapshot_download.py +11 -3
  5. huggingface_hub/_upload_large_folder.py +16 -36
  6. huggingface_hub/commands/huggingface_cli.py +2 -0
  7. huggingface_hub/commands/repo.py +147 -0
  8. huggingface_hub/commands/user.py +2 -108
  9. huggingface_hub/constants.py +9 -1
  10. huggingface_hub/dataclasses.py +2 -2
  11. huggingface_hub/file_download.py +13 -11
  12. huggingface_hub/hf_api.py +48 -19
  13. huggingface_hub/hub_mixin.py +2 -2
  14. huggingface_hub/inference/_client.py +8 -7
  15. huggingface_hub/inference/_generated/_async_client.py +8 -7
  16. huggingface_hub/inference/_generated/types/__init__.py +4 -1
  17. huggingface_hub/inference/_generated/types/chat_completion.py +43 -9
  18. huggingface_hub/inference/_mcp/__init__.py +0 -0
  19. huggingface_hub/inference/_mcp/agent.py +99 -0
  20. huggingface_hub/inference/_mcp/cli.py +153 -0
  21. huggingface_hub/inference/_mcp/constants.py +80 -0
  22. huggingface_hub/inference/_mcp/mcp_client.py +322 -0
  23. huggingface_hub/inference/_mcp/utils.py +123 -0
  24. huggingface_hub/inference/_providers/__init__.py +13 -1
  25. huggingface_hub/inference/_providers/_common.py +1 -0
  26. huggingface_hub/inference/_providers/cerebras.py +1 -1
  27. huggingface_hub/inference/_providers/cohere.py +20 -3
  28. huggingface_hub/inference/_providers/fireworks_ai.py +18 -0
  29. huggingface_hub/inference/_providers/hf_inference.py +8 -1
  30. huggingface_hub/inference/_providers/nebius.py +28 -0
  31. huggingface_hub/inference/_providers/nscale.py +44 -0
  32. huggingface_hub/inference/_providers/sambanova.py +14 -0
  33. huggingface_hub/inference/_providers/together.py +15 -0
  34. huggingface_hub/utils/_experimental.py +7 -5
  35. huggingface_hub/utils/insecure_hashlib.py +8 -4
  36. {huggingface_hub-0.31.4.dist-info → huggingface_hub-0.32.0rc0.dist-info}/METADATA +30 -8
  37. {huggingface_hub-0.31.4.dist-info → huggingface_hub-0.32.0rc0.dist-info}/RECORD +41 -32
  38. {huggingface_hub-0.31.4.dist-info → huggingface_hub-0.32.0rc0.dist-info}/entry_points.txt +1 -0
  39. {huggingface_hub-0.31.4.dist-info → huggingface_hub-0.32.0rc0.dist-info}/LICENSE +0 -0
  40. {huggingface_hub-0.31.4.dist-info → huggingface_hub-0.32.0rc0.dist-info}/WHEEL +0 -0
  41. {huggingface_hub-0.31.4.dist-info → huggingface_hub-0.32.0rc0.dist-info}/top_level.txt +0 -0
@@ -46,7 +46,7 @@ import sys
46
46
  from typing import TYPE_CHECKING
47
47
 
48
48
 
49
- __version__ = "0.31.4"
49
+ __version__ = "0.32.0.rc0"
50
50
 
51
51
  # Alphabetical order of definitions is ensured in tests
52
52
  # WARNING: any comment added in this dictionary definition will be lost when
@@ -70,6 +70,13 @@ _SUBMOD_ATTRS = {
70
70
  "logout",
71
71
  "notebook_login",
72
72
  ],
73
+ "_oauth": [
74
+ "OAuthInfo",
75
+ "OAuthOrgInfo",
76
+ "OAuthUserInfo",
77
+ "attach_huggingface_oauth",
78
+ "parse_huggingface_oauth",
79
+ ],
73
80
  "_snapshot_download": [
74
81
  "snapshot_download",
75
82
  ],
@@ -294,10 +301,13 @@ _SUBMOD_ATTRS = {
294
301
  "ChatCompletionInputFunctionDefinition",
295
302
  "ChatCompletionInputFunctionName",
296
303
  "ChatCompletionInputGrammarType",
297
- "ChatCompletionInputGrammarTypeType",
304
+ "ChatCompletionInputJSONSchema",
298
305
  "ChatCompletionInputMessage",
299
306
  "ChatCompletionInputMessageChunk",
300
307
  "ChatCompletionInputMessageChunkType",
308
+ "ChatCompletionInputResponseFormatJSONObject",
309
+ "ChatCompletionInputResponseFormatJSONSchema",
310
+ "ChatCompletionInputResponseFormatText",
301
311
  "ChatCompletionInputStreamOptions",
302
312
  "ChatCompletionInputTool",
303
313
  "ChatCompletionInputToolCall",
@@ -433,6 +443,12 @@ _SUBMOD_ATTRS = {
433
443
  "ZeroShotObjectDetectionOutputElement",
434
444
  "ZeroShotObjectDetectionParameters",
435
445
  ],
446
+ "inference._mcp.agent": [
447
+ "Agent",
448
+ ],
449
+ "inference._mcp.mcp_client": [
450
+ "MCPClient",
451
+ ],
436
452
  "inference_api": [
437
453
  "InferenceApi",
438
454
  ],
@@ -512,6 +528,7 @@ _SUBMOD_ATTRS = {
512
528
  # ```
513
529
 
514
530
  __all__ = [
531
+ "Agent",
515
532
  "AsyncInferenceClient",
516
533
  "AudioClassificationInput",
517
534
  "AudioClassificationOutputElement",
@@ -535,10 +552,13 @@ __all__ = [
535
552
  "ChatCompletionInputFunctionDefinition",
536
553
  "ChatCompletionInputFunctionName",
537
554
  "ChatCompletionInputGrammarType",
538
- "ChatCompletionInputGrammarTypeType",
555
+ "ChatCompletionInputJSONSchema",
539
556
  "ChatCompletionInputMessage",
540
557
  "ChatCompletionInputMessageChunk",
541
558
  "ChatCompletionInputMessageChunkType",
559
+ "ChatCompletionInputResponseFormatJSONObject",
560
+ "ChatCompletionInputResponseFormatJSONSchema",
561
+ "ChatCompletionInputResponseFormatText",
542
562
  "ChatCompletionInputStreamOptions",
543
563
  "ChatCompletionInputTool",
544
564
  "ChatCompletionInputToolCall",
@@ -637,10 +657,14 @@ __all__ = [
637
657
  "InferenceEndpointType",
638
658
  "InferenceTimeoutError",
639
659
  "KerasModelHubMixin",
660
+ "MCPClient",
640
661
  "ModelCard",
641
662
  "ModelCardData",
642
663
  "ModelHubMixin",
643
664
  "ModelInfo",
665
+ "OAuthInfo",
666
+ "OAuthOrgInfo",
667
+ "OAuthUserInfo",
644
668
  "ObjectDetectionBoundingBox",
645
669
  "ObjectDetectionInput",
646
670
  "ObjectDetectionOutputElement",
@@ -762,6 +786,7 @@ __all__ = [
762
786
  "add_collection_item",
763
787
  "add_space_secret",
764
788
  "add_space_variable",
789
+ "attach_huggingface_oauth",
765
790
  "auth_check",
766
791
  "auth_list",
767
792
  "auth_switch",
@@ -862,6 +887,7 @@ __all__ = [
862
887
  "move_repo",
863
888
  "notebook_login",
864
889
  "paper_info",
890
+ "parse_huggingface_oauth",
865
891
  "parse_safetensors_file_metadata",
866
892
  "pause_inference_endpoint",
867
893
  "pause_space",
@@ -1026,6 +1052,13 @@ if TYPE_CHECKING: # pragma: no cover
1026
1052
  logout, # noqa: F401
1027
1053
  notebook_login, # noqa: F401
1028
1054
  )
1055
+ from ._oauth import (
1056
+ OAuthInfo, # noqa: F401
1057
+ OAuthOrgInfo, # noqa: F401
1058
+ OAuthUserInfo, # noqa: F401
1059
+ attach_huggingface_oauth, # noqa: F401
1060
+ parse_huggingface_oauth, # noqa: F401
1061
+ )
1029
1062
  from ._snapshot_download import snapshot_download # noqa: F401
1030
1063
  from ._space_api import (
1031
1064
  SpaceHardware, # noqa: F401
@@ -1244,10 +1277,13 @@ if TYPE_CHECKING: # pragma: no cover
1244
1277
  ChatCompletionInputFunctionDefinition, # noqa: F401
1245
1278
  ChatCompletionInputFunctionName, # noqa: F401
1246
1279
  ChatCompletionInputGrammarType, # noqa: F401
1247
- ChatCompletionInputGrammarTypeType, # noqa: F401
1280
+ ChatCompletionInputJSONSchema, # noqa: F401
1248
1281
  ChatCompletionInputMessage, # noqa: F401
1249
1282
  ChatCompletionInputMessageChunk, # noqa: F401
1250
1283
  ChatCompletionInputMessageChunkType, # noqa: F401
1284
+ ChatCompletionInputResponseFormatJSONObject, # noqa: F401
1285
+ ChatCompletionInputResponseFormatJSONSchema, # noqa: F401
1286
+ ChatCompletionInputResponseFormatText, # noqa: F401
1251
1287
  ChatCompletionInputStreamOptions, # noqa: F401
1252
1288
  ChatCompletionInputTool, # noqa: F401
1253
1289
  ChatCompletionInputToolCall, # noqa: F401
@@ -1383,6 +1419,8 @@ if TYPE_CHECKING: # pragma: no cover
1383
1419
  ZeroShotObjectDetectionOutputElement, # noqa: F401
1384
1420
  ZeroShotObjectDetectionParameters, # noqa: F401
1385
1421
  )
1422
+ from .inference._mcp.agent import Agent # noqa: F401
1423
+ from .inference._mcp.mcp_client import MCPClient # noqa: F401
1386
1424
  from .inference_api import InferenceApi # noqa: F401
1387
1425
  from .keras_mixin import (
1388
1426
  KerasModelHubMixin, # noqa: F401
@@ -149,6 +149,7 @@ class LocalUploadFileMetadata:
149
149
  should_ignore: Optional[bool] = None
150
150
  sha256: Optional[str] = None
151
151
  upload_mode: Optional[str] = None
152
+ remote_oid: Optional[str] = None
152
153
  is_uploaded: bool = False
153
154
  is_committed: bool = False
154
155
 
@@ -172,6 +173,9 @@ class LocalUploadFileMetadata:
172
173
 
173
174
  if self.upload_mode is not None:
174
175
  f.write(self.upload_mode)
176
+
177
+ if self.remote_oid is not None:
178
+ f.write(self.remote_oid)
175
179
  f.write("\n")
176
180
 
177
181
  f.write(str(int(self.is_uploaded)) + "\n")
@@ -346,6 +350,9 @@ def read_upload_metadata(local_dir: Path, filename: str) -> LocalUploadFileMetad
346
350
  if upload_mode not in (None, "regular", "lfs"):
347
351
  raise ValueError(f"Invalid upload mode in metadata {paths.path_in_repo}: {upload_mode}")
348
352
 
353
+ _remote_oid = f.readline().strip()
354
+ remote_oid = None if _remote_oid == "" else _remote_oid
355
+
349
356
  is_uploaded = bool(int(f.readline().strip()))
350
357
  is_committed = bool(int(f.readline().strip()))
351
358
 
@@ -355,6 +362,7 @@ def read_upload_metadata(local_dir: Path, filename: str) -> LocalUploadFileMetad
355
362
  should_ignore=should_ignore,
356
363
  sha256=sha256,
357
364
  upload_mode=upload_mode,
365
+ remote_oid=remote_oid,
358
366
  is_uploaded=is_uploaded,
359
367
  is_committed=is_committed,
360
368
  )
@@ -0,0 +1,464 @@
1
+ import datetime
2
+ import hashlib
3
+ import logging
4
+ import os
5
+ import time
6
+ import urllib.parse
7
+ import warnings
8
+ from dataclasses import dataclass
9
+ from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
10
+
11
+ from . import constants
12
+ from .hf_api import whoami
13
+ from .utils import experimental, get_token
14
+
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+ if TYPE_CHECKING:
19
+ import fastapi
20
+
21
+
22
+ @dataclass
23
+ class OAuthOrgInfo:
24
+ """
25
+ Information about an organization linked to a user logged in with OAuth.
26
+
27
+ Attributes:
28
+ sub (`str`):
29
+ Unique identifier for the org. OpenID Connect field.
30
+ name (`str`):
31
+ The org's full name. OpenID Connect field.
32
+ preferred_username (`str`):
33
+ The org's username. OpenID Connect field.
34
+ picture (`str`):
35
+ The org's profile picture URL. OpenID Connect field.
36
+ is_enterprise (`bool`):
37
+ Whether the org is an enterprise org. Hugging Face field.
38
+ can_pay (`Optional[bool]`, *optional*):
39
+ Whether the org has a payment method set up. Hugging Face field.
40
+ role_in_org (`Optional[str]`, *optional*):
41
+ The user's role in the org. Hugging Face field.
42
+ pending_sso (`Optional[bool]`, *optional*):
43
+ Indicates if the user granted the OAuth app access to the org but didn't complete SSO. Hugging Face field.
44
+ missing_mfa (`Optional[bool]`, *optional*):
45
+ Indicates if the user granted the OAuth app access to the org but didn't complete MFA. Hugging Face field.
46
+ """
47
+
48
+ sub: str
49
+ name: str
50
+ preferred_username: str
51
+ picture: str
52
+ is_enterprise: bool
53
+ can_pay: Optional[bool] = None
54
+ role_in_org: Optional[str] = None
55
+ pending_sso: Optional[bool] = None
56
+ missing_mfa: Optional[bool] = None
57
+
58
+
59
+ @dataclass
60
+ class OAuthUserInfo:
61
+ """
62
+ Information about a user logged in with OAuth.
63
+
64
+ Attributes:
65
+ sub (`str`):
66
+ Unique identifier for the user, even in case of rename. OpenID Connect field.
67
+ name (`str`):
68
+ The user's full name. OpenID Connect field.
69
+ preferred_username (`str`):
70
+ The user's username. OpenID Connect field.
71
+ email_verified (`Optional[bool]`, *optional*):
72
+ Indicates if the user's email is verified. OpenID Connect field.
73
+ email (`Optional[str]`, *optional*):
74
+ The user's email address. OpenID Connect field.
75
+ picture (`str`):
76
+ The user's profile picture URL. OpenID Connect field.
77
+ profile (`str`):
78
+ The user's profile URL. OpenID Connect field.
79
+ website (`Optional[str]`, *optional*):
80
+ The user's website URL. OpenID Connect field.
81
+ is_pro (`bool`):
82
+ Whether the user is a pro user. Hugging Face field.
83
+ can_pay (`Optional[bool]`, *optional*):
84
+ Whether the user has a payment method set up. Hugging Face field.
85
+ orgs (`Optional[List[OrgInfo]]`, *optional*):
86
+ List of organizations the user is part of. Hugging Face field.
87
+ """
88
+
89
+ sub: str
90
+ name: str
91
+ preferred_username: str
92
+ email_verified: Optional[bool]
93
+ email: Optional[str]
94
+ picture: str
95
+ profile: str
96
+ website: Optional[str]
97
+ is_pro: bool
98
+ can_pay: Optional[bool]
99
+ orgs: Optional[List[OAuthOrgInfo]]
100
+
101
+
102
+ @dataclass
103
+ class OAuthInfo:
104
+ """
105
+ Information about the OAuth login.
106
+
107
+ Attributes:
108
+ access_token (`str`):
109
+ The access token.
110
+ access_token_expires_at (`datetime.datetime`):
111
+ The expiration date of the access token.
112
+ user_info ([`OAuthUserInfo`]):
113
+ The user information.
114
+ state (`str`, *optional*):
115
+ State passed to the OAuth provider in the original request to the OAuth provider.
116
+ scope (`str`):
117
+ Granted scope.
118
+ """
119
+
120
+ access_token: str
121
+ access_token_expires_at: datetime.datetime
122
+ user_info: OAuthUserInfo
123
+ state: Optional[str]
124
+ scope: str
125
+
126
+
127
+ @experimental
128
+ def attach_huggingface_oauth(app: "fastapi.FastAPI", route_prefix: str = "/"):
129
+ """
130
+ Add OAuth endpoints to a FastAPI app to enable OAuth login with Hugging Face.
131
+
132
+ How to use:
133
+ - Call this method on your FastAPI app to add the OAuth endpoints.
134
+ - Inside your route handlers, call `parse_huggingface_oauth(request)` to retrieve the OAuth info.
135
+ - If user is logged in, an [`OAuthInfo`] object is returned with the user's info. If not, `None` is returned.
136
+ - In your app, make sure to add links to `/oauth/huggingface/login` and `/oauth/huggingface/logout` for the user to log in and out.
137
+
138
+ Example:
139
+ ```py
140
+ from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
141
+
142
+ # Create a FastAPI app
143
+ app = FastAPI()
144
+
145
+ # Add OAuth endpoints to the FastAPI app
146
+ attach_huggingface_oauth(app)
147
+
148
+ # Add a route that greets the user if they are logged in
149
+ @app.get("/")
150
+ def greet_json(request: Request):
151
+ # Retrieve the OAuth info from the request
152
+ oauth_info = parse_huggingface_oauth(request) # e.g. OAuthInfo dataclass
153
+ if oauth_info is None:
154
+ return {"msg": "Not logged in!"}
155
+ return {"msg": f"Hello, {oauth_info.user_info.preferred_username}!"}
156
+ ```
157
+ """
158
+ # TODO: handle generic case (handling OAuth in a non-Space environment with custom dev values) (low priority)
159
+
160
+ # Add SessionMiddleware to the FastAPI app to store the OAuth info in the session.
161
+ # Session Middleware requires a secret key to sign the cookies. Let's use a hash
162
+ # of the OAuth secret key to make it unique to the Space + updated in case OAuth
163
+ # config gets updated. When ran locally, we use an empty string as a secret key.
164
+ try:
165
+ from starlette.middleware.sessions import SessionMiddleware
166
+ except ImportError as e:
167
+ raise ImportError(
168
+ "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
169
+ "`huggingface_hub[oauth]` to your requirements.txt file in order to install the required dependencies."
170
+ ) from e
171
+ session_secret = (constants.OAUTH_CLIENT_SECRET or "") + "-v1"
172
+ app.add_middleware(
173
+ SessionMiddleware, # type: ignore[arg-type]
174
+ secret_key=hashlib.sha256(session_secret.encode()).hexdigest(),
175
+ same_site="none",
176
+ https_only=True,
177
+ ) # type: ignore
178
+
179
+ # Add OAuth endpoints to the FastAPI app:
180
+ # - {route_prefix}/oauth/huggingface/login
181
+ # - {route_prefix}/oauth/huggingface/callback
182
+ # - {route_prefix}/oauth/huggingface/logout
183
+ # If the app is running in a Space, OAuth is enabled normally.
184
+ # Otherwise, we mock the endpoints to make the user log in with a fake user profile - without any calls to hf.co.
185
+ route_prefix = route_prefix.strip("/")
186
+ if os.getenv("SPACE_ID") is not None:
187
+ logger.info("OAuth is enabled in the Space. Adding OAuth routes.")
188
+ _add_oauth_routes(app, route_prefix=route_prefix)
189
+ else:
190
+ logger.info("App is not running in a Space. Adding mocked OAuth routes.")
191
+ _add_mocked_oauth_routes(app, route_prefix=route_prefix)
192
+
193
+
194
+ def parse_huggingface_oauth(request: "fastapi.Request") -> Optional[OAuthInfo]:
195
+ """
196
+ Returns the information from a logged in user as a [`OAuthInfo`] object.
197
+
198
+ For flexibility and future-proofing, this method is very lax in its parsing and does not raise errors.
199
+ Missing fields are set to `None` without a warning.
200
+
201
+ Return `None`, if the user is not logged in (no info in session cookie).
202
+
203
+ See [`attach_huggingface_oauth`] for an example on how to use this method.
204
+ """
205
+ if "oauth_info" not in request.session:
206
+ logger.debug("No OAuth info in session.")
207
+ return None
208
+
209
+ logger.debug("Parsing OAuth info from session.")
210
+ oauth_data = request.session["oauth_info"]
211
+ user_data = oauth_data.get("userinfo", {})
212
+ orgs_data = user_data.get("orgs", [])
213
+
214
+ orgs = (
215
+ [
216
+ OAuthOrgInfo(
217
+ sub=org.get("sub"),
218
+ name=org.get("name"),
219
+ preferred_username=org.get("preferred_username"),
220
+ picture=org.get("picture"),
221
+ is_enterprise=org.get("isEnterprise"),
222
+ can_pay=org.get("canPay"),
223
+ role_in_org=org.get("roleInOrg"),
224
+ pending_sso=org.get("pendingSSO"),
225
+ missing_mfa=org.get("missingMFA"),
226
+ )
227
+ for org in orgs_data
228
+ ]
229
+ if orgs_data
230
+ else None
231
+ )
232
+
233
+ user_info = OAuthUserInfo(
234
+ sub=user_data.get("sub"),
235
+ name=user_data.get("name"),
236
+ preferred_username=user_data.get("preferred_username"),
237
+ email_verified=user_data.get("email_verified"),
238
+ email=user_data.get("email"),
239
+ picture=user_data.get("picture"),
240
+ profile=user_data.get("profile"),
241
+ website=user_data.get("website"),
242
+ is_pro=user_data.get("isPro"),
243
+ can_pay=user_data.get("canPay"),
244
+ orgs=orgs,
245
+ )
246
+
247
+ return OAuthInfo(
248
+ access_token=oauth_data.get("access_token"),
249
+ access_token_expires_at=datetime.datetime.fromtimestamp(oauth_data.get("expires_at")),
250
+ user_info=user_info,
251
+ state=oauth_data.get("state"),
252
+ scope=oauth_data.get("scope"),
253
+ )
254
+
255
+
256
+ def _add_oauth_routes(app: "fastapi.FastAPI", route_prefix: str) -> None:
257
+ """Add OAuth routes to the FastAPI app (login, callback handler and logout)."""
258
+ try:
259
+ import fastapi
260
+ from authlib.integrations.base_client.errors import MismatchingStateError
261
+ from authlib.integrations.starlette_client import OAuth
262
+ from fastapi.responses import RedirectResponse
263
+ except ImportError as e:
264
+ raise ImportError(
265
+ "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
266
+ "`huggingface_hub[oauth]` to your requirements.txt file."
267
+ ) from e
268
+
269
+ # Check environment variables
270
+ msg = (
271
+ "OAuth is required but '{}' environment variable is not set. Make sure you've enabled OAuth in your Space by"
272
+ " setting `hf_oauth: true` in the Space metadata."
273
+ )
274
+ if constants.OAUTH_CLIENT_ID is None:
275
+ raise ValueError(msg.format("OAUTH_CLIENT_ID"))
276
+ if constants.OAUTH_CLIENT_SECRET is None:
277
+ raise ValueError(msg.format("OAUTH_CLIENT_SECRET"))
278
+ if constants.OAUTH_SCOPES is None:
279
+ raise ValueError(msg.format("OAUTH_SCOPES"))
280
+ if constants.OPENID_PROVIDER_URL is None:
281
+ raise ValueError(msg.format("OPENID_PROVIDER_URL"))
282
+
283
+ # Register OAuth server
284
+ oauth = OAuth()
285
+ oauth.register(
286
+ name="huggingface",
287
+ client_id=constants.OAUTH_CLIENT_ID,
288
+ client_secret=constants.OAUTH_CLIENT_SECRET,
289
+ client_kwargs={"scope": constants.OAUTH_SCOPES},
290
+ server_metadata_url=constants.OPENID_PROVIDER_URL + "/.well-known/openid-configuration",
291
+ )
292
+
293
+ login_uri, callback_uri, logout_uri = _get_oauth_uris(route_prefix)
294
+
295
+ # Register OAuth endpoints
296
+ @app.get(login_uri)
297
+ async def oauth_login(request: fastapi.Request) -> RedirectResponse:
298
+ """Endpoint that redirects to HF OAuth page."""
299
+ redirect_uri = _generate_redirect_uri(request)
300
+ return await oauth.huggingface.authorize_redirect(request, redirect_uri) # type: ignore
301
+
302
+ @app.get(callback_uri)
303
+ async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
304
+ """Endpoint that handles the OAuth callback."""
305
+ try:
306
+ oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
307
+ except MismatchingStateError:
308
+ # Parse query params
309
+ nb_redirects = int(request.query_params.get("_nb_redirects", 0))
310
+ target_url = request.query_params.get("_target_url")
311
+
312
+ # Build redirect URI with the same query params as before and bump nb_redirects count
313
+ query_params: Dict[str, Union[int, str]] = {"_nb_redirects": nb_redirects + 1}
314
+ if target_url:
315
+ query_params["_target_url"] = target_url
316
+
317
+ redirect_uri = f"{login_uri}?{urllib.parse.urlencode(query_params)}"
318
+
319
+ # If the user is redirected more than 3 times, it is very likely that the cookie is not working properly.
320
+ # (e.g. browser is blocking third-party cookies in iframe). In this case, redirect the user in the
321
+ # non-iframe view.
322
+ if nb_redirects > constants.OAUTH_MAX_REDIRECTS:
323
+ host = os.environ.get("SPACE_HOST")
324
+ if host is None: # cannot happen in a Space
325
+ raise RuntimeError(
326
+ "App is not running in a Space (SPACE_HOST environment variable is not set). Cannot redirect to non-iframe view."
327
+ ) from None
328
+ host_url = "https://" + host.rstrip("/")
329
+ return RedirectResponse(host_url + redirect_uri)
330
+
331
+ # Redirect the user to the login page again
332
+ return RedirectResponse(redirect_uri)
333
+
334
+ # OAuth login worked => store the user info in the session and redirect
335
+ logger.debug("Successfully logged in with OAuth. Storing user info in session.")
336
+ request.session["oauth_info"] = oauth_info
337
+ return RedirectResponse(_get_redirect_target(request))
338
+
339
+ @app.get(logout_uri)
340
+ async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
341
+ """Endpoint that logs out the user (e.g. delete info from cookie session)."""
342
+ logger.debug("Logged out with OAuth. Removing user info from session.")
343
+ request.session.pop("oauth_info", None)
344
+ return RedirectResponse(_get_redirect_target(request))
345
+
346
+
347
+ def _add_mocked_oauth_routes(app: "fastapi.FastAPI", route_prefix: str = "/") -> None:
348
+ """Add fake oauth routes if app is run locally and OAuth is enabled.
349
+
350
+ Using OAuth will have the same behavior as in a Space but instead of authenticating with HF, a mocked user profile
351
+ is added to the session.
352
+ """
353
+ try:
354
+ import fastapi
355
+ from fastapi.responses import RedirectResponse
356
+ from starlette.datastructures import URL
357
+ except ImportError as e:
358
+ raise ImportError(
359
+ "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
360
+ "`huggingface_hub[oauth]` to your requirements.txt file."
361
+ ) from e
362
+
363
+ warnings.warn(
364
+ "OAuth is not supported outside of a Space environment. To help you debug your app locally, the oauth endpoints"
365
+ " are mocked to return your profile and token. To make it work, your machine must be logged in to Huggingface."
366
+ )
367
+ mocked_oauth_info = _get_mocked_oauth_info()
368
+
369
+ login_uri, callback_uri, logout_uri = _get_oauth_uris(route_prefix)
370
+
371
+ # Define OAuth routes
372
+ @app.get(login_uri)
373
+ async def oauth_login(request: fastapi.Request) -> RedirectResponse:
374
+ """Fake endpoint that redirects to HF OAuth page."""
375
+ # Define target (where to redirect after login)
376
+ redirect_uri = _generate_redirect_uri(request)
377
+ return RedirectResponse(callback_uri + "?" + urllib.parse.urlencode({"_target_url": redirect_uri}))
378
+
379
+ @app.get(callback_uri)
380
+ async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
381
+ """Endpoint that handles the OAuth callback."""
382
+ request.session["oauth_info"] = mocked_oauth_info
383
+ return RedirectResponse(_get_redirect_target(request))
384
+
385
+ @app.get(logout_uri)
386
+ async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
387
+ """Endpoint that logs out the user (e.g. delete cookie session)."""
388
+ request.session.pop("oauth_info", None)
389
+ logout_url = URL("/").include_query_params(**request.query_params)
390
+ return RedirectResponse(url=logout_url, status_code=302) # see https://github.com/gradio-app/gradio/pull/9659
391
+
392
+
393
+ def _generate_redirect_uri(request: "fastapi.Request") -> str:
394
+ if "_target_url" in request.query_params:
395
+ # if `_target_url` already in query params => respect it
396
+ target = request.query_params["_target_url"]
397
+ else:
398
+ # otherwise => keep query params
399
+ target = "/?" + urllib.parse.urlencode(request.query_params)
400
+
401
+ redirect_uri = request.url_for("oauth_redirect_callback").include_query_params(_target_url=target)
402
+ redirect_uri_as_str = str(redirect_uri)
403
+ if redirect_uri.netloc.endswith(".hf.space"):
404
+ # In Space, FastAPI redirect as http but we want https
405
+ redirect_uri_as_str = redirect_uri_as_str.replace("http://", "https://")
406
+ return redirect_uri_as_str
407
+
408
+
409
+ def _get_redirect_target(request: "fastapi.Request", default_target: str = "/") -> str:
410
+ return request.query_params.get("_target_url", default_target)
411
+
412
+
413
+ def _get_mocked_oauth_info() -> Dict:
414
+ token = get_token()
415
+ if token is None:
416
+ raise ValueError(
417
+ "Your machine must be logged in to HF to debug an OAuth app locally. Please"
418
+ " run `huggingface-cli login` or set `HF_TOKEN` as environment variable "
419
+ "with one of your access token. You can generate a new token in your "
420
+ "settings page (https://huggingface.co/settings/tokens)."
421
+ )
422
+
423
+ user = whoami()
424
+ if user["type"] != "user":
425
+ raise ValueError(
426
+ "Your machine is not logged in with a personal account. Please use a "
427
+ "personal access token. You can generate a new token in your settings page"
428
+ " (https://huggingface.co/settings/tokens)."
429
+ )
430
+
431
+ return {
432
+ "access_token": token,
433
+ "token_type": "bearer",
434
+ "expires_in": 8 * 60 * 60, # 8 hours
435
+ "id_token": "FOOBAR",
436
+ "scope": "openid profile",
437
+ "refresh_token": "hf_oauth__refresh_token",
438
+ "expires_at": int(time.time()) + 8 * 60 * 60, # 8 hours
439
+ "userinfo": {
440
+ "sub": "0123456789",
441
+ "name": user["fullname"],
442
+ "preferred_username": user["name"],
443
+ "profile": f"https://huggingface.co/{user['name']}",
444
+ "picture": user["avatarUrl"],
445
+ "website": "",
446
+ "aud": "00000000-0000-0000-0000-000000000000",
447
+ "auth_time": 1691672844,
448
+ "nonce": "aaaaaaaaaaaaaaaaaaa",
449
+ "iat": 1691672844,
450
+ "exp": 1691676444,
451
+ "iss": "https://huggingface.co",
452
+ },
453
+ }
454
+
455
+
456
+ def _get_oauth_uris(route_prefix: str = "/") -> Tuple[str, str, str]:
457
+ route_prefix = route_prefix.strip("/")
458
+ if route_prefix:
459
+ route_prefix = f"/{route_prefix}"
460
+ return (
461
+ f"{route_prefix}/oauth/huggingface/login",
462
+ f"{route_prefix}/oauth/huggingface/callback",
463
+ f"{route_prefix}/oauth/huggingface/logout",
464
+ )
@@ -7,7 +7,13 @@ from tqdm.auto import tqdm as base_tqdm
7
7
  from tqdm.contrib.concurrent import thread_map
8
8
 
9
9
  from . import constants
10
- from .errors import GatedRepoError, LocalEntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError
10
+ from .errors import (
11
+ GatedRepoError,
12
+ HfHubHTTPError,
13
+ LocalEntryNotFoundError,
14
+ RepositoryNotFoundError,
15
+ RevisionNotFoundError,
16
+ )
11
17
  from .file_download import REGEX_COMMIT_HASH, hf_hub_download, repo_folder_name
12
18
  from .hf_api import DatasetInfo, HfApi, ModelInfo, SpaceInfo
13
19
  from .utils import OfflineModeIsEnabled, filter_repo_objects, logging, validate_hf_hub_args
@@ -228,8 +234,10 @@ def snapshot_download(
228
234
  "outgoing traffic has been disabled. To enable repo look-ups and downloads online, set "
229
235
  "'HF_HUB_OFFLINE=0' as environment variable."
230
236
  ) from api_call_error
231
- elif isinstance(api_call_error, RepositoryNotFoundError) or isinstance(api_call_error, GatedRepoError):
232
- # Repo not found => let's raise the actual error
237
+ elif isinstance(api_call_error, (RepositoryNotFoundError, GatedRepoError)) or (
238
+ isinstance(api_call_error, HfHubHTTPError) and api_call_error.response.status_code == 401
239
+ ):
240
+ # Repo not found, gated, or specific authentication error => let's raise the actual error
233
241
  raise api_call_error
234
242
  else:
235
243
  # Otherwise: most likely a connection issue or Hub downtime => let's warn the user