lemms commited on
Commit
dfb73fe
Β·
verified Β·
1 Parent(s): be3e0ec

Add authentication test script

Browse files
Files changed (1) hide show
  1. space_auth_test.py +127 -0
space_auth_test.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Space Authentication Test for OpenLLM Training
4
+
5
+ This script verifies that Hugging Face authentication is working correctly
6
+ in your Space using GitHub secrets. Add this to your Space to test before training.
7
+
8
+ Usage:
9
+ Add this file 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 test_space_authentication():
25
+ """Test authentication in Hugging Face Space using GitHub secrets."""
26
+ print("πŸ” Testing 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
+ return False
50
+
51
+ print(f"βœ… HF_TOKEN found: {token[:8]}...{token[-4:]}")
52
+ print(f" - Source: GitHub secrets")
53
+
54
+ try:
55
+ # Test authentication
56
+ print("\nπŸ”„ Testing 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
+ 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
+ print(f"πŸ”„ Creating test repository: {repo_id}")
76
+ create_repo(
77
+ repo_id=repo_id,
78
+ repo_type="model",
79
+ exist_ok=True,
80
+ private=True
81
+ )
82
+ print(f"βœ… Repository created successfully")
83
+
84
+ # Clean up
85
+ print(f"πŸ”„ Cleaning up test repository...")
86
+ delete_repo(repo_id=repo_id, repo_type="model")
87
+ print(f"βœ… Repository deleted")
88
+
89
+ print(f"\nπŸŽ‰ All authentication tests passed!")
90
+ print(f" - Authentication: βœ… Working")
91
+ print(f" - API Access: βœ… Working")
92
+ print(f" - Repository Creation: βœ… Working")
93
+ print(f" - GitHub Secrets Integration: βœ… Working")
94
+ print(f" - Ready for OpenLLM training and model uploads!")
95
+
96
+ return True
97
+
98
+ except Exception as e:
99
+ print(f"❌ Authentication test failed: {e}")
100
+ print(f"\nπŸ”§ Troubleshooting:")
101
+ print(f"1. Check if HF_TOKEN is set correctly in GitHub repository secrets")
102
+ print(f"2. Verify token has 'Write' permissions")
103
+ print(f"3. Check Space logs for detailed error messages")
104
+ print(f"4. Ensure your Space is connected to the GitHub repository")
105
+ return False
106
+
107
+
108
+ def main():
109
+ """Main test function."""
110
+ print("πŸš€ OpenLLM - Space Authentication Test")
111
+ print("=" * 45)
112
+
113
+ success = test_space_authentication()
114
+
115
+ if success:
116
+ print(f"\nβœ… Authentication test completed successfully!")
117
+ print(f" - Your Space is ready for OpenLLM training")
118
+ print(f" - Model uploads will work correctly")
119
+ print(f" - GitHub secrets integration is working")
120
+ else:
121
+ print(f"\n❌ Authentication test failed")
122
+ print(f" - Please fix authentication issues before training")
123
+ sys.exit(1)
124
+
125
+
126
+ if __name__ == "__main__":
127
+ main()