File size: 2,445 Bytes
e09df37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Deploy the IncidentCommander environment to a Hugging Face Space.

Uses the ``huggingface_hub`` Python API instead of ``git`` so this works on
Windows / macOS / Linux without requiring the git CLI.

Required env vars:

  HF_TOKEN       Hugging Face access token with `write` scope.
  SPACE_REPO_ID  e.g. ``alice/incident-commander``.

Optional:

  SPACE_PRIVATE  ``true`` to create a private Space (default: false).

Usage (PowerShell)::

    $env:HF_TOKEN = "hf_..."
    $env:SPACE_REPO_ID = "alice/incident-commander"
    python scripts/deploy_to_space.py

The script uploads the project root (excluding venv / build artifacts /
results) and HF's Space runner builds the Dockerfile automatically.
"""

from __future__ import annotations

import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent

IGNORE_PATTERNS = [
    ".venv",
    ".venv/*",
    ".git",
    ".git/*",
    "__pycache__",
    "*/__pycache__/*",
    "*.pyc",
    ".pytest_cache",
    ".pytest_cache/*",
    "build",
    "build/*",
    "dist",
    "dist/*",
    "*.egg-info",
    "*.egg-info/*",
    "results",
    "results/*",
    ".ipynb_checkpoints",
    "*/.ipynb_checkpoints/*",
    ".coverage",
    "coverage.xml",
]


def main() -> int:
    token = os.environ.get("HF_TOKEN")
    repo_id = os.environ.get("SPACE_REPO_ID")
    private = os.environ.get("SPACE_PRIVATE", "").lower() in {"1", "true", "yes"}

    if not token:
        print("ERROR: HF_TOKEN is not set", file=sys.stderr)
        return 2
    if not repo_id:
        print("ERROR: SPACE_REPO_ID is not set", file=sys.stderr)
        return 2

    from huggingface_hub import HfApi

    api = HfApi(token=token)

    print(f"Ensuring Space {repo_id} exists (private={private}) ...")
    api.create_repo(
        repo_id=repo_id,
        repo_type="space",
        space_sdk="docker",
        private=private,
        exist_ok=True,
    )

    print(f"Uploading {ROOT} to {repo_id} ...")
    commit = api.upload_folder(
        folder_path=str(ROOT),
        repo_id=repo_id,
        repo_type="space",
        ignore_patterns=IGNORE_PATTERNS,
        commit_message="Deploy IncidentCommander OpenEnv",
    )
    print(f"Uploaded. Commit: {commit}")
    print(f"Space URL: https://huggingface.co/spaces/{repo_id}")
    print("HF will now build the Dockerfile. Watch the build log on the Space page.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())