File size: 14,317 Bytes
1cb5d9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
#!/usr/bin/env python3
"""
Hugging Face Space Authentication Setup (GitHub Secrets)
This script helps set up proper authentication for Hugging Face Spaces
using GitHub secrets to ensure training and model uploads work correctly.
Features:
- Sets up authentication for Hugging Face Spaces using GitHub secrets
- Tests repository creation and upload capabilities
- Provides configuration for Space environments
- Validates authentication before training
Usage:
python setup_hf_space_auth.py
Author: Louis Chua Bean Chong
License: GPLv3
"""
import os
import sys
import json
from pathlib import Path
try:
from huggingface_hub import HfApi, login, whoami, create_repo
from huggingface_hub.utils import HfHubHTTPError
HF_AVAILABLE = True
except ImportError:
HF_AVAILABLE = False
print("β huggingface_hub not installed")
sys.exit(1)
class HuggingFaceSpaceAuthSetup:
"""
Sets up authentication for Hugging Face Spaces training and upload using GitHub secrets.
"""
def __init__(self):
"""Initialize the Space authentication setup."""
self.api = None
self.username = None
self.is_authenticated = False
self.space_config = {}
def setup_space_authentication(self) -> bool:
"""
Set up authentication specifically for Hugging Face Spaces using GitHub secrets.
Returns:
True if authentication successful, False otherwise
"""
print("π Hugging Face Space Authentication Setup (GitHub Secrets)")
print("=" * 65)
# Check if we're in a Hugging Face Space
is_space = self._check_if_in_space()
if is_space:
print("β
Running in Hugging Face Space environment")
return self._setup_space_env_auth()
else:
print("βΉοΈ Running in local environment")
return self._setup_local_auth()
def _check_if_in_space(self) -> bool:
"""Check if we're running in a Hugging Face Space."""
space_env_vars = [
"SPACE_ID",
"SPACE_HOST",
"SPACE_REPO_ID",
"HF_HUB_ENABLE_HF_TRANSFER"
]
for var in space_env_vars:
if os.getenv(var):
print(f" - Found Space environment variable: {var}")
return True
return False
def _setup_space_env_auth(self) -> bool:
"""Set up authentication in Hugging Face Space environment using GitHub secrets."""
print("\nπ Setting up Space Environment Authentication (GitHub Secrets)")
print("-" * 60)
# In Spaces with GitHub integration, authentication is handled via HF_TOKEN from GitHub secrets
token = os.getenv("HF_TOKEN")
if not token:
print("β HF_TOKEN not found in Space environment")
print(" - This should be set in your GitHub repository secrets")
print(" - Go to your GitHub repository β Settings β Secrets and variables β Actions")
print(" - Add HF_TOKEN with your Hugging Face token")
print(" - The Space will automatically have access to this secret")
return False
try:
# Login with the token
login(token=token)
# Test authentication
self.api = HfApi()
user_info = whoami()
self.username = user_info["name"]
self.is_authenticated = True
print(f"β
Space authentication successful!")
print(f" - Username: {self.username}")
print(f" - Token: {token[:8]}...{token[-4:]}")
print(f" - Source: GitHub secrets")
# Store Space configuration
self.space_config = {
"is_space": True,
"username": self.username,
"space_id": os.getenv("SPACE_ID"),
"space_host": os.getenv("SPACE_HOST"),
"space_repo_id": os.getenv("SPACE_REPO_ID"),
"auth_source": "github_secrets"
}
return True
except Exception as e:
print(f"β Space authentication failed: {e}")
return False
def _setup_local_auth(self) -> bool:
"""Set up authentication in local environment."""
print("\nπ Setting up Local Environment Authentication")
print("-" * 45)
try:
# Try to get current user info first
user_info = whoami()
self.username = user_info["name"]
self.api = HfApi()
self.is_authenticated = True
print(f"β
Already authenticated as: {self.username}")
return True
except Exception as e:
print(f"β Not authenticated: {e}")
print("π Attempting to authenticate...")
# Try environment variable
token = os.getenv("HUGGING_FACE_HUB_TOKEN") or os.getenv("HF_TOKEN")
if token:
try:
login(token=token)
user_info = whoami()
self.username = user_info["name"]
self.api = HfApi()
self.is_authenticated = True
print(f"β
Authenticated with token as: {self.username}")
return True
except Exception as token_error:
print(f"β Token authentication failed: {token_error}")
print("β Authentication failed")
return False
def test_repository_creation(self) -> bool:
"""
Test repository creation capabilities.
Returns:
True if successful, False otherwise
"""
if not self.is_authenticated:
print("β Not authenticated")
return False
print(f"\nπ§ͺ Testing Repository Creation")
print("-" * 35)
try:
# Create test repository
repo_name = "test-openllm-space-auth"
repo_id = f"{self.username}/{repo_name}"
print(f"π Creating test repository: {repo_id}")
create_repo(
repo_id=repo_id,
repo_type="model",
exist_ok=True,
private=True
)
print(f"β
Test repository created successfully")
# Clean up - delete the test repository
from huggingface_hub import delete_repo
print(f"π Cleaning up test repository...")
delete_repo(repo_id=repo_id, repo_type="model")
print(f"β
Test repository deleted")
return True
except Exception as e:
print(f"β Repository creation test failed: {e}")
return False
def test_model_upload(self) -> bool:
"""
Test model upload capabilities.
Returns:
True if successful, False otherwise
"""
if not self.is_authenticated:
print("β Not authenticated")
return False
print(f"\nπ€ Testing Model Upload")
print("-" * 25)
try:
# Create test file
test_file = "test_upload.txt"
with open(test_file, "w") as f:
f.write("This is a test file for Hugging Face Space upload verification.\n")
f.write("Generated by setup_hf_space_auth.py (GitHub Secrets)\n")
# Create test repository
repo_name = "test-upload-openllm-space"
repo_id = f"{self.username}/{repo_name}"
from huggingface_hub import create_repo, delete_repo
create_repo(
repo_id=repo_id,
repo_type="model",
exist_ok=True,
private=True
)
# Upload test file
print(f"π Uploading test file to {repo_id}...")
self.api.upload_file(
path_or_fileobj=test_file,
path_in_repo="test_file.txt",
repo_id=repo_id,
repo_type="model",
commit_message="Test upload from OpenLLM Space auth setup (GitHub Secrets)"
)
print(f"β
Test upload successful!")
# Clean up
print(f"π Cleaning up...")
delete_repo(repo_id=repo_id, repo_type="model")
os.remove(test_file)
print(f"β
Cleanup completed")
return True
except Exception as e:
print(f"β Upload test failed: {e}")
# Clean up test file if it exists
if os.path.exists("test_upload.txt"):
os.remove("test_upload.txt")
return False
def create_space_config(self) -> bool:
"""
Create configuration file for Hugging Face Space.
Returns:
True if successful, False otherwise
"""
if not self.is_authenticated:
print("β Not authenticated")
return False
print(f"\nπ Creating Space Configuration")
print("-" * 30)
config = {
"authentication": {
"username": self.username,
"authenticated": self.is_authenticated,
"method": "github_secrets" if self.space_config.get("auth_source") == "github_secrets" else "local_token"
},
"space_config": self.space_config,
"model_upload": {
"default_repo_prefix": f"{self.username}/openllm",
"supported_sizes": ["small", "medium", "large"],
"upload_enabled": True
},
"training": {
"checkpoint_upload": True,
"final_model_upload": True,
"create_model_card": True
}
}
# Save configuration
config_path = ".hf_space_config.json"
with open(config_path, "w") as f:
json.dump(config, f, indent=2)
print(f"β
Space configuration saved to: {config_path}")
return True
def generate_space_instructions(self) -> str:
"""Generate instructions for setting up Hugging Face Space with GitHub secrets."""
instructions = f"""
π― Hugging Face Space Setup Instructions (GitHub Secrets)
1. **Set up GitHub Repository Secrets:**
- Go to your GitHub repository: https://github.com/your-username/your-repo
- Click on "Settings" tab
- Click on "Secrets and variables" β "Actions"
- Click "New repository secret"
- Add a new secret:
- Name: HF_TOKEN
- Value: Your Hugging Face token (get from https://huggingface.co/settings/tokens)
2. **Verify Authentication:**
- Run this script in your Space to verify authentication works
- The script will test repository creation and upload capabilities
- GitHub secrets are automatically available in Hugging Face Spaces
3. **Training Configuration:**
- Your training script should use the authentication setup
- Model uploads will go to: https://huggingface.co/{self.username}/openllm-*
- Checkpoints will be saved during training
4. **Expected Results:**
- Training will complete successfully
- Model will be uploaded to Hugging Face Hub
- Repository will be created with proper model files
- Model card and configuration will be generated
5. **Troubleshooting:**
- If upload fails, check HF_TOKEN is set correctly in GitHub secrets
- Verify token has "Write" permissions
- Check Space logs for detailed error messages
- Ensure your Space is connected to the GitHub repository
"""
return instructions
def main():
"""Main function to run the Space authentication setup."""
print("π OpenLLM - Hugging Face Space Authentication Setup (GitHub Secrets)")
print("=" * 70)
# Initialize setup
auth_setup = HuggingFaceSpaceAuthSetup()
# Set up authentication
if not auth_setup.setup_space_authentication():
print("\nβ Authentication setup failed")
print("\nπ§ Troubleshooting:")
print("1. Get a Hugging Face token from https://huggingface.co/settings/tokens")
print("2. In GitHub: Set HF_TOKEN in Repository secrets (Settings β Secrets and variables β Actions)")
print("3. Locally: Set HUGGING_FACE_HUB_TOKEN environment variable")
sys.exit(1)
print(f"\nβ
Authentication successful!")
print(f" - Username: {auth_setup.username}")
# Test repository creation
repo_success = auth_setup.test_repository_creation()
# Test model upload
upload_success = auth_setup.test_model_upload()
# Create configuration
config_success = auth_setup.create_space_config()
# Summary
print(f"\nπ Setup Results")
print("-" * 20)
print(f"β
Authentication: PASSED")
print(f"{'β
' if repo_success else 'β'} Repository Creation: {'PASSED' if repo_success else 'FAILED'}")
print(f"{'β
' if upload_success else 'β'} Model Upload: {'PASSED' if upload_success else 'FAILED'}")
print(f"{'β
' if config_success else 'β'} Configuration: {'PASSED' if config_success else 'FAILED'}")
if repo_success and upload_success:
print(f"\nπ Space authentication setup completed successfully!")
print(f" - You can now run training in Hugging Face Spaces")
print(f" - Model uploads will work correctly")
print(f" - Your models will be uploaded to: https://huggingface.co/{auth_setup.username}")
# Show Space instructions
print(auth_setup.generate_space_instructions())
else:
print(f"\nβ οΈ Some tests failed. Check the error messages above.")
sys.exit(1)
return True
if __name__ == "__main__":
main()
|