huggingface-hub 0.31.0rc0__py3-none-any.whl → 1.1.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.
Files changed (150) hide show
  1. huggingface_hub/__init__.py +145 -46
  2. huggingface_hub/_commit_api.py +168 -119
  3. huggingface_hub/_commit_scheduler.py +15 -15
  4. huggingface_hub/_inference_endpoints.py +15 -12
  5. huggingface_hub/_jobs_api.py +301 -0
  6. huggingface_hub/_local_folder.py +18 -3
  7. huggingface_hub/_login.py +31 -63
  8. huggingface_hub/_oauth.py +460 -0
  9. huggingface_hub/_snapshot_download.py +239 -80
  10. huggingface_hub/_space_api.py +5 -5
  11. huggingface_hub/_tensorboard_logger.py +15 -19
  12. huggingface_hub/_upload_large_folder.py +172 -76
  13. huggingface_hub/_webhooks_payload.py +3 -3
  14. huggingface_hub/_webhooks_server.py +13 -25
  15. huggingface_hub/{commands → cli}/__init__.py +1 -15
  16. huggingface_hub/cli/_cli_utils.py +173 -0
  17. huggingface_hub/cli/auth.py +147 -0
  18. huggingface_hub/cli/cache.py +841 -0
  19. huggingface_hub/cli/download.py +189 -0
  20. huggingface_hub/cli/hf.py +60 -0
  21. huggingface_hub/cli/inference_endpoints.py +377 -0
  22. huggingface_hub/cli/jobs.py +772 -0
  23. huggingface_hub/cli/lfs.py +175 -0
  24. huggingface_hub/cli/repo.py +315 -0
  25. huggingface_hub/cli/repo_files.py +94 -0
  26. huggingface_hub/{commands/env.py → cli/system.py} +10 -13
  27. huggingface_hub/cli/upload.py +294 -0
  28. huggingface_hub/cli/upload_large_folder.py +117 -0
  29. huggingface_hub/community.py +20 -12
  30. huggingface_hub/constants.py +38 -53
  31. huggingface_hub/dataclasses.py +609 -0
  32. huggingface_hub/errors.py +80 -30
  33. huggingface_hub/fastai_utils.py +30 -41
  34. huggingface_hub/file_download.py +435 -351
  35. huggingface_hub/hf_api.py +2050 -1124
  36. huggingface_hub/hf_file_system.py +269 -152
  37. huggingface_hub/hub_mixin.py +43 -63
  38. huggingface_hub/inference/_client.py +347 -434
  39. huggingface_hub/inference/_common.py +133 -121
  40. huggingface_hub/inference/_generated/_async_client.py +397 -541
  41. huggingface_hub/inference/_generated/types/__init__.py +5 -1
  42. huggingface_hub/inference/_generated/types/automatic_speech_recognition.py +3 -3
  43. huggingface_hub/inference/_generated/types/base.py +10 -7
  44. huggingface_hub/inference/_generated/types/chat_completion.py +59 -23
  45. huggingface_hub/inference/_generated/types/depth_estimation.py +2 -2
  46. huggingface_hub/inference/_generated/types/document_question_answering.py +2 -2
  47. huggingface_hub/inference/_generated/types/feature_extraction.py +2 -2
  48. huggingface_hub/inference/_generated/types/fill_mask.py +2 -2
  49. huggingface_hub/inference/_generated/types/image_to_image.py +6 -2
  50. huggingface_hub/inference/_generated/types/image_to_video.py +60 -0
  51. huggingface_hub/inference/_generated/types/sentence_similarity.py +3 -3
  52. huggingface_hub/inference/_generated/types/summarization.py +2 -2
  53. huggingface_hub/inference/_generated/types/table_question_answering.py +5 -5
  54. huggingface_hub/inference/_generated/types/text2text_generation.py +2 -2
  55. huggingface_hub/inference/_generated/types/text_generation.py +10 -10
  56. huggingface_hub/inference/_generated/types/text_to_video.py +2 -2
  57. huggingface_hub/inference/_generated/types/token_classification.py +2 -2
  58. huggingface_hub/inference/_generated/types/translation.py +2 -2
  59. huggingface_hub/inference/_generated/types/zero_shot_classification.py +2 -2
  60. huggingface_hub/inference/_generated/types/zero_shot_image_classification.py +2 -2
  61. huggingface_hub/inference/_generated/types/zero_shot_object_detection.py +1 -3
  62. huggingface_hub/inference/_mcp/__init__.py +0 -0
  63. huggingface_hub/inference/_mcp/_cli_hacks.py +88 -0
  64. huggingface_hub/inference/_mcp/agent.py +100 -0
  65. huggingface_hub/inference/_mcp/cli.py +247 -0
  66. huggingface_hub/inference/_mcp/constants.py +81 -0
  67. huggingface_hub/inference/_mcp/mcp_client.py +395 -0
  68. huggingface_hub/inference/_mcp/types.py +45 -0
  69. huggingface_hub/inference/_mcp/utils.py +128 -0
  70. huggingface_hub/inference/_providers/__init__.py +82 -7
  71. huggingface_hub/inference/_providers/_common.py +129 -27
  72. huggingface_hub/inference/_providers/black_forest_labs.py +6 -6
  73. huggingface_hub/inference/_providers/cerebras.py +1 -1
  74. huggingface_hub/inference/_providers/clarifai.py +13 -0
  75. huggingface_hub/inference/_providers/cohere.py +20 -3
  76. huggingface_hub/inference/_providers/fal_ai.py +183 -56
  77. huggingface_hub/inference/_providers/featherless_ai.py +38 -0
  78. huggingface_hub/inference/_providers/fireworks_ai.py +18 -0
  79. huggingface_hub/inference/_providers/groq.py +9 -0
  80. huggingface_hub/inference/_providers/hf_inference.py +69 -30
  81. huggingface_hub/inference/_providers/hyperbolic.py +4 -4
  82. huggingface_hub/inference/_providers/nebius.py +33 -5
  83. huggingface_hub/inference/_providers/novita.py +5 -5
  84. huggingface_hub/inference/_providers/nscale.py +44 -0
  85. huggingface_hub/inference/_providers/openai.py +3 -1
  86. huggingface_hub/inference/_providers/publicai.py +6 -0
  87. huggingface_hub/inference/_providers/replicate.py +31 -13
  88. huggingface_hub/inference/_providers/sambanova.py +18 -4
  89. huggingface_hub/inference/_providers/scaleway.py +28 -0
  90. huggingface_hub/inference/_providers/together.py +20 -5
  91. huggingface_hub/inference/_providers/wavespeed.py +138 -0
  92. huggingface_hub/inference/_providers/zai_org.py +17 -0
  93. huggingface_hub/lfs.py +33 -100
  94. huggingface_hub/repocard.py +34 -38
  95. huggingface_hub/repocard_data.py +57 -57
  96. huggingface_hub/serialization/__init__.py +0 -1
  97. huggingface_hub/serialization/_base.py +12 -15
  98. huggingface_hub/serialization/_dduf.py +8 -8
  99. huggingface_hub/serialization/_torch.py +69 -69
  100. huggingface_hub/utils/__init__.py +19 -8
  101. huggingface_hub/utils/_auth.py +7 -7
  102. huggingface_hub/utils/_cache_manager.py +92 -147
  103. huggingface_hub/utils/_chunk_utils.py +2 -3
  104. huggingface_hub/utils/_deprecation.py +1 -1
  105. huggingface_hub/utils/_dotenv.py +55 -0
  106. huggingface_hub/utils/_experimental.py +7 -5
  107. huggingface_hub/utils/_fixes.py +0 -10
  108. huggingface_hub/utils/_git_credential.py +5 -5
  109. huggingface_hub/utils/_headers.py +8 -30
  110. huggingface_hub/utils/_http.py +398 -239
  111. huggingface_hub/utils/_pagination.py +4 -4
  112. huggingface_hub/utils/_parsing.py +98 -0
  113. huggingface_hub/utils/_paths.py +5 -5
  114. huggingface_hub/utils/_runtime.py +61 -24
  115. huggingface_hub/utils/_safetensors.py +21 -21
  116. huggingface_hub/utils/_subprocess.py +9 -9
  117. huggingface_hub/utils/_telemetry.py +4 -4
  118. huggingface_hub/{commands/_cli_utils.py → utils/_terminal.py} +4 -4
  119. huggingface_hub/utils/_typing.py +25 -5
  120. huggingface_hub/utils/_validators.py +55 -74
  121. huggingface_hub/utils/_verification.py +167 -0
  122. huggingface_hub/utils/_xet.py +64 -17
  123. huggingface_hub/utils/_xet_progress_reporting.py +162 -0
  124. huggingface_hub/utils/insecure_hashlib.py +3 -5
  125. huggingface_hub/utils/logging.py +8 -11
  126. huggingface_hub/utils/tqdm.py +5 -4
  127. {huggingface_hub-0.31.0rc0.dist-info → huggingface_hub-1.1.3.dist-info}/METADATA +94 -85
  128. huggingface_hub-1.1.3.dist-info/RECORD +155 -0
  129. {huggingface_hub-0.31.0rc0.dist-info → huggingface_hub-1.1.3.dist-info}/WHEEL +1 -1
  130. huggingface_hub-1.1.3.dist-info/entry_points.txt +6 -0
  131. huggingface_hub/commands/delete_cache.py +0 -474
  132. huggingface_hub/commands/download.py +0 -200
  133. huggingface_hub/commands/huggingface_cli.py +0 -61
  134. huggingface_hub/commands/lfs.py +0 -200
  135. huggingface_hub/commands/repo_files.py +0 -128
  136. huggingface_hub/commands/scan_cache.py +0 -181
  137. huggingface_hub/commands/tag.py +0 -159
  138. huggingface_hub/commands/upload.py +0 -314
  139. huggingface_hub/commands/upload_large_folder.py +0 -129
  140. huggingface_hub/commands/user.py +0 -304
  141. huggingface_hub/commands/version.py +0 -37
  142. huggingface_hub/inference_api.py +0 -217
  143. huggingface_hub/keras_mixin.py +0 -500
  144. huggingface_hub/repository.py +0 -1477
  145. huggingface_hub/serialization/_tensorflow.py +0 -95
  146. huggingface_hub/utils/_hf_folder.py +0 -68
  147. huggingface_hub-0.31.0rc0.dist-info/RECORD +0 -135
  148. huggingface_hub-0.31.0rc0.dist-info/entry_points.txt +0 -6
  149. {huggingface_hub-0.31.0rc0.dist-info → huggingface_hub-1.1.3.dist-info/licenses}/LICENSE +0 -0
  150. {huggingface_hub-0.31.0rc0.dist-info → huggingface_hub-1.1.3.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,460 @@
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, Literal, Optional, 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
+ security_restrictions (`Optional[list[Literal["ip", "token-policy", "mfa", "sso"]]]`, *optional*):
43
+ Array of security restrictions that the user hasn't completed for this org. Possible values: "ip", "token-policy", "mfa", "sso". Hugging Face field.
44
+ """
45
+
46
+ sub: str
47
+ name: str
48
+ preferred_username: str
49
+ picture: str
50
+ is_enterprise: bool
51
+ can_pay: Optional[bool] = None
52
+ role_in_org: Optional[str] = None
53
+ security_restrictions: Optional[list[Literal["ip", "token-policy", "mfa", "sso"]]] = None
54
+
55
+
56
+ @dataclass
57
+ class OAuthUserInfo:
58
+ """
59
+ Information about a user logged in with OAuth.
60
+
61
+ Attributes:
62
+ sub (`str`):
63
+ Unique identifier for the user, even in case of rename. OpenID Connect field.
64
+ name (`str`):
65
+ The user's full name. OpenID Connect field.
66
+ preferred_username (`str`):
67
+ The user's username. OpenID Connect field.
68
+ email_verified (`Optional[bool]`, *optional*):
69
+ Indicates if the user's email is verified. OpenID Connect field.
70
+ email (`Optional[str]`, *optional*):
71
+ The user's email address. OpenID Connect field.
72
+ picture (`str`):
73
+ The user's profile picture URL. OpenID Connect field.
74
+ profile (`str`):
75
+ The user's profile URL. OpenID Connect field.
76
+ website (`Optional[str]`, *optional*):
77
+ The user's website URL. OpenID Connect field.
78
+ is_pro (`bool`):
79
+ Whether the user is a pro user. Hugging Face field.
80
+ can_pay (`Optional[bool]`, *optional*):
81
+ Whether the user has a payment method set up. Hugging Face field.
82
+ orgs (`Optional[list[OrgInfo]]`, *optional*):
83
+ List of organizations the user is part of. Hugging Face field.
84
+ """
85
+
86
+ sub: str
87
+ name: str
88
+ preferred_username: str
89
+ email_verified: Optional[bool]
90
+ email: Optional[str]
91
+ picture: str
92
+ profile: str
93
+ website: Optional[str]
94
+ is_pro: bool
95
+ can_pay: Optional[bool]
96
+ orgs: Optional[list[OAuthOrgInfo]]
97
+
98
+
99
+ @dataclass
100
+ class OAuthInfo:
101
+ """
102
+ Information about the OAuth login.
103
+
104
+ Attributes:
105
+ access_token (`str`):
106
+ The access token.
107
+ access_token_expires_at (`datetime.datetime`):
108
+ The expiration date of the access token.
109
+ user_info ([`OAuthUserInfo`]):
110
+ The user information.
111
+ state (`str`, *optional*):
112
+ State passed to the OAuth provider in the original request to the OAuth provider.
113
+ scope (`str`):
114
+ Granted scope.
115
+ """
116
+
117
+ access_token: str
118
+ access_token_expires_at: datetime.datetime
119
+ user_info: OAuthUserInfo
120
+ state: Optional[str]
121
+ scope: str
122
+
123
+
124
+ @experimental
125
+ def attach_huggingface_oauth(app: "fastapi.FastAPI", route_prefix: str = "/"):
126
+ """
127
+ Add OAuth endpoints to a FastAPI app to enable OAuth login with Hugging Face.
128
+
129
+ How to use:
130
+ - Call this method on your FastAPI app to add the OAuth endpoints.
131
+ - Inside your route handlers, call `parse_huggingface_oauth(request)` to retrieve the OAuth info.
132
+ - If user is logged in, an [`OAuthInfo`] object is returned with the user's info. If not, `None` is returned.
133
+ - In your app, make sure to add links to `/oauth/huggingface/login` and `/oauth/huggingface/logout` for the user to log in and out.
134
+
135
+ Example:
136
+ ```py
137
+ from huggingface_hub import attach_huggingface_oauth, parse_huggingface_oauth
138
+
139
+ # Create a FastAPI app
140
+ app = FastAPI()
141
+
142
+ # Add OAuth endpoints to the FastAPI app
143
+ attach_huggingface_oauth(app)
144
+
145
+ # Add a route that greets the user if they are logged in
146
+ @app.get("/")
147
+ def greet_json(request: Request):
148
+ # Retrieve the OAuth info from the request
149
+ oauth_info = parse_huggingface_oauth(request) # e.g. OAuthInfo dataclass
150
+ if oauth_info is None:
151
+ return {"msg": "Not logged in!"}
152
+ return {"msg": f"Hello, {oauth_info.user_info.preferred_username}!"}
153
+ ```
154
+ """
155
+ # TODO: handle generic case (handling OAuth in a non-Space environment with custom dev values) (low priority)
156
+
157
+ # Add SessionMiddleware to the FastAPI app to store the OAuth info in the session.
158
+ # Session Middleware requires a secret key to sign the cookies. Let's use a hash
159
+ # of the OAuth secret key to make it unique to the Space + updated in case OAuth
160
+ # config gets updated. When ran locally, we use an empty string as a secret key.
161
+ try:
162
+ from starlette.middleware.sessions import SessionMiddleware
163
+ except ImportError as e:
164
+ raise ImportError(
165
+ "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
166
+ "`huggingface_hub[oauth]` to your requirements.txt file in order to install the required dependencies."
167
+ ) from e
168
+ session_secret = (constants.OAUTH_CLIENT_SECRET or "") + "-v1"
169
+ app.add_middleware(
170
+ SessionMiddleware, # type: ignore[arg-type]
171
+ secret_key=hashlib.sha256(session_secret.encode()).hexdigest(),
172
+ same_site="none",
173
+ https_only=True,
174
+ ) # type: ignore
175
+
176
+ # Add OAuth endpoints to the FastAPI app:
177
+ # - {route_prefix}/oauth/huggingface/login
178
+ # - {route_prefix}/oauth/huggingface/callback
179
+ # - {route_prefix}/oauth/huggingface/logout
180
+ # If the app is running in a Space, OAuth is enabled normally.
181
+ # Otherwise, we mock the endpoints to make the user log in with a fake user profile - without any calls to hf.co.
182
+ route_prefix = route_prefix.strip("/")
183
+ if os.getenv("SPACE_ID") is not None:
184
+ logger.info("OAuth is enabled in the Space. Adding OAuth routes.")
185
+ _add_oauth_routes(app, route_prefix=route_prefix)
186
+ else:
187
+ logger.info("App is not running in a Space. Adding mocked OAuth routes.")
188
+ _add_mocked_oauth_routes(app, route_prefix=route_prefix)
189
+
190
+
191
+ def parse_huggingface_oauth(request: "fastapi.Request") -> Optional[OAuthInfo]:
192
+ """
193
+ Returns the information from a logged-in user as a [`OAuthInfo`] object.
194
+
195
+ For flexibility and future-proofing, this method is very lax in its parsing and does not raise errors.
196
+ Missing fields are set to `None` without a warning.
197
+
198
+ Return `None`, if the user is not logged in (no info in session cookie).
199
+
200
+ See [`attach_huggingface_oauth`] for an example on how to use this method.
201
+ """
202
+ if "oauth_info" not in request.session:
203
+ logger.debug("No OAuth info in session.")
204
+ return None
205
+
206
+ logger.debug("Parsing OAuth info from session.")
207
+ oauth_data = request.session["oauth_info"]
208
+ user_data = oauth_data.get("userinfo", {})
209
+ orgs_data = user_data.get("orgs", [])
210
+
211
+ orgs = (
212
+ [
213
+ OAuthOrgInfo(
214
+ sub=org.get("sub"),
215
+ name=org.get("name"),
216
+ preferred_username=org.get("preferred_username"),
217
+ picture=org.get("picture"),
218
+ is_enterprise=org.get("isEnterprise"),
219
+ can_pay=org.get("canPay"),
220
+ role_in_org=org.get("roleInOrg"),
221
+ security_restrictions=org.get("securityRestrictions"),
222
+ )
223
+ for org in orgs_data
224
+ ]
225
+ if orgs_data
226
+ else None
227
+ )
228
+
229
+ user_info = OAuthUserInfo(
230
+ sub=user_data.get("sub"),
231
+ name=user_data.get("name"),
232
+ preferred_username=user_data.get("preferred_username"),
233
+ email_verified=user_data.get("email_verified"),
234
+ email=user_data.get("email"),
235
+ picture=user_data.get("picture"),
236
+ profile=user_data.get("profile"),
237
+ website=user_data.get("website"),
238
+ is_pro=user_data.get("isPro"),
239
+ can_pay=user_data.get("canPay"),
240
+ orgs=orgs,
241
+ )
242
+
243
+ return OAuthInfo(
244
+ access_token=oauth_data.get("access_token"),
245
+ access_token_expires_at=datetime.datetime.fromtimestamp(oauth_data.get("expires_at")),
246
+ user_info=user_info,
247
+ state=oauth_data.get("state"),
248
+ scope=oauth_data.get("scope"),
249
+ )
250
+
251
+
252
+ def _add_oauth_routes(app: "fastapi.FastAPI", route_prefix: str) -> None:
253
+ """Add OAuth routes to the FastAPI app (login, callback handler and logout)."""
254
+ try:
255
+ import fastapi
256
+ from authlib.integrations.base_client.errors import MismatchingStateError
257
+ from authlib.integrations.starlette_client import OAuth
258
+ from fastapi.responses import RedirectResponse
259
+ except ImportError as e:
260
+ raise ImportError(
261
+ "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
262
+ "`huggingface_hub[oauth]` to your requirements.txt file."
263
+ ) from e
264
+
265
+ # Check environment variables
266
+ msg = (
267
+ "OAuth is required but '{}' environment variable is not set. Make sure you've enabled OAuth in your Space by"
268
+ " setting `hf_oauth: true` in the Space metadata."
269
+ )
270
+ if constants.OAUTH_CLIENT_ID is None:
271
+ raise ValueError(msg.format("OAUTH_CLIENT_ID"))
272
+ if constants.OAUTH_CLIENT_SECRET is None:
273
+ raise ValueError(msg.format("OAUTH_CLIENT_SECRET"))
274
+ if constants.OAUTH_SCOPES is None:
275
+ raise ValueError(msg.format("OAUTH_SCOPES"))
276
+ if constants.OPENID_PROVIDER_URL is None:
277
+ raise ValueError(msg.format("OPENID_PROVIDER_URL"))
278
+
279
+ # Register OAuth server
280
+ oauth = OAuth()
281
+ oauth.register(
282
+ name="huggingface",
283
+ client_id=constants.OAUTH_CLIENT_ID,
284
+ client_secret=constants.OAUTH_CLIENT_SECRET,
285
+ client_kwargs={"scope": constants.OAUTH_SCOPES},
286
+ server_metadata_url=constants.OPENID_PROVIDER_URL + "/.well-known/openid-configuration",
287
+ )
288
+
289
+ login_uri, callback_uri, logout_uri = _get_oauth_uris(route_prefix)
290
+
291
+ # Register OAuth endpoints
292
+ @app.get(login_uri)
293
+ async def oauth_login(request: fastapi.Request) -> RedirectResponse:
294
+ """Endpoint that redirects to HF OAuth page."""
295
+ redirect_uri = _generate_redirect_uri(request)
296
+ return await oauth.huggingface.authorize_redirect(request, redirect_uri) # type: ignore
297
+
298
+ @app.get(callback_uri)
299
+ async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
300
+ """Endpoint that handles the OAuth callback."""
301
+ try:
302
+ oauth_info = await oauth.huggingface.authorize_access_token(request) # type: ignore
303
+ except MismatchingStateError:
304
+ # Parse query params
305
+ nb_redirects = int(request.query_params.get("_nb_redirects", 0))
306
+ target_url = request.query_params.get("_target_url")
307
+
308
+ # Build redirect URI with the same query params as before and bump nb_redirects count
309
+ query_params: dict[str, Union[int, str]] = {"_nb_redirects": nb_redirects + 1}
310
+ if target_url:
311
+ query_params["_target_url"] = target_url
312
+
313
+ redirect_uri = f"{login_uri}?{urllib.parse.urlencode(query_params)}"
314
+
315
+ # If the user is redirected more than 3 times, it is very likely that the cookie is not working properly.
316
+ # (e.g. browser is blocking third-party cookies in iframe). In this case, redirect the user in the
317
+ # non-iframe view.
318
+ if nb_redirects > constants.OAUTH_MAX_REDIRECTS:
319
+ host = os.environ.get("SPACE_HOST")
320
+ if host is None: # cannot happen in a Space
321
+ raise RuntimeError(
322
+ "App is not running in a Space (SPACE_HOST environment variable is not set). Cannot redirect to non-iframe view."
323
+ ) from None
324
+ host_url = "https://" + host.rstrip("/")
325
+ return RedirectResponse(host_url + redirect_uri)
326
+
327
+ # Redirect the user to the login page again
328
+ return RedirectResponse(redirect_uri)
329
+
330
+ # OAuth login worked => store the user info in the session and redirect
331
+ logger.debug("Successfully logged in with OAuth. Storing user info in session.")
332
+ request.session["oauth_info"] = oauth_info
333
+ return RedirectResponse(_get_redirect_target(request))
334
+
335
+ @app.get(logout_uri)
336
+ async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
337
+ """Endpoint that logs out the user (e.g. delete info from cookie session)."""
338
+ logger.debug("Logged out with OAuth. Removing user info from session.")
339
+ request.session.pop("oauth_info", None)
340
+ return RedirectResponse(_get_redirect_target(request))
341
+
342
+
343
+ def _add_mocked_oauth_routes(app: "fastapi.FastAPI", route_prefix: str = "/") -> None:
344
+ """Add fake oauth routes if app is run locally and OAuth is enabled.
345
+
346
+ Using OAuth will have the same behavior as in a Space but instead of authenticating with HF, a mocked user profile
347
+ is added to the session.
348
+ """
349
+ try:
350
+ import fastapi
351
+ from fastapi.responses import RedirectResponse
352
+ from starlette.datastructures import URL
353
+ except ImportError as e:
354
+ raise ImportError(
355
+ "Cannot initialize OAuth to due a missing library. Please run `pip install huggingface_hub[oauth]` or add "
356
+ "`huggingface_hub[oauth]` to your requirements.txt file."
357
+ ) from e
358
+
359
+ warnings.warn(
360
+ "OAuth is not supported outside of a Space environment. To help you debug your app locally, the oauth endpoints"
361
+ " are mocked to return your profile and token. To make it work, your machine must be logged in to Huggingface."
362
+ )
363
+ mocked_oauth_info = _get_mocked_oauth_info()
364
+
365
+ login_uri, callback_uri, logout_uri = _get_oauth_uris(route_prefix)
366
+
367
+ # Define OAuth routes
368
+ @app.get(login_uri)
369
+ async def oauth_login(request: fastapi.Request) -> RedirectResponse:
370
+ """Fake endpoint that redirects to HF OAuth page."""
371
+ # Define target (where to redirect after login)
372
+ redirect_uri = _generate_redirect_uri(request)
373
+ return RedirectResponse(callback_uri + "?" + urllib.parse.urlencode({"_target_url": redirect_uri}))
374
+
375
+ @app.get(callback_uri)
376
+ async def oauth_redirect_callback(request: fastapi.Request) -> RedirectResponse:
377
+ """Endpoint that handles the OAuth callback."""
378
+ request.session["oauth_info"] = mocked_oauth_info
379
+ return RedirectResponse(_get_redirect_target(request))
380
+
381
+ @app.get(logout_uri)
382
+ async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
383
+ """Endpoint that logs out the user (e.g. delete cookie session)."""
384
+ request.session.pop("oauth_info", None)
385
+ logout_url = URL("/").include_query_params(**request.query_params)
386
+ return RedirectResponse(url=logout_url, status_code=302) # see https://github.com/gradio-app/gradio/pull/9659
387
+
388
+
389
+ def _generate_redirect_uri(request: "fastapi.Request") -> str:
390
+ if "_target_url" in request.query_params:
391
+ # if `_target_url` already in query params => respect it
392
+ target = request.query_params["_target_url"]
393
+ else:
394
+ # otherwise => keep query params
395
+ target = "/?" + urllib.parse.urlencode(request.query_params)
396
+
397
+ redirect_uri = request.url_for("oauth_redirect_callback").include_query_params(_target_url=target)
398
+ redirect_uri_as_str = str(redirect_uri)
399
+ if redirect_uri.netloc.endswith(".hf.space"):
400
+ # In Space, FastAPI redirect as http but we want https
401
+ redirect_uri_as_str = redirect_uri_as_str.replace("http://", "https://")
402
+ return redirect_uri_as_str
403
+
404
+
405
+ def _get_redirect_target(request: "fastapi.Request", default_target: str = "/") -> str:
406
+ return request.query_params.get("_target_url", default_target)
407
+
408
+
409
+ def _get_mocked_oauth_info() -> dict:
410
+ token = get_token()
411
+ if token is None:
412
+ raise ValueError(
413
+ "Your machine must be logged in to HF to debug an OAuth app locally. Please"
414
+ " run `hf auth login` or set `HF_TOKEN` as environment variable "
415
+ "with one of your access token. You can generate a new token in your "
416
+ "settings page (https://huggingface.co/settings/tokens)."
417
+ )
418
+
419
+ user = whoami()
420
+ if user["type"] != "user":
421
+ raise ValueError(
422
+ "Your machine is not logged in with a personal account. Please use a "
423
+ "personal access token. You can generate a new token in your settings page"
424
+ " (https://huggingface.co/settings/tokens)."
425
+ )
426
+
427
+ return {
428
+ "access_token": token,
429
+ "token_type": "bearer",
430
+ "expires_in": 8 * 60 * 60, # 8 hours
431
+ "id_token": "FOOBAR",
432
+ "scope": "openid profile",
433
+ "refresh_token": "hf_oauth__refresh_token",
434
+ "expires_at": int(time.time()) + 8 * 60 * 60, # 8 hours
435
+ "userinfo": {
436
+ "sub": "0123456789",
437
+ "name": user["fullname"],
438
+ "preferred_username": user["name"],
439
+ "profile": f"https://huggingface.co/{user['name']}",
440
+ "picture": user["avatarUrl"],
441
+ "website": "",
442
+ "aud": "00000000-0000-0000-0000-000000000000",
443
+ "auth_time": 1691672844,
444
+ "nonce": "aaaaaaaaaaaaaaaaaaa",
445
+ "iat": 1691672844,
446
+ "exp": 1691676444,
447
+ "iss": "https://huggingface.co",
448
+ },
449
+ }
450
+
451
+
452
+ def _get_oauth_uris(route_prefix: str = "/") -> tuple[str, str, str]:
453
+ route_prefix = route_prefix.strip("/")
454
+ if route_prefix:
455
+ route_prefix = f"/{route_prefix}"
456
+ return (
457
+ f"{route_prefix}/oauth/huggingface/login",
458
+ f"{route_prefix}/oauth/huggingface/callback",
459
+ f"{route_prefix}/oauth/huggingface/logout",
460
+ )