File size: 1,963 Bytes
c87f72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Deploy the current repository to a single HuggingFace Space.

Intended to be called from CI (see ``.github/workflows/deploy_spaces.yml``),
one invocation per matrix entry so that deployments run in parallel.

Environment variables:

* ``HF_TOKEN``    -- HuggingFace access token (required).
* ``HF_USERNAME`` -- HuggingFace username/organisation (required).
* ``SPACE_NAME``  -- Target Space name (required).
* ``SPACE_SDK``   -- Space SDK, defaults to ``streamlit``.
"""

from __future__ import annotations

import os
import sys

from huggingface_hub import HfApi, create_repo


IGNORE_PATTERNS = [
    ".git/*",
    ".github/*",
    "*.pyc",
    "__pycache__/*",
    ".env",
    "venv/*",
    ".venv/*",
]


def _require(name: str) -> str:
    value = os.environ.get(name)
    if not value:
        print(f"ERROR: {name} is not set", file=sys.stderr)
        sys.exit(1)
    return value


def main() -> int:
    token = _require("HF_TOKEN")
    username = _require("HF_USERNAME")
    space_name = _require("SPACE_NAME")
    sdk = os.environ.get("SPACE_SDK", "streamlit")

    repo_id = f"{username}/{space_name}"
    api = HfApi(token=token)

    try:
        create_repo(
            repo_id=repo_id,
            token=token,
            repo_type="space",
            space_sdk=sdk,
            private=False,
        )
        print(f"Created space: {repo_id}")
    except Exception as exc:  # noqa: BLE001 - HF client raises varied errors
        if "already exists" in str(exc).lower():
            print(f"Space {repo_id} already exists")
        else:
            raise

    api.upload_folder(
        folder_path=".",
        path_in_repo="",
        repo_id=repo_id,
        repo_type="space",
        commit_message="Deploy from GitHub Actions",
        ignore_patterns=IGNORE_PATTERNS,
    )

    print(f"Deployed to: https://huggingface.co/spaces/{repo_id}")
    return 0


if __name__ == "__main__":
    sys.exit(main())