File size: 2,172 Bytes
371d90c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Create (or update) the Space and upload this folder.

Usage:
  python scripts/deploy.py --repo USER/beyond-tokens-decoding [--private] [--hardware zero-a10g] [--message "..."]

Needs a write token (`hf auth login`). Gated models (Llama 3.2, Gemma 3) also need an HF_TOKEN secret on the Space
with access to those repos; add it in the Space settings.
"""

from __future__ import annotations

import argparse
from pathlib import Path

from huggingface_hub import HfApi

ROOT = Path(__file__).resolve().parents[1]
IGNORE = [".venv/*", ".tools/*", "**/__pycache__/*", "__pycache__/*", ".pytest_cache/*", "*.log", ".DS_Store",
          "**/.DS_Store", "shots/*"]


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--repo", required=True)
    ap.add_argument("--private", action="store_true")
    ap.add_argument("--hardware", default="zero-a10g", help="ZeroGPU's flavor id; '' to leave unchanged")
    ap.add_argument("--message", default="Deploy the decoding playground")
    args = ap.parse_args()

    api = HfApi()
    # Request the hardware at creation: free accounts may not create Gradio Spaces on cpu-basic,
    # but may host ZeroGPU Spaces.
    url = api.create_repo(args.repo, repo_type="space", space_sdk="gradio", private=args.private, exist_ok=True,
                          space_hardware=args.hardware or None)
    print("space:", url)
    if args.hardware:
        try:
            api.request_space_hardware(args.repo, args.hardware)
            print("hardware requested:", args.hardware)
        except Exception as exc:  # e.g. account not eligible for ZeroGPU
            print(f"could not set hardware {args.hardware!r}: {exc}\n  -> choose ZeroGPU in the Space settings")
    info = api.upload_folder(repo_id=args.repo, repo_type="space", folder_path=str(ROOT), ignore_patterns=IGNORE,
                             commit_message=args.message)
    print("uploaded:", info.commit_url if hasattr(info, "commit_url") else info)
    runtime = api.get_space_runtime(args.repo)
    print("runtime:", runtime.stage, "hardware:", runtime.hardware, "requested:", runtime.requested_hardware)


if __name__ == "__main__":
    main()