lemms commited on
Commit
8789093
Β·
verified Β·
1 Parent(s): d70f1a2

Upload space_auth_test.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. space_auth_test.py +34 -32
space_auth_test.py CHANGED
@@ -11,26 +11,29 @@ License: GPLv3
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
17
  except ImportError:
18
  HF_AVAILABLE = False
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 various authentication methods
32
  print(f"\nπŸ” Checking authentication methods...")
33
-
34
  # Method 1: Check for Space's built-in authentication (primary method)
35
  try:
36
  # Try to get current user info (this will use Space's built-in access token)
@@ -38,50 +41,46 @@ def test_space_authentication():
38
  user_info = whoami()
39
  print(f"βœ… Space built-in authentication successful!")
40
  print(f"πŸ‘€ User: {user_info}")
41
-
42
  # Test API access by listing Space files
43
  print(f"\nπŸ“ Testing Space file access...")
44
- files = api.list_repo_files(repo_id=space_id, repo_type='space')
45
  print(f"βœ… Successfully listed {len(files)} files in Space")
46
-
47
  # Test repository creation/deletion (temporary test)
48
  test_repo_name = f"test-repo-{os.getpid()}"
49
  print(f"\nπŸ§ͺ Testing repository operations...")
50
-
51
  try:
52
  # Create a temporary test repository
53
- api.create_repo(
54
- repo_id=test_repo_name,
55
- repo_type="model",
56
- private=True,
57
- exist_ok=True
58
- )
59
  print(f"βœ… Successfully created test repository: {test_repo_name}")
60
-
61
  # Delete the test repository
62
  api.delete_repo(repo_id=test_repo_name, repo_type="model")
63
  print(f"βœ… Successfully deleted test repository: {test_repo_name}")
64
-
65
  except Exception as e:
66
  print(f"⚠️ Repository operations test failed: {e}")
67
  print(" This is normal if the token doesn't have full permissions")
68
-
69
  print(f"\nπŸŽ‰ Space authentication test completed successfully!")
70
  return True
71
-
72
  except Exception as e:
73
  print(f"❌ Space built-in authentication failed: {e}")
74
  print(f"\nπŸ”„ Trying alternative authentication methods...")
75
-
76
  # Method 2: Check for HF access token environment variable (fallback)
77
- hf_token = os.environ.get('HF_TOKEN')
78
  if hf_token:
79
  print(f"βœ… HF access token found in environment")
80
  print(f" Token: {hf_token[:8]}...{hf_token[-4:]}")
81
-
82
  try:
83
  # Try to use the HF_TOKEN
84
  from huggingface_hub import login
 
85
  login(token=hf_token)
86
  user_info = whoami()
87
  print(f"βœ… HF access token authentication successful!")
@@ -91,16 +90,17 @@ def test_space_authentication():
91
  print(f"❌ HF access token authentication failed: {e}")
92
  else:
93
  print(f"⚠️ HF access token not found in environment")
94
-
95
  # Method 3: Check for HUGGING_FACE_HUB_TOKEN (fallback)
96
- hf_hub_token = os.environ.get('HUGGING_FACE_HUB_TOKEN')
97
  if hf_hub_token:
98
  print(f"βœ… HUGGING_FACE_HUB_TOKEN found in environment")
99
  print(f" Token: {hf_hub_token[:8]}...{hf_hub_token[-4:]}")
100
-
101
  try:
102
  # Try to use the HUGGING_FACE_HUB_TOKEN
103
  from huggingface_hub import login
 
104
  login(token=hf_hub_token)
105
  user_info = whoami()
106
  print(f"βœ… HUGGING_FACE_HUB_TOKEN authentication successful!")
@@ -110,7 +110,7 @@ def test_space_authentication():
110
  print(f"❌ HUGGING_FACE_HUB_TOKEN authentication failed: {e}")
111
  else:
112
  print(f"⚠️ HUGGING_FACE_HUB_TOKEN not found in environment")
113
-
114
  # If all authentication methods failed
115
  print(f"\n❌ All authentication methods failed")
116
  print(f"\nπŸ”§ TROUBLESHOOTING STEPS:")
