File size: 1,935 Bytes
e05b8ab
 
 
 
 
 
 
 
 
 
ffdbdfc
e05b8ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Create a private HF Space and upload only the application source files."""

import argparse
import getpass
import os
from pathlib import Path
import subprocess

ROOT = Path(__file__).resolve().parents[1]
FILES = ["README.md", "app.py", "router.py", "attachments.py", "requirements.txt",
         "packages.txt", ".gitignore", ".env.example", "scripts/deploy.py", "scripts/smoke_remote.py"]


def run(args: list[str], **kwargs) -> subprocess.CompletedProcess:
    return subprocess.run(["hf", *args], check=True, text=True, **kwargs)


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("repo_id", help="Hugging Face username/space-name")
    parser.add_argument("--hardware", choices=["cpu-basic", "zero-a10g"], default="cpu-basic")
    args = parser.parse_args()
    if args.repo_id.count("/") != 1:
        parser.error("repo_id must be username/space-name")
    run(["auth", "whoami"])
    key = os.environ.get("OPENROUTER_API_KEY") or getpass.getpass("OpenRouter API Key (hidden): ")
    if not key.strip() or "\n" in key or "\r" in key:
        raise SystemExit("A valid single-line OpenRouter API Key is required.")
    # --secrets-file - keeps values off disk and out of the process arguments.
    create = ["repos", "create", args.repo_id, "--type", "space", "--space-sdk", "gradio",
              "--private", "--flavor", args.hardware, "--secrets-file", "-"]
    if args.hardware == "zero-a10g":
        create += ["--env", "USE_ZEROGPU=1"]
    run(create, input="OPENROUTER_API_KEY=" + key.strip() + "\n")
    del key
    upload = ["upload", args.repo_id, str(ROOT), ".", "--repo-type", "space",
              "--commit-message", "Add Claude document chat with OpenRouter"]
    for filename in FILES:
        upload.extend(["--include", filename])
    run(upload)
    print("Space: https://huggingface.co/spaces/" + args.repo_id)


if __name__ == "__main__":
    main()