"""Deploy FinNER to HuggingFace Spaces. Usage: HF_TOKEN=hf_xxx python3 scripts/deploy_hf_space.py Get a write token from: https://huggingface.co/settings/tokens """ import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def run(cmd: list[str], **kwargs) -> subprocess.CompletedProcess: print(f" $ {' '.join(cmd)}") result = subprocess.run(cmd, **kwargs) if result.returncode != 0: print(f"ERROR: command failed (exit {result.returncode})") sys.exit(1) return result def main(): token = os.environ.get("HF_TOKEN", "").strip() if not token: print("ERROR: Set HF_TOKEN environment variable.") print(" Get a write token at: https://huggingface.co/settings/tokens") print(" Then run: HF_TOKEN=hf_xxx python3 scripts/deploy_hf_space.py") sys.exit(1) try: from huggingface_hub import HfApi except ImportError: print("Installing huggingface_hub...") run([sys.executable, "-m", "pip", "install", "-q", "huggingface_hub"]) from huggingface_hub import HfApi api = HfApi(token=token) user_info = api.whoami() username = user_info["name"] repo_id = f"{username}/finner" space_url = f"https://huggingface.co/spaces/{repo_id}" repo_url = f"https://huggingface.co/spaces/{repo_id}" repo_url_with_auth = f"https://{username}:{token}@huggingface.co/spaces/{repo_id}" print(f"\n→ Creating Space: {space_url}") api.create_repo( repo_id=repo_id, repo_type="space", space_sdk="docker", exist_ok=True, private=False, ) print(" Space ready.") print("\n→ Setting git remote...") os.chdir(ROOT) result = subprocess.run(["git", "remote", "get-url", "hf"], capture_output=True) if result.returncode == 0: run(["git", "remote", "set-url", "hf", repo_url_with_auth]) else: run(["git", "remote", "add", "hf", repo_url_with_auth]) print("\n→ Ensuring git LFS is initialised...") run(["git", "lfs", "install"]) print("\n→ Committing any outstanding local changes...") subprocess.run(["git", "add", "-A"], cwd=ROOT) subprocess.run( ["git", "commit", "-m", "Deploy: update inference pipeline and API"], cwd=ROOT, ) # allowed to fail if nothing to commit print("\n→ Pushing to HuggingFace (model.safetensors via LFS — may take a minute)...") run(["git", "lfs", "push", "--all", "hf", "main"]) # --force overwrites the default HF Space commit created on Space creation run(["git", "push", "hf", "main", "--force"]) # Remove token from remote URL after push run(["git", "remote", "set-url", "hf", repo_url]) print(f"\n✓ Deployed! Your Space is building at:\n {space_url}") print("\nThe build takes ~5 min (Docker image). Once green:") print(f" • Tagger: {space_url}") print(f" • Health: {space_url}/health") print(f" • API docs: {space_url}/docs") if __name__ == "__main__": main()