File size: 2,942 Bytes
c17c7eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f94e3a
c17c7eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import subprocess
import sys
import os

def check_huggingface_login():
    """Verify the user is logged into Hugging Face CLI."""
    try:
        from huggingface_hub import HfApi
        api = HfApi()
        api.whoami()
        return True
    except Exception:
        return False

def init_space(space_name: str, private: bool = True):
    """Initialize a Hugging Face space for the environment."""
    print(f"--- Initializing Hugging Face Space: {space_name} ---")
    
    if not check_huggingface_login():
        print("Error: You are not logged in to Hugging Face CLI. Please run 'huggingface-cli login' first.")
        sys.exit(1)
        
    try:
        from huggingface_hub import HfApi
        api = HfApi()
        api.create_repo(repo_id=space_name, repo_type="space", space_sdk="docker", private=private, exist_ok=True)
        print("Space created or verified successfully!")
    except Exception as e:
        print(f"Failed to create space: {e}")
        print("Note: If it already exists, deployment will proceed.")

def deploy_to_space(space_name: str):
    """Deploy the current directory to the Hugging Face Space."""
    print(f"\n--- Deploying AegisGym to {space_name} ---")
    
    # Normally this would involve git add/commit/push to the HF remote
    # or using the huggingface_hub Python library to upload the folder seamlessly.
    try:
        from huggingface_hub import HfApi
        api = HfApi()
        print("Uploading files to Hub...")
        
        # Don't upload the cache or local virtual environments
        ignore_patterns = ["__pycache__/*", "*.git*", ".env", "venv/*", ".venv/*", "*.egg-info/*", "uv.lock"]
        
        url = api.upload_folder(
            folder_path=".",
            repo_id=space_name,
            repo_type="space",
            ignore_patterns=ignore_patterns
        )
        print(f"Deployment successful! Your environment is live at: {url}")
        
    except ImportError:
        print("Error: 'huggingface_hub' is not installed. Run 'pip install huggingface_hub'.")
        sys.exit(1)
    except Exception as e:
        print(f"Deployment failed: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Deploy AegisGym to Hugging Face Spaces.")
    parser.add_argument("space_name", help="The name of your huggingface space (e.g. username/aegisgym)")
    parser.add_argument("--public", action="store_true", help="Make the space public (default is private)")
    
    args = parser.parse_args()
    
    init_space(args.space_name, private=not args.public)
    deploy_to_space(args.space_name)
    
    print("\n--- Next Steps ---")
    print("1. Your Space is building the Docker container using your OpenEnv configurations.")
    print(f"2. You can access it directly via: https://huggingface.co/spaces/{args.space_name}")
    print("3. In your training script, switch your Env URL to match your Space.")