Add Space verification script
Browse files- verify_space_auth.py +125 -0
verify_space_auth.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Simple Space Authentication Verification (GitHub Secrets)
|
| 4 |
+
|
| 5 |
+
This script can be added to your Hugging Face Space to verify
|
| 6 |
+
that authentication is working correctly using GitHub secrets.
|
| 7 |
+
|
| 8 |
+
Usage:
|
| 9 |
+
Add this to your Space and run it to verify authentication.
|
| 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
|
| 18 |
+
except ImportError:
|
| 19 |
+
HF_AVAILABLE = False
|
| 20 |
+
print("β huggingface_hub not installed")
|
| 21 |
+
sys.exit(1)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def verify_space_authentication():
|
| 25 |
+
"""Verify authentication is working in the Space using GitHub secrets."""
|
| 26 |
+
print("π Verifying Space Authentication (GitHub Secrets)")
|
| 27 |
+
print("=" * 55)
|
| 28 |
+
|
| 29 |
+
# Check if we're in a Space
|
| 30 |
+
space_vars = ["SPACE_ID", "SPACE_HOST", "SPACE_REPO_ID"]
|
| 31 |
+
is_space = any(os.getenv(var) for var in space_vars)
|
| 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 |
+
print(" - The Space will automatically have access to this secret")
|
| 50 |
+
return False
|
| 51 |
+
|
| 52 |
+
print(f"β
HF_TOKEN found: {token[:8]}...{token[-4:]}")
|
| 53 |
+
print(f" - Source: GitHub secrets")
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# Test authentication
|
| 57 |
+
login(token=token)
|
| 58 |
+
|
| 59 |
+
user_info = whoami()
|
| 60 |
+
username = user_info["name"]
|
| 61 |
+
|
| 62 |
+
print(f"β
Authentication successful!")
|
| 63 |
+
print(f" - Username: {username}")
|
| 64 |
+
|
| 65 |
+
# Test API access
|
| 66 |
+
api = HfApi()
|
| 67 |
+
print(f"β
API access working")
|
| 68 |
+
|
| 69 |
+
# Test repository creation
|
| 70 |
+
print(f"\nπ§ͺ Testing Repository Creation")
|
| 71 |
+
repo_name = "test-openllm-verification"
|
| 72 |
+
repo_id = f"{username}/{repo_name}"
|
| 73 |
+
|
| 74 |
+
print(f"π Creating test repository: {repo_id}")
|
| 75 |
+
create_repo(
|
| 76 |
+
repo_id=repo_id,
|
| 77 |
+
repo_type="model",
|
| 78 |
+
exist_ok=True,
|
| 79 |
+
private=True
|
| 80 |
+
)
|
| 81 |
+
print(f"β
Repository created successfully")
|
| 82 |
+
|
| 83 |
+
# Clean up
|
| 84 |
+
print(f"π Cleaning up test repository...")
|
| 85 |
+
delete_repo(repo_id=repo_id, repo_type="model")
|
| 86 |
+
print(f"β
Repository deleted")
|
| 87 |
+
|
| 88 |
+
print(f"\nπ All verification tests passed!")
|
| 89 |
+
print(f" - Authentication: β
Working")
|
| 90 |
+
print(f" - Repository Creation: β
Working")
|
| 91 |
+
print(f" - GitHub Secrets Integration: β
Working")
|
| 92 |
+
print(f" - Ready for training and model uploads!")
|
| 93 |
+
|
| 94 |
+
return True
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"β Authentication failed: {e}")
|
| 98 |
+
print(f"\nπ§ Troubleshooting:")
|
| 99 |
+
print(f"1. Check if HF_TOKEN is set correctly in GitHub repository secrets")
|
| 100 |
+
print(f"2. Verify token has 'Write' permissions")
|
| 101 |
+
print(f"3. Check Space logs for detailed error messages")
|
| 102 |
+
print(f"4. Ensure your Space is connected to the GitHub repository")
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def main():
|
| 107 |
+
"""Main verification function."""
|
| 108 |
+
print("π OpenLLM - Space Authentication Verification (GitHub Secrets)")
|
| 109 |
+
print("=" * 65)
|
| 110 |
+
|
| 111 |
+
success = verify_space_authentication()
|
| 112 |
+
|
| 113 |
+
if success:
|
| 114 |
+
print(f"\nβ
Verification completed successfully!")
|
| 115 |
+
print(f" - Your Space is ready for OpenLLM training")
|
| 116 |
+
print(f" - Model uploads will work correctly")
|
| 117 |
+
print(f" - GitHub secrets integration is working")
|
| 118 |
+
else:
|
| 119 |
+
print(f"\nβ Verification failed")
|
| 120 |
+
print(f" - Please fix authentication issues before training")
|
| 121 |
+
sys.exit(1)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
main()
|