File size: 2,282 Bytes
b9a427c
 
 
 
 
 
 
 
e2e2130
b9a427c
 
 
7a10114
 
b9a427c
a70790f
 
 
 
 
 
 
 
b9a427c
a70790f
 
b9a427c
7a10114
 
 
 
b9a427c
 
 
 
 
 
 
 
 
 
e2e2130
a70790f
b9a427c
 
7a10114
b9a427c
0ef7862
b9a427c
 
 
 
 
 
7a10114
 
 
 
 
 
e2e2130
7a10114
 
 
 
 
 
 
b9a427c
 
 
 
7a10114
b9a427c
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Deploy the chat site: Modal backend + HuggingFace Space + secrets."""

import os
import re
import subprocess

from dotenv import load_dotenv
from huggingface_hub import HfApi, SpaceHardware

load_dotenv()

SPACE_TITLE = "posttraining-practice"


def require_env(name: str) -> str:
    """Get required environment variable or exit with error."""
    value = os.environ.get(name)
    if value is None:
        raise SystemExit(f"ERROR: {name} must be set in .env")
    return value


def main():
    api_key = require_env("MODEL_SITE_API_KEY")
    site_password = require_env("SITE_PASSWORD")

    api = HfApi()
    user = api.whoami()["name"]
    space_id = f"{user}/{SPACE_TITLE}"

    # Deploy Modal backend
    print("Deploying Modal backend...")
    result = subprocess.run(
        ["uv", "run", "modal", "deploy", "site/backend.py"],
        capture_output=True,
        text=True,
    )
    print(result.stdout + result.stderr)

    match = re.search(r"https://[^\s]+\.modal\.run", result.stdout + result.stderr)
    if match is None:
        raise SystemExit("ERROR: Could not find Modal endpoint URL")
    modal_endpoint = match.group(0)

    # Generate requirements.txt
    result = subprocess.run(
        ["uv", "export", "--only-group", "site", "--no-hashes", "--no-dev"],
        capture_output=True,
        text=True,
    )
    with open("site/requirements.txt", "w") as f:
        f.write(result.stdout)

    # Create/update HuggingFace Space
    print(f"Deploying to HuggingFace Space {space_id}...")
    api.create_repo(
        repo_id=space_id,
        repo_type="space",
        space_sdk="gradio",
        space_hardware=SpaceHardware.CPU_BASIC,
        exist_ok=True,
    )

    api.upload_folder(
        folder_path="site",
        repo_id=space_id,
        repo_type="space",
    )
    os.remove("site/requirements.txt")

    # Set secrets
    print("Setting secrets...")
    api.add_space_secret(repo_id=space_id, key="MODAL_ENDPOINT", value=modal_endpoint)
    api.add_space_secret(repo_id=space_id, key="MODEL_SITE_API_KEY", value=api_key)
    api.add_space_secret(repo_id=space_id, key="SITE_PASSWORD", value=site_password)

    print(f"Done! https://huggingface.co/spaces/{space_id}")


if __name__ == "__main__":
    main()