@@ -118,39 +118,40 @@ def test_space_authentication():
118
  print(f" - Ensure the Space has proper access to Hugging Face Hub")
119
  print(f" - Check Space settings for authentication configuration")
120
  print(f" - Verify the Space has necessary permissions")
121
-
122
  print(f"\n2. Alternative: Set HF Access Token in Space Settings:")
123
  print(f" - Go to https://huggingface.co/spaces/{space_id}/settings")
124
  print(f" - Navigate to 'Repository secrets' section")
125
  print(f" - Add HF_TOKEN with your HF access token")
126
  print(f" - Token should have 'Write' permissions")
127
-
128
  print(f"\n3. Create HF Access Token:")
129
  print(f" - Go to https://huggingface.co/settings/tokens")
130
  print(f" - Create a new token with 'Write' permissions")
131
  print(f" - Copy the token (starts with 'hf_')")
132
  print(f" - Add it to Space secrets as HF_TOKEN")
133
-
134
  print(f"\n4. Verify HF Access Token:")
135
  print(f" - Token must start with 'hf_' (Hugging Face format)")
136
  print(f" - Token must have 'Write' access to repositories")
137
  print(f" - Token must be valid and not expired")
138
  print(f" - Token must be associated with the correct HF account")
139
-
140
  return False
141
 
 
142
  def main():
143
  """Main function to run authentication tests."""
144
  print("πŸš€ OpenLLM Space Authentication Test")
145
  print("=" * 50)
146
-
147
  if not HF_AVAILABLE:
148
  print("❌ Required dependencies not available")
149
  sys.exit(1)
150
-
151
  # Run authentication test
152
  success = test_space_authentication()
153
-
154
  if success:
155
  print(f"\nβœ… All authentication tests passed!")
156
  print(f"πŸš€ Ready for OpenLLM training!")
@@ -160,5 +161,6 @@ def main():
160
  print(f"πŸ”§ Please follow the troubleshooting steps above.")
161
  sys.exit(1)
162
 
 
163
  if __name__ == "__main__":
164
  main()
 
11
 
12
  import os
13
  import sys
14
+
15
  try:
16
  from huggingface_hub import HfApi, login, whoami, create_repo, delete_repo
17
+
18
  HF_AVAILABLE = True
19
  except ImportError:
20
  HF_AVAILABLE = False
21
  print("❌ huggingface_hub not installed")
22
  sys.exit(1)
23
 
24
+
25
  def test_space_authentication():
26
  """Test authentication using Space's access token."""
27
  print("πŸ” Testing Space Authentication")
28
  print("=" * 40)
29
+
30
  # Check if we're in a Space environment
31
+ space_id = os.environ.get("SPACE_ID", "lemms/openllm")
32
  print(f"πŸ“ Space ID: {space_id}")
33
+
34
  # Check for various authentication methods
35
  print(f"\nπŸ” Checking authentication methods...")
36
+
37
  # Method 1: Check for Space's built-in authentication (primary method)
38
  try:
39
  # Try to get current user info (this will use Space's built-in access token)
 
41
  user_info = whoami()
42
  print(f"βœ… Space built-in authentication successful!")
43
  print(f"πŸ‘€ User: {user_info}")
44
+
45
  # Test API access by listing Space files
46
  print(f"\nπŸ“ Testing Space file access...")
47
+ files = api.list_repo_files(repo_id=space_id, repo_type="space")
48
  print(f"βœ… Successfully listed {len(files)} files in Space")
49
+
50
  # Test repository creation/deletion (temporary test)
51
  test_repo_name = f"test-repo-{os.getpid()}"
52
  print(f"\nπŸ§ͺ Testing repository operations...")
53
+
54
  try:
55
  # Create a temporary test repository
56
+ api.create_repo(repo_id=test_repo_name, repo_type="model", private=True, exist_ok=True)
 
 
 
 
 
57
  print(f"βœ… Successfully created test repository: {test_repo_name}")
58
+
59
  # Delete the test repository
60
  api.delete_repo(repo_id=test_repo_name, repo_type="model")
61
  print(f"βœ… Successfully deleted test repository: {test_repo_name}")
