lemms commited on
Commit
2654698
Β·
verified Β·
1 Parent(s): 4ea4547

Add authentication test script

Browse files
Files changed (1) hide show
  1. 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 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
@@ -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 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()
 
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()