Skip to content

Commit

Permalink
Improve whoami() error messages by specifying token source (#2814)
Browse files Browse the repository at this point in the history
* Improve whoami() error messages by specifying token source

* handled the required review

* Update src/huggingface_hub/hf_api.py

* Update src/huggingface_hub/hf_api.py

* Completed the cleaning

* Update src/huggingface_hub/hf_api.py

added suggestion

Co-authored-by: Lucain <[email protected]>

* Added import

* cleaned whitespaces and added import before decorator

* moved to import section

* Update hf_api.py

* Update hf_api.py

* Update hf_api.py

* Update hf_api.py

* Update hf_api.py

---------

Co-authored-by: Lucain <[email protected]>
Co-authored-by: Lucain <[email protected]>
  • Loading branch information
3 people authored Feb 5, 2025
1 parent a70b69d commit d29290f
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@
filter_repo_objects,
fix_hf_endpoint_in_url,
get_session,
get_token,
hf_raise_for_status,
logging,
paginate,
parse_datetime,
validate_hf_hub_args,
)
from .utils import tqdm as hf_tqdm
from .utils._auth import _get_token_from_environment, _get_token_from_file, _get_token_from_google_colab
from .utils._deprecation import _deprecate_method
from .utils._typing import CallableT
from .utils.endpoint_helpers import _is_emission_within_threshold
Expand Down Expand Up @@ -1627,24 +1629,27 @@ def whoami(self, token: Union[bool, str, None] = None) -> Dict:
https://huggingface.co/docs/huggingface_hub/quick-start#authentication).
To disable authentication, pass `False`.
"""
# Get the effective token using the helper function get_token
effective_token = token or self.token or get_token() or True
r = get_session().get(
f"{self.endpoint}/api/whoami-v2",
headers=self._build_hf_headers(
# If `token` is provided and not `None`, it will be used by default.
# Otherwise, the token must be retrieved from cache or env variable.
token=(token or self.token or True),
),
headers=self._build_hf_headers(token=effective_token),
)
try:
hf_raise_for_status(r)
except HTTPError as e:
raise HTTPError(
"Invalid user token. If you didn't pass a user token, make sure you "
"are properly logged in by executing `huggingface-cli login`, and "
"if you did pass a user token, double-check it's correct.",
request=e.request,
response=e.response,
) from e
error_message = "Invalid user token."
# Check which token is the effective one and generate the error message accordingly
if effective_token == _get_token_from_google_colab():
error_message += " The token from Google Colab vault is invalid. Please update it from the UI."
elif effective_token == _get_token_from_environment():
error_message += (
" The token from HF_TOKEN environment variable is invalid. "
"Note that HF_TOKEN takes precedence over `huggingface-cli login`."
)
elif effective_token == _get_token_from_file():
error_message += " The token stored is invalid. Please run `huggingface-cli login` to update it."
raise HTTPError(error_message, request=e.request, response=e.response) from e
return r.json()

@_deprecate_method(
Expand Down

0 comments on commit d29290f

Please sign in to comment.