File size: 2,339 Bytes
324ecd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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)