Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading