beam-boot / scripts /publish_env_space.py
StarAtNyte1's picture
Publish Beam Boot / Super Beam source
324ecd2 verified
Raw History Blame Contribute Delete
2.34 kB
"""Publish envs/superbeam_env as a Docker Space (StarAtNyte1/superbeam-env) using the
Modal-held HF write token. The token stays in memory; it is never printed or saved.
modal run scripts/publish_env_space.py
Bundles the game files first (scripts/prepare_superbeam_env.py) and uploads server/Dockerfile
as the repo-root Dockerfile (Spaces requires it there).
"""
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
import modal
REPO_ID = "StarAtNyte1/superbeam-env"
ROOT = Path(__file__).resolve().parent.parent
ENV_DIR = ROOT / "envs" / "superbeam_env"
app = modal.App("superbeam-env-publish")
@app.function(image=modal.Image.debian_slim(python_version="3.12"),
secrets=[modal.Secret.from_name("steerability-submission-write")])
def get_write_token():
token = os.environ.get("HUGGINGFACE_TOKEN") or os.environ.get("HF_TOKEN")
if not token:
raise RuntimeError("No HF write token in configured secret")
return token
@app.local_entrypoint()
def main(private: bool = False, repo_type: str = "space"):
"""repo_type 'space' = Docker Space (needs HF PRO on free hardware); 'model' = a plain Hub repo
holding the same files, run locally with uvicorn or `docker build`."""
from huggingface_hub import HfApi
subprocess.run([sys.executable, str(ROOT / "scripts" / "prepare_superbeam_env.py")], check=True,
stdout=subprocess.DEVNULL)
token = get_write_token.remote()
api = HfApi(token=token)
if not api.repo_exists(repo_id=REPO_ID, repo_type=repo_type):
kw = {"space_sdk": "docker"} if repo_type == "space" else {}
api.create_repo(repo_id=REPO_ID, repo_type=repo_type, private=private, **kw)
url = "https://huggingface.co/%s%s" % ("spaces/" if repo_type == "space" else "", REPO_ID)
with tempfile.TemporaryDirectory() as tmp:
stage = Path(tmp) / "env"
shutil.copytree(ENV_DIR, stage, ignore=shutil.ignore_patterns("__pycache__", "*.pyc", ".venv*"))
shutil.copy2(stage / "server" / "Dockerfile", stage / "Dockerfile")
print(f"Uploading to {url}", flush=True)
api.upload_folder(repo_id=REPO_ID, repo_type=repo_type, folder_path=str(stage),
commit_message="SUPERBEAM OpenEnv environment")
print(f"Done: {url}", flush=True)