| # π― Hugging Face Space Authentication - Complete Solution (GitHub Secrets) | |
| ## π¨ Problem Summary | |
| Your OpenLLM training in Hugging Face Spaces completed successfully, but the model upload failed with authentication errors: | |
| ``` | |
| β οΈ Repository creation warning: 401 Client Error: Unauthorized | |
| Invalid username or password. | |
| β Failed to save/upload model: 401 Client Error | |
| Repository Not Found for url: https://huggingface.co/api/models/lemms/openllm-small-extended-8k/preupload/main | |
| ``` | |
| ## β Solution Provided | |
| I've created a comprehensive solution to ensure your Hugging Face Space has proper authentication using GitHub secrets for future training runs. | |
| ### π Files Created | |
| 1. **`setup_hf_space_auth.py`** - Complete Space authentication setup and testing (GitHub Secrets) | |
| 2. **`verify_space_auth.py`** - Simple verification script for Spaces (GitHub Secrets) | |
| 3. **`HUGGINGFACE_SPACE_SETUP_GUIDE.md`** - Comprehensive setup guide (GitHub Secrets) | |
| 4. **`SPACE_AUTHENTICATION_SUMMARY.md`** - This summary document | |
| ## π§ Quick Setup for Your Space | |
| ### Step 1: Set Up GitHub Repository Secrets | |
| 1. Go to your GitHub repository: | |
| ``` | |
| https://github.com/your-username/your-repo | |
| ``` | |
| 2. Click on the "Settings" tab | |
| 3. In the left sidebar, click "Secrets and variables" β "Actions" | |
| 4. Click "New repository secret" | |
| 5. Add a new secret: | |
| - **Name**: `HF_TOKEN` | |
| - **Value**: Your Hugging Face token (get from https://huggingface.co/settings/tokens) | |
| 6. Click "Add secret" | |
| **Note**: Hugging Face Spaces automatically have access to GitHub repository secrets, so you don't need to set them separately in the Space. | |
| ### Step 2: Add Verification to Your Space | |
| Add this code to your Space to verify authentication: | |
| ```python | |
| # Add this to your Space's main script | |
| import os | |
| from huggingface_hub import HfApi, login, whoami | |
| def verify_space_auth(): | |
| """Verify authentication is working in the Space using GitHub secrets.""" | |
| print("π Verifying Space Authentication (GitHub Secrets)") | |
| # Check if HF_TOKEN is set (from GitHub secrets) | |
| token = os.getenv("HF_TOKEN") | |
| if not token: | |
| print("β HF_TOKEN not found in Space environment") | |
| print(" - Please set HF_TOKEN in your GitHub repository secrets") | |
| print(" - Go to GitHub repository β Settings β Secrets and variables β Actions") | |
| return False | |
| try: | |
| # Test authentication | |
| login(token=token) | |
| user_info = whoami() | |
| username = user_info["name"] | |
| print(f"β Authentication successful!") | |
| print(f" - Username: {username}") | |
| print(f" - Source: GitHub secrets") | |
| # Test API access | |
| api = HfApi() | |
| print(f"β API access working") | |
| return True | |
| except Exception as e: | |
| print(f"β Authentication failed: {e}") | |
| return False | |
| # Run verification before training | |
| if __name__ == "__main__": | |
| verify_space_auth() | |
| ``` | |
| ### Step 3: Update Your Training Script | |
| Modify your training script to include proper authentication using GitHub secrets: | |
| ```python | |
| import os | |
| from huggingface_hub import HfApi, login, create_repo | |
| class SpaceTrainingManager: | |
| def __init__(self): | |
| self.setup_authentication() | |
| def setup_authentication(self): | |
| """Set up authentication for the Space using GitHub secrets.""" | |
| token = os.getenv("HF_TOKEN") | |
| if not token: | |
| raise ValueError("HF_TOKEN not found in Space environment. Please set it in GitHub repository secrets.") | |
| login(token=token) | |
| self.api = HfApi() | |
| user_info = whoami() | |
| self.username = user_info["name"] | |
| print(f"β Space authentication successful: {self.username}") | |
| print(f" - Source: GitHub secrets") | |
| def upload_model(self, model_dir: str, model_size: str = "small", steps: int = 8000): | |
| """Upload the trained model to Hugging Face Hub.""" | |
| repo_name = f"openllm-{model_size}-extended-{steps//1000}k" | |
| repo_id = f"{self.username}/{repo_name}" | |
| print(f"π€ Uploading model to {repo_id}") | |
| # Create repository | |
| create_repo(repo_id=repo_id, repo_type="model", exist_ok=True) | |
| # Upload all files | |
| self.api.upload_folder( | |
| folder_path=model_dir, | |
| repo_id=repo_id, | |
| repo_type="model", | |
| commit_message=f"Add OpenLLM {model_size} model ({steps} steps)" | |
| ) | |
| print(f"β Model uploaded successfully!") | |
| print(f" - Repository: https://huggingface.co/{repo_id}") | |
| return repo_id | |
| # Usage in your training script | |
| def main(): | |
| # Initialize training manager | |
| training_manager = SpaceTrainingManager() | |
| # Your training code here... | |
| # ... (training logic) ... | |
| # After training completes, upload the model | |
| model_dir = "./openllm-trained" | |
| repo_id = training_manager.upload_model(model_dir, "small", 8000) | |
| print(f"π Training and upload completed!") | |
| if __name__ == "__main__": | |
| main() | |
| ``` | |
| ## π§ͺ Testing the Setup | |
| ### Local Testing (Optional) | |
| You can test the setup locally first: | |
| ```bash | |
| # Test the Space authentication setup | |
| python setup_hf_space_auth.py | |
| # Test the verification script | |
| python verify_space_auth.py | |
| ``` | |
| ### Space Testing | |
| Add this to your Space to test authentication: | |
| ```python | |
| # Add this to your Space | |
| from verify_space_auth import verify_space_authentication | |
| # Run verification | |
| verify_space_authentication() | |
| ``` | |
| ## π― Expected Results | |
| After setting up your Space with proper authentication using GitHub secrets, you should see: | |
| ``` | |
| β Running in Hugging Face Space environment | |
| β HF_TOKEN found: hf_xxxx...xxxx | |
| - Source: GitHub secrets | |
| β Authentication successful! | |
| - Username: lemms | |
| β API access working | |
| π§ͺ Testing Repository Creation | |
| π Creating test repository: lemms/test-openllm-verification | |
| β Repository created successfully | |
| π Cleaning up test repository... | |
| β Repository deleted | |
| π All verification tests passed! | |
| - Authentication: β Working | |
| - Repository Creation: β Working | |
| - GitHub Secrets Integration: β Working | |
| - Ready for training and model uploads! | |
| ``` | |
| ## π Next Steps | |
| 1. **Set up your GitHub repository secrets** with HF_TOKEN | |
| 2. **Add the verification code** to your Space | |
| 3. **Update your training script** with the SpaceTrainingManager | |
| 4. **Re-run your training** - the upload should now work correctly | |
| 5. **Monitor the Space logs** for successful upload messages | |
| ## π Security Notes | |
| - **Token Security**: HF_TOKEN is stored securely in GitHub repository secrets | |
| - **Repository Access**: Only you can access your model repositories | |
| - **Cleanup**: Test repositories are automatically deleted | |
| - **Monitoring**: Check Space logs for any issues | |
| - **GitHub Integration**: Secrets are automatically available in connected Spaces | |
| ## π Complete Workflow | |
| 1. **Set up GitHub repository secrets** with your HF_TOKEN | |
| 2. **Verify authentication** using the verification script | |
| 3. **Run your training** with the updated training manager | |
| 4. **Monitor upload progress** in the Space logs | |
| 5. **Verify the model** appears on Hugging Face Hub | |
| ## π Success Criteria | |
| Your setup is successful when: | |
| - β Authentication verification passes | |
| - β Repository creation test passes | |
| - β Training completes without upload errors | |
| - β Model appears on Hugging Face Hub at `https://huggingface.co/lemms/openllm-small-extended-8k` | |
| ## π Benefits of GitHub Secrets | |
| 1. **Centralized Management**: All secrets managed in one place | |
| 2. **Automatic Access**: Spaces automatically have access to repository secrets | |
| 3. **Version Control**: Secrets are tied to your repository | |
| 4. **Security**: GitHub provides secure secret management | |
| 5. **Easy Updates**: Update secrets without touching Space settings | |
| ## π If You Need Help | |
| 1. **Check the comprehensive guide**: `HUGGINGFACE_SPACE_SETUP_GUIDE.md` | |
| 2. **Run verification tests** in your Space | |
| 3. **Check Space logs** for detailed error messages | |
| 4. **Verify token permissions** at https://huggingface.co/settings/tokens | |
| 5. **Ensure Space-GitHub connection** is properly set up | |
| --- | |
| **Status**: β **SOLUTION READY** - Follow the steps above to fix your Space authentication using GitHub secrets and ensure successful model uploads! | |