Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(model-trellis): persist task state to GCS, not just in-memory
nirholasa8500e8df2e2
diff --git a/workers/model-trellis/main.py b/workers/model-trellis/main.pyindex 0a4aa56d2..13adf8894 100644--- a/workers/model-trellis/main.py+++ b/workers/model-trellis/main.py@@ -27,6 +27,7 @@ from __future__ import annotationsimport asyncioimport base64import io+import jsonimport loggingimport osimport time@@ -43,6 +44,7 @@ from typing import Optionalimport torchfrom fastapi import FastAPI, HTTPException, Header, BackgroundTasks+from google.api_core.exceptions import NotFoundfrom google.cloud import storagefrom PIL import Imagefrom pydantic import BaseModel, Field@@ -70,6 +72,12 @@ _bucket: Optional[storage.Bucket] = None_sem: Optional[asyncio.Semaphore] = None_ready: Optional[asyncio.Event] = None_load_error: Optional[str] = None+# In-memory cache only — Cloud Run runs this service across up to _MAX_INSTANCES+# containers with no session affinity, so a POST /infer and a later GET+# /tasks/:id can land on different instances. The durable source of truth is+# the `tasks/{task_id}.json` blob in GCS (see _update_task / get_task); this+# dict just avoids a GCS round-trip when a poll happens to hit the same warm+# instance that ran the job._tasks: dict[str, dict] = {}@@ -144,24 +152,43 @@ def _decode_image(src: str) -> Image.Image:raise ValueError(f"unsupported image source: {src[:60]}")+def _task_blob(task_id: str):+ return _bucket.blob(f"tasks/{task_id}.json")+++async def _update_task(task_id: str, **fields) -> dict:+ """Merge `fields` into the task, then persist to GCS as the source of truth+ (see the comment on `_tasks`)."""+ task = _tasks.setdefault(task_id, {"task_id": task_id})+ task.update(fields)+ loop = asyncio.get_event_loop()+ await loop.run_in_executor(+ None,+ lambda: _task_blob(task_id).upload_from_string(+ json.dumps(task), content_type="application/json"+ ),+ )+ return task++async def _run_inference(task_id: str, images: list[str], body_type: str) -> None:# Wait for the background pipeline load before touching the GPU. Warm# instances pass instantly; a cold one waits out the load rather than# NoneType-crashing. A failed load surfaces as a designed task error.if _load_error:- _tasks[task_id].update({"status": "failed", "error": f"pipeline unavailable: {_load_error}"})+ await _update_task(task_id, status="failed", error=f"pipeline unavailable: {_load_error}")returntry:await asyncio.wait_for(_ready.wait(), timeout=600)except asyncio.TimeoutError:- _tasks[task_id].update({"status": "failed", "error": "pipeline not ready (model load timed out)"})+ await _update_task(task_id, status="failed", error="pipeline not ready (model load timed out)")returnif _load_error:- _tasks[task_id].update({"status": "failed", "error": f"pipeline unavailable: {_load_error}"})+ await _update_task(task_id, status="failed", error=f"pipeline unavailable: {_load_error}")returnasync with _sem:- _tasks[task_id]["status"] = "running"+ await _update_task(task_id, status="running")loop = asyncio.get_event_loop()t0 = time.time()try:@@ -199,19 +226,21 @@ async def _run_inference(task_id: str, images: list[str], body_type: str) -> Nongcs_url = f"https://storage.googleapis.com/{GCS_BUCKET}/{blob_name}"elapsed = time.time() - t0- _tasks[task_id].update({- "status": "done",- "result_gcs_url": gcs_url,- "elapsed_ms": int(elapsed * 1000),- })+ await _update_task(+ task_id,+ status="done",+ result_gcs_url=gcs_url,+ elapsed_ms=int(elapsed * 1000),+ )log.info("[%s] done in %.1fs — %d bytes → %s", task_id, elapsed, len(glb_bytes), gcs_url)except Exception as exc:- _tasks[task_id].update({- "status": "failed",- "error": safe_error(exc, context=f"[{task_id}] inference"),- "elapsed_ms": int((time.time() - t0) * 1000),- })+ await _update_task(+ task_id,+ status="failed",+ error=safe_error(exc, context=f"[{task_id}] inference"),+ elapsed_ms=int((time.time() - t0) * 1000),+ )class InferRequest(BaseModel):@@ -228,7 +257,10 @@ async def infer() -> dict:_require_api_key(authorization)task_id = str(uuid.uuid4())- _tasks[task_id] = {"task_id": task_id, "status": "queued", "model": "trellis-large"}+ # Persist the "queued" record before responding — a poll can reach a+ # different instance than this one the moment the 202 lands, and that+ # instance has nothing in its local `_tasks` dict to fall back on.+ await _update_task(task_id, status="queued", model="trellis-large")background_tasks.add_task(_run_inference, task_id, body.images, body.body_type)return {"task_id": task_id, "status": "queued"}@@ -237,8 +269,21 @@ async def infer(async def get_task(task_id: str, authorization: str = Header(...)) -> dict:_require_api_key(authorization)task = _tasks.get(task_id)- if task is None:+ if task is not None:+ return task+ # Not in this instance's local cache — fall back to the durable GCS+ # record, which every instance writes to on every state transition.+ loop = asyncio.get_event_loop()+ try:+ data = await loop.run_in_executor(None, _task_blob(task_id).download_as_bytes)+ except NotFound:raise HTTPException(status_code=404, detail="task not found")+ except Exception as exc:+ raise HTTPException(+ status_code=502, detail=safe_error(exc, context="task lookup")+ ) from exc+ task = json.loads(data)+ _tasks[task_id] = taskreturn task