Add authentication test script
Browse files- space_auth_test.py +69 -43
space_auth_test.py
CHANGED
|
@@ -31,28 +31,12 @@ def test_space_authentication():
|
|
| 31 |
# Check for various authentication methods
|
| 32 |
print(f"\nπ Checking authentication methods...")
|
| 33 |
|
| 34 |
-
# Method 1: Check for
|
| 35 |
-
hf_token = os.environ.get('HF_TOKEN')
|
| 36 |
-
if hf_token:
|
| 37 |
-
print(f"β
HF_TOKEN found in environment")
|
| 38 |
-
print(f" Token: {hf_token[:8]}...{hf_token[-4:]}")
|
| 39 |
-
else:
|
| 40 |
-
print(f"β οΈ HF_TOKEN not found in environment")
|
| 41 |
-
|
| 42 |
-
# Method 2: Check for HUGGING_FACE_HUB_TOKEN
|
| 43 |
-
hf_hub_token = os.environ.get('HUGGING_FACE_HUB_TOKEN')
|
| 44 |
-
if hf_hub_token:
|
| 45 |
-
print(f"β
HUGGING_FACE_HUB_TOKEN found in environment")
|
| 46 |
-
print(f" Token: {hf_hub_token[:8]}...{hf_hub_token[-4:]}")
|
| 47 |
-
else:
|
| 48 |
-
print(f"β οΈ HUGGING_FACE_HUB_TOKEN not found in environment")
|
| 49 |
-
|
| 50 |
-
# Method 3: Check for Space's built-in authentication
|
| 51 |
try:
|
| 52 |
-
# Try to get current user info (this will use Space's access token)
|
| 53 |
api = HfApi()
|
| 54 |
user_info = whoami()
|
| 55 |
-
print(f"β
Space authentication successful!")
|
| 56 |
print(f"π€ User: {user_info}")
|
| 57 |
|
| 58 |
# Test API access by listing Space files
|
|
@@ -86,32 +70,74 @@ def test_space_authentication():
|
|
| 86 |
return True
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
-
print(f"β
|
| 90 |
-
print(f"\n
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
print(f"
|
| 96 |
-
|
| 97 |
-
print(f"\n2. Create HF Access Token:")
|
| 98 |
-
print(f" - Go to https://huggingface.co/settings/tokens")
|
| 99 |
-
print(f" - Create a new token with 'Write' permissions")
|
| 100 |
-
print(f" - Copy the token (starts with 'hf_')")
|
| 101 |
-
print(f" - Add it to Space secrets as HF_TOKEN")
|
| 102 |
-
|
| 103 |
-
print(f"\n3. Verify HF Access Token:")
|
| 104 |
-
print(f" - Token must start with 'hf_' (Hugging Face format)")
|
| 105 |
-
print(f" - Token must have 'Write' access to repositories")
|
| 106 |
-
print(f" - Token must be valid and not expired")
|
| 107 |
-
print(f" - Token must be associated with the correct HF account")
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
def main():
|
| 117 |
"""Main function to run authentication tests."""
|
|
|
|
| 31 |
# Check for various authentication methods
|
| 32 |
print(f"\nπ Checking authentication methods...")
|
| 33 |
|
| 34 |
+
# Method 1: Check for Space's built-in authentication (primary method)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
+
# Try to get current user info (this will use Space's built-in access token)
|
| 37 |
api = HfApi()
|
| 38 |
user_info = whoami()
|
| 39 |
+
print(f"β
Space built-in authentication successful!")
|
| 40 |
print(f"π€ User: {user_info}")
|
| 41 |
|
| 42 |
# Test API access by listing Space files
|
|
|
|
| 70 |
return True
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
+
print(f"β Space built-in authentication failed: {e}")
|
| 74 |
+
print(f"\nπ Trying alternative authentication methods...")
|
| 75 |
+
|
| 76 |
+
# Method 2: Check for HF access token environment variable (fallback)
|
| 77 |
+
hf_token = os.environ.get('HF_TOKEN')
|
| 78 |
+
if hf_token:
|
| 79 |
+
print(f"β
HF access token found in environment")
|
| 80 |
+
print(f" Token: {hf_token[:8]}...{hf_token[-4:]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
try:
|
| 83 |
+
# Try to use the HF_TOKEN
|
| 84 |
+
from huggingface_hub import login
|
| 85 |
+
login(token=hf_token)
|
| 86 |
+
user_info = whoami()
|
| 87 |
+
print(f"β
HF access token authentication successful!")
|
| 88 |
+
print(f"π€ User: {user_info}")
|
| 89 |
+
return True
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"β HF access token authentication failed: {e}")
|
| 92 |
+
else:
|
| 93 |
+
print(f"β οΈ HF access token not found in environment")
|
| 94 |
+
|
| 95 |
+
# Method 3: Check for HUGGING_FACE_HUB_TOKEN (fallback)
|
| 96 |
+
hf_hub_token = os.environ.get('HUGGING_FACE_HUB_TOKEN')
|
| 97 |
+
if hf_hub_token:
|
| 98 |
+
print(f"β
HUGGING_FACE_HUB_TOKEN found in environment")
|
| 99 |
+
print(f" Token: {hf_hub_token[:8]}...{hf_hub_token[-4:]}")
|
| 100 |
|
| 101 |
+
try:
|
| 102 |
+
# Try to use the HUGGING_FACE_HUB_TOKEN
|
| 103 |
+
from huggingface_hub import login
|
| 104 |
+
login(token=hf_hub_token)
|
| 105 |
+
user_info = whoami()
|
| 106 |
+
print(f"β
HUGGING_FACE_HUB_TOKEN authentication successful!")
|
| 107 |
+
print(f"π€ User: {user_info}")
|
| 108 |
+
return True
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"β HUGGING_FACE_HUB_TOKEN authentication failed: {e}")
|
| 111 |
+
else:
|
| 112 |
+
print(f"β οΈ HUGGING_FACE_HUB_TOKEN not found in environment")
|
| 113 |
+
|
| 114 |
+
# If all authentication methods failed
|
| 115 |
+
print(f"\nβ All authentication methods failed")
|
| 116 |
+
print(f"\nπ§ TROUBLESHOOTING STEPS:")
|
| 117 |
+
print(f"1. Check Space Built-in Authentication:")
|
| 118 |
+
print(f" - Ensure the Space has proper access to Hugging Face Hub")
|
| 119 |
+
print(f" - Check Space settings for authentication configuration")
|
| 120 |
+
print(f" - Verify the Space has necessary permissions")
|
| 121 |
+
|
| 122 |
+
print(f"\n2. Alternative: Set HF Access Token in Space Settings:")
|
| 123 |
+
print(f" - Go to https://huggingface.co/spaces/{space_id}/settings")
|
| 124 |
+
print(f" - Navigate to 'Repository secrets' section")
|
| 125 |
+
print(f" - Add HF_TOKEN with your HF access token")
|
| 126 |
+
print(f" - Token should have 'Write' permissions")
|
| 127 |
+
|
| 128 |
+
print(f"\n3. Create HF Access Token:")
|
| 129 |
+
print(f" - Go to https://huggingface.co/settings/tokens")
|
| 130 |
+
print(f" - Create a new token with 'Write' permissions")
|
| 131 |
+
print(f" - Copy the token (starts with 'hf_')")
|
| 132 |
+
print(f" - Add it to Space secrets as HF_TOKEN")
|
| 133 |
+
|
| 134 |
+
print(f"\n4. Verify HF Access Token:")
|
| 135 |
+
print(f" - Token must start with 'hf_' (Hugging Face format)")
|
| 136 |
+
print(f" - Token must have 'Write' access to repositories")
|
| 137 |
+
print(f" - Token must be valid and not expired")
|
| 138 |
+
print(f" - Token must be associated with the correct HF account")
|
| 139 |
+
|
| 140 |
+
return False
|
| 141 |
|
| 142 |
def main():
|
| 143 |
"""Main function to run authentication tests."""
|