Spaces:
Sleeping

File size: 11,324 Bytes
3f4c5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""FastAPI + WebSocket transport for the Butterfly game (one GPU, one dream).

Protocol (client -> server, JSON):
    {"type":"new",    "image":"0003.png", "seed":1234}
    {"type":"action", "keys":["w"], "mouse":[0.0,0.1]}
    {"type":"anchor", "label":"before the bridge"}
    {"type":"rewind", "anchor_id":"..."}
    {"type":"duel",   "anchor_id":"...", "seed":7}
    {"type":"tree"} | {"type":"inspect"}

Server -> client: JSON events (started/stepped/anchored/duel_*/collapsed/...)
and binary frames: [1B stream: 0=live 1=ghost][4B LE frame idx][JPEG].
"""

from __future__ import annotations

import asyncio
import json
import os
import struct
import time

import numpy as np

try:
    import cv2
except ImportError:  # pragma: no cover
    cv2 = None

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import FileResponse, HTMLResponse

PROJ = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
WEB_DIR = os.path.join(PROJ, "web")

app = FastAPI(title="Butterfly — SavePoint")

GAME = None
SAVE_DIR = os.environ.get("SAVEPOINT_SAVE_DIR", os.path.join(PROJ, "saves"))
IMAGE_DIR = os.environ.get(
    "SAVEPOINT_IMAGE_DIR",
    os.path.join(PROJ, "third_party/mg2_src_fetch/Matrix-Game-2/demo_images/universal"))
LOCK = asyncio.Lock()

# one dreamer at a time: first socket holds the seat, the rest watch and wait
CLIENTS: list = []            # connection order = queue order
PLAYER: "WebSocket|None" = None


async def _send_safe(ws, *, text=None, blob=None):
    try:
        if text is not None:
            await ws.send_text(text)
        if blob is not None:
            await ws.send_bytes(blob)
    except Exception:
        pass  # a dead spectator must never kill the dream


async def _broadcast(*, text=None, blob=None):
    for ws in list(CLIENTS):
        await _send_safe(ws, text=text, blob=blob)


async def _seat_update():
    """Tell every client its role and place in line."""
    for i, ws in enumerate(list(CLIENTS)):
        role = "player" if ws is PLAYER else "waiting"
        await _send_safe(ws, text=json.dumps(
            {"type": "seat", "role": role,
             "position": 0 if ws is PLAYER else i,
             "watchers": len(CLIENTS) - 1}))


@app.on_event("startup")
def startup() -> None:
    global GAME
    if os.environ.get("SAVEPOINT_NO_GPU"):  # UI development without a model
        return
    import sys
    mg2_root = os.environ.get(
        "SAVEPOINT_MG2_ROOT",
        os.path.join(PROJ, "third_party/mg2_src_fetch/Matrix-Game-2"))
    sys.path.insert(0, mg2_root)
    sys.path.insert(0, os.path.join(PROJ, "src"))
    from savepoint.hosts.matrix_game2 import MatrixGame2Host
    from savepoint.timeline import Timeline
    from server.game import ButterflyGame
    host = MatrixGame2Host(
        mg2_root,
        os.environ.get("SAVEPOINT_CKPT_DIR", os.path.join(PROJ, "ckpts/Matrix-Game-2.0")),
        latents_window_frames=24)
    os.makedirs(SAVE_DIR, exist_ok=True)
    GAME = ButterflyGame(host, Timeline(SAVE_DIR), SAVE_DIR,
                         chaos_baseline_path=os.environ.get("SAVEPOINT_CHAOS_BASELINE"))
    # warm up torch.compile off the player's first move (one throwaway block)
    from server.game import ButterflyGame as _BG  # noqa: F401
    warm_img = _load_image(os.environ.get("SAVEPOINT_WARMUP_IMAGE", "0003.png"))
    host.prime(warm_img, seed=0)
    host.step({"keys": [], "mouse": [0.0, 0.0]})
    print("server warm: first-block compile done", flush=True)


@app.get("/")
def index() -> HTMLResponse:
    with open(os.path.join(WEB_DIR, "index.html")) as fh:
        return HTMLResponse(fh.read())


@app.get("/health")
def health() -> dict:
    return {"gpu": GAME is not None, "clients": len(CLIENTS),
            "seat_taken": PLAYER is not None}


@app.get("/export/{anchor_id}")
def export(anchor_id: str) -> FileResponse:
    """Relay: download an anchor's .wsave (the file is immutable once written)."""
    from fastapi import HTTPException
    if GAME is None:
        raise HTTPException(503, "no world model loaded (SAVEPOINT_NO_GPU)")
    try:
        info = GAME.export_info(anchor_id)
    except KeyError:
        raise HTTPException(404, f"unknown anchor {anchor_id}")
    return FileResponse(info["path"], media_type="application/octet-stream",
                        filename=f"butterfly_{anchor_id[:8]}.wsave")


def _load_image(name: str):
    import sys
    sys.path.insert(0, os.path.join(PROJ, "src"))
    from bench.p0_smoke import load_start_image
    return load_start_image(os.path.join(IMAGE_DIR, os.path.basename(name)))


def _jpeg_frames(frames_u8, stream_id: int, first_index: int):
    """frames_u8: torch uint8 [T,H,W,3] -> length-prefixed binary messages."""
    out = []
    for i, frame in enumerate(frames_u8.numpy()):
        ok, buf = cv2.imencode(".jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR),
                               [cv2.IMWRITE_JPEG_QUALITY, 85])
        if ok:
            out.append(struct.pack("<BI", stream_id, first_index + i) + buf.tobytes())
    return out


