lemms commited on
Commit
53e769b
Β·
verified Β·
1 Parent(s): 1cb5d9f

Add Space verification script

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