DJ-Goanna-Coding/oppo-node
0
1#!/usr/bin/env python32"""Deploy the current repository to a single HuggingFace Space.3 4Intended to be called from CI (see ``.github/workflows/deploy_spaces.yml``),5one invocation per matrix entry so that deployments run in parallel.6 7Environment variables:8 9* ``HF_TOKEN`` -- HuggingFace access token (required).10* ``HF_USERNAME`` -- HuggingFace username/organisation (required).11* ``SPACE_NAME`` -- Target Space name (required).12* ``SPACE_SDK`` -- Space SDK, defaults to ``streamlit``.13"""14 15from __future__ import annotations16 17import os18import sys19 20from huggingface_hub import HfApi, create_repo21 22 23IGNORE_PATTERNS = [24 ".git/*",25 ".github/*",26 "*.pyc",27 "__pycache__/*",28 ".env",29 "venv/*",30 ".venv/*",31]32 33 34def _require(name: str) -> str:35 value = os.environ.get(name)36 if not value:37 print(f"ERROR: {name} is not set", file=sys.stderr)38 sys.exit(1)39 return value40 41 42def main() -> int:43 token = _require("HF_TOKEN")44 username = _require("HF_USERNAME")45 space_name = _require("SPACE_NAME")46 sdk = os.environ.get("SPACE_SDK", "streamlit")47 48 repo_id = f"{username}/{space_name}"49 api = HfApi(token=token)50 51 try:52 create_repo(53 repo_id=repo_id,54 token=token,55 repo_type="space",56 space_sdk=sdk,57 private=False,58 )59 print(f"Created space: {repo_id}")60 except Exception as exc: # noqa: BLE001 - HF client raises varied errors61 if "already exists" in str(exc).lower():62 print(f"Space {repo_id} already exists")63 else:64 raise65 66 api.upload_folder(67 folder_path=".",68 path_in_repo="",69 repo_id=repo_id,70 repo_type="space",71 commit_message="Deploy from GitHub Actions",72 ignore_patterns=IGNORE_PATTERNS,73 )74 75 print(f"Deployed to: https://huggingface.co/spaces/{repo_id}")76 return 077 78 79if __name__ == "__main__":80 sys.exit(main())81 