| |
| """ |
| 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 |
| """ |
| |
| repo_path = Path(__file__).parent |
|
|
| |
| api = HfApi() |
|
|
| |
| if token: |
| print("Setting HuggingFace token...") |
| os.environ["HF_TOKEN"] = token |
|
|
| try: |
| |
| print(f"\nπ Deploying to HuggingFace Spaces...") |
| print(f"Repository path: {repo_path}") |
| print(f"Space name: {space_name or 'code-interpreter-sandbox'}") |
|
|
| |
| repo_url = api.create_repo( |
| repo_id=space_name or "code-interpreter-sandbox", |
| repo_type="space", |
| space_sdk="docker", |
| private=private, |
| exist_ok=True |
| ) |
|
|
| |
| repo_id = f"{api.whoami()['name']}/{space_name or 'code-interpreter-sandbox'}" |
|
|
| print(f"\nβ
Repository created: {repo_url}") |
|
|
| |
| 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__": |
| |
| 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" |
|
|
| |
| 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) |
|
|