huggingface-hub 0.29.0rc2__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.
- huggingface_hub/__init__.py +160 -46
- huggingface_hub/_commit_api.py +277 -71
- huggingface_hub/_commit_scheduler.py +15 -15
- huggingface_hub/_inference_endpoints.py +33 -22
- huggingface_hub/_jobs_api.py +301 -0
- huggingface_hub/_local_folder.py +18 -3
- huggingface_hub/_login.py +31 -63
- huggingface_hub/_oauth.py +460 -0
- huggingface_hub/_snapshot_download.py +241 -81
- huggingface_hub/_space_api.py +18 -10
- huggingface_hub/_tensorboard_logger.py +15 -19
- huggingface_hub/_upload_large_folder.py +196 -76
- huggingface_hub/_webhooks_payload.py +3 -3
- huggingface_hub/_webhooks_server.py +15 -25
- huggingface_hub/{commands → cli}/__init__.py +1 -15
- huggingface_hub/cli/_cli_utils.py +173 -0
- huggingface_hub/cli/auth.py +147 -0
- huggingface_hub/cli/cache.py +841 -0
- huggingface_hub/cli/download.py +189 -0
- huggingface_hub/cli/hf.py +60 -0
- huggingface_hub/cli/inference_endpoints.py +377 -0
- huggingface_hub/cli/jobs.py +772 -0
- huggingface_hub/cli/lfs.py +175 -0
- huggingface_hub/cli/repo.py +315 -0
- huggingface_hub/cli/repo_files.py +94 -0
- huggingface_hub/{commands/env.py → cli/system.py} +10 -13
- huggingface_hub/cli/upload.py +294 -0
- huggingface_hub/cli/upload_large_folder.py +117 -0
- huggingface_hub/community.py +20 -12
- huggingface_hub/constants.py +83 -59
- huggingface_hub/dataclasses.py +609 -0
- huggingface_hub/errors.py +99 -30
- huggingface_hub/fastai_utils.py +30 -41
- huggingface_hub/file_download.py +606 -346
- huggingface_hub/hf_api.py +2445 -1132
- huggingface_hub/hf_file_system.py +269 -152
- huggingface_hub/hub_mixin.py +61 -66
- huggingface_hub/inference/_client.py +501 -630
- huggingface_hub/inference/_common.py +133 -121
- huggingface_hub/inference/_generated/_async_client.py +536 -722
- huggingface_hub/inference/_generated/types/__init__.py +6 -1
- huggingface_hub/inference/_generated/types/automatic_speech_recognition.py +5 -6
- huggingface_hub/inference/_generated/types/base.py +10 -7
- huggingface_hub/inference/_generated/types/chat_completion.py +77 -31
- huggingface_hub/inference/_generated/types/depth_estimation.py +2 -2
- huggingface_hub/inference/_generated/types/document_question_answering.py +2 -2
- huggingface_hub/inference/_generated/types/feature_extraction.py +2 -2
- huggingface_hub/inference/_generated/types/fill_mask.py +2 -2
- huggingface_hub/inference/_generated/types/image_to_image.py +8 -2
- huggingface_hub/inference/_generated/types/image_to_text.py +2 -3
- huggingface_hub/inference/_generated/types/image_to_video.py +60 -0
- huggingface_hub/inference/_generated/types/sentence_similarity.py +3 -3
- huggingface_hub/inference/_generated/types/summarization.py +2 -2
- huggingface_hub/inference/_generated/types/table_question_answering.py +5 -5
- huggingface_hub/inference/_generated/types/text2text_generation.py +2 -2
- huggingface_hub/inference/_generated/types/text_generation.py +11 -11
- huggingface_hub/inference/_generated/types/text_to_audio.py +1 -2
- huggingface_hub/inference/_generated/types/text_to_speech.py +1 -2
- huggingface_hub/inference/_generated/types/text_to_video.py +2 -2
- huggingface_hub/inference/_generated/types/token_classification.py +2 -2
- huggingface_hub/inference/_generated/types/translation.py +2 -2
- huggingface_hub/inference/_generated/types/zero_shot_classification.py +2 -2
- huggingface_hub/inference/_generated/types/zero_shot_image_classification.py +2 -2
- huggingface_hub/inference/_generated/types/zero_shot_object_detection.py +1 -3
- huggingface_hub/inference/_mcp/__init__.py +0 -0
- huggingface_hub/inference/_mcp/_cli_hacks.py +88 -0
- huggingface_hub/inference/_mcp/agent.py +100 -0
- huggingface_hub/inference/_mcp/cli.py +247 -0
- huggingface_hub/inference/_mcp/constants.py +81 -0
- huggingface_hub/inference/_mcp/mcp_client.py +395 -0
- huggingface_hub/inference/_mcp/types.py +45 -0
- huggingface_hub/inference/_mcp/utils.py +128 -0
- huggingface_hub/inference/_providers/__init__.py +149 -20
- huggingface_hub/inference/_providers/_common.py +160 -37
- huggingface_hub/inference/_providers/black_forest_labs.py +12 -9
- huggingface_hub/inference/_providers/cerebras.py +6 -0
- huggingface_hub/inference/_providers/clarifai.py +13 -0
- huggingface_hub/inference/_providers/cohere.py +32 -0
- huggingface_hub/inference/_providers/fal_ai.py +231 -22
- huggingface_hub/inference/_providers/featherless_ai.py +38 -0
- huggingface_hub/inference/_providers/fireworks_ai.py +22 -1
- huggingface_hub/inference/_providers/groq.py +9 -0
- huggingface_hub/inference/_providers/hf_inference.py +143 -33
- huggingface_hub/inference/_providers/hyperbolic.py +9 -5
- huggingface_hub/inference/_providers/nebius.py +47 -5
- huggingface_hub/inference/_providers/novita.py +48 -5
- huggingface_hub/inference/_providers/nscale.py +44 -0
- huggingface_hub/inference/_providers/openai.py +25 -0
- huggingface_hub/inference/_providers/publicai.py +6 -0
- huggingface_hub/inference/_providers/replicate.py +46 -9
- huggingface_hub/inference/_providers/sambanova.py +37 -1
- huggingface_hub/inference/_providers/scaleway.py +28 -0
- huggingface_hub/inference/_providers/together.py +34 -5
- huggingface_hub/inference/_providers/wavespeed.py +138 -0
- huggingface_hub/inference/_providers/zai_org.py +17 -0
- huggingface_hub/lfs.py +33 -100
- huggingface_hub/repocard.py +34 -38
- huggingface_hub/repocard_data.py +79 -59
- huggingface_hub/serialization/__init__.py +0 -1
- huggingface_hub/serialization/_base.py +12 -15
- huggingface_hub/serialization/_dduf.py +8 -8
- huggingface_hub/serialization/_torch.py +69 -69
- huggingface_hub/utils/__init__.py +27 -8
- huggingface_hub/utils/_auth.py +7 -7
- huggingface_hub/utils/_cache_manager.py +92 -147
- huggingface_hub/utils/_chunk_utils.py +2 -3
- huggingface_hub/utils/_deprecation.py +1 -1
- huggingface_hub/utils/_dotenv.py +55 -0
- huggingface_hub/utils/_experimental.py +7 -5
- huggingface_hub/utils/_fixes.py +0 -10
- huggingface_hub/utils/_git_credential.py +5 -5
- huggingface_hub/utils/_headers.py +8 -30
- huggingface_hub/utils/_http.py +399 -237
- huggingface_hub/utils/_pagination.py +6 -6
- huggingface_hub/utils/_parsing.py +98 -0
- huggingface_hub/utils/_paths.py +5 -5
- huggingface_hub/utils/_runtime.py +74 -22
- huggingface_hub/utils/_safetensors.py +21 -21
- huggingface_hub/utils/_subprocess.py +13 -11
- huggingface_hub/utils/_telemetry.py +4 -4
- huggingface_hub/{commands/_cli_utils.py → utils/_terminal.py} +4 -4
- huggingface_hub/utils/_typing.py +25 -5
- huggingface_hub/utils/_validators.py +55 -74
- huggingface_hub/utils/_verification.py +167 -0
- huggingface_hub/utils/_xet.py +235 -0
- huggingface_hub/utils/_xet_progress_reporting.py +162 -0
- huggingface_hub/utils/insecure_hashlib.py +3 -5
- huggingface_hub/utils/logging.py +8 -11
- huggingface_hub/utils/tqdm.py +33 -4
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info}/METADATA +94 -82
- huggingface_hub-1.1.3.dist-info/RECORD +155 -0
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info}/WHEEL +1 -1
- huggingface_hub-1.1.3.dist-info/entry_points.txt +6 -0
- huggingface_hub/commands/delete_cache.py +0 -428
- huggingface_hub/commands/download.py +0 -200
- huggingface_hub/commands/huggingface_cli.py +0 -61
- huggingface_hub/commands/lfs.py +0 -200
- huggingface_hub/commands/repo_files.py +0 -128
- huggingface_hub/commands/scan_cache.py +0 -181
- huggingface_hub/commands/tag.py +0 -159
- huggingface_hub/commands/upload.py +0 -299
- huggingface_hub/commands/upload_large_folder.py +0 -129
- huggingface_hub/commands/user.py +0 -304
- huggingface_hub/commands/version.py +0 -37
- huggingface_hub/inference_api.py +0 -217
- huggingface_hub/keras_mixin.py +0 -500
- huggingface_hub/repository.py +0 -1477
- huggingface_hub/serialization/_tensorflow.py +0 -95
- huggingface_hub/utils/_hf_folder.py +0 -68
- huggingface_hub-0.29.0rc2.dist-info/RECORD +0 -131
- huggingface_hub-0.29.0rc2.dist-info/entry_points.txt +0 -6
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info/licenses}/LICENSE +0 -0
- {huggingface_hub-0.29.0rc2.dist-info → huggingface_hub-1.1.3.dist-info}/top_level.txt +0 -0
huggingface_hub/_login.py
CHANGED
|
@@ -19,9 +19,11 @@ from getpass import getpass
|
|
|
19
19
|
from pathlib import Path
|
|
20
20
|
from typing import Optional
|
|
21
21
|
|
|
22
|
+
import typer
|
|
23
|
+
|
|
22
24
|
from . import constants
|
|
23
|
-
from .commands._cli_utils import ANSI
|
|
24
25
|
from .utils import (
|
|
26
|
+
ANSI,
|
|
25
27
|
capture_output,
|
|
26
28
|
get_token,
|
|
27
29
|
is_google_colab,
|
|
@@ -41,7 +43,6 @@ from .utils._auth import (
|
|
|
41
43
|
_save_token,
|
|
42
44
|
get_stored_tokens,
|
|
43
45
|
)
|
|
44
|
-
from .utils._deprecation import _deprecate_arguments, _deprecate_positional_args
|
|
45
46
|
|
|
46
47
|
|
|
47
48
|
logger = logging.get_logger(__name__)
|
|
@@ -55,18 +56,11 @@ _HF_LOGO_ASCII = """
|
|
|
55
56
|
"""
|
|
56
57
|
|
|
57
58
|
|
|
58
|
-
@_deprecate_arguments(
|
|
59
|
-
version="1.0",
|
|
60
|
-
deprecated_args="write_permission",
|
|
61
|
-
custom_message="Fine-grained tokens added complexity to the permissions, making it irrelevant to check if a token has 'write' access.",
|
|
62
|
-
)
|
|
63
|
-
@_deprecate_positional_args(version="1.0")
|
|
64
59
|
def login(
|
|
65
60
|
token: Optional[str] = None,
|
|
66
61
|
*,
|
|
67
62
|
add_to_git_credential: bool = False,
|
|
68
|
-
|
|
69
|
-
write_permission: bool = False,
|
|
63
|
+
skip_if_logged_in: bool = False,
|
|
70
64
|
) -> None:
|
|
71
65
|
"""Login the machine to access the Hub.
|
|
72
66
|
|
|
@@ -75,24 +69,18 @@ def login(
|
|
|
75
69
|
components. If `token` is not provided, it will be prompted to the user either with
|
|
76
70
|
a widget (in a notebook) or via the terminal.
|
|
77
71
|
|
|
78
|
-
To log in from outside of a script, one can also use `
|
|
72
|
+
To log in from outside of a script, one can also use `hf auth login` which is
|
|
79
73
|
a cli command that wraps [`login`].
|
|
80
74
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
extends its capabilities.
|
|
85
|
-
|
|
86
|
-
</Tip>
|
|
87
|
-
|
|
88
|
-
<Tip>
|
|
75
|
+
> [!TIP]
|
|
76
|
+
> [`login`] is a drop-in replacement method for [`notebook_login`] as it wraps and
|
|
77
|
+
> extends its capabilities.
|
|
89
78
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
</Tip>
|
|
79
|
+
> [!TIP]
|
|
80
|
+
> When the token is not passed, [`login`] will automatically detect if the script runs
|
|
81
|
+
> in a notebook or not. However, this detection might not be accurate due to the
|
|
82
|
+
> variety of notebooks that exists nowadays. If that is the case, you can always force
|
|
83
|
+
> the UI by using [`notebook_login`] or [`interpreter_login`].
|
|
96
84
|
|
|
97
85
|
Args:
|
|
98
86
|
token (`str`, *optional*):
|
|
@@ -102,10 +90,8 @@ def login(
|
|
|
102
90
|
is configured, a warning will be displayed to the user. If `token` is `None`,
|
|
103
91
|
the value of `add_to_git_credential` is ignored and will be prompted again
|
|
104
92
|
to the end user.
|
|
105
|
-
|
|
106
|
-
If `True`,
|
|
107
|
-
write_permission (`bool`):
|
|
108
|
-
Ignored and deprecated argument.
|
|
93
|
+
skip_if_logged_in (`bool`, defaults to `False`):
|
|
94
|
+
If `True`, do not prompt for token if user is already logged in.
|
|
109
95
|
Raises:
|
|
110
96
|
[`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
|
|
111
97
|
If an organization token is passed. Only personal account tokens are valid
|
|
@@ -120,14 +106,14 @@ def login(
|
|
|
120
106
|
logger.info(
|
|
121
107
|
"The token has not been saved to the git credentials helper. Pass "
|
|
122
108
|
"`add_to_git_credential=True` in this function directly or "
|
|
123
|
-
"`--add-to-git-credential` if using via `
|
|
109
|
+
"`--add-to-git-credential` if using via `hf`CLI if "
|
|
124
110
|
"you want to set the git credential as well."
|
|
125
111
|
)
|
|
126
112
|
_login(token, add_to_git_credential=add_to_git_credential)
|
|
127
113
|
elif is_notebook():
|
|
128
|
-
notebook_login(
|
|
114
|
+
notebook_login(skip_if_logged_in=skip_if_logged_in)
|
|
129
115
|
else:
|
|
130
|
-
interpreter_login(
|
|
116
|
+
interpreter_login(skip_if_logged_in=skip_if_logged_in)
|
|
131
117
|
|
|
132
118
|
|
|
133
119
|
def logout(token_name: Optional[str] = None) -> None:
|
|
@@ -137,7 +123,7 @@ def logout(token_name: Optional[str] = None) -> None:
|
|
|
137
123
|
|
|
138
124
|
Args:
|
|
139
125
|
token_name (`str`, *optional*):
|
|
140
|
-
Name of the access token to logout from. If `None`, will
|
|
126
|
+
Name of the access token to logout from. If `None`, will log out from all saved access tokens.
|
|
141
127
|
Raises:
|
|
142
128
|
[`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError):
|
|
143
129
|
If the access token name is not found.
|
|
@@ -233,7 +219,7 @@ def auth_list() -> None:
|
|
|
233
219
|
)
|
|
234
220
|
elif current_token_name is None:
|
|
235
221
|
logger.warning(
|
|
236
|
-
"\nNote: No active token is set and no environment variable `HF_TOKEN` is found. Use `
|
|
222
|
+
"\nNote: No active token is set and no environment variable `HF_TOKEN` is found. Use `hf auth login` to log in."
|
|
237
223
|
)
|
|
238
224
|
|
|
239
225
|
|
|
@@ -242,13 +228,7 @@ def auth_list() -> None:
|
|
|
242
228
|
###
|
|
243
229
|
|
|
244
230
|
|
|
245
|
-
|
|
246
|
-
version="1.0",
|
|
247
|
-
deprecated_args="write_permission",
|
|
248
|
-
custom_message="Fine-grained tokens added complexity to the permissions, making it irrelevant to check if a token has 'write' access.",
|
|
249
|
-
)
|
|
250
|
-
@_deprecate_positional_args(version="1.0")
|
|
251
|
-
def interpreter_login(*, new_session: bool = True, write_permission: bool = False) -> None:
|
|
231
|
+
def interpreter_login(*, skip_if_logged_in: bool = False) -> None:
|
|
252
232
|
"""
|
|
253
233
|
Displays a prompt to log in to the HF website and store the token.
|
|
254
234
|
|
|
@@ -259,22 +239,18 @@ def interpreter_login(*, new_session: bool = True, write_permission: bool = Fals
|
|
|
259
239
|
For more details, see [`login`].
|
|
260
240
|
|
|
261
241
|
Args:
|
|
262
|
-
|
|
263
|
-
If `True`,
|
|
264
|
-
write_permission (`bool`):
|
|
265
|
-
Ignored and deprecated argument.
|
|
242
|
+
skip_if_logged_in (`bool`, defaults to `False`):
|
|
243
|
+
If `True`, do not prompt for token if user is already logged in.
|
|
266
244
|
"""
|
|
267
|
-
if not
|
|
245
|
+
if not skip_if_logged_in and get_token() is not None:
|
|
268
246
|
logger.info("User is already logged in.")
|
|
269
247
|
return
|
|
270
248
|
|
|
271
|
-
from .commands.delete_cache import _ask_for_confirmation_no_tui
|
|
272
|
-
|
|
273
249
|
print(_HF_LOGO_ASCII)
|
|
274
250
|
if get_token() is not None:
|
|
275
251
|
logger.info(
|
|
276
|
-
" A token is already saved on your machine. Run `
|
|
277
|
-
"
|
|
252
|
+
" A token is already saved on your machine. Run `hf auth whoami`"
|
|
253
|
+
" to get more information or `hf auth logout` if you want"
|
|
278
254
|
" to log out."
|
|
279
255
|
)
|
|
280
256
|
logger.info(" Setting a new token will erase the existing one.")
|
|
@@ -285,7 +261,7 @@ def interpreter_login(*, new_session: bool = True, write_permission: bool = Fals
|
|
|
285
261
|
if os.name == "nt":
|
|
286
262
|
logger.info("Token can be pasted using 'Right-Click'.")
|
|
287
263
|
token = getpass("Enter your token (input will not be visible): ")
|
|
288
|
-
add_to_git_credential =
|
|
264
|
+
add_to_git_credential = typer.confirm("Add token as git credential?")
|
|
289
265
|
|
|
290
266
|
_login(token=token, add_to_git_credential=add_to_git_credential)
|
|
291
267
|
|
|
@@ -314,13 +290,7 @@ NOTEBOOK_LOGIN_TOKEN_HTML_END = """
|
|
|
314
290
|
notebooks. </center>"""
|
|
315
291
|
|
|
316
292
|
|
|
317
|
-
|
|
318
|
-
version="1.0",
|
|
319
|
-
deprecated_args="write_permission",
|
|
320
|
-
custom_message="Fine-grained tokens added complexity to the permissions, making it irrelevant to check if a token has 'write' access.",
|
|
321
|
-
)
|
|
322
|
-
@_deprecate_positional_args(version="1.0")
|
|
323
|
-
def notebook_login(*, new_session: bool = True, write_permission: bool = False) -> None:
|
|
293
|
+
def notebook_login(*, skip_if_logged_in: bool = False) -> None:
|
|
324
294
|
"""
|
|
325
295
|
Displays a widget to log in to the HF website and store the token.
|
|
326
296
|
|
|
@@ -331,10 +301,8 @@ def notebook_login(*, new_session: bool = True, write_permission: bool = False)
|
|
|
331
301
|
For more details, see [`login`].
|
|
332
302
|
|
|
333
303
|
Args:
|
|
334
|
-
|
|
335
|
-
If `True`,
|
|
336
|
-
write_permission (`bool`):
|
|
337
|
-
Ignored and deprecated argument.
|
|
304
|
+
skip_if_logged_in (`bool`, defaults to `False`):
|
|
305
|
+
If `True`, do not prompt for token if user is already logged in.
|
|
338
306
|
"""
|
|
339
307
|
try:
|
|
340
308
|
import ipywidgets.widgets as widgets # type: ignore
|
|
@@ -344,7 +312,7 @@ def notebook_login(*, new_session: bool = True, write_permission: bool = False)
|
|
|
344
312
|
"The `notebook_login` function can only be used in a notebook (Jupyter or"
|
|
345
313
|
" Colab) and you need the `ipywidgets` module: `pip install ipywidgets`."
|
|
346
314
|
)
|
|
347
|
-
if not
|
|
315
|
+
if not skip_if_logged_in and get_token() is not None:
|
|
348
316
|
logger.info("User is already logged in.")
|
|
349
317
|
return
|
|
350
318
|
|
|
@@ -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
|
+
)
|