File size: 2,981 Bytes
523f6c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Deploy Code Interpreter to HuggingFace Spaces
"""

import os
import sys
import json
from huggingface_hub import HfApi
from pathlib import Path

def deploy_to_spaces(space_name=None, token=None, private=False):
    """
    Deploy the code interpreter to HuggingFace Spaces
    """
    # Get current directory
    repo_path = Path(__file__).parent

    # Initialize HuggingFace API
    api = HfApi()

    # Login using environment variable
    if token:
        print("Setting HuggingFace token...")
        os.environ["HF_TOKEN"] = token

    try:
        # Create or get repository
        print(f"\n🚀 Deploying to HuggingFace Spaces...")
        print(f"Repository path: {repo_path}")
        print(f"Space name: {space_name or 'code-interpreter-sandbox'}")

        # Create Space repository
        repo_url = api.create_repo(
            repo_id=space_name or "code-interpreter-sandbox",
            repo_type="space",
            space_sdk="docker",
            private=private,
            exist_ok=True
        )

        # Extract repo_id from URL
        repo_id = f"{api.whoami()['name']}/{space_name or 'code-interpreter-sandbox'}"

        print(f"\n✅ Repository created: {repo_url}")

        # Upload files
        print("\n📤 Uploading files...")
        api.upload_folder(
            repo_id=repo_id,
            repo_type="space",
            folder_path=repo_path,
            commit_message="Initial commit: Advanced Code Interpreter Sandbox"
        )

        print("\n🎉 Deployment successful!")
        print(f"\n📍 Your Space is available at:")
        print(f"   https://huggingface.co/spaces/{repo_id}")

        print(f"\n⏳ Build status: https://huggingface.co/spaces/{repo_id}/settings")

        return repo_id

    except Exception as e:
        print(f"\n❌ Deployment failed: {str(e)}")
        print(f"\nError type: {type(e).__name__}")

        if "401" in str(e) or "token" in str(e).lower():
            print("\n💡 Tip: Make sure you have a valid HuggingFace token.")
            print("   Get one at: https://huggingface.co/settings/tokens")

        if "404" in str(e) or "not found" in str(e).lower():
            print("\n💡 Tip: You may not have permission to create this space.")
            print("   Make sure you're logged in to the correct account.")

        return None

if __name__ == "__main__":
    # Configuration
    SPACE_NAME = os.environ.get("HF_SPACE_NAME", "code-interpreter-sandbox")
    TOKEN = os.environ.get("HF_TOKEN", None)
    PRIVATE = os.environ.get("HF_PRIVATE", "false").lower() == "true"

    # Deploy
    result = deploy_to_spaces(
        space_name=SPACE_NAME,
        token=TOKEN,
        private=PRIVATE
    )

    if result:
        print("\n" + "="*60)
        print("✨ DEPLOYMENT COMPLETE!")
        print("="*60)
        sys.exit(0)
    else:
        print("\n" + "="*60)
        print("❌ DEPLOYMENT FAILED")
        print("="*60)
        sys.exit(1)