Anirudh Esthuri commited on
Commit
3653027
·
1 Parent(s): 15dc14b

Use huggingface_hub library for token validation - more reliable authentication

Browse files
Files changed (2) hide show
  1. app.py +30 -6
  2. requirements.txt +1 -0
app.py CHANGED
@@ -367,9 +367,34 @@ with st.sidebar:
367
  if not token:
368
  return False, "", "Token cannot be empty"
369
 
370
- # Use the HF whoami endpoint
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371
  endpoint = "https://huggingface.co/api/whoami"
372
- headers = {"Authorization": f"Bearer {token}"}
 
 
 
373
 
374
  try:
375
  resp = requests.get(endpoint, headers=headers, timeout=10)
@@ -386,8 +411,7 @@ with st.sidebar:
386
  if username:
387
  return True, username, ""
388
  else:
389
- # Log the response for debugging
390
- return False, "", f"Token validated but username not found. Please try regenerating your token."
391
  elif resp.status_code == 401:
392
  error_detail = ""
393
  try:
@@ -395,9 +419,9 @@ with st.sidebar:
395
  error_detail = error_data.get("error", "")
396
  except:
397
  pass
398
- return False, "", f"Invalid token (401 Unauthorized). {error_detail} Please check your token at https://huggingface.co/settings/tokens"
399
  elif resp.status_code == 403:
400
- return False, "", f"Token access denied (403 Forbidden). Please ensure your token has read permissions."
401
  else:
402
  error_text = ""
403
  try:
 
367
  if not token:
368
  return False, "", "Token cannot be empty"
369
 
370
+ # Remove any whitespace or newlines that might have been copied
371
+ token = "".join(token.split())
372
+
373
+ # Try using huggingface_hub library if available, otherwise fall back to API
374
+ try:
375
+ from huggingface_hub import whoami
376
+ try:
377
+ user_info = whoami(token=token)
378
+ username = user_info.get("name") or user_info.get("username") or ""
379
+ if username:
380
+ return True, username, ""
381
+ else:
382
+ return False, "", "Token validated but username not found in response."
383
+ except Exception as e:
384
+ error_msg = str(e)
385
+ if "401" in error_msg or "Unauthorized" in error_msg or "Invalid" in error_msg:
386
+ return False, "", f"Invalid token. Please verify your token is correct and has Read permissions. Error: {error_msg[:100]}"
387
+ return False, "", f"Validation error: {error_msg[:150]}"
388
+ except ImportError:
389
+ # Fall back to direct API call if huggingface_hub not available
390
+ pass
391
+
392
+ # Fallback: Use the HF whoami endpoint directly
393
  endpoint = "https://huggingface.co/api/whoami"
394
+ headers = {
395
+ "Authorization": f"Bearer {token}",
396
+ "User-Agent": "MemMachine-Playground/1.0"
397
+ }
398
 
399
  try:
400
  resp = requests.get(endpoint, headers=headers, timeout=10)
 
411
  if username:
412
  return True, username, ""
413
  else:
414
+ return False, "", f"Token validated but username not found. Response: {str(user_data)[:100]}"
 
415
  elif resp.status_code == 401:
416
  error_detail = ""
417
  try:
 
419
  error_detail = error_data.get("error", "")
420
  except:
421
  pass
422
+ return False, "", f"Invalid token (401). The token may be expired, revoked, or incorrect. {error_detail} Please create a new Read token at https://huggingface.co/settings/tokens"
423
  elif resp.status_code == 403:
424
+ return False, "", f"Token access denied (403). Please ensure your token has Read permissions."
425
  else:
426
  error_text = ""
427
  try:
requirements.txt CHANGED
@@ -10,3 +10,4 @@ anthropic
10
  tiktoken
11
  pydantic
12
  boto3
 
 
10
  tiktoken
11
  pydantic
12
  boto3
13
+ huggingface_hub