fix: only cache the HF token after whoami validation

This commit is contained in:
Yi-111-a 2026-11-03 17:16:09 +08:00
parent 297eb4bf2f
commit e240b4e89d
1 changed files with 10 additions and 5 deletions

View File

@ -1009,12 +1009,17 @@ def run():
if not token:
continue
# Remember the token so that a subsequent upload in the same
# session does not prompt for it again. It is intentionally kept
# out of `settings`, and is never persisted to disk.
upload_token = token
# Validate the token before remembering it, so that a mistyped
# token does not stay cached for the rest of the session.
try:
user = huggingface_hub.whoami(token)
except Exception:
# Clear any cached token so the next upload attempt prompts
# for a replacement. It is intentionally kept out of
# `settings`, and is never persisted to disk.
upload_token = None
raise
user = huggingface_hub.whoami(token)
fullname = user.get(
"fullname",
user.get("name", "unknown user"),