huggingface-hub 0.20.1__py3-none-any.whl → 0.20.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.
@@ -46,7 +46,7 @@ import sys
46
46
  from typing import TYPE_CHECKING
47
47
 
48
48
 
49
- __version__ = "0.20.1"
49
+ __version__ = "0.20.3"
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
@@ -13,12 +13,12 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  """Contain helper class to retrieve/store token from/to local cache."""
16
- import os
17
16
  import warnings
18
17
  from pathlib import Path
19
18
  from typing import Optional
20
19
 
21
20
  from .. import constants
21
+ from ._token import get_token
22
22
 
23
23
 
24
24
  class HfFolder:
@@ -50,12 +50,8 @@ class HfFolder:
50
50
  """
51
51
  Get token or None if not existent.
52
52
 
53
- Note that a token can be also provided using the `HF_TOKEN` environment variable.
54
-
55
- Token is saved in the huggingface home folder. You can configure it by setting
56
- the `HF_HOME` environment variable. Previous location was `~/.huggingface/token`.
57
- If token is found in old location but not in new location, it is copied there first.
58
- For more details, see https://github.com/huggingface/huggingface_hub/issues/1232.
53
+ This method is deprecated in favor of [`huggingface_hub.get_token`] but is kept for backward compatibility.
54
+ Its behavior is the same as [`huggingface_hub.get_token`].
59
55
 
60
56
  Returns:
61
57
  `str` or `None`: The token, `None` if it doesn't exist.
@@ -66,22 +62,7 @@ class HfFolder:
66
62
  except Exception: # if not possible (e.g. PermissionError), do not raise
67
63
  pass
68
64
 
69
- # 1. Is it set by environment variable ?
70
- token: Optional[str] = os.environ.get("HF_TOKEN")
71
- if token is None: # Ensure backward compatibility but doesn't have priority
72
- token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
73
- if token is not None:
74
- token = token.replace("\r", "").replace("\n", "").strip()
75
- if token != "":
76
- return token
77
-
78
- # 2. Is it set in token path ?
79
- try:
80
- token = cls.path_token.read_text()
81
- token = token.replace("\r", "").replace("\n", "").strip()
82
- return token
83
- except FileNotFoundError:
84
- return None
65
+ return get_token()
85
66
 
86
67
  # TODO: deprecate when adapted in transformers/datasets/gradio
87
68
  # @_deprecate_method(version="1.0", message="Use `huggingface_hub.logout` instead.")
@@ -15,13 +15,16 @@
15
15
  import os
16
16
  import warnings
17
17
  from pathlib import Path
18
+ from threading import Lock
18
19
  from typing import Optional
19
20
 
20
21
  from .. import constants
21
22
  from ._runtime import is_google_colab
22
23
 
23
24
 
24
- _CHECK_GOOGLE_COLAB_SECRET = True
25
+ _IS_GOOGLE_COLAB_CHECKED = False
26
+ _GOOGLE_COLAB_SECRET_LOCK = Lock()
27
+ _GOOGLE_COLAB_SECRET: Optional[str] = None
25
28
 
26
29
 
27
30
  def get_token() -> Optional[str]:
@@ -42,52 +45,66 @@ def get_token() -> Optional[str]:
42
45
 
43
46
 
44
47
  def _get_token_from_google_colab() -> Optional[str]:
45
- if not is_google_colab():
46
- return None
47
-
48
- global _CHECK_GOOGLE_COLAB_SECRET
49
- if not _CHECK_GOOGLE_COLAB_SECRET: # request access only once
50
- return None
51
-
52
- try:
53
- from google.colab import userdata
54
- from google.colab.errors import Error as ColabError
55
- except ImportError:
56
- return None
48
+ """Get token from Google Colab secrets vault using `google.colab.userdata.get(...)`.
57
49
 
58
- try:
59
- token = userdata.get("HF_TOKEN")
60
- except userdata.NotebookAccessError:
61
- # Means the user has a secret call `HF_TOKEN` and got a popup "please grand access to HF_TOKEN" and refused it
62
- # => warn user but ignore error => do not re-request access to user
63
- warnings.warn(
64
- "\nAccess to the secret `HF_TOKEN` has not been granted on this notebook."
65
- "\nYou will not be requested again."
66
- "\nPlease restart the session if you want to be prompted again."
67
- )
68
- _CHECK_GOOGLE_COLAB_SECRET = False
69
- return None
70
- except userdata.SecretNotFoundError:
71
- # Means the user did not define a `HF_TOKEN` secret => warn
72
- warnings.warn(
73
- "\nThe secret `HF_TOKEN` does not exist in your Colab secrets."
74
- "\nTo authenticate with the Hugging Face Hub, create a token in your settings tab "
75
- "(https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session."
76
- "\nYou will be able to reuse this secret in all of your notebooks."
77
- "\nPlease note that authentication is recommended but still optional to access public models or datasets."
78
- )
79
- return None
80
- except ColabError as e:
81
- # Something happen but we don't know what => recommend to open a GitHub issue
82
- warnings.warn(
83
- f"\nError while fetching `HF_TOKEN` secret value from your vault: '{str(e)}'."
84
- "\nYou are not authenticated with the Hugging Face Hub in this notebook."
85
- "\nIf the error persists, please let us know by opening an issue on GitHub "
86
- "(https://github.com/huggingface/huggingface_hub/issues/new)."
87
- )
50
+ Token is read from the vault only once per session and then stored in a global variable to avoid re-requesting
51
+ access to the vault.
52
+ """
53
+ if not is_google_colab():
88
54
  return None
89
55
 
90
- return _clean_token(token)
56
+ # `google.colab.userdata` is not thread-safe
57
+ # This can lead to a deadlock if multiple threads try to access it at the same time
58
+ # (typically when using `snapshot_download`)
59
+ # => use a lock
60
+ # See https://github.com/huggingface/huggingface_hub/issues/1952 for more details.
61
+ with _GOOGLE_COLAB_SECRET_LOCK:
62
+ global _GOOGLE_COLAB_SECRET
63
+ global _IS_GOOGLE_COLAB_CHECKED
64
+
65
+ if _IS_GOOGLE_COLAB_CHECKED: # request access only once
66
+ return _GOOGLE_COLAB_SECRET
67
+
68
+ try:
69
+ from google.colab import userdata
70
+ from google.colab.errors import Error as ColabError
71
+ except ImportError:
72
+ return None
73
+
74
+ try:
75
+ token = userdata.get("HF_TOKEN")
76
+ _GOOGLE_COLAB_SECRET = _clean_token(token)
77
+ except userdata.NotebookAccessError:
78
+ # Means the user has a secret call `HF_TOKEN` and got a popup "please grand access to HF_TOKEN" and refused it
79
+ # => warn user but ignore error => do not re-request access to user
80
+ warnings.warn(
81
+ "\nAccess to the secret `HF_TOKEN` has not been granted on this notebook."
82
+ "\nYou will not be requested again."
83
+ "\nPlease restart the session if you want to be prompted again."
84
+ )
85
+ _GOOGLE_COLAB_SECRET = None
86
+ except userdata.SecretNotFoundError:
87
+ # Means the user did not define a `HF_TOKEN` secret => warn
88
+ warnings.warn(
89
+ "\nThe secret `HF_TOKEN` does not exist in your Colab secrets."
90
+ "\nTo authenticate with the Hugging Face Hub, create a token in your settings tab "
91
+ "(https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session."
92
+ "\nYou will be able to reuse this secret in all of your notebooks."
93
+ "\nPlease note that authentication is recommended but still optional to access public models or datasets."
94
+ )
95
+ _GOOGLE_COLAB_SECRET = None
96
+ except ColabError as e:
97
+ # Something happen but we don't know what => recommend to open a GitHub issue
98
+ warnings.warn(
99
+ f"\nError while fetching `HF_TOKEN` secret value from your vault: '{str(e)}'."
100
+ "\nYou are not authenticated with the Hugging Face Hub in this notebook."
101
+ "\nIf the error persists, please let us know by opening an issue on GitHub "
102
+ "(https://github.com/huggingface/huggingface_hub/issues/new)."
103
+ )
104
+ _GOOGLE_COLAB_SECRET = None
105
+
106
+ _IS_GOOGLE_COLAB_CHECKED = True
107
+ return _GOOGLE_COLAB_SECRET
91
108
 
92
109
 
93
110
  def _get_token_from_environment() -> Optional[str]:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: huggingface-hub
3
- Version: 0.20.1
3
+ Version: 0.20.3
4
4
  Summary: Client library to download and publish models, datasets and other repos on the huggingface.co hub
5
5
  Home-page: https://github.com/huggingface/huggingface_hub
6
6
  Author: Hugging Face, Inc.
@@ -1,4 +1,4 @@
1
- huggingface_hub/__init__.py,sha256=WvvtgmprtEwfH1CVyhFwhOmWzxwyuIs4Ydqm3MQQ67A,20809
1
+ huggingface_hub/__init__.py,sha256=uaa0OgkPFs3mbcVaOYGzb-CCIm_56yQY90UKWTGwGiM,20809
2
2
  huggingface_hub/_commit_api.py,sha256=LWwx0PmTHzh-gwO6-cl0GWTrSnzCy8tfZ8DDCxnfb7Y,27795
3
3
  huggingface_hub/_commit_scheduler.py,sha256=FgfjYv3E0oK3iBxDdy45Y7t78FWkmjnBR4dRd5aZviU,13653
4
4
  huggingface_hub/_inference_endpoints.py,sha256=FW36nfm9UNNnc6L6hyL2KLiItglPGcQuYJ6DA2w4Gp4,14929
@@ -52,7 +52,7 @@ huggingface_hub/utils/_experimental.py,sha256=rBx4gV2NU1dT_OfeRzsCmCWyIF4Wxcf0Pd
52
52
  huggingface_hub/utils/_fixes.py,sha256=wFvfTYj62Il2OwkQB_Qp0xONG6SARQ5oEkT3_FhB4rc,2437
53
53
  huggingface_hub/utils/_git_credential.py,sha256=NMfMmuqdub_QX3T2d32Jhpf3RBnf2eh4VnDhHoqyZRA,4595
54
54
  huggingface_hub/utils/_headers.py,sha256=wz0kPrpu9PHpeCIJAq8MBiHuR2HbNWGukd0QgWS6lWo,9344
55
- huggingface_hub/utils/_hf_folder.py,sha256=9FlyCbiMP-PuyR4MP8sh5wiYbphkCjP18sCma0P7XDw,4488
55
+ huggingface_hub/utils/_hf_folder.py,sha256=5fxKNZ8y12szgmLhxZWJsjK_zx-wopMtVoFPCuwI1VI,3612
56
56
  huggingface_hub/utils/_http.py,sha256=qJ9wlsv-SU9L4Epr8FLHznY3COIcOrUUmGMjJXfrQvI,12889
57
57
  huggingface_hub/utils/_pagination.py,sha256=VfpmMLyNCRo24fw0o_yWysMK69d9M6sSg2-nWtuypO4,1840
58
58
  huggingface_hub/utils/_paths.py,sha256=nUaxXN-R2EcWfHE8ivFWfHqEKMIvXEdUeCGDC_QHMqc,4397
@@ -60,7 +60,7 @@ huggingface_hub/utils/_runtime.py,sha256=6sMMfwixkJAJj7pqYAva9A1g3-MBNWenKtlvBB9
60
60
  huggingface_hub/utils/_safetensors.py,sha256=EE9v9HflWBUqIegn0dCGHgNu9G9Db3v2aszvG4ldPF8,4876
61
61
  huggingface_hub/utils/_subprocess.py,sha256=LW9b8TWh9rsm3pW9_5b-mVV_AtYNyLXgC6e09SthkWI,4616
62
62
  huggingface_hub/utils/_telemetry.py,sha256=jHAdgWNcL9nVvMT3ec3i78O-cwL09GnlifuokzpQjMI,4641
63
- huggingface_hub/utils/_token.py,sha256=3DYRsV6z2byAg0rjuz7mFD2Q52qN2K2craVYEEXhMqo,4502
63
+ huggingface_hub/utils/_token.py,sha256=e3GGABkd6zPYLE4-RdUxnH6vyen4vsvNxEl2PgStiTA,5475
64
64
  huggingface_hub/utils/_typing.py,sha256=zTA0nTJAILGveXbJKyeh6u9uIagrFgPoRqr-uCEGDQI,921
65
65
  huggingface_hub/utils/_validators.py,sha256=3ZmHubjslDRwFYe1oKyaUw6DZrc3DsuV2gABPrx7PTw,9358
66
66
  huggingface_hub/utils/endpoint_helpers.py,sha256=reLS2ic6_BTe9RuZY5WLcd5dLjIWt5Klh5EHZ7XvHng,8533
@@ -68,9 +68,9 @@ huggingface_hub/utils/insecure_hashlib.py,sha256=OjxlvtSQHpbLp9PWSrXBDJ0wHjxCBU-
68
68
  huggingface_hub/utils/logging.py,sha256=mARNwc5gY6apMQ9IM5zymn-RsYnFbYW3b0HDMYXmBS0,4729
69
69
  huggingface_hub/utils/sha.py,sha256=IVi7CfBthfu-ExLduY_CQltTy-tVGTbrvURCTOWKcLA,901
70
70
  huggingface_hub/utils/tqdm.py,sha256=zBWgoxxwHooOceABVREVqSNpJGcMpaByKFVDU8VbuUQ,6334
71
- huggingface_hub-0.20.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
72
- huggingface_hub-0.20.1.dist-info/METADATA,sha256=_mwPR3T6IoDiH8sbuyp_os1yo5_h5MCYFgYb2wuKj5Q,12930
73
- huggingface_hub-0.20.1.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
74
- huggingface_hub-0.20.1.dist-info/entry_points.txt,sha256=Y3Z2L02rBG7va_iE6RPXolIgwOdwUFONyRN3kXMxZ0g,131
75
- huggingface_hub-0.20.1.dist-info/top_level.txt,sha256=8KzlQJAY4miUvjAssOAJodqKOw3harNzuiwGQ9qLSSk,16
76
- huggingface_hub-0.20.1.dist-info/RECORD,,
71
+ huggingface_hub-0.20.3.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
72
+ huggingface_hub-0.20.3.dist-info/METADATA,sha256=QwoMs3u0SUk5JBYWVZXnvNFKSSiIOF34zHM9_QYkUiQ,12930
73
+ huggingface_hub-0.20.3.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
74
+ huggingface_hub-0.20.3.dist-info/entry_points.txt,sha256=Y3Z2L02rBG7va_iE6RPXolIgwOdwUFONyRN3kXMxZ0g,131
75
+ huggingface_hub-0.20.3.dist-info/top_level.txt,sha256=8KzlQJAY4miUvjAssOAJodqKOw3harNzuiwGQ9qLSSk,16
76
+ huggingface_hub-0.20.3.dist-info/RECORD,,