CoolFace
Apppublic

sachiniyer/posttraining-practice

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
deploy.py83 linesDownload Raw Back to root
1#!/usr/bin/env python32"""Deploy the chat site: Modal backend + HuggingFace Space + secrets."""3 4import os5import re6import subprocess7 8from dotenv import load_dotenv9from huggingface_hub import HfApi, SpaceHardware10 11load_dotenv()12 13SPACE_TITLE = "posttraining-practice"14 15 16def require_env(name: str) -> str:17    """Get required environment variable or exit with error."""18    value = os.environ.get(name)19    if value is None:20        raise SystemExit(f"ERROR: {name} must be set in .env")21    return value22 23 24def main():25    api_key = require_env("MODEL_SITE_API_KEY")26    site_password = require_env("SITE_PASSWORD")27 28    api = HfApi()29    user = api.whoami()["name"]30    space_id = f"{user}/{SPACE_TITLE}"31 32    # Deploy Modal backend33    print("Deploying Modal backend...")34    result = subprocess.run(35        ["uv", "run", "modal", "deploy", "site/backend.py"],36        capture_output=True,37        text=True,38    )39    print(result.stdout + result.stderr)40 41    match = re.search(r"https://[^\s]+\.modal\.run", result.stdout + result.stderr)42    if match is None:43        raise SystemExit("ERROR: Could not find Modal endpoint URL")44    modal_endpoint = match.group(0)45 46    # Generate requirements.txt47    result = subprocess.run(48        ["uv", "export", "--only-group", "site", "--no-hashes", "--no-dev"],49        capture_output=True,50        text=True,51    )52    with open("site/requirements.txt", "w") as f:53        f.write(result.stdout)54 55    # Create/update HuggingFace Space56    print(f"Deploying to HuggingFace Space {space_id}...")57    api.create_repo(58        repo_id=space_id,59        repo_type="space",60        space_sdk="gradio",61        space_hardware=SpaceHardware.CPU_BASIC,62        exist_ok=True,63    )64 65    api.upload_folder(66        folder_path="site",67        repo_id=space_id,68        repo_type="space",69    )70    os.remove("site/requirements.txt")71 72    # Set secrets73    print("Setting secrets...")74    api.add_space_secret(repo_id=space_id, key="MODAL_ENDPOINT", value=modal_endpoint)75    api.add_space_secret(repo_id=space_id, key="MODEL_SITE_API_KEY", value=api_key)76    api.add_space_secret(repo_id=space_id, key="SITE_PASSWORD", value=site_password)77 78    print(f"Done! https://huggingface.co/spaces/{space_id}")79 80 81if __name__ == "__main__":82    main()83