#!/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)