FRAME_NO = 0
READ_ONLY = {"tree", "inspect"}


@app.websocket("/ws")
async def ws(sock: WebSocket) -> None:
    global PLAYER, FRAME_NO
    await sock.accept()
    CLIENTS.append(sock)
    if PLAYER is None:
        PLAYER = sock
    await _seat_update()
    try:
        while True:
            msg = json.loads(await sock.receive_text())
            mtype = msg.get("type")
            async with LOCK:
                try:
                    if GAME is None:
                        await _send_safe(sock, text=json.dumps(
                            {"type": "error", "error":
                             "this hosted dream has no GPU yet — a grant is "
                             "pending; clone the repo and play on your own GPU"}))
                        continue
                    if sock is not PLAYER and mtype not in READ_ONLY:
                        if mtype == "handoff":
                            continue  # only the player can hand the seat over
                        pos = CLIENTS.index(sock) if sock in CLIENTS else -1
                        await _send_safe(sock, text=json.dumps(
                            {"type": "error", "error":
                             f"the seat is taken — you are watching "
                             f"(#{pos} in line); the dream is shared live"}))
                        continue
                    if mtype in ("new", "mission"):
                        image = _load_image(msg.get("image", "0003.png"))
                        t0 = time.time()
                        fn = GAME.start_mission if mtype == "mission" else GAME.new_game
                        info = await asyncio.to_thread(
                            fn, image, int(msg.get("seed", 1234)))
                        info["prime_ms"] = (time.time() - t0) * 1000
                        FRAME_NO = 0
                        await _broadcast(text=json.dumps(info))
                    elif mtype == "mission_tear":
                        await _broadcast(text=json.dumps(await asyncio.to_thread(
                            GAME.mission_tear,
                            int(msg.get("seed", int(time.time()) % 100000)))))
                    elif mtype == "mission_abandon":
                        await _broadcast(text=json.dumps(await asyncio.to_thread(
                            GAME.abandon_mission)))
                    elif mtype == "handoff":
                        # the player gives up the seat: rotate to the next in line
                        CLIENTS.remove(sock)
                        CLIENTS.append(sock)
                        PLAYER = CLIENTS[0]
                        await _seat_update()
                    elif mtype == "action":
                        t0 = time.time()
                        live, ghost, info = await asyncio.to_thread(
                            GAME.step, {"keys": msg.get("keys", []),
                                        "mouse": msg.get("mouse", [0.0, 0.0]),
                                        "flap": bool(msg.get("flap", False))})
                        if live is not None:
                            for blob in _jpeg_frames(live, 0, FRAME_NO):
                                await _broadcast(blob=blob)
                            if ghost is not None:
                                for blob in _jpeg_frames(ghost, 1, FRAME_NO):
                                    await _broadcast(blob=blob)
                            FRAME_NO += live.shape[0]
                        info["step_ms"] = (time.time() - t0) * 1000
                        await _broadcast(text=json.dumps(info))
                    elif mtype == "anchor":
                        await _broadcast(text=json.dumps(await asyncio.to_thread(
                            GAME.drop_anchor, msg.get("label", ""))))
                    elif mtype == "rewind":
                        await _broadcast(text=json.dumps(await asyncio.to_thread(
                            GAME.rewind, msg["anchor_id"])))
                    elif mtype == "duel":
                        await _broadcast(text=json.dumps(await asyncio.to_thread(
                            GAME.start_duel, msg["anchor_id"],
                            int(msg.get("seed", int(time.time()) % 100000)))))
                    elif mtype == "butterfly":
                        await _broadcast(text=json.dumps(await asyncio.to_thread(
                            GAME.start_butterfly, msg["anchor_id"])))
                    elif mtype == "import":
                        await _broadcast(text=json.dumps(await asyncio.to_thread(
                            GAME.import_save, msg["path"], msg.get("label", ""))))
                    elif mtype == "tree":
                        await _send_safe(sock, text=json.dumps(
                            {"type": "tree", "tree": GAME.timeline.to_tree(),
                             "current": getattr(GAME, "_current_anchor", None)}))
                    elif mtype == "inspect":
                        comp = await asyncio.to_thread(GAME.host.state_components)
                        await _send_safe(sock, text=json.dumps(
                            {"type": "inspect",
                             "components": {k: int(v) for k, v in comp.items()}}))
                    else:
                        await _send_safe(sock, text=json.dumps(
                            {"type": "error", "error": f"unknown type {mtype}"}))
                except Exception as e:  # report to the sender; keep sockets alive
                    import traceback
                    traceback.print_exc()
                    await _send_safe(sock, text=json.dumps(
                        {"type": "error", "error": f"{type(e).__name__}: {e}"}))
    except WebSocketDisconnect:
        pass
    finally:
        if sock in CLIENTS:
            CLIENTS.remove(sock)
        if sock is PLAYER:
            PLAYER = CLIENTS[0] if CLIENTS else None
        await _seat_update()


def main() -> None:
    import argparse
    import uvicorn
    ap = argparse.ArgumentParser()
    ap.add_argument("--host", default="0.0.0.0")
    ap.add_argument("--port", type=int, default=7860)
    args = ap.parse_args()
    uvicorn.run(app, host=args.host, port=args.port)


if __name__ == "__main__":
    main()