Add authentication test script
Browse files- space_auth_test.py +59 -85
space_auth_test.py
CHANGED
|
@@ -2,16 +2,15 @@
|
|
| 2 |
"""
|
| 3 |
Space Authentication Test for OpenLLM Training
|
| 4 |
|
| 5 |
-
This script verifies that
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
"""
|
| 11 |
|
| 12 |
import os
|
| 13 |
import sys
|
| 14 |
-
|
| 15 |
try:
|
| 16 |
from huggingface_hub import HfApi, login, whoami, create_repo, delete_repo
|
| 17 |
HF_AVAILABLE = True
|
|
@@ -20,108 +19,83 @@ except ImportError:
|
|
| 20 |
print("β huggingface_hub not installed")
|
| 21 |
sys.exit(1)
|
| 22 |
|
| 23 |
-
|
| 24 |
def test_space_authentication():
|
| 25 |
-
"""Test authentication
|
| 26 |
-
print("
|
| 27 |
-
print("=" *
|
| 28 |
|
| 29 |
-
# Check if we're in a Space
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
if is_space:
|
| 34 |
-
print("β
Running in Hugging Face Space environment")
|
| 35 |
-
for var in space_vars:
|
| 36 |
-
value = os.getenv(var)
|
| 37 |
-
if value:
|
| 38 |
-
print(f" - {var}: {value}")
|
| 39 |
-
else:
|
| 40 |
-
print("βΉοΈ Running in local environment")
|
| 41 |
-
|
| 42 |
-
# Check HF_TOKEN from GitHub secrets
|
| 43 |
-
token = os.getenv("HF_TOKEN")
|
| 44 |
-
if not token:
|
| 45 |
-
print("β HF_TOKEN not found in environment")
|
| 46 |
-
print(" - Please set HF_TOKEN in your GitHub repository secrets")
|
| 47 |
-
print(" - Go to GitHub repository β Settings β Secrets and variables β Actions")
|
| 48 |
-
print(" - Add HF_TOKEN with your Hugging Face token")
|
| 49 |
-
return False
|
| 50 |
-
|
| 51 |
-
print(f"β
HF_TOKEN found: {token[:8]}...{token[-4:]}")
|
| 52 |
-
print(f" - Source: GitHub secrets")
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
login(token=token)
|
| 58 |
-
|
| 59 |
user_info = whoami()
|
| 60 |
-
username = user_info["name"]
|
| 61 |
-
|
| 62 |
print(f"β
Authentication successful!")
|
| 63 |
-
print(f"
|
| 64 |
-
|
| 65 |
-
# Test API access
|
| 66 |
-
print("\nπ Testing API access...")
|
| 67 |
-
api = HfApi()
|
| 68 |
-
print(f"β
API access working")
|
| 69 |
-
|
| 70 |
-
# Test repository creation
|
| 71 |
-
print("\nπ§ͺ Testing Repository Creation")
|
| 72 |
-
repo_name = "test-openllm-auth"
|
| 73 |
-
repo_id = f"{username}/{repo_name}"
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
exist_ok=True,
|
| 80 |
-
private=True
|
| 81 |
-
)
|
| 82 |
-
print(f"β
Repository created successfully")
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
print(f"β
Repository deleted")
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
|
|
|
| 96 |
return True
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
-
print(f"β Authentication
|
| 100 |
-
print(f"\n
|
| 101 |
-
print(f"
|
| 102 |
-
print(f"
|
| 103 |
-
print(f"
|
| 104 |
-
print(f"4. Ensure your Space is connected to the GitHub repository")
|
| 105 |
return False
|
| 106 |
|
| 107 |
-
|
| 108 |
def main():
|
| 109 |
-
"""Main
|
| 110 |
-
print("π OpenLLM
|
| 111 |
-
print("=" *
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
| 113 |
success = test_space_authentication()
|
| 114 |
|
| 115 |
if success:
|
| 116 |
-
print(f"\nβ
|
| 117 |
-
print(f"
|
| 118 |
-
|
| 119 |
-
print(f" - GitHub secrets integration is working")
|
| 120 |
else:
|
| 121 |
-
print(f"\nβ Authentication
|
| 122 |
-
print(f"
|
| 123 |
sys.exit(1)
|
| 124 |
|
| 125 |
-
|
| 126 |
if __name__ == "__main__":
|
| 127 |
main()
|
|
|
|
| 2 |
"""
|
| 3 |
Space Authentication Test for OpenLLM Training
|
| 4 |
|
| 5 |
+
This script verifies that authentication is working correctly in a Hugging Face Space
|
| 6 |
+
environment. It uses the Space's own access token for authentication.
|
| 7 |
|
| 8 |
+
Author: Louis Chua Bean Chong
|
| 9 |
+
License: GPLv3
|
| 10 |
"""
|
| 11 |
|
| 12 |
import os
|
| 13 |
import sys
|
|
|
|
| 14 |
try:
|
| 15 |
from huggingface_hub import HfApi, login, whoami, create_repo, delete_repo
|
| 16 |
HF_AVAILABLE = True
|
|
|
|
| 19 |
print("β huggingface_hub not installed")
|
| 20 |
sys.exit(1)
|
| 21 |
|
|
|
|
| 22 |
def test_space_authentication():
|
| 23 |
+
"""Test authentication using Space's access token."""
|
| 24 |
+
print("π Testing Space Authentication")
|
| 25 |
+
print("=" * 40)
|
| 26 |
|
| 27 |
+
# Check if we're in a Space environment
|
| 28 |
+
space_id = os.environ.get('SPACE_ID', 'lemms/openllm')
|
| 29 |
+
print(f"π Space ID: {space_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Check for Space access token
|
| 32 |
+
# In Hugging Face Spaces, the access token should be automatically available
|
| 33 |
+
# through the Space's own authentication mechanism
|
| 34 |
try:
|
| 35 |
+
# Try to get current user info (this will use Space's access token)
|
| 36 |
+
api = HfApi()
|
|
|
|
|
|
|
| 37 |
user_info = whoami()
|
|
|
|
|
|
|
| 38 |
print(f"β
Authentication successful!")
|
| 39 |
+
print(f"π€ User: {user_info}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Test API access by listing Space files
|
| 42 |
+
print(f"\nπ Testing Space file access...")
|
| 43 |
+
files = api.list_repo_files(repo_id=space_id, repo_type='space')
|
| 44 |
+
print(f"β
Successfully listed {len(files)} files in Space")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Test repository creation/deletion (temporary test)
|
| 47 |
+
test_repo_name = f"test-repo-{os.getpid()}"
|
| 48 |
+
print(f"\nπ§ͺ Testing repository operations...")
|
|
|
|
| 49 |
|
| 50 |
+
try:
|
| 51 |
+
# Create a temporary test repository
|
| 52 |
+
api.create_repo(
|
| 53 |
+
repo_id=test_repo_name,
|
| 54 |
+
repo_type="model",
|
| 55 |
+
private=True,
|
| 56 |
+
exist_ok=True
|
| 57 |
+
)
|
| 58 |
+
print(f"β
Successfully created test repository: {test_repo_name}")
|
| 59 |
+
|
| 60 |
+
# Delete the test repository
|
| 61 |
+
api.delete_repo(repo_id=test_repo_name, repo_type="model")
|
| 62 |
+
print(f"β
Successfully deleted test repository: {test_repo_name}")
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"β οΈ Repository operations test failed: {e}")
|
| 66 |
+
print(" This is normal if the token doesn't have full permissions")
|
| 67 |
|
| 68 |
+
print(f"\nπ Space authentication test completed successfully!")
|
| 69 |
return True
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
print(f"β Authentication failed: {e}")
|
| 73 |
+
print(f"\nπ‘ Troubleshooting:")
|
| 74 |
+
print(f" - Ensure the Space has proper access token configured")
|
| 75 |
+
print(f" - Check Space settings for authentication configuration")
|
| 76 |
+
print(f" - Verify the Space has necessary permissions")
|
|
|
|
| 77 |
return False
|
| 78 |
|
|
|
|
| 79 |
def main():
|
| 80 |
+
"""Main function to run authentication tests."""
|
| 81 |
+
print("π OpenLLM Space Authentication Test")
|
| 82 |
+
print("=" * 50)
|
| 83 |
+
|
| 84 |
+
if not HF_AVAILABLE:
|
| 85 |
+
print("β Required dependencies not available")
|
| 86 |
+
sys.exit(1)
|
| 87 |
|
| 88 |
+
# Run authentication test
|
| 89 |
success = test_space_authentication()
|
| 90 |
|
| 91 |
if success:
|
| 92 |
+
print(f"\nβ
All authentication tests passed!")
|
| 93 |
+
print(f"π Ready for OpenLLM training!")
|
| 94 |
+
sys.exit(0)
|
|
|
|
| 95 |
else:
|
| 96 |
+
print(f"\nβ Authentication tests failed!")
|
| 97 |
+
print(f"π§ Please check Space configuration and try again.")
|
| 98 |
sys.exit(1)
|
| 99 |
|
|
|
|
| 100 |
if __name__ == "__main__":
|
| 101 |
main()
|