File size: 3,075 Bytes
0796851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8c4f91
 
 
 
 
 
 
 
 
 
0796851
 
a8c4f91
 
0796851
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
"""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()