Skip to content

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.

45.8 KB
Raw Blame
<!doctype html>
<html lang="en">
	<head>
		<script>/* three.ws theme boot — no-flash; runtime in /theme-switcher.js */(function(){try{var m=localStorage.getItem('twx_theme');var l=m==='auto'?(window.matchMedia&&window.matchMedia('(prefers-color-scheme: light)').matches):m==='light';document.documentElement.setAttribute('data-theme',l?'light':'dark');}catch(e){document.documentElement.setAttribute('data-theme','dark');}})();</script>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title data-i18n="forge_nim.meta_title">Forge · NIM Self-Host · Image → 3D · three.ws</title>
		<meta
			name="description"
			content="Drive a self-hosted TRELLIS NIM (nvcr.io/nim/microsoft/trellis) directly. Drop a photo, hit /v1/infer, and get the textured GLB back synchronously as base64: rendered live and downloadable."
		 data-i18n-attr="content:forge_nim.meta_description"/>
		<meta property="og:title" content="Forge · Self-Hosted NIM · Image to 3D · three.ws"  data-i18n-attr="content:forge_nim.meta_og_title"/>
		<meta
			property="og:description"
			content="A forge-styled demo for a self-hosted TRELLIS NIM. Real photo in, synchronous GLB out of /v1/infer. No async poll, no mocks."
		 data-i18n-attr="content:forge_nim.meta_og_description"/>
		<meta property="og:type" content="website" />
		<meta property="og:site_name" content="three.ws" />
		<meta property="og:url" content="https://three.ws/forge-nim" />
		<meta property="og:image" content="https://three.ws/api/page-og?s=build&amp;t=Forge+%C2%B7+NIM%3A+Self-Hosted+Image+to+3D&amp;d=Drive+a+self-hosted+TRELLIS+NIM+%28nvcr.io%2Fnim%2Fmicrosoft%2Ftrellis%29+directly%3A+drop+a+photo%2C+hit+%2Fv1%2Finfer%2C+and+get+the+textured+GLB+back+synchronously+as+base64&amp;p=%2Fforge-nim" />
		<meta property="og:image:width" content="1200" />
		<meta property="og:image:height" content="630" />
		<meta name="twitter:card" content="summary_large_image" />
		<meta name="twitter:site" content="@trythreews" />
		<meta name="twitter:title" content="Forge · NIM Self-Host · Image → 3D · three.ws"  data-i18n-attr="content:forge_nim.meta_twitter_title"/>
		<meta
			name="twitter:description"
			content="Drive a self-hosted TRELLIS NIM directly: photo in, synchronous GLB out of /v1/infer."
		 data-i18n-attr="content:forge_nim.meta_twitter_description"/>
		<meta name="twitter:image" content="https://three.ws/api/page-og?s=build&amp;t=Forge+%C2%B7+NIM%3A+Self-Hosted+Image+to+3D&amp;d=Drive+a+self-hosted+TRELLIS+NIM+%28nvcr.io%2Fnim%2Fmicrosoft%2Ftrellis%29+directly%3A+drop+a+photo%2C+hit+%2Fv1%2Finfer%2C+and+get+the+textured+GLB+back+synchronously+as+base64&amp;p=%2Fforge-nim" />
		<meta name="theme-color" content="#080814" />
		<link rel="icon" href="/favicon.ico" sizes="any" />
		<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
		<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
		<link rel="manifest" href="/manifest.webmanifest" />
		<link rel="canonical" href="https://three.ws/forge-nim" />
		<link rel="stylesheet" href="/style.css" />
		<script src="/model-viewer-meshopt.js"></script>
		<script
			type="module"
			src="https://cdn.jsdelivr.net/npm/@google/[email protected]/dist/model-viewer.min.js"
			integrity="sha384-wjsPzEO+lXOf7d9csIDfAQpPt2OKuUU3m0K9qsdKIuzX4N8czSF6hdh23eAbZa3p"
			crossorigin="anonymous"
		></script>
		<style>
			.forge-main {
				max-width: none;
				margin: 0;
				padding: var(--space-md) clamp(var(--space-md), 4vw, var(--space-2xl)) var(--space-2xl);
			}
			/* Page header ------------------------------------------------- */
			.forge-head {
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				gap: var(--space-md);
				flex-wrap: wrap;
				margin-bottom: var(--space-lg);
			}
			.forge-head h1 {
				margin: 0 0 0.35rem;
				font-family: var(--font-display);
				font-size: clamp(1.4rem, 3.2vw, 2rem);
				line-height: 1.15;
				color: var(--ink);
			}
			.forge-head .forge-sub {
				margin: 0;
				max-width: 62ch;
				font-size: var(--text-sm);
				line-height: var(--leading-normal);
				color: var(--ink-dim);
			}
			.nim-status {
				display: inline-flex;
				align-items: center;
				gap: 0.45rem;
				flex-shrink: 0;
				border: 1px solid var(--stroke);
				border-radius: 999px;
				padding: 0.3rem 0.75rem;
				background: var(--surface-1);
				font-family: var(--font-mono);
				font-size: var(--text-2xs, 0.66rem);
				letter-spacing: 0.05em;
				text-transform: uppercase;
				color: var(--ink-dim);
			}
			.nim-status .dot {
				width: 7px;
				height: 7px;
				border-radius: 50%;
				background: var(--ink-dim);
				flex-shrink: 0;
			}
			.nim-status[data-state='checking'] .dot {
				animation: nim-pulse 1.1s ease-in-out infinite;
			}
			.nim-status[data-state='live'] {
				color: #76b900;
				border-color: rgba(118, 185, 0, 0.4);
			}
			.nim-status[data-state='live'] .dot {
				background: #76b900;
				box-shadow: 0 0 0 3px rgba(118, 185, 0, 0.18);
			}
			.nim-status[data-state='down'],
			.nim-status[data-state='off'] {
				color: var(--warning, #e0a93e);
				border-color: rgba(224, 169, 62, 0.4);
			}
			.nim-status[data-state='down'] .dot,
			.nim-status[data-state='off'] .dot {
				background: var(--warning, #e0a93e);
			}
			@keyframes nim-pulse {
				0%,
				100% {
					opacity: 1;
				}
				50% {
					opacity: 0.25;
				}
			}
			@media (prefers-reduced-motion: reduce) {
				.nim-status[data-state='checking'] .dot,
				.loader-ring,
				.spinner {
					animation: none;
				}
			}

			/* Workspace --------------------------------------------------- */
			.forge-workspace {
				display: grid;
				grid-template-columns: minmax(0, 1fr);
				gap: var(--space-lg);
				align-items: start;
			}
			@media (min-width: 1080px) {
				.forge-workspace {
					grid-template-columns: minmax(380px, 440px) minmax(0, 1fr);
					gap: var(--space-xl);
				}
				.forge-rail {
					position: sticky;
					top: 84px;
				}
			}
			.forge-stage {
				min-width: 0;
				display: flex;
				flex-direction: column;
				gap: var(--space-lg);
			}

			/* Composer ---------------------------------------------------- */
			.composer {
				background: var(--surface-glass);
				border: 1px solid var(--stroke-strong);
				border-radius: var(--radius-lg);
				padding: var(--space-md);
				box-shadow: 0 24px 60px -30px rgba(0, 0, 0, 0.8);
				display: flex;
				flex-direction: column;
				gap: var(--space-md);
			}
			.mode-switch {
				display: inline-flex;
				gap: 2px;
				background: var(--surface-1);
				border: 1px solid var(--stroke);
				border-radius: var(--radius-md);
				padding: 2px;
				width: 100%;
			}
			.mode-switch button {
				flex: 1;
				background: transparent;
				border: none;
				color: var(--ink-dim);
				font-family: var(--font-display);
				font-weight: 600;
				font-size: var(--text-sm);
				padding: 0.5rem 0.6rem;
				border-radius: var(--radius-sm);
				cursor: pointer;
				transition: background var(--duration-fast), color var(--duration-fast);
			}
			.mode-switch button:hover {
				color: var(--ink);
			}
			.mode-switch button[aria-selected='true'] {
				background: var(--surface-3);
				color: var(--ink);
			}
			.pane {
				display: none;
			}
			.pane.active {
				display: block;
			}

			/* Dropzone ---------------------------------------------------- */
			.dropzone {
				position: relative;
				border: 1.5px dashed var(--stroke-strong);
				border-radius: var(--radius-md);
				background: var(--surface-1);
				min-height: 200px;
				display: grid;
				place-items: center;
				text-align: center;
				padding: var(--space-lg);
				cursor: pointer;
				transition: border-color var(--duration-fast), background var(--duration-fast);
				color: var(--ink-dim);
			}
			.dropzone:hover,
			.dropzone:focus-visible {
				border-color: #76b900;
				background: var(--surface-2);
				outline: none;
			}
			.dropzone.dragover {
				border-color: #76b900;
				background: rgba(118, 185, 0, 0.06);
			}
			.dropzone .dz-inner {
				display: flex;
				flex-direction: column;
				align-items: center;
				gap: 0.45rem;
				pointer-events: none;
			}
			.dropzone svg {
				color: var(--ink-dim);
			}
			.dropzone .dz-title {
				color: var(--ink);
				font-weight: 600;
				font-size: var(--text-sm);
			}
			.dropzone .dz-hint {
				font-size: var(--text-xs);
				color: var(--ink-dim);
			}
			.dropzone img.dz-preview {
				position: absolute;
				inset: 4px;
				width: calc(100% - 8px);
				height: calc(100% - 8px);
				object-fit: contain;
				border-radius: calc(var(--radius-md) - 3px);
				background: var(--surface-0, #0b0b12);
			}
			.dz-clear {
				position: absolute;
				top: 8px;
				right: 8px;
				z-index: 2;
				width: 26px;
				height: 26px;
				border-radius: 50%;
				border: 1px solid var(--stroke-strong);
				background: rgba(10, 10, 16, 0.72);
				color: var(--ink);
				cursor: pointer;
				display: none;
				align-items: center;
				justify-content: center;
				font-size: 14px;
				line-height: 1;
			}
			.dz-clear:hover {
				background: rgba(10, 10, 16, 0.92);
			}
			.dropzone.has-image .dz-clear {
				display: inline-flex;
			}
			.dropzone.has-image .dz-inner {
				display: none;
			}

			.composer textarea {
				width: 100%;
				min-height: 120px;
				resize: vertical;
				background: var(--surface-1);
				border: 1px solid var(--stroke);
				border-radius: var(--radius-md);
				padding: 0.7rem 0.8rem;
				outline: none;
				color: var(--ink);
				font-family: var(--font-body);
				font-size: var(--text-base);
				line-height: var(--leading-normal);
			}
			.composer textarea:focus {
				border-color: #76b900;
			}
			.composer textarea::placeholder {
				color: var(--ink-dim);
			}

			.field-label {
				display: block;
				font-family: var(--font-mono);
				font-size: var(--text-2xs, 0.66rem);
				letter-spacing: 0.06em;
				text-transform: uppercase;
				color: var(--ink-dim);
				margin-bottom: 0.4rem;
			}
			.tier {
				display: inline-flex;
				gap: 2px;
				background: var(--surface-1);
				border: 1px solid var(--stroke);
				border-radius: var(--radius-md);
				padding: 2px;
			}
			.tier button {
				background: transparent;
				border: none;
				color: var(--ink-dim);
				font-family: var(--font-mono);
				font-size: var(--text-xs);
				padding: 0.35rem 0.7rem;
				border-radius: var(--radius-sm);
				cursor: pointer;
				transition: background var(--duration-fast), color var(--duration-fast);
			}
			.tier button:hover {
				color: var(--ink);
			}
			.tier button[aria-pressed='true'] {
				background: var(--surface-3);
				color: var(--ink);
			}
			.composer-row {
				display: flex;
				align-items: flex-end;
				justify-content: space-between;
				gap: var(--space-sm);
				flex-wrap: wrap;
			}

			/* Buttons ----------------------------------------------------- */
			.btn {
				display: inline-flex;
				align-items: center;
				gap: 0.5rem;
				font-family: var(--font-display);
				font-weight: 600;
				font-size: var(--text-base);
				color: #0b0b0b;
				background: var(--accent);
				border: 1px solid var(--accent);
				border-radius: var(--radius-md);
				padding: 0.6rem 1.2rem;
				cursor: pointer;
				transition: transform var(--duration-fast) var(--ease-standard), opacity var(--duration-fast), box-shadow var(--duration-fast);
			}
			.btn:hover {
				box-shadow: 0 0 0 4px var(--accent-soft);
			}
			.btn:active {
				transform: translateY(1px);
			}
			.btn[disabled] {
				opacity: 0.45;
				cursor: not-allowed;
			}
			.btn-ghost {
				background: transparent;
				color: var(--ink);
				border: 1px solid var(--stroke-strong);
			}
			.btn-ghost:hover {
				background: var(--surface-2);
				box-shadow: none;
			}
			#generate {
				width: 100%;
				justify-content: center;
				font-size: var(--text-lg);
				padding: 0.8rem 1.2rem;
				background: #76b900;
				border-color: #76b900;
				color: #07120a;
			}
			#generate:hover:not([disabled]) {
				box-shadow: 0 0 0 4px rgba(118, 185, 0, 0.25), 0 10px 30px -10px rgba(118, 185, 0, 0.5);
			}
			.spinner {
				width: 16px;
				height: 16px;
				border-radius: 50%;
				border: 2px solid rgba(7, 18, 10, 0.35);
				border-top-color: #07120a;
				animation: nim-spin 0.7s linear infinite;
			}
			@keyframes nim-spin {
				to {
					transform: rotate(360deg);
				}
			}

			/* Advanced / endpoint ----------------------------------------- */
			details.advanced {
				border-top: 1px solid var(--stroke);
				padding-top: var(--space-sm);
			}
			details.advanced summary {
				cursor: pointer;
				font-family: var(--font-mono);
				font-size: var(--text-xs);
				color: var(--ink-dim);
				list-style: none;
			}
			details.advanced summary::-webkit-details-marker {
				display: none;
			}
			details.advanced summary::before {
				content: '▸ ';
			}
			details.advanced[open] summary::before {
				content: '▾ ';
			}
			details.advanced input {
				width: 100%;
				margin-top: 0.5rem;
				background: var(--surface-1);
				border: 1px solid var(--stroke);
				border-radius: var(--radius-md);
				padding: 0.55rem 0.7rem;
				color: var(--ink);
				font-family: var(--font-mono);
				font-size: var(--text-xs);
				outline: none;
			}
			details.advanced input:focus {
				border-color: #76b900;
			}
			.contract {
				font-family: var(--font-mono);
				font-size: var(--text-2xs, 0.66rem);
				line-height: 1.6;
				color: var(--ink-dim);
				background: var(--surface-0, #0b0b12);
				border: 1px solid var(--stroke);
				border-radius: var(--radius-md);
				padding: 0.7rem 0.8rem;
				overflow-x: auto;
				white-space: pre;
			}
			.contract .k {
				color: #76b900;
			}

			/* Stage ------------------------------------------------------- */
			.stage {
				background: var(--surface-glass);
				border: 1px solid var(--stroke-strong);
				border-radius: var(--radius-lg);
				min-height: 460px;
				position: relative;
				overflow: hidden;
				display: flex;
				flex-direction: column;
			}
			.stage-view {
				position: relative;
				flex: 1;
				min-height: 420px;
				background: radial-gradient(120% 120% at 50% 18%, #141b2e 0%, #0b0e18 55%, #05060c 100%);
			}
			/* Absolute fill, not height:100%. `.stage-view` is a flex item with an auto
			   specified height, so a percentage height on this child resolves to auto:
			   the viewer collapsed to 0px, and model-viewer's own visibility observer
			   then never loaded the GLB, so a finished reconstruction rendered nothing. */
			model-viewer {
				position: absolute;
				inset: 0;
				width: 100%;
				height: 100%;
				--poster-color: transparent;
				background: transparent;
			}
			.stage-state {
				position: absolute;
				inset: 0;
				display: none;
				place-items: center;
				text-align: center;
				padding: var(--space-xl);
			}
			.stage-state.show {
				display: grid;
			}
			.state-inner {
				max-width: 360px;
				display: flex;
				flex-direction: column;
				align-items: center;
				gap: 0.7rem;
				color: var(--ink-dim);
			}
			.state-inner h3 {
				margin: 0;
				color: var(--ink);
				font-family: var(--font-display);
				font-size: var(--text-lg);
			}
			.state-inner p {
				margin: 0;
				font-size: var(--text-sm);
				line-height: var(--leading-normal);
			}
			.state-icon {
				width: 46px;
				height: 46px;
				color: var(--ink-dim);
				opacity: 0.8;
			}
			#state-error .state-icon {
				color: var(--warning, #e0a93e);
			}
			/* Loading */
			.loader-ring {
				width: 54px;
				height: 54px;
				border-radius: 50%;
				border: 3px solid rgba(118, 185, 0, 0.18);
				border-top-color: #76b900;
				animation: nim-spin 0.9s linear infinite;
			}
			.elapsed {
				font-family: var(--font-mono);
				font-size: var(--text-xs);
				color: var(--ink-dim);
			}
			.loading-steps {
				font-family: var(--font-mono);
				font-size: var(--text-2xs, 0.66rem);
				color: var(--ink-faint, #6a6a78);
				min-height: 1.2em;
			}

			/* Result bar -------------------------------------------------- */
			.result-bar {
				display: flex;
				align-items: center;
				gap: var(--space-sm);
				flex-wrap: wrap;
				padding: var(--space-sm) var(--space-md);
				border-top: 1px solid var(--stroke);
				background: var(--surface-1);
			}
			.result-bar .meta {
				display: inline-flex;
				gap: 0.45rem;
				align-items: center;
				flex-wrap: wrap;
				font-family: var(--font-mono);
				font-size: var(--text-2xs, 0.66rem);
				color: var(--ink-dim);
			}
			.result-bar .chip {
				border: 1px solid var(--stroke);
				border-radius: 999px;
				padding: 0.15rem 0.5rem;
				white-space: nowrap;
			}
			.result-bar .chip.ok {
				color: #76b900;
				border-color: rgba(118, 185, 0, 0.4);
			}
			.result-bar .spacer {
				flex: 1;
			}
			.result-bar.is-hidden {
				display: none;
			}

			/* Reference strip under the stage ----------------------------- */
			.note {
				font-size: var(--text-xs);
				color: var(--ink-dim);
				line-height: var(--leading-normal);
			}
			.note a {
				color: var(--ink);
			}
			.sample-row {
				display: flex;
				gap: 0.5rem;
				flex-wrap: wrap;
				margin-top: 0.5rem;
			}
			.sample-row button {
				background: var(--surface-1);
				border: 1px solid var(--stroke);
				border-radius: var(--radius-sm);
				color: var(--ink-dim);
				font-family: var(--font-mono);
				font-size: var(--text-2xs, 0.66rem);
				padding: 0.3rem 0.55rem;
				cursor: pointer;
			}
			.sample-row button:hover {
				color: var(--ink);
				border-color: var(--stroke-strong);
			}

			/* Unconfigured state ------------------------------------------ */
			.state-actions {
				display: flex;
				gap: 0.5rem;
				flex-wrap: wrap;
				justify-content: center;
				margin-top: 0.3rem;
			}
			.state-links {
				display: flex;
				gap: 0.9rem;
				flex-wrap: wrap;
				justify-content: center;
				font-size: var(--text-xs);
			}
			.state-links a {
				color: var(--ink-dim);
				text-decoration: underline;
				text-underline-offset: 3px;
			}
			.state-links a:hover,
			.state-links a:focus-visible {
				color: var(--ink);
			}
			.state-inner code {
				font-family: var(--font-mono);
				font-size: 0.92em;
				color: var(--ink);
			}
			/* Every focusable control on this page gets the same visible ring. */
			.forge-page :is(button, a, input, textarea, summary, [tabindex]):focus-visible {
				outline: 2px solid #76b900;
				outline-offset: 2px;
				border-radius: var(--radius-sm);
			}
		</style>
	
	<meta name="robots" content="index, follow, max-image-preview:large, max-snippet:-1">
	<meta property="og:locale" content="en_US">
	<meta property="og:image:alt" content="Forge · NIM: Self-Hosted Image to 3D · three.ws">
	<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"WebPage","name":"Forge · NIM - Self-Hosted Image to 3D","description":"Drive a self-hosted TRELLIS NIM (nvcr.io/nim/microsoft/trellis) directly: drop a photo, hit /v1/infer, and get the textured GLB back synchronously as base64 - rendered live and downloadable.","url":"https://three.ws/forge-nim","isPartOf":{"@type":"WebSite","name":"three.ws","url":"https://three.ws"},"primaryImageOfPage":"https://three.ws/api/page-og?s=build&t=Forge+%C2%B7+NIM+-+Self-Hosted+Image+to+3D&d=Drive+a+self-hosted+TRELLIS+NIM+%28nvcr.io%2Fnim%2Fmicrosoft%2Ftrellis%29+directly%3A+drop+a+photo%2C+hit+%2Fv1%2Finfer%2C+and+get+the+textured+GLB+back+synchronously+as+base64+-+r&p=%2Fforge-nim"}]}</script>
</head>
	<body class="forge-page">
		<div id="nav-container"></div>

		<main class="forge-main">
			<header class="forge-head">
				<div>
					<h1 data-i18n="forge_nim.h1">Self-hosted TRELLIS NIM</h1>
					<p class="forge-sub" data-i18n-html="forge_nim.subtitle">
						Drop a photo, POST it once to <code>/v1/infer</code>, and a textured GLB comes back synchronously as
						base64: rendered live here, downloadable, AR-ready. No async poll, no storage round trip.
					</p>
				</div>
				<div class="nim-status" id="nim-status" data-state="checking" role="status" aria-live="polite">
					<span class="dot" aria-hidden="true"></span>
					<span id="nim-status-text" data-i18n="forge_nim.status_checking">Checking NIM</span>
				</div>
			</header>

			<div class="forge-workspace">
				<!-- Composer rail -->
				<div class="forge-rail">
					<form class="composer" id="composer" autocomplete="off">
						<div class="mode-switch" role="tablist" aria-label="Generation mode">
							<button type="button" role="tab" id="tab-image" data-mode="image" aria-selected="true" data-i18n="forge_nim.from_a_photo">
								From a photo
							</button>
							<button type="button" role="tab" id="tab-text" data-mode="text" aria-selected="false" data-i18n="forge_nim.from_text">
								From text
							</button>
						</div>

						<!-- Image pane -->
						<div class="pane active" id="pane-image">
							<div
								class="dropzone"
								id="dropzone"
								role="button"
								tabindex="0"
								aria-label="Drop a reference image, click to choose, or paste"
							>
								<button type="button" class="dz-clear" id="dz-clear" aria-label="Remove image" data-i18n-attr="aria-label:forge_nim.remove_image_arialabel">✕</button>
								<img class="dz-preview" id="dz-preview" alt="" hidden />
								<div class="dz-inner">
									<svg width="34" height="34" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
										<rect x="3" y="3" width="18" height="18" rx="2" />
										<circle cx="9" cy="9" r="2" />
										<path d="m21 15-3.6-3.6a2 2 0 0 0-2.8 0L6 20" />
									</svg>
									<span class="dz-title" data-i18n="forge_nim.drop_a_photo">Drop a photo</span>
									<span class="dz-hint" data-i18n="forge_nim.click_to_browse_or_paste_png_jpg">click to browse · or paste · PNG/JPG/WebP, ≤ 10 MB</span>
								</div>
								<input type="file" id="file-input" accept="image/*" hidden />
							</div>
						</div>

						<!-- Text pane -->
						<div class="pane" id="pane-text">
							<label class="field-label" for="prompt" data-i18n="forge_nim.describe_one_object">Describe one object</label>
							<textarea
								id="prompt"
								placeholder="a worn brass pocket watch, studio lighting"
								maxlength="120"
							></textarea>
						</div>

						<div class="composer-row">
							<div>
								<label class="field-label" data-i18n="forge_nim.quality">Quality</label>
								<div class="tier" role="group" aria-label="Sampling quality">
									<button type="button" data-tier="draft" aria-pressed="true" data-i18n="forge_nim.draft_15_steps">Draft · 15 steps</button>
									<button type="button" data-tier="high" aria-pressed="false" data-i18n="forge_nim.high_40_steps">High · 40 steps</button>
								</div>
							</div>
						</div>

						<!-- Both labels live in the markup and toggle with `hidden`. The button
						     itself carries no data-i18n-html: that key held the label's own
						     markup, so translating it replaced this subtree and detached the
						     node the script had captured, leaving the label frozen and the
						     in-flight spinner invisible. -->
						<button class="btn" id="generate" type="submit">
							<span class="spinner" id="generate-spinner" hidden aria-hidden="true"></span>
							<span id="label-image" data-i18n="forge_nim.label_reconstruct">Reconstruct 3D</span>
							<span id="label-text" hidden data-i18n="forge_nim.label_generate">Generate 3D</span>
						</button>

						<details class="advanced">
							<summary data-i18n="forge_nim.nim_endpoint_wire_contract">NIM endpoint &amp; wire contract</summary>
							<input
								type="url"
								id="base-url"
								placeholder="https://your-nim-host  (defaults to NIM_TRELLIS_URL)"
								spellcheck="false"
								autocapitalize="off"
							/>
							<div class="contract" aria-hidden="true">POST <span class="k" data-i18n="forge_nim.baseurl">{baseUrl}</span>/v1/infer
{ <span class="k" data-i18n="forge_nim.mode">mode</span>: "image", <span class="k" data-i18n="forge_nim.image">image</span>: "data:…",
  ss_sampling_steps, slat_sampling_steps,
  <span class="k" data-i18n="forge_nim.output_format">output_format</span>: "glb" }
→ 200 { <span class="k" data-i18n="forge_nim.artifacts">artifacts</span>: [ { <span class="k" data-i18n="forge_nim.base64">base64</span> } ] }</div>
						</details>
					</form>
				</div>

				<!-- Stage -->
				<div class="forge-stage">
					<div class="stage">
						<div class="stage-view" id="stage-view">
							<model-viewer
								id="viewer"
								camera-controls
								auto-rotate
								auto-rotate-delay="0"
								rotation-per-second="22deg"
								shadow-intensity="1.0"
								shadow-softness="0.9"
								exposure="1.25"
								tone-mapping="aces"
								interaction-prompt="none"
								ar
								ar-modes="webxr scene-viewer quick-look"
								crossorigin="anonymous"
								alt="Reconstructed 3D model"
								style="display: none"
							></model-viewer>

							<!-- Idle -->
							<div class="stage-state show" id="state-idle">
								<div class="state-inner">
									<svg class="state-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
										<path d="M12 2 2 7l10 5 10-5-10-5Z" />
										<path d="m2 17 10 5 10-5" />
										<path d="m2 12 10 5 10-5" />
									</svg>
									<h3 data-i18n="forge_nim.drop_a_photo_to_begin">Drop a photo to begin</h3>
									<p data-i18n="forge_nim.one_clear_subject_plain_background_the_self">One clear subject, plain background. The self-hosted NIM reconstructs a textured GLB in a single hop, then orbit, AR, or download it here.</p>
								</div>
							</div>

							<!-- No endpoint wired -->
							<div class="stage-state" id="state-unconfigured">
								<div class="state-inner">
									<svg class="state-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
										<rect x="2" y="3" width="20" height="7" rx="2" />
										<rect x="2" y="14" width="20" height="7" rx="2" />
										<line x1="6" y1="6.5" x2="6.01" y2="6.5" />
										<line x1="6" y1="17.5" x2="6.01" y2="17.5" />
									</svg>
									<h3 data-i18n="forge_nim.unconfigured_title">Point this at your NIM</h3>
									<p id="unconfigured-msg" data-i18n-html="forge_nim.unconfigured_body">
										This page drives a TRELLIS NIM you host yourself, and no endpoint is wired to this
										deployment. Paste the base URL of your own <code>nvcr.io/nim/microsoft/trellis</code>
										container to reconstruct right here, or set <code>NIM_TRELLIS_URL</code> on the deployment
										to wire it for everyone.
									</p>
									<div class="state-actions">
										<button type="button" class="btn btn-ghost" id="set-endpoint" data-i18n="forge_nim.set_endpoint">Set the endpoint</button>
										<button type="button" class="btn btn-ghost" id="retry-health" hidden data-i18n="forge_nim.retry_check">Retry check</button>
									</div>
									<div class="state-links">
										<a href="/forge" data-i18n="forge_nim.link_hosted_forge">Use the hosted forge instead</a>
										<a href="/docs/nvidia-models" data-i18n="forge_nim.link_nvidia_docs">NVIDIA model docs</a>
									</div>
								</div>
							</div>

							<!-- Loading -->
							<div class="stage-state" id="state-loading" role="status" aria-live="polite">
								<div class="state-inner">
									<div class="loader-ring"></div>
									<h3 id="loading-title" data-i18n="forge_nim.reconstructing">Reconstructing…</h3>
									<p class="loading-steps" id="loading-steps" data-i18n="forge_nim.post_v1_infer">POST /v1/infer</p>
									<div class="elapsed" id="elapsed">0.0s</div>
									<button type="button" class="btn btn-ghost" id="cancel" style="margin-top: 0.4rem" data-i18n="forge_nim.cancel">Cancel</button>
								</div>
							</div>

							<!-- Error -->
							<div class="stage-state" id="state-error" role="alert">
								<div class="state-inner">
									<svg class="state-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
										<path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0Z" />
										<line x1="12" y1="9" x2="12" y2="13" />
										<line x1="12" y1="17" x2="12.01" y2="17" />
									</svg>
									<h3 id="error-title" data-i18n="forge_nim.couldn_t_reconstruct">Couldn't reconstruct</h3>
									<p id="error-msg" data-i18n="forge_nim.something_went_wrong">Something went wrong.</p>
									<button type="button" class="btn btn-ghost" id="error-retry" style="margin-top: 0.4rem" data-i18n="forge_nim.try_again">Try again</button>
								</div>
							</div>
						</div>

						<div class="result-bar is-hidden" id="result-bar">
							<div class="meta" id="result-meta"></div>
							<span class="spacer"></span>
							<a class="btn btn-ghost" id="download" download="trellis-nim.glb" hidden style="font-size: var(--text-sm); padding: 0.45rem 0.9rem" data-i18n="forge_nim.download_glb">Download GLB</a>
						</div>
					</div>

					<p class="note" data-i18n-html="forge_nim.try_a_text_prompt">
						<span class="sample-row" id="sample-row">
							<button type="button" data-sample="text" data-prompt="a worn brass pocket watch, studio lighting">Try a text prompt</button>
						</span>
					</p>
				</div>
			</div>
		</main>

		<script src="/nav.js" defer></script>
		<script src="/footer.js" defer></script>
		<script type="module">
			const $ = (id) => document.getElementById(id);

			const els = {
				status: $('nim-status'),
				statusText: $('nim-status-text'),
				composer: $('composer'),
				tabImage: $('tab-image'),
				tabText: $('tab-text'),
				paneImage: $('pane-image'),
				paneText: $('pane-text'),
				dropzone: $('dropzone'),
				fileInput: $('file-input'),
				preview: $('dz-preview'),
				dzClear: $('dz-clear'),
				prompt: $('prompt'),
				baseUrl: $('base-url'),
				generate: $('generate'),
				labelImage: $('label-image'),
				labelText: $('label-text'),
				spinner: $('generate-spinner'),
				viewer: $('viewer'),
				stateIdle: $('state-idle'),
				stateLoading: $('state-loading'),
				stateError: $('state-error'),
				stateUnconfigured: $('state-unconfigured'),
				unconfiguredMsg: $('unconfigured-msg'),
				setEndpoint: $('set-endpoint'),
				retryHealth: $('retry-health'),
				advanced: document.querySelector('details.advanced'),
				errorTitle: $('error-title'),
				errorMsg: $('error-msg'),
				errorRetry: $('error-retry'),
				loadingSteps: $('loading-steps'),
				elapsed: $('elapsed'),
				cancel: $('cancel'),
				resultBar: $('result-bar'),
				resultMeta: $('result-meta'),
				download: $('download'),
				sampleRow: $('sample-row'),
			};

			const state = {
				mode: 'image',
				tier: 'draft',
				imageDataUri: null,
				objectUrl: null,
				abort: null,
				elapsedTimer: null,
				stepTimer: null,
				// hasEndpoint gates the whole page: with no NIM wired to the deployment and
				// no override typed, every POST is a guaranteed 503, so the composer stays
				// disabled and the stage explains how to point it at a NIM instead.
				hasEndpoint: false,
				healthSeq: 0,
				stage: 'idle',
			};

			// ── Health ────────────────────────────────────────────────────
			// Ask the API whether a NIM is wired (NIM_TRELLIS_URL) or, when the operator
			// typed one, whether THAT box answers /v1/health/ready. Late replies from a
			// superseded check are dropped so a slow probe can't overwrite a newer verdict.
			async function checkHealth(overrideUrl) {
				const seq = ++state.healthSeq;
				const override = (overrideUrl || '').trim();
				setStatus('checking', override ? 'Probing endpoint' : 'Checking NIM');
				let d;
				try {
					const qs = override ? `&baseUrl=${encodeURIComponent(override)}` : '';
					const r = await fetch(`/api/forge-nim?action=health${qs}`, { headers: { accept: 'application/json' } });
					d = await r.json();
				} catch {
					if (seq !== state.healthSeq) return;
					// The health probe itself failed to answer. An override is still usable:
					// only the deployment default is unknown, so do not lock the composer.
					state.hasEndpoint = Boolean(override);
					setStatus('down', 'Status unavailable');
					setUnconfiguredCopy(
						'The status check could not reach this site’s API, so whether a NIM is wired here is unknown. Retry the check, or paste the base URL of your own TRELLIS NIM to run against it directly.',
						true,
					);
					applyEndpointState();
					return;
				}
				if (seq !== state.healthSeq) return;

				// A rejected override answers from the env instead of the box, and says so
				// via `reason` with no masked baseUrl echoed back.
				// A rejected override is a typo the user has to fix: the composer would
				// otherwise POST that same bad URL and can only ever get a 400 back.
				const overrideRejected = Boolean(override) && !d.baseUrl;
				state.hasEndpoint = overrideRejected ? false : Boolean(override) || Boolean(d.configured);
				setUnconfiguredCopy(
					overrideRejected ? `That endpoint was rejected: ${d.reason || 'it must be a public https URL.'}` : null,
					false,
				);

				if (overrideRejected) {
					setStatus('down', 'Endpoint rejected');
				} else if (!d.configured && !override) {
					setStatus('off', 'No NIM wired');
				} else if (d.reachable) {
					setStatus('live', override ? 'Endpoint ready' : 'NIM ready');
				} else {
					// A NIM behind a gateway may refuse /v1/health/ready yet serve /v1/infer,
					// so unreachable warns without locking the composer.
					setStatus('down', override ? 'Ready check failed' : 'NIM unreachable');
				}
				applyEndpointState();
			}

			// The setup card explains the ONE thing that is actually wrong. Its default is
			// "no NIM is wired here"; a failed status check is a different problem with a
			// different fix, so that case swaps the copy and offers a retry.
			const unconfiguredDefaultHtml = els.unconfiguredMsg.innerHTML;
			function setUnconfiguredCopy(text, offerRetry) {
				if (text) {
					els.unconfiguredMsg.textContent = text;
					els.unconfiguredMsg.dataset.i18nOwned = '1';
				} else {
					els.unconfiguredMsg.innerHTML = unconfiguredDefaultHtml;
					delete els.unconfiguredMsg.dataset.i18nOwned;
				}
				els.retryHealth.hidden = !offerRetry;
			}
			els.retryHealth.addEventListener('click', () => checkHealth(els.baseUrl.value));

			function setStatus(s, text) {
				els.status.dataset.state = s;
				els.statusText.textContent = text;
				// The pill reports live state from here on; claim it so a locale switch
				// cannot restore the boot label over a real verdict.
				els.statusText.dataset.i18nOwned = '1';
			}

			// Reflect endpoint availability in the composer and the stage. Only run while
			// the stage is idle so it never stomps a result, an in-flight run, or an error.
			function applyEndpointState() {
				updateGenerateEnabled();
				if (state.stage === 'idle' || state.stage === 'unconfigured') {
					showState(state.hasEndpoint ? 'idle' : 'unconfigured');
				}
			}

			// ── Mode switch ───────────────────────────────────────────────
			function setMode(mode) {
				state.mode = mode;
				const isImage = mode === 'image';
				els.tabImage.setAttribute('aria-selected', String(isImage));
				els.tabText.setAttribute('aria-selected', String(!isImage));
				els.paneImage.classList.toggle('active', isImage);
				els.paneText.classList.toggle('active', !isImage);
				setBusy(false);
				updateGenerateEnabled();
			}
			// The button shows exactly one of: the spinner, the photo label, the text label.
			// Toggling pre-translated nodes keeps every state localized without the script
			// ever writing copy of its own.
			function setBusy(busy) {
				els.spinner.hidden = !busy;
				els.labelImage.hidden = busy || state.mode !== 'image';
				els.labelText.hidden = busy || state.mode !== 'text';
			}
			els.tabImage.addEventListener('click', () => setMode('image'));
			els.tabText.addEventListener('click', () => setMode('text'));

			// ── Tier ──────────────────────────────────────────────────────
			document.querySelectorAll('.tier button').forEach((b) => {
				b.addEventListener('click', () => {
					state.tier = b.dataset.tier;
					document.querySelectorAll('.tier button').forEach((x) =>
						x.setAttribute('aria-pressed', String(x === b)),
					);
				});
			});

			// ── Dropzone ──────────────────────────────────────────────────
			function loadFile(file) {
				if (!file || !file.type.startsWith('image/')) return;
				if (file.size > 10 * 1024 * 1024) {
					showError('Image too large', 'Pick an image under 10 MB.');
					return;
				}
				const reader = new FileReader();
				reader.onload = () => {
					state.imageDataUri = reader.result;
					els.preview.src = reader.result;
					els.preview.hidden = false;
					els.dropzone.classList.add('has-image');
					updateGenerateEnabled();
				};
				reader.readAsDataURL(file);
			}
			function clearImage() {
				state.imageDataUri = null;
				els.preview.src = '';
				els.preview.hidden = true;
				els.fileInput.value = '';
				els.dropzone.classList.remove('has-image');
				updateGenerateEnabled();
			}
			els.dropzone.addEventListener('click', (e) => {
				if (e.target === els.dzClear) return;
				els.fileInput.click();
			});
			els.dropzone.addEventListener('keydown', (e) => {
				if (e.key === 'Enter' || e.key === ' ') {
					e.preventDefault();
					els.fileInput.click();
				}
			});
			els.fileInput.addEventListener('change', () => loadFile(els.fileInput.files[0]));
			els.dzClear.addEventListener('click', (e) => {
				e.stopPropagation();
				clearImage();
			});
			['dragenter', 'dragover'].forEach((ev) =>
				els.dropzone.addEventListener(ev, (e) => {
					e.preventDefault();
					els.dropzone.classList.add('dragover');
				}),
			);
			['dragleave', 'drop'].forEach((ev) =>
				els.dropzone.addEventListener(ev, (e) => {
					e.preventDefault();
					els.dropzone.classList.remove('dragover');
				}),
			);
			els.dropzone.addEventListener('drop', (e) => {
				const file = e.dataTransfer?.files?.[0];
				if (file) loadFile(file);
			});
			window.addEventListener('paste', (e) => {
				if (state.mode !== 'image') return;
				const item = [...(e.clipboardData?.items || [])].find((i) => i.type.startsWith('image/'));
				if (item) loadFile(item.getAsFile());
			});

			els.prompt.addEventListener('input', updateGenerateEnabled);
			function updateGenerateEnabled() {
				const hasInput =
					state.mode === 'image' ? Boolean(state.imageDataUri) : els.prompt.value.trim().length >= 3;
				els.generate.disabled = !hasInput || !state.hasEndpoint;
				els.generate.title = state.hasEndpoint
					? ''
					: 'No NIM endpoint yet. Add one under "NIM endpoint & wire contract".';
			}
			updateGenerateEnabled();

			// ── Endpoint override ─────────────────────────────────────────
			// Typing a base URL re-probes THAT box, so the pill and the composer describe
			// the endpoint the next POST will actually hit, not the deployment default.
			let baseUrlTimer = null;
			function scheduleHealth() {
				clearTimeout(baseUrlTimer);
				baseUrlTimer = setTimeout(() => checkHealth(els.baseUrl.value), 600);
			}
			els.baseUrl.addEventListener('input', scheduleHealth);
			els.baseUrl.addEventListener('change', () => {
				clearTimeout(baseUrlTimer);
				checkHealth(els.baseUrl.value);
			});
			els.setEndpoint.addEventListener('click', () => {
				els.advanced.open = true;
				els.baseUrl.focus();
				els.baseUrl.scrollIntoView({ block: 'center', behavior: 'smooth' });
			});

			// ── Samples ───────────────────────────────────────────────────
			els.sampleRow.addEventListener('click', (e) => {
				const btn = e.target.closest('button[data-sample]');
				if (!btn) return;
				setMode('text');
				els.prompt.value = btn.dataset.prompt || '';
				updateGenerateEnabled();
				els.prompt.focus();
			});

			// ── Stage states ──────────────────────────────────────────────
			function showState(name) {
				state.stage = name;
				els.stateIdle.classList.toggle('show', name === 'idle');
				els.stateLoading.classList.toggle('show', name === 'loading');
				els.stateError.classList.toggle('show', name === 'error');
				els.stateUnconfigured.classList.toggle('show', name === 'unconfigured');
				els.viewer.style.display = name === 'result' ? 'block' : 'none';
				if (name !== 'result') {
					els.resultBar.classList.add('is-hidden');
					els.download.hidden = true;
				}
			}
			// "Back to start" always lands on the state that is actually true right now:
			// the idle prompt when an endpoint exists, the setup card when none does.
			function showRest() {
				showState(state.hasEndpoint ? 'idle' : 'unconfigured');
			}
			function showError(title, msg) {
				els.errorTitle.textContent = title;
				els.errorMsg.textContent = msg;
				// This copy is the specific failure, not translatable boilerplate: claim both
				// nodes so switching language mid-error cannot replace it with the generic line.
				els.errorTitle.dataset.i18nOwned = '1';
				els.errorMsg.dataset.i18nOwned = '1';
				showState('error');
				stopTimers();
			}

			function startTimers() {
				const t0 = performance.now();
				els.elapsed.textContent = '0.0s';
				state.elapsedTimer = setInterval(() => {
					els.elapsed.textContent = ((performance.now() - t0) / 1000).toFixed(1) + 's';
				}, 100);
				const steps = [
					'POST /v1/infer',
					'sparse structure sampling…',
					'structured latent sampling…',
					'decoding mesh + baking texture…',
					'encoding GLB…',
				];
				let i = 0;
				els.loadingSteps.textContent = steps[0];
				state.stepTimer = setInterval(() => {
					i = Math.min(i + 1, steps.length - 1);
					els.loadingSteps.textContent = steps[i];
				}, 2600);
			}
			function stopTimers() {
				clearInterval(state.elapsedTimer);
				clearInterval(state.stepTimer);
			}

			// ── Generate ──────────────────────────────────────────────────
			els.composer.addEventListener('submit', (e) => {
				e.preventDefault();
				generate();
			});
			els.errorRetry.addEventListener('click', () => {
				// Retrying with no endpoint would fail identically, so send the user to the
				// one action that can fix it instead of replaying the same 503.
				if (!state.hasEndpoint) {
					showState('unconfigured');
					els.setEndpoint.focus();
					return;
				}
				generate();
			});
			els.cancel.addEventListener('click', () => {
				state.abort?.abort();
				stopTimers();
				showRest();
			});

			async function generate() {
				if (els.generate.disabled) return;
				const payload = { mode: state.mode, tier: state.tier };
				if (state.mode === 'image') {
					if (!state.imageDataUri) return;
					payload.image = state.imageDataUri;
				} else {
					payload.prompt = els.prompt.value.trim();
					if (payload.prompt.length < 3) return;
				}
				const baseUrl = els.baseUrl.value.trim();
				if (baseUrl) payload.baseUrl = baseUrl;

				state.abort = new AbortController();
				els.generate.disabled = true;
				setBusy(true);
				showState('loading');
				startTimers();

				try {
					const r = await fetch('/api/forge-nim', {
						method: 'POST',
						headers: { 'content-type': 'application/json', accept: 'application/json' },
						body: JSON.stringify(payload),
						signal: state.abort.signal,
					});
					const d = await r.json().catch(() => ({}));
					if (!r.ok || !d.ok) {
						// The API's boundary errors carry the human sentence in
						// `error_description`; `error` is only the machine code, so reading it
						// first showed the user a bare `nim_unconfigured` instead of the reason.
						throw new Error(d.error_description || d.message || d.error || `Request failed (${r.status})`);
					}
					renderResult(d);
				} catch (err) {
					if (err.name === 'AbortError') return;
					showError('Couldn’t reconstruct', err.message || 'Unexpected error.');
				} finally {
					stopTimers();
					els.generate.disabled = false;
					setBusy(false);
					updateGenerateEnabled();
				}
			}

			function renderResult(d) {
				// Decode the synchronous { artifacts:[{ base64 }] } GLB into a Blob and
				// hand it straight to the viewer: the NIM's wire shape, end to end.
				const bytes = base64ToBytes(d.glb_base64);
				const blob = new Blob([bytes], { type: 'model/gltf-binary' });
				if (state.objectUrl) URL.revokeObjectURL(state.objectUrl);
				state.objectUrl = URL.createObjectURL(blob);
				els.viewer.src = state.objectUrl;
				els.download.href = state.objectUrl;
				els.download.hidden = false;
				showState('result');

				const kb = (d.bytes / 1024).toFixed(0);
				const secs = (d.ms / 1000).toFixed(1);
				els.resultMeta.innerHTML = '';
				const chips = [
					{ t: `${d.mode} → 3D`, cls: 'chip ok' },
					{ t: `${kb} KB`, cls: 'chip' },
					{ t: `${secs}s synchronous`, cls: 'chip' },
					{ t: d.contract, cls: 'chip' },
				];
				for (const c of chips) {
					const span = document.createElement('span');
					span.className = c.cls;
					span.textContent = c.t;
					els.resultMeta.appendChild(span);
				}
				els.resultBar.classList.remove('is-hidden');
			}

			function base64ToBytes(b64) {
				const bin = atob(b64);
				const out = new Uint8Array(bin.length);
				for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
				return out;
			}

			els.viewer.addEventListener('error', () => {
				showError('Model failed to load', 'The GLB came back but the viewer rejected it. Try the download button to inspect it directly.');
			});

			setMode('image');
			checkHealth(els.baseUrl.value);
		</script>
		<!-- Runtime i18n: detects locale, swaps annotated copy, mounts <lang-switcher>. -->
	<script type="module" src="/i18n.js"></script>
</body>
</html>