File size: 2,407 Bytes
fcb838d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Deploy CodeCourt to a Hugging Face Space.

Usage:
    python scripts/deploy_space.py --space-id your-username/codecourt

Auth:
    Set HF_TOKEN in the environment or run `huggingface-cli login` first.
"""

import argparse
import os
import shutil
import sys
import tempfile
from pathlib import Path

from huggingface_hub import HfApi
from dotenv import load_dotenv


PROJECT_ROOT = Path(__file__).resolve().parent.parent
SPACE_APP_FILES = [
    "Dockerfile",
    "app.py",
    "README.md",
    "openenv.yaml",
    "requirements.txt",
    "outputs",
    "dashboard",
    "env",
    "oracle",
    "rewards",
    "agents",
]


def parse_args():
    parser = argparse.ArgumentParser(description="Deploy CodeCourt to Hugging Face Spaces")
    parser.add_argument("--space-id", default=os.getenv("HF_SPACE_ID"))
    parser.add_argument("--token", default=os.getenv("HF_TOKEN"))
    parser.add_argument("--space-sdk", default="docker", choices=["static", "gradio", "streamlit", "docker"])
    parser.add_argument("--private", action="store_true")
    return parser.parse_args()


def copy_artifacts(staging_dir: Path) -> None:
    for rel_path in SPACE_APP_FILES:
        src = PROJECT_ROOT / rel_path
        dst = staging_dir / rel_path
        if src.is_dir():
            shutil.copytree(src, dst, dirs_exist_ok=True)
        elif src.is_file():
            dst.parent.mkdir(parents=True, exist_ok=True)
            shutil.copy2(src, dst)


def main():
    load_dotenv()
    args = parse_args()
    if not args.space_id:
        raise SystemExit("Missing --space-id or HF_SPACE_ID")

    api = HfApi(token=args.token)
    api.create_repo(
        repo_id=args.space_id,
        repo_type="space",
        space_sdk=args.space_sdk,
        private=args.private,
        exist_ok=True,
    )

    with tempfile.TemporaryDirectory() as tmpdir:
        staging_dir = Path(tmpdir) / "space"
        staging_dir.mkdir(parents=True, exist_ok=True)
        copy_artifacts(staging_dir)

        api.upload_folder(
            folder_path=str(staging_dir),
            repo_id=args.space_id,
            repo_type="space",
            commit_message="Deploy CodeCourt Space",
        )

    print(f"Deployed to https://huggingface.co/spaces/{args.space_id}")


if __name__ == "__main__":
    try:
        main()
    except Exception as exc:
        print(f"Space deploy failed: {exc}", file=sys.stderr)
        raise