62
+
63
  except Exception as e:
64
  print(f"⚠️ Repository operations test failed: {e}")
65
  print(" This is normal if the token doesn't have full permissions")
66
+
67
  print(f"\nπŸŽ‰ Space authentication test completed successfully!")
68
  return True
69
+
70
  except Exception as e:
71
  print(f"❌ Space built-in authentication failed: {e}")
72
  print(f"\nπŸ”„ Trying alternative authentication methods...")
73
+
74
  # Method 2: Check for HF access token environment variable (fallback)
75
+ hf_token = os.environ.get("HF_TOKEN")
76
  if hf_token:
77
  print(f"βœ… HF access token found in environment")
78
  print(f" Token: {hf_token[:8]}...{hf_token[-4:]}")
79
+
80
  try:
81
  # Try to use the HF_TOKEN
82
  from huggingface_hub import login
83
+
84
  login(token=hf_token)
85
  user_info = whoami()
86
  print(f"βœ… HF access token authentication successful!")
 
90
  print(f"❌ HF access token authentication failed: {e}")
91
  else:
92
  print(f"⚠️ HF access token not found in environment")
93
+
94
  # Method 3: Check for HUGGING_FACE_HUB_TOKEN (fallback)
95
+ hf_hub_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
96
  if hf_hub_token:
97
  print(f"βœ… HUGGING_FACE_HUB_TOKEN found in environment")
98
  print(f" Token: {hf_hub_token[:8]}...{hf_hub_token[-4:]}")
99
+
100
  try:
101
  # Try to use the HUGGING_FACE_HUB_TOKEN
102
  from huggingface_hub import login
103
+
104
  login(token=hf_hub_token)
105
  user_info = whoami()
106
  print(f"βœ… HUGGING_FACE_HUB_TOKEN authentication successful!")
 
110
  print(f"❌ HUGGING_FACE_HUB_TOKEN authentication failed: {e}")
111
  else:
112
  print(f"⚠️ HUGGING_FACE_HUB_TOKEN not found in environment")
113
+
114
  # If all authentication methods failed
115
  print(f"\n❌ All authentication methods failed")
116
  print(f"\nπŸ”§ TROUBLESHOOTING STEPS:")
 
118
  print(f" - Ensure the Space has proper access to Hugging Face Hub")
119
  print(f" - Check Space settings for authentication configuration")
120
  print(f" - Verify the Space has necessary permissions")
121
+
122
  print(f"\n2. Alternative: Set HF Access Token in Space Settings:")
123
  print(f" - Go to https://huggingface.co/spaces/{space_id}/settings")
124
  print(f" - Navigate to 'Repository secrets' section")
125
  print(f" - Add HF_TOKEN with your HF access token")
126
  print(f" - Token should have 'Write' permissions")
127
+
128
  print(f"\n3. Create HF Access Token:")
129
  print(f" - Go to https://huggingface.co/settings/tokens")
130
  print(f" - Create a new token with 'Write' permissions")
131
  print(f" - Copy the token (starts with 'hf_')")
132
  print(f" - Add it to Space secrets as HF_TOKEN")
133
+
134
  print(f"\n4. Verify HF Access Token:")
135
  print(f" - Token must start with 'hf_' (Hugging Face format)")
136
  print(f" - Token must have 'Write' access to repositories")
137
  print(f" - Token must be valid and not expired")
138
  print(f" - Token must be associated with the correct HF account")
139
+
140
  return False
141
 
142
+
143
  def main():
144
  """Main function to run authentication tests."""
145
  print("πŸš€ OpenLLM Space Authentication Test")
146
  print("=" * 50)
147
+
148
  if not HF_AVAILABLE:
149
  print("❌ Required dependencies not available")
150
  sys.exit(1)
151
+
152
  # Run authentication test
153
  success = test_space_authentication()
154
+
155
  if success:
156
  print(f"\nβœ… All authentication tests passed!")
157
  print(f"πŸš€ Ready for OpenLLM training!")
 
161
  print(f"πŸ”§ Please follow the troubleshooting steps above.")
162
  sys.exit(1)
163
 
164
+
165
  if __name__ == "__main__":
166
  main()