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ready 3D asset pipeline
3
+ ====================================================
4
+
5
+ Milestone 1: Foundation
6
+ -----------------------
7
+ This is the scaffold. It validates that:
8
+ - The HF Space builds with the right Gradio + ZeroGPU configuration
9
+ - `@spaces.GPU` allocates and releases an H200 successfully
10
+ - `gr.Model3D` renders GLB files inline
11
+ - The 5-tab UI structure is sound
12
+ - Workspace folder management works
13
+ - Quota tracking persists across page reloads
14
+
15
+ Subsequent milestones fill in the actual pipeline stages.
16
+
17
+ Deployed exclusively on HF Spaces ZeroGPU — no local execution path.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import time
23
+ from pathlib import Path
24
+
25
+ import gradio as gr
26
+ import spaces # HF ZeroGPU
27
+
28
+ from src import quota, ui_helpers, workspace
29
+
30
+ # ---------------------------------------------------------------------------
31
+ # ZeroGPU test function — proves that GPU allocation works.
32
+ # Replaced in Milestone 2 with the actual TRELLIS.2 generator.
33
+ # ---------------------------------------------------------------------------
34
+
35
+ @spaces.GPU(duration=15)
36
+ def zerogpu_smoke_test() -> str:
37
+ """Allocate a GPU, run a trivial torch op, release. Reports timing."""
38
+ start = time.time()
39
+ try:
40
+ import torch
41
+ if not torch.cuda.is_available():
42
+ return "❌ GPU not available inside @spaces.GPU. Something is wrong."
43
+
44
+ device = torch.device("cuda")
45
+ # Trivial GPU work
46
+ a = torch.randn(1024, 1024, device=device)
47
+ b = torch.randn(1024, 1024, device=device)
48
+ c = a @ b
49
+ torch.cuda.synchronize()
50
+ result_sum = float(c.sum().item())
51
+
52
+ gpu_name = torch.cuda.get_device_name(0)
53
+ vram_total = torch.cuda.get_device_properties(0).total_memory / 1e9
54
+ elapsed = time.time() - start
55
+
56
+ quota.record_usage("smoke_test", elapsed)
57
+
58
+ return (
59
+ f"✅ **GPU allocation successful**\n\n"
60
+ f"- Device: `{gpu_name}`\n"
61
+ f"- VRAM: `{vram_total:.1f} GB`\n"
62
+ f"- Test op (1024×1024 matmul): `{elapsed:.2f}s`\n"
63
+ f"- Result sum: `{result_sum:.2f}` (sanity check, non-zero = ✓)\n\n"
64
+ f"ZeroGPU integration is working. Ready for Milestone 2."
65
+ )
66
+ except Exception as e:
67
+ elapsed = time.time() - start
68
+ return f"❌ GPU test failed after {elapsed:.2f}s:\n```\n{type(e).__name__}: {e}\n```"
69
+
70
+
71
+ # ---------------------------------------------------------------------------
72
+ # Stage stubs — return placeholder messages until milestones implement them.
73
+ # ---------------------------------------------------------------------------
74
+
75
+ def _stub(stage: str) -> str:
76
+ return (
77
+ f"🚧 **{stage}** — not implemented yet.\n\n"
78
+ f"This stub will be replaced in a future milestone. "
79
+ f"See `PLAN.md` for the full pipeline spec."
80
+ )
81
+
82
+
83
+ def stub_generate(*_args, **_kwargs):
84
+ return _stub("Stage 1: Generation")
85
+
86
+
87
+ def stub_post_process(*_args, **_kwargs):
88
+ return _stub("Stage 2: Post-Processing")
89
+
90
+
91
+ def stub_auto_rig(*_args, **_kwargs):
92
+ return _stub("Stage 3: Auto-Rigging")
93
+
94
+
95
+ def stub_export(*_args, **_kwargs):
96
+ return _stub("Stage 4: Export"), None # message, file
97
+
98
+
99
+ # ---------------------------------------------------------------------------
100
+ # Presets tab logic (this one is real even in Milestone 1)
101
+ # ---------------------------------------------------------------------------
102
+
103
+ def ui_refresh_presets() -> gr.Dropdown:
104
+ return gr.Dropdown(choices=workspace.list_presets(), label="Saved presets")
105
+
106
+
107
+ def ui_save_preset(name: str) -> tuple[str, gr.Dropdown]:
108
+ if not name or not name.strip():
109
+ return "❌ Preset name cannot be empty.", ui_refresh_presets()
110
+ # Placeholder config — Milestone 9 wires in real settings from each tab
111
+ config = {
112
+ "name": name,
113
+ "version": 1,
114
+ "created_at": time.time(),
115
+ "note": "Placeholder — real preset data wired in Milestone 9",
116
+ }
117
+ workspace.save_preset(name, config)
118
+ return f"✅ Saved preset: `{name}`", ui_refresh_presets()
119
+
120
+
121
+ def ui_delete_preset(name: str) -> tuple[str, gr.Dropdown]:
122
+ if not name:
123
+ return "❌ Select a preset to delete.", ui_refresh_presets()
124
+ if workspace.delete_preset(name):
125
+ return f"🗑️ Deleted preset: `{name}`", ui_refresh_presets()
126
+ return f"❌ Preset not found: `{name}`", ui_refresh_presets()
127
+
128
+
129
+ # ---------------------------------------------------------------------------
130
+ # Layout
131
+ # ---------------------------------------------------------------------------
132
+
133
+ CUSTOM_CSS = """
134
+ .status-bar {
135
+ font-size: 0.85em;
136
+ color: #888;
137
+ padding: 8px 12px;
138
+ border-top: 1px solid #333;
139
+ margin-top: 12px;
140
+ }
141
+ .asset-summary {
142
+ font-size: 0.9em;
143
+ background: rgba(255,255,255,0.03);
144
+ padding: 12px;
145
+ border-radius: 6px;
146
+ border: 1px solid rgba(255,255,255,0.08);
147
+ }
148
+ """
149
+
150
+
151
+ def build_ui() -> gr.Blocks:
152
+ with gr.Blocks(
153
+ title="Open3DForge",
154
+ theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"),
155
+ css=CUSTOM_CSS,
156
+ ) as demo:
157
+
158
+ # --- Header --------------------------------------------------------
159
+ gr.Markdown(
160
+ "# 🛠️ Open3DForge\n"
161
+ "*Personal image-to-game-ready 3D asset pipeline · UE5-first · "
162
+ "Built on HF ZeroGPU*"
163
+ )
164
+
165
+ # --- Tabs ----------------------------------------------------------
166
+ with gr.Tabs() as tabs:
167
+
168
+ # ============ Tab 1: Generate =================================
169
+ with gr.Tab("1. Generate", id=1):
170
+ gr.Markdown(
171
+ "### Stage 1 — Image to 3D\n"
172
+ "Upload 1–4 reference images. Multi-view dramatically "
173
+ "improves quality for characters (front / 3-quarter / side / back)."
174
+ )
175
+ with gr.Row():
176
+ with gr.Column(scale=1):
177
+ gen_images = gr.File(
178
+ label="Reference images (1–4)",
179
+ file_count="multiple",
180
+ file_types=["image"],
181
+ )
182
+ gen_model = gr.Radio(
183
+ choices=[
184
+ "TRELLIS.2 (Hard Surface)",
185
+ "Hunyuan3D-2 (Organic / Characters)",
186
+ ],
187
+ value="TRELLIS.2 (Hard Surface)",
188
+ label="Generation model",
189
+ )
190
+ gen_quality = gr.Radio(
191
+ choices=["Fast (~30s)", "Balanced (~60s)", "Hero (~90s)"],
192
+ value="Balanced (~60s)",
193
+ label="Quality preset",
194
+ )
195
+ with gr.Accordion("Advanced", open=False):
196
+ gen_seed = gr.Number(value=42, label="Seed", precision=0)
197
+ gen_steps = gr.Slider(
198
+ 20, 50, value=35, step=5,
199
+ label="Inference steps",
200
+ )
201
+ gen_octree = gr.Dropdown(
202
+ choices=[128, 192, 256], value=192,
203
+ label="Octree resolution",
204
+ )
205
+ gen_tex_size = gr.Dropdown(
206
+ choices=[1024, 2048, 4096], value=2048,
207
+ label="Texture size",
208
+ )
209
+ gen_symmetry = gr.Radio(
210
+ choices=["off", "bilateral", "radial"],
211
+ value="off",
212
+ label="Symmetry hint",
213
+ )
214
+ gen_btn = gr.Button("Generate", variant="primary")
215
+ with gr.Column(scale=1):
216
+ gen_status = gr.Markdown("*Awaiting input.*")
217
+ gen_btn.click(
218
+ fn=stub_generate,
219
+ inputs=[gen_images, gen_model, gen_quality, gen_seed,
220
+ gen_steps, gen_octree, gen_tex_size, gen_symmetry],
221
+ outputs=gen_status,
222
+ )
223
+
224
+ # ============ Tab 2: Post-Process =============================
225
+ with gr.Tab("2. Post-Process", id=2):
226
+ gr.Markdown(
227
+ "### Stage 2 — Mesh cleanup, UV unwrap, texture baking\n"
228
+ "Toggle steps on/off. Decimation has a live preview, the "
229
+ "rest run on confirm."
230
+ )
231
+ with gr.Row():
232
+ with gr.Column(scale=1):
233
+ pp_repair = gr.Checkbox(value=True, label="Mesh repair (pymeshfix)")
234
+ pp_cleanup = gr.Checkbox(value=True, label="Geometry cleanup (PyMeshLab)")
235
+ pp_decimate = gr.Checkbox(value=True, label="Decimation")
236
+ pp_target_faces = gr.Slider(
237
+ 1000, 200000, value=25000, step=1000,
238
+ label="Target faces",
239
+ )
240
+ pp_symmetry = gr.Checkbox(value=False, label="Enforce bilateral symmetry")
241
+ pp_unwrap = gr.Checkbox(value=True, label="UV unwrap (xatlas)")
242
+ pp_normal_bake = gr.Checkbox(value=True, label="Normal bake (nvdiffrast)")
243
+ pp_normal_format = gr.Radio(
244
+ choices=["DirectX (UE5)", "OpenGL (Unity/Godot)"],
245
+ value="DirectX (UE5)",
246
+ label="Normal format",
247
+ )
248
+ pp_albedo_bake = gr.Checkbox(value=True, label="Albedo bake")
249
+ pp_material_bake = gr.Checkbox(value=True, label="Material bake (TRELLIS.2 attrs)")
250
+ pp_ao = gr.Checkbox(value=True, label="AO bake")
251
+ pp_ao_quality = gr.Radio(
252
+ choices=["Fast", "Standard", "High"],
253
+ value="Standard",
254
+ label="AO quality",
255
+ )
256
+ pp_inpaint = gr.Checkbox(value=False, label="SDXL inpaint hidden UVs (~30s GPU)")
257
+ pp_lods = gr.Checkbox(value=True, label="Generate LODs (LOD0/1/2)")
258
+ pp_collision = gr.Checkbox(value=True, label="Collision mesh (CoACD)")
259
+ pp_pivot = gr.Radio(
260
+ choices=["bottom_center", "geometric_center", "custom"],
261
+ value="bottom_center",
262
+ label="Pivot point",
263
+ )
264
+ pp_scale_m = gr.Number(value=1.8, label="Real-world height (meters)")
265
+ pp_btn = gr.Button("Run Post-Processing", variant="primary")
266
+ with gr.Column(scale=1):
267
+ pp_status = gr.Markdown("*No asset to process. Generate one first.*")
268
+ pp_btn.click(
269
+ fn=stub_post_process,
270
+ inputs=[pp_repair, pp_cleanup, pp_decimate, pp_target_faces,
271
+ pp_symmetry, pp_unwrap, pp_normal_bake, pp_normal_format,
272
+ pp_albedo_bake, pp_material_bake, pp_ao, pp_ao_quality,
273
+ pp_inpaint, pp_lods, pp_collision, pp_pivot, pp_scale_m],
274
+ outputs=pp_status,
275
+ )
276
+
277
+ # ============ Tab 3: Auto-Rig =================================
278
+ with gr.Tab("3. Auto-Rig", id=3):
279
+ gr.Markdown(
280
+ "### Stage 3 — Auto-rigging (optional)\n"
281
+ "Uses UniRig (VAST-AI). For characters and creatures. "
282
+ "After rigging, drop the FBX into [Mixamo](https://mixamo.com) "
283
+ "for free animation presets."
284
+ )
285
+ with gr.Row():
286
+ with gr.Column(scale=1):
287
+ rig_type = gr.Dropdown(
288
+ choices=["Humanoid", "Quadruped", "Bird", "Insect", "Custom"],
289
+ value="Humanoid",
290
+ label="Character type",
291
+ )
292
+ rig_seed = gr.Number(value=0, label="Skeleton seed", precision=0)
293
+ rig_spring = gr.Checkbox(value=False, label="Spring bones (hair/cloth/tail)")
294
+ rig_format = gr.Radio(
295
+ choices=["FBX (UE5 recommended)", "GLB"],
296
+ value="FBX (UE5 recommended)",
297
+ label="Export format",
298
+ )
299
+ rig_btn = gr.Button("Auto-Rig", variant="primary")
300
+ with gr.Column(scale=1):
301
+ rig_status = gr.Markdown("*Process an asset in Stage 2 first.*")
302
+ rig_btn.click(
303
+ fn=stub_auto_rig,
304
+ inputs=[rig_type, rig_seed, rig_spring, rig_format],
305
+ outputs=rig_status,
306
+ )
307
+
308
+ # ============ Tab 4: Export ===================================
309
+ with gr.Tab("4. Export", id=4):
310
+ gr.Markdown(
311
+ "### Stage 4 — Engine-ready export\n"
312
+ "UE5 default: FBX with DirectX normals + ORM-packed textures."
313
+ )
314
+ with gr.Row():
315
+ with gr.Column(scale=1):
316
+ ex_engine = gr.Dropdown(
317
+ choices=["UE5", "Unity (HDRP)", "Godot 4", "Blender", "Web (Three.js)"],
318
+ value="UE5",
319
+ label="Target engine",
320
+ )
321
+ ex_name = gr.Textbox(value="Asset_01", label="Asset name")
322
+ ex_type = gr.Radio(
323
+ choices=["Character (SK_)", "Prop (SM_)", "Environment (SM_)"],
324
+ value="Prop (SM_)",
325
+ label="Asset type",
326
+ )
327
+ ex_include_lods = gr.Checkbox(value=True, label="Include LODs")
328
+ ex_include_collision = gr.Checkbox(value=True, label="Include collision mesh")
329
+ ex_btn = gr.Button("Export", variant="primary")
330
+ with gr.Column(scale=1):
331
+ ex_status = gr.Markdown("*Nothing to export yet.*")
332
+ ex_file = gr.File(label="Download", visible=True)
333
+ ex_btn.click(
334
+ fn=stub_export,
335
+ inputs=[ex_engine, ex_name, ex_type, ex_include_lods, ex_include_collision],
336
+ outputs=[ex_status, ex_file],
337
+ )
338
+
339
+ # ============ Tab 5: Presets ==================================
340
+ with gr.Tab("5. Presets", id=5):
341
+ gr.Markdown(
342
+ "### Saved configurations\n"
343
+ "Save parameter sets for asset types you create repeatedly "
344
+ "(e.g., \"character_UE5_hero\", \"prop_UE5_standard\"). "
345
+ "Wired up properly in Milestone 9."
346
+ )
347
+ with gr.Row():
348
+ with gr.Column():
349
+ pr_list = gr.Dropdown(
350
+ choices=workspace.list_presets(),
351
+ label="Saved presets",
352
+ )
353
+ pr_refresh = gr.Button("Refresh list", size="sm")
354
+ with gr.Row():
355
+ pr_name = gr.Textbox(label="New preset name", scale=2)
356
+ pr_save = gr.Button("Save current settings", variant="primary", scale=1)
357
+ pr_delete = gr.Button("Delete selected", variant="stop")
358
+ pr_status = gr.Markdown()
359
+ pr_refresh.click(fn=ui_refresh_presets, outputs=pr_list)
360
+ pr_save.click(fn=ui_save_preset, inputs=pr_name, outputs=[pr_status, pr_list])
361
+ pr_delete.click(fn=ui_delete_preset, inputs=pr_list, outputs=[pr_status, pr_list])
362
+
363
+ # ============ Tab 6: Diagnostics (hidden in prod, useful now) =
364
+ with gr.Tab("Diagnostics", id=99):
365
+ gr.Markdown(
366
+ "### Milestone 1 — Foundation check\n"
367
+ "Verify the Space environment is working correctly before "
368
+ "building out the pipeline."
369
+ )
370
+ with gr.Row():
371
+ with gr.Column():
372
+ diag_btn = gr.Button("🧪 Run GPU smoke test", variant="primary")
373
+ diag_out = gr.Markdown()
374
+ with gr.Column():
375
+ gr.Markdown("**Workspace state:**")
376
+ diag_state = gr.JSON(value=workspace.get_state().to_dict())
377
+ diag_refresh = gr.Button("Refresh state", size="sm")
378
+ diag_btn.click(fn=zerogpu_smoke_test, outputs=diag_out)
379
+ diag_refresh.click(
380
+ fn=lambda: workspace.get_state().to_dict(),
381
+ outputs=diag_state,
382
+ )
383
+
384
+ # --- Persistent right-side viewer + asset summary ------------------
385
+ gr.Markdown("---")
386
+ with gr.Row():
387
+ with gr.Column(scale=2):
388
+ viewer = gr.Model3D(
389
+ label="3D viewer",
390
+ value=ui_helpers.get_viewer_model_path(),
391
+ clear_color=[0.1, 0.1, 0.12, 1.0],
392
+ height=500,
393
+ )
394
+ with gr.Column(scale=1):
395
+ summary = gr.Markdown(
396
+ value=ui_helpers.get_asset_summary(),
397
+ elem_classes=["asset-summary"],
398
+ )
399
+ refresh_summary = gr.Button("🔄 Refresh viewer", size="sm")
400
+ refresh_summary.click(
401
+ fn=lambda: (
402
+ ui_helpers.get_viewer_model_path(),
403
+ ui_helpers.get_asset_summary(),
404
+ ),
405
+ outputs=[viewer, summary],
406
+ )
407
+
408
+ # --- Global status bar --------------------------------------------
409
+ status_bar = gr.Markdown(
410
+ value=ui_helpers.get_status_bar(),
411
+ elem_classes=["status-bar"],
412
+ )
413
+
414
+ # --- Global refresh: every pipeline action updates the viewer, summary,
415
+ # and status bar. We chain .then() onto each button so the refresh runs
416
+ # only after the action completes (and even if it fails — Gradio still
417
+ # fires .then() after exceptions inside the main handler).
418
+ def _global_refresh():
419
+ return (
420
+ ui_helpers.get_viewer_model_path(),
421
+ ui_helpers.get_asset_summary(),
422
+ ui_helpers.get_status_bar(),
423
+ )
424
+
425
+ for trigger_btn in (gen_btn, pp_btn, rig_btn, ex_btn,
426
+ diag_btn, diag_refresh, refresh_summary):
427
+ trigger_btn.click(
428
+ fn=_global_refresh,
429
+ outputs=[viewer, summary, status_bar],
430
+ )
431
+
432
+ return demo
433
+
434
+
435
+ # ---------------------------------------------------------------------------
436
+ # Entrypoint
437
+ # ---------------------------------------------------------------------------
438
+ # On HF Spaces, app.py is executed directly. We construct the demo and call
439
+ # .launch() at module level. The HF Space runtime handles all networking;
440
+ # we just need to bind to 0.0.0.0:7860.
441
+
442
+ workspace.ensure_dirs()
443
+ demo = build_ui()
444
+ demo.queue(default_concurrency_limit=1).launch(
445
+ server_name="0.0.0.0",
446
+ server_port=7860,
447
+ show_error=True,
448
+ show_api=False,
449
+ )