File size: 2,525 Bytes
071ba6b | 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 | """Deploy FATHOM env server to HuggingFace Docker Space -- ENV-10.
Run: python scripts/deploy_space.py
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
# Force UTF-8 output on Windows
if sys.platform == "win32":
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
HF_TOKEN = os.getenv("HF_TOKEN")
SPACE_NAME = os.environ.get("FATHOM_SPACE_NAME", "Pratham-math/fathom-env")
REPO_ROOT = Path(__file__).parent.parent
def deploy():
from huggingface_hub import HfApi
api = HfApi(token=HF_TOKEN)
me = api.whoami()
print(f"Logged in as: {me['name']}")
# 1. Create Space (Docker SDK, public so env URL works without login)
print(f"Ensuring Space exists: {SPACE_NAME} ...")
api.create_repo(
repo_id=SPACE_NAME,
repo_type="space",
space_sdk="docker",
private=False,
exist_ok=True,
)
print("Space ready.")
# 2. Collect files to upload
uploads = [
(REPO_ROOT / "space" / "Dockerfile", "Dockerfile"),
(REPO_ROOT / "space" / "README.md", "README.md"),
(REPO_ROOT / "pyproject.toml", "pyproject.toml"),
]
# env/ package (server source)
for p in (REPO_ROOT / "env").rglob("*.py"):
rel = str(p.relative_to(REPO_ROOT)).replace("\\", "/")
uploads.append((p, rel))
# configs/ YAML files
for p in (REPO_ROOT / "configs").rglob("*.yaml"):
rel = str(p.relative_to(REPO_ROOT)).replace("\\", "/")
uploads.append((p, rel))
print(f"Uploading {len(uploads)} files...")
for local, remote in uploads:
if local.exists():
api.upload_file(
path_or_fileobj=str(local),
path_in_repo=remote,
repo_id=SPACE_NAME,
repo_type="space",
commit_message=f"Deploy: {remote}",
)
print(f" OK {remote}")
else:
print(f" SKIP (missing): {local}")
space_url_base = SPACE_NAME.replace("/", "-")
space_url = f"https://{space_url_base}.hf.space"
hf_url = f"https://huggingface.co/spaces/{SPACE_NAME}"
print("=" * 60)
print("Deploy complete!")
print(f" HF Space : {hf_url}")
print(f" App URL : {space_url}")
print(f" Health : {space_url}/healthz")
print("Space is building (~2-5 min). Once green, set:")
print(f" FATHOM_SPACE_URL={space_url}")
print("=" * 60)
if __name__ == "__main__":
deploy()
|