#!/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())