File size: 3,940 Bytes
b887a7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import os
from pathlib import Path

from dotenv import dotenv_values
from huggingface_hub import HfApi


IGNORE_PATTERNS = [
    ".git/*",
    ".venv/*",
    ".uv-cache/*",
    ".docker-config/*",
    ".pytest_cache/*",
    "__pycache__/*",
    ".codex",
    ".env",
    "*.log",
    "*.docx",
    "*.pdf",
]


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Create or update a Hugging Face Docker Space from this folder."
    )
    parser.add_argument(
        "--repo-id",
        required=True,
        help="Target Space repo id in the form <user-or-org>/<space-name>.",
    )
    parser.add_argument(
        "--private",
        action="store_true",
        help="Create the Space as private if it does not already exist.",
    )
    parser.add_argument(
        "--skip-secrets",
        action="store_true",
        help="Do not sync API_BASE_URL / MODEL_NAME / HF_TOKEN into Space settings.",
    )
    parser.add_argument(
        "--skip-upload",
        action="store_true",
        help="Create the Space and sync config, but do not upload the local folder.",
    )
    parser.add_argument(
        "--skip-restart",
        action="store_true",
        help="Do not request a Space restart after upload/config sync.",
    )
    return parser.parse_args()


def require_env(name: str) -> str:
    value = os.getenv(name)
    if not value:
        raise SystemExit(f"Missing required environment variable: {name}")
    return value


def load_runtime_config() -> dict[str, str]:
    env_path = Path(".env")
    if not env_path.exists():
        raise SystemExit("Missing .env file. This script reads runtime values from .env.")

    values = dotenv_values(env_path)
    runtime = {}
    for key in ("API_BASE_URL", "MODEL_NAME", "HF_TOKEN"):
        value = values.get(key)
        if not value:
            raise SystemExit(f"Missing required key in .env: {key}")
        runtime[key] = value
    return runtime


def main() -> None:
    args = parse_args()
    hub_token = require_env("HF_HUB_TOKEN")
    runtime = load_runtime_config()

    api = HfApi(token=hub_token)

    repo_url = api.create_repo(
        repo_id=args.repo_id,
        repo_type="space",
        space_sdk="docker",
        private=args.private,
        exist_ok=True,
    )
    print(f"Space ready: {repo_url}")

    if not args.skip_secrets:
        api.add_space_variable(
            args.repo_id,
            "API_BASE_URL",
            runtime["API_BASE_URL"],
            description="OpenAI-compatible provider base URL",
            token=hub_token,
        )
        api.add_space_variable(
            args.repo_id,
            "MODEL_NAME",
            runtime["MODEL_NAME"],
            description="Inference model name",
            token=hub_token,
        )
        api.add_space_secret(
            args.repo_id,
            "HF_TOKEN",
            runtime["HF_TOKEN"],
            description="Runtime provider API key used by inference.py",
            token=hub_token,
        )
        print("Space variables/secrets synced from .env")

    if not args.skip_upload:
        commit = api.upload_folder(
            repo_id=args.repo_id,
            repo_type="space",
            folder_path=".",
            ignore_patterns=IGNORE_PATTERNS,
            commit_message="Upload support triage environment",
            token=hub_token,
        )
        print(f"Upload commit: {commit.commit_url}")

    if not args.skip_restart:
        runtime_info = api.restart_space(args.repo_id, token=hub_token)
        print(f"Space restart requested. Stage: {runtime_info.stage}")
    else:
        runtime_info = api.get_space_runtime(args.repo_id, token=hub_token)
        print(f"Current Space stage: {runtime_info.stage}")

    print(f"Space URL: https://huggingface.co/spaces/{args.repo_id}")


if __name__ == "__main__":
    main()