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
feat(3d-quality): portrait realism cues + hunyuan3d reliability fixes for GPU deploy
nirholas400110334d4f
diff --git a/api/_mcp3d/text-to-image.js b/api/_mcp3d/text-to-image.jsindex 2ad89b340..c001ee0d2 100644--- a/api/_mcp3d/text-to-image.js+++ b/api/_mcp3d/text-to-image.js@@ -322,11 +322,32 @@ const ART_STYLE_RE = new RegExp(`\\b(?:${ART_STYLE_WORDS.join('|')})\\b`, 'i');const REALISM_SUFFIX =', photorealistic, true-to-life materials and surface detail, sharp focus, professional product photograph';+// A human/humanoid subject needs portrait-photography language, not+// product-photography language — "professional product photograph" reads+// wrong for a person and steers FLUX toward glossy-mannequin skin instead of+// the pores/asymmetry/texture that make a reconstructed avatar look like an+// actual IRL person. Matched as whole words so "personality" or "manatee"+// can't false-positive off "person"/"man".+const PERSON_SUBJECT_WORDS = [+ 'person', 'human', 'man', 'woman', 'guy', 'girl', 'boy', 'lady', 'gentleman',+ 'people', 'face', 'portrait', 'selfie', 'model(?!s? of)', 'character(?!\\s+prop)',+ 'warrior', 'knight', 'soldier', 'wizard', 'ninja', 'astronaut', 'pirate',+ 'king', 'queen', 'hero', 'villain', 'avatar',+];+const PERSON_SUBJECT_RE = new RegExp(`\\b(?:${PERSON_SUBJECT_WORDS.join('|')})\\b`, 'i');+const PERSON_REALISM_SUFFIX =+ ', photorealistic human, natural skin texture with visible pores and subtle asymmetry, ' ++ 'realistic hair strands, natural catchlight in the eyes, portrait photography, shot on a DSLR ' ++ 'with an 85mm lens, sharp focus';+export function enhanceFluxPrompt(raw) {const text = String(raw || '').trim();if (!text) return text;if (COMPOSITION_CUE_RE.test(text)) return text;- const realism = ART_STYLE_RE.test(text) ? '' : REALISM_SUFFIX;+ let realism = '';+ if (!ART_STYLE_RE.test(text)) {+ realism = PERSON_SUBJECT_RE.test(text) ? PERSON_REALISM_SUFFIX : REALISM_SUFFIX;+ }return text + realism + FLUX_STYLE_SUFFIX;}diff --git a/src/agent-screen.js b/src/agent-screen.jsindex 9232e0e58..c1cea774f 100644--- a/src/agent-screen.js+++ b/src/agent-screen.js@@ -2365,7 +2365,11 @@ async function boot(id) {} catch { /* best-effort broadcast — a non-owner viewer simply can't push */ }}async function pollForgeJob(jobId, narrate) {- const deadline = Date.now() + 180000;+ // Matches the 5-minute ceiling used by every other forge poll loop+ // (src/forge.js, home-forge.js, forge-studio/forge.js, scene-compose.js) —+ // higher-quality self-host inference (more diffusion steps, larger texture+ // bake) runs longer than the old 3-minute budget assumed.+ const deadline = Date.now() + 5 * 60 * 1000;let lastStatus = 'queued';while (Date.now() < deadline) {await new Promise((r) => setTimeout(r, 3000)); // real poll interval, not fake progressdiff --git a/workers/model-hunyuan3d/cloudbuild.yaml b/workers/model-hunyuan3d/cloudbuild.yamlindex 8fe4a907b..cb8afc410 100644--- a/workers/model-hunyuan3d/cloudbuild.yaml+++ b/workers/model-hunyuan3d/cloudbuild.yaml@@ -5,7 +5,11 @@ substitutions:_REPO: model-hunyuan3d_GCS_BUCKET: three-ws-avatar-reconstructions_WEIGHTS_BUCKET: three-ws-model-weights- _MIN_INSTANCES: "0"+ # min=1: model-trellis found a cold FUSE-mounted weight load (this tree is+ # 13.89 GiB, larger than TRELLIS's 3 GiB) can stall 15+ minutes instead of+ # loading in seconds — keeping one instance warm avoids paying that on every+ # idle-to-active transition. GCP credit budget covers the always-on GPU-hour.+ _MIN_INSTANCES: "1"_MAX_INSTANCES: "3"steps:diff --git a/workers/model-hunyuan3d/main.py b/workers/model-hunyuan3d/main.pyindex 642e47011..09c36376c 100644--- a/workers/model-hunyuan3d/main.py+++ b/workers/model-hunyuan3d/main.py@@ -29,6 +29,7 @@ from __future__ import annotationsimport asyncioimport base64import io+import jsonimport loggingimport osimport time@@ -38,6 +39,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@@ -63,9 +65,34 @@ MAX_CONCURRENT = int(os.environ.get("MAX_CONCURRENT", "1"))_pipeline = None_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 multiple instances+# 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. (Same pattern as workers/model-trellis.)_tasks: dict[str, dict] = {}+def _task_blob(task_id: str):+ return _bucket.blob(f"tasks/{task_id}.json")+++async def _update_task(task_id: str, **fields) -> dict:+ 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++def _load_pipeline():global _pipelinefrom hy3dgen.shapegen import Hunyuan3DDiTFlowMatchingPipeline@@ -93,14 +120,30 @@ def _load_pipeline():log.info("Hunyuan3D-2.1 pipelines loaded")+async def _load_pipeline_bg():+ """Load the ~10 GB pipeline off the request path and signal readiness when+ done. Runs in a worker thread so the event loop (and the HTTP port) stay+ live — a blocking lifespan load here would delay the port past Cloud Run's+ startup TCP-probe window and the revision would be marked failed."""+ global _load_error+ loop = asyncio.get_event_loop()+ try:+ await loop.run_in_executor(None, _load_pipeline)+ _ready.set()+ log.info("Hunyuan3D-2.1 pipeline ready")+ except Exception as exc: # noqa: BLE001 — surfaced via /health + task status+ _load_error = safe_error(exc, context="model load")+ log.error("Hunyuan3D-2.1 pipeline load FAILED: %s", exc)++@asynccontextmanagerasync def lifespan(app: FastAPI):- global _bucket, _sem+ global _bucket, _sem, _ready_bucket = storage.Client().bucket(GCS_BUCKET)_sem = asyncio.Semaphore(MAX_CONCURRENT)- loop = asyncio.get_event_loop()- await loop.run_in_executor(None, _load_pipeline)- log.info("Service ready — max_concurrent=%d", MAX_CONCURRENT)+ _ready = asyncio.Event()+ asyncio.create_task(_load_pipeline_bg())+ log.info("Service starting — pipeline loading in background (max_concurrent=%d)", MAX_CONCURRENT)yield@@ -130,24 +173,44 @@ def _decode_image(src: str) -> Image.Image: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:+ await _update_task(task_id, status="failed", error=f"pipeline unavailable: {_load_error}")+ return+ try:+ await asyncio.wait_for(_ready.wait(), timeout=900)+ except asyncio.TimeoutError:+ await _update_task(task_id, status="failed", error="pipeline not ready (model load timed out)")+ return+ if _load_error:+ await _update_task(task_id, status="failed", error=f"pipeline unavailable: {_load_error}")+ return+async with _sem:- _tasks[task_id]["status"] = "running"+ await _update_task(task_id, status="running")loop = asyncio.get_event_loop()t0 = time.time()try:img = await loop.run_in_executor(None, _decode_image, images[0])def _generate():+ # Quality over speed (GPU time is cheap against the platform's GCP+ # credit budget): 50/30 steps and a 384 octree resolution — well+ # above Hunyuan3D's own quickstart defaults (30/20 steps, 256+ # octree) — for materially sharper geometry and texture detail on+ # realistic human/object subjects.mesh = _pipeline["shape"](image=img,- num_inference_steps=30,+ num_inference_steps=50,guidance_scale=5.5,- octree_resolution=256,+ octree_resolution=384,)textured = _pipeline["texture"](mesh=mesh,image=img,- num_inference_steps=20,+ num_inference_steps=30,guidance_scale=7.5,)buf = io.BytesIO()@@ -165,19 +228,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):@@ -194,7 +259,10 @@ async def infer() -> dict:_require_api_key(authorization)task_id = str(uuid.uuid4())- _tasks[task_id] = {"task_id": task_id, "status": "queued", "model": "hunyuan3d-2.1"}+ # 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="hunyuan3d-2.1")background_tasks.add_task(_run_inference, task_id, body.images, body.body_type)return {"task_id": task_id, "status": "queued"}@@ -203,8 +271,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@@ -216,4 +297,6 @@ async def health() -> dict:"gpu_available": torch.cuda.is_available(),"gpu_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else None,"pipeline_loaded": _pipeline is not None,+ "ready": bool(_ready and _ready.is_set()),+ "load_error": _load_error,}