"""Moxie bench FastAPI server. Serves the timeline UI (bench/ui-dist), the fixed-pipeline REST API and a WebSocket that mirrors ComfyUI's push-event model (execution_start, job_progress, execution_success/execution_error) so the ported editor can subscribe with the same patterns it uses inside ComfyUI. """ from __future__ import annotations import asyncio import json import shutil import subprocess from pathlib import Path from fastapi import FastAPI, File, Form, HTTPException, UploadFile, WebSocket, WebSocketDisconnect from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse, JSONResponse, Response from fastapi.staticfiles import StaticFiles from . import __version__ from .config import BenchConfig from .jobs import JobManager from .pipeline import run_pipeline from .tracks import TrackDataError, build_render_plan from .workflow import ( EDITOR_TYPE, WorkflowError, describe, editor_resolution, load_base_workflow, parse_workflow_dict, project_data as spec_project_data, ) _MEDIA_KEYS = ("images", "videos", "gifs", "audio") def create_app(cfg: BenchConfig | None = None) -> FastAPI: cfg = cfg or BenchConfig.from_args() app = FastAPI(title="Moxie Multimedia Bench", version=__version__) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) loop_holder: dict = {} subscribers: set[WebSocket] = set() @app.on_event("startup") async def _capture_loop() -> None: loop_holder["loop"] = asyncio.get_running_loop() def broadcast(payload: dict) -> None: loop = loop_holder.get("loop") if loop is None: return def _publish() -> None: dead = [] for ws in list(subscribers): asyncio.ensure_future(_send(ws, payload, dead)) for ws in dead: subscribers.discard(ws) try: loop.call_soon_threadsafe(_publish) except RuntimeError: pass async def _send(ws: WebSocket, payload: dict, dead: list) -> None: try: await ws.send_json(payload) except Exception: # noqa: BLE001 dead.append(ws) jobs = JobManager(broadcast) # -- spec helpers -------------------------------------------------------- def current_spec(raw_override: dict | None = None): if raw_override is not None: return parse_workflow_dict(raw_override) return load_base_workflow(cfg.base_workflow_path) def build_plan(spec, overrides: dict): w, h = editor_resolution(spec) pd = spec_project_data(spec) ups = describe(spec)["upscale"] if "RTXVideoSuperResolution" in spec.stages else None ups_target = (ups or {}).get("target") or {} return build_render_plan( spec.widget(EDITOR_TYPE, "track_data", "{}"), width=int(overrides.get("width") or w), height=int(overrides.get("height") or h), frame_rate=overrides.get("frame_rate") or pd.get("frame_rate") or 24, steps=int(overrides.get("steps") or spec.widget("MoxieProject", "steps", 4)), seed=int(overrides.get("seed") or spec.widget("MoxieProject", "seed", 0) or 0), project_name=overrides.get("project_name") or spec.widget("MoxieProject", "project_name", "MoxieBench"), lora=overrides.get("lora") or spec.widget("LoraLoaderModelOnly", "lora_name"), lora_strength=float( overrides.get("lora_strength") or spec.widget("LoraLoaderModelOnly", "strength_model", 1.0) ), upscale_width=ups_target.get("width"), upscale_height=ups_target.get("height"), upscale_quality=(ups or {}).get("quality", "ULTRA"), codec=overrides.get("codec", "h264"), ) # -- core API ------------------------------------------------------------ @app.get("/api/health") def health(): return {"ok": True, "version": __version__, "backend": cfg.resolve_backend()} @app.get("/api/config") def config(): from .backends import available return { "version": __version__, "backend": cfg.resolve_backend(), "backends": available(cfg), "work_dir": str(cfg.work_dir), "outputs_dir": str(cfg.outputs_dir), } @app.get("/api/workflow") def get_workflow(): spec = current_spec() return { "summary": describe(spec), "track_data": spec.widget(EDITOR_TYPE, "track_data"), "project_data": spec_project_data(spec), } @app.get("/api/plan") def get_plan(overrides: str = "{}"): spec = current_spec() plan = build_plan(spec, json.loads(overrides or "{}")) return plan.summary() @app.post("/api/render") async def render(body: dict): try: spec = current_spec(body.get("workflow")) except WorkflowError as exc: raise HTTPException(400, str(exc)) from exc overrides = body.get("overrides") or {} # UI-submitted track data replaces the template's copy if body.get("track_data") is not None: editor = spec.stages[EDITOR_TYPE] editor.widget_values["track_data"] = ( body["track_data"] if isinstance(body["track_data"], str) else json.dumps(body["track_data"]) ) for key in ("resolution",): if overrides.get(key): editor = spec.stages[EDITOR_TYPE] editor.widget_values[key] = overrides[key] try: plan = build_plan(spec, overrides) except TrackDataError as exc: raise HTTPException(400, str(exc)) from exc backend = overrides.get("backend") or body.get("backend") or cfg.resolve_backend() job = jobs.create(backend, plan.summary()) def runner(active: Job, progress) -> dict: job_dir = cfg.outputs_dir / active.id return run_pipeline(spec, plan, active.backend, cfg, progress, job_dir) jobs.submit(job, runner) return {"job_id": job.id, "plan": plan.summary()} @app.get("/api/jobs") def list_jobs(): return {"jobs": jobs.list()} @app.get("/api/jobs/{job_id}") def get_job(job_id: str): job = jobs.get(job_id) if not job: raise HTTPException(404, "no such job") snap = job.snapshot() snap["events"] = job.events[-200:] return snap @app.delete("/api/jobs/{job_id}") def delete_job(job_id: str): job = jobs.get(job_id) if not job: raise HTTPException(404, "no such job") job.status = "deleted" return {"ok": True} # -- media ---------------------------------------------------------------- def _safe(name: str) -> str: name = Path(name).name if not name or name.startswith("."): raise HTTPException(400, "bad filename") return name @app.get("/api/view") def view(filename: str, type: str = "output", job: str | None = None): name = _safe(filename) if type == "input": path = cfg.inputs_dir / name elif type == "temp": path = cfg.temp_dir / name else: base = cfg.outputs_dir / _safe(job) if job else cfg.outputs_dir path = base / name if not path.is_file(): raise HTTPException(404, "no such file") media = _guess_media_type(path) return FileResponse(path, media_type=media) @app.post("/api/upload/image") async def upload_image(image: UploadFile = File(None), subfolder: str = Form("")): return await _save_upload(image, subfolder) @app.post("/api/easy-media/upload") async def easy_upload(file: UploadFile = File(None), subfolder: str = Form("")): return await _save_upload(file, subfolder) async def _save_upload(upload: UploadFile | None, subfolder: str) -> dict: if upload is None: raise HTTPException(400, "missing file") name = _safe(upload.filename or "upload.bin") target_dir = cfg.inputs_dir if subfolder: target_dir = target_dir / Path(subfolder).name target_dir.mkdir(parents=True, exist_ok=True) target = target_dir / name with open(target, "wb") as fh: shutil.copyfileobj(upload.file, fh) return {"name": name, "subfolder": str(target.parent.relative_to(cfg.inputs_dir)), "type": "input"} @app.post("/api/easy-media/download-url") async def download_url(body: dict): import urllib.request url = body.get("url") if not url: raise HTTPException(400, "missing url") name = _safe(body.get("name") or Path(url).name or "download.bin") dst = cfg.inputs_dir / name try: with urllib.request.urlopen(url, timeout=60) as resp, open(dst, "wb") as fh: shutil.copyfileobj(resp, fh) except Exception as exc: # noqa: BLE001 raise HTTPException(502, f"download failed: {exc}") from exc return {"name": name, "subfolder": "", "type": "input"} @app.get("/api/easy-media/media/list") def media_list(kind: str = "input"): base = cfg.inputs_dir if kind == "input" else cfg.outputs_dir items = [] for p in sorted(base.rglob("*")): if p.is_file() and not p.name.startswith("."): rel = str(p.relative_to(base).parent) items.append({ "name": p.name, "path": str(p.relative_to(base)), "subfolder": "" if rel == "." else rel, "type": "input" if kind == "input" else "output", "size": p.stat().st_size, }) return {"items": items} @app.get("/api/easy-media/media/recent") def media_recent(): return media_list("output") @app.get("/api/easy-media/projects") def easy_projects(): projects_dir = cfg.work_dir / "projects" projects_dir.mkdir(parents=True, exist_ok=True) return {"projects": sorted( p.stem for p in projects_dir.glob("*.json") if p.is_file())} @app.get("/api/easy-media/project") def easy_project(project_name: str): path = cfg.work_dir / "projects" / f"{Path(project_name).stem}.json" if not path.is_file(): raise HTTPException(404, "no such project") return JSONResponse(content=json.loads(path.read_text())) @app.get("/api/easy-media/prompt/system-prompts") def system_prompts(): return {"prompts": [ {"id": "default", "name": "Default", "prompt": ""}, ]} @app.post("/api/easy-media/video/source-fps") def source_fps(body: dict): name = _safe(body.get("filename") or "") path = cfg.inputs_dir / name if not path.is_file(): raise HTTPException(404, "no such file") try: out = subprocess.run( ["ffprobe", "-v", "quiet", "-print_format", "json", "-show_streams", str(path)], capture_output=True, text=True, timeout=30, ).stdout streams = json.loads(out).get("streams") or [] rate = (streams[0].get("r_frame_rate") or "24/1") if streams else "24/1" num, _, den = rate.partition("/") fps = float(num) / float(den or 1) except Exception as exc: # noqa: BLE001 raise HTTPException(500, f"ffprobe failed: {exc}") from exc return {"fps": fps} @app.post("/api/easy-media/video/preview-frame") def preview_frame(body: dict): name = _safe(body.get("filename") or "") path = cfg.inputs_dir / name if not path.is_file(): raise HTTPException(404, "no such file") t = float(body.get("time") or 0) try: out = subprocess.run( ["ffmpeg", "-v", "quiet", "-ss", str(t), "-i", str(path), "-frames:v", "1", "-f", "image2pipe", "-vcodec", "mjpeg", "pipe:1"], capture_output=True, timeout=60, ) except subprocess.TimeoutExpired as exc: raise HTTPException(504, "ffmpeg timeout") from exc if not out.stdout: raise HTTPException(500, "ffmpeg produced no frame") return Response(content=out.stdout, media_type="image/jpeg") # -- ComfyUI-compatible root aliases -------------------------------------- # The ported editor (and its media elements) call the exact ComfyUI paths: # /view, /upload/image, /easy-media/*. Register them alongside /api/* so # the UI shim needs no URL rewriting at all. app.add_api_route("/view", view, methods=["GET"], include_in_schema=False) app.add_api_route("/upload/image", _save_upload, methods=["POST"], include_in_schema=False) app.add_api_route("/easy-media/upload", _save_upload, methods=["POST"], include_in_schema=False) app.add_api_route("/easy-media/media/list", media_list, methods=["GET"], include_in_schema=False) app.add_api_route("/easy-media/media/recent", media_recent, methods=["GET"], include_in_schema=False) app.add_api_route("/easy-media/projects", easy_projects, methods=["GET"], include_in_schema=False) app.add_api_route("/easy-media/project", easy_project, methods=["GET"], include_in_schema=False) app.add_api_route("/easy-media/prompt/system-prompts", system_prompts, methods=["GET"], include_in_schema=False) app.add_api_route("/easy-media/video/source-fps", source_fps, methods=["POST"], include_in_schema=False) app.add_api_route("/easy-media/video/preview-frame", preview_frame, methods=["POST"], include_in_schema=False) app.add_api_route("/easy-media/download-url", download_url, methods=["POST"], include_in_schema=False) # fallthrough for endpoints the editor may call in v1 @app.api_route("/api/easy-media/{rest:path}", methods=["GET", "POST", "PATCH", "DELETE"]) def easy_fallback(rest: str): return JSONResponse( status_code=501, content={"error": f"bench does not implement /api/easy-media/{rest} yet"}, ) @app.api_route("/easy-media/{rest:path}", methods=["GET", "POST", "PATCH", "DELETE"]) def easy_fallback_root(rest: str): return easy_fallback(rest) # -- websocket ------------------------------------------------------------- @app.websocket("/api/ws") async def ws_endpoint(ws: WebSocket): await ws.accept() subscribers.add(ws) try: while True: await ws.receive_text() # keepalive pings from the UI except WebSocketDisconnect: pass finally: subscribers.discard(ws) # -- static UI -------------------------------------------------------------- ui_dist = cfg.ui_dist if (ui_dist / "index.html").is_file(): app.mount("/", StaticFiles(directory=str(ui_dist), html=True), name="ui") else: @app.get("/") def no_ui(): return JSONResponse({ "error": "bench UI not built", "hint": "cd frontend && bun run build:bench", }) # silence 404 noise for browsers requesting favicons @app.get("/favicon.ico") def favicon(): return Response(status_code=204) return app def _guess_media_type(path: Path) -> str: ext = path.suffix.lower() return { ".mp4": "video/mp4", ".webm": "video/webm", ".mov": "video/quicktime", ".mkv": "video/x-matroska", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp", ".gif": "image/gif", ".wav": "audio/wav", ".mp3": "audio/mpeg", ".flac": "audio/flac", ".srt": "text/plain", }.get(ext, "application/octet-stream") def main() -> None: # pragma: no cover - manual entry import uvicorn cfg = BenchConfig.from_args() uvicorn.run(create_app(cfg), host=cfg.host, port=cfg.port, log_level="info")