|
|
|
|
|
""" |
|
|
Space Authentication Test for OpenLLM Training |
|
|
|
|
|
This script verifies that Hugging Face authentication is working correctly |
|
|
in your Space using GitHub secrets. Add this to your Space to test before training. |
|
|
|
|
|
Usage: |
|
|
Add this file to your Space and run it to verify authentication. |
|
|
""" |
|
|
|
|
|
import os |
|
|
import sys |
|
|
|
|
|
try: |
|
|
from huggingface_hub import HfApi, login, whoami, create_repo, delete_repo |
|
|
HF_AVAILABLE = True |
|
|
except ImportError: |
|
|
HF_AVAILABLE = False |
|
|
print("β huggingface_hub not installed") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
def test_space_authentication(): |
|
|
"""Test authentication in Hugging Face Space using GitHub secrets.""" |
|
|
print("π Testing Space Authentication (GitHub Secrets)") |
|
|
print("=" * 55) |
|
|
|
|
|
|
|
|
space_vars = ["SPACE_ID", "SPACE_HOST", "SPACE_REPO_ID"] |
|
|
is_space = any(os.getenv(var) for var in space_vars) |
|
|
|
|
|
if is_space: |
|
|
print("β
Running in Hugging Face Space environment") |
|
|
for var in space_vars: |
|
|
value = os.getenv(var) |
|
|
if value: |
|
|
print(f" - {var}: {value}") |
|
|
else: |
|
|
print("βΉοΈ Running in local environment") |
|
|
|
|
|
|
|
|
token = os.getenv("HF_TOKEN") |
|
|
if not token: |
|
|
print("β HF_TOKEN not found in environment") |
|
|
print(" - Please set HF_TOKEN in your GitHub repository secrets") |
|
|
print(" - Go to GitHub repository β Settings β Secrets and variables β Actions") |
|
|
print(" - Add HF_TOKEN with your Hugging Face token") |
|
|
return False |
|
|
|
|
|
print(f"β
HF_TOKEN found: {token[:8]}...{token[-4:]}") |
|
|
print(f" - Source: GitHub secrets") |
|
|
|
|
|
try: |
|
|
|
|
|
print("\nπ Testing authentication...") |
|
|
login(token=token) |
|
|
|
|
|
user_info = whoami() |
|
|
username = user_info["name"] |
|
|
|
|
|
print(f"β
Authentication successful!") |
|
|
print(f" - Username: {username}") |
|
|
|
|
|
|
|
|
print("\nπ Testing API access...") |
|
|
api = HfApi() |
|
|
print(f"β
API access working") |
|
|
|
|
|
|
|
|
print("\nπ§ͺ Testing Repository Creation") |
|
|
repo_name = "test-openllm-auth" |
|
|
repo_id = f"{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"β
Repository created successfully") |
|
|
|
|
|
|
|
|
print(f"π Cleaning up test repository...") |
|
|
delete_repo(repo_id=repo_id, repo_type="model") |
|
|
print(f"β
Repository deleted") |
|
|
|
|
|
print(f"\nπ All authentication tests passed!") |
|
|
print(f" - Authentication: β
Working") |
|
|
print(f" - API Access: β
Working") |
|
|
print(f" - Repository Creation: β
Working") |
|
|
print(f" - GitHub Secrets Integration: β
Working") |
|
|
print(f" - Ready for OpenLLM training and model uploads!") |
|
|
|
|
|
return True |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β Authentication test failed: {e}") |
|
|
print(f"\nπ§ Troubleshooting:") |
|
|
print(f"1. Check if HF_TOKEN is set correctly in GitHub repository secrets") |
|
|
print(f"2. Verify token has 'Write' permissions") |
|
|
print(f"3. Check Space logs for detailed error messages") |
|
|
print(f"4. Ensure your Space is connected to the GitHub repository") |
|
|
return False |
|
|
|
|
|
|
|
|
def main(): |
|
|
"""Main test function.""" |
|
|
print("π OpenLLM - Space Authentication Test") |
|
|
print("=" * 45) |
|
|
|
|
|
success = test_space_authentication() |
|
|
|
|
|
if success: |
|
|
print(f"\nβ
Authentication test completed successfully!") |
|
|
print(f" - Your Space is ready for OpenLLM training") |
|
|
print(f" - Model uploads will work correctly") |
|
|
print(f" - GitHub secrets integration is working") |
|
|
else: |
|
|
print(f"\nβ Authentication test failed") |
|
|
print(f" - Please fix authentication issues before training") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|