File size: 4,939 Bytes
877add7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""Create/update the private Hugging Face Space used for PolyGuard training."""

from __future__ import annotations

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

from huggingface_hub import HfApi


ROOT = Path(__file__).resolve().parents[1]


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Deploy the PolyGuard remote training Space.")
    parser.add_argument("--repo-id", default="TheJackBright/polyguard-openenv-training")
    parser.add_argument("--artifact-repo-id", default="TheJackBright/polyguard-openenv-training-artifacts")
    parser.add_argument("--model-id", default="Qwen/Qwen2.5-0.5B-Instruct")
    parser.add_argument("--hardware", default="t4-small")
    parser.add_argument("--sleep-time", type=int, default=3600)
    parser.add_argument("--bundle-dir", default="/tmp/polyguard-openenv-training-space")
    parser.add_argument("--public", action="store_true")
    parser.add_argument("--skip-upload", action="store_true")
    parser.add_argument("--bundle-only", action="store_true")
    return parser.parse_args()


def _ignore(_dir: str, names: list[str]) -> set[str]:
    ignored = {
        ".git",
        ".venv",
        "__pycache__",
        ".pytest_cache",
        ".mypy_cache",
        ".ruff_cache",
        "outputs",
        "checkpoints",
        "polyguard_rl.egg-info",
        "dist",
        "build",
    }
    return {
        name
        for name in names
        if name in ignored
        or name.endswith(".pyc")
        or name == "node_modules"
        or name == ".DS_Store"
    }


def build_bundle(bundle_dir: Path) -> None:
    if bundle_dir.exists():
        shutil.rmtree(bundle_dir)
    shutil.copytree(ROOT, bundle_dir, ignore=_ignore)
    shutil.copy2(ROOT / "app" / "hf_space" / "Dockerfile", bundle_dir / "Dockerfile")
    project_readme = bundle_dir / "PROJECT_README.md"
    if (bundle_dir / "README.md").exists():
        (bundle_dir / "README.md").replace(project_readme)
    (bundle_dir / "README.md").write_text(
        """---
title: PolyGuard HF Training
sdk: docker
app_port: 7860
pinned: false
---

# PolyGuard HF Training

Private Docker Space for running PolyGuard SFT/GRPO training on Hugging Face hardware.

The original project README is included as `PROJECT_README.md`.
""",
        encoding="utf-8",
    )


def main() -> None:
    args = parse_args()
    bundle_dir = Path(args.bundle_dir)
    build_bundle(bundle_dir)
    if args.bundle_only:
        print(f"bundle_dir={bundle_dir}")
        return

    token = os.getenv("HF_TOKEN")
    api = HfApi(token=token)
    whoami = api.whoami(token=token)
    username = str(whoami.get("name") or whoami.get("fullname") or "")
    if username and not args.repo_id.startswith(f"{username}/"):
        print(f"[deploy_training_space] authenticated as {username}; target={args.repo_id}")

    space_variables = [
        {"key": "POLYGUARD_MODEL_ID", "value": args.model_id},
        {"key": "POLYGUARD_OFFLINE_MODE", "value": "false"},
        {"key": "POLYGUARD_AUTORUN", "value": "1"},
        {"key": "POLYGUARD_ARTIFACT_REPO_ID", "value": args.artifact_repo_id},
        {"key": "POLYGUARD_SPACE_REPO_ID", "value": args.repo_id},
    ]
    space_secrets = [{"key": "HF_TOKEN", "value": token}] if token else None

    api.create_repo(repo_id=args.artifact_repo_id, repo_type="model", private=True, exist_ok=True)
    api.create_repo(
        repo_id=args.repo_id,
        repo_type="space",
        space_sdk="docker",
        private=not args.public,
        exist_ok=True,
        space_hardware=args.hardware,
        space_sleep_time=args.sleep_time,
        space_variables=space_variables,
        space_secrets=space_secrets,
    )

    for variable in space_variables:
        api.add_space_variable(repo_id=args.repo_id, key=variable["key"], value=variable["value"])
    if token:
        api.add_space_secret(repo_id=args.repo_id, key="HF_TOKEN", value=token)
    if not args.skip_upload:
        api.upload_folder(
            repo_id=args.repo_id,
            repo_type="space",
            folder_path=str(bundle_dir),
            commit_message="Deploy PolyGuard HF training Space",
            ignore_patterns=[
                ".git/*",
                ".venv/*",
                "**/node_modules/*",
                "outputs/*",
                "checkpoints/*",
                "**/__pycache__/*",
                "*.pyc",
            ],
        )

    try:
        api.request_space_hardware(repo_id=args.repo_id, hardware=args.hardware, sleep_time=args.sleep_time)
    except Exception as exc:  # noqa: BLE001
        print(f"hardware_request_warning={exc}", file=sys.stderr)

    print(f"space_url=https://huggingface.co/spaces/{args.repo_id}")
    print(f"artifact_repo=https://huggingface.co/{args.artifact_repo_id}")
    print(f"bundle_dir={bundle_dir}")


if __name__ == "__main__":
    main()