fathom-code / scripts /deploy_space.py
23f2002275
clean repo without secrets or data
071ba6b
Raw
History Blame Contribute Delete
2.53 kB
"""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()