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