prithivMLmods commited on
Commit
0e02b7b
·
verified ·
1 Parent(s): 28b8328

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -62
app.py CHANGED
@@ -4,42 +4,28 @@ import torch
4
  import numpy as np
5
  from PIL import Image
6
  import tempfile
7
- import uuid
8
  from typing import *
9
  from datetime import datetime
10
  from pathlib import Path
11
 
12
  from typing import Iterable
 
 
13
  from gradio.themes import Soft
14
  from gradio.themes.utils import colors, fonts, sizes
15
 
 
16
  import rerun as rr
17
  from gradio_rerun import Rerun
18
 
19
- # --- Monkey Patch for cumesh to fix CUDA Error 9 ---
20
- # This prevents the app from crashing if fill_holes fails due to mesh density
21
- try:
22
- import cumesh
23
- # Save the original method just in case
24
- if hasattr(cumesh, "Mesh"):
25
- _original_fill_holes = cumesh.Mesh.fill_holes
26
-
27
- def safe_fill_holes(self, *args, **kwargs):
28
- try:
29
- return _original_fill_holes(self, *args, **kwargs)
30
- except RuntimeError as e:
31
- print(f"Warning: cumesh.fill_holes failed (CUDA error skipped): {e}")
32
- # We simply return None or pass, effectively skipping the step
33
- return None
34
-
35
- cumesh.Mesh.fill_holes = safe_fill_holes
36
- print("Successfully monkey-patched cumesh.Mesh.fill_holes to handle CUDA errors.")
37
- except ImportError:
38
- print("Warning: cumesh not found, skipping monkey patch.")
39
- except Exception as e:
40
- print(f"Warning: Failed to monkey-patch cumesh: {e}")
41
 
42
- # --- Theme Configuration ---
 
 
 
 
43
  colors.orange_red = colors.Color(
44
  name="orange_red",
45
  c50="#FFF0E5",
@@ -108,20 +94,14 @@ class OrangeRedTheme(Soft):
108
 
109
  orange_red_theme = OrangeRedTheme()
110
 
111
- # --- Environment Setup ---
112
  os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
113
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
114
  os.environ["ATTN_BACKEND"] = "flash_attn_3"
 
115
  os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
116
  os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
117
 
118
- import gradio as gr
119
- from gradio_client import Client, handle_file
120
- import spaces
121
- from diffusers import ZImagePipeline
122
- from trellis2.pipelines import Trellis2ImageTo3DPipeline
123
- import o_voxel
124
-
125
  MAX_SEED = np.iinfo(np.int32).max
126
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
127
 
@@ -154,6 +134,8 @@ except Exception as e:
154
 
155
  rmbg_client = Client("briaai/BRIA-RMBG-2.0")
156
 
 
 
157
  def start_session(req: gr.Request):
158
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
159
  os.makedirs(user_dir, exist_ok=True)
@@ -325,35 +307,29 @@ def generate_3d(
325
  now = datetime.now()
326
  timestamp = now.strftime("%Y-%m-%dT%H%M%S")
327
  glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
 
 
328
  glb.export(glb_path, extension_webp=True)
329
 
330
- # --- Rerun Visualization Logic ---
331
- progress(0.95, desc="Creating Rerun Visualization...")
332
 
333
- run_id = str(uuid.uuid4())
 
334
 
335
- # Robustly handle different Rerun SDK versions
336
- rec = None
337
- if hasattr(rr, "new_recording"):
338
- rec = rr.new_recording(application_id="TRELLIS-3D-Viewer", recording_id=run_id)
339
- elif hasattr(rr, "RecordingStream"):
340
- rec = rr.RecordingStream(application_id="TRELLIS-3D-Viewer", recording_id=run_id)
341
- else:
342
- rr.init("TRELLIS-3D-Viewer", recording_id=run_id, spawn=False)
343
- rec = rr
344
-
345
- # Log the GLB asset using path (most robust way)
346
- rec.log("generated_model", rr.Asset3D(path=glb_path), static=True)
347
 
348
- # Also log the input image for context
349
- rec.log("input_image", rr.Image(np.array(image)), static=True)
 
 
 
350
 
351
- # Save the Rerun recording (.rrd)
352
- rrd_path = os.path.join(user_dir, f'trellis_output_{timestamp}.rrd')
353
- rec.save(rrd_path)
354
-
355
  # Clean up
356
  torch.cuda.empty_cache()
 
 
357
  return rrd_path, glb_path
358
 
359
  except Exception as e:
@@ -376,6 +352,7 @@ if __name__ == "__main__":
376
  gr.Markdown("""
377
  **Workflow:**
378
  Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
 
379
  """)
380
 
381
  with gr.Row():
@@ -395,6 +372,7 @@ if __name__ == "__main__":
395
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
396
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
397
 
 
398
  decimation_target = gr.Slider(50000, 500000, label="Target Faces", value=150000, step=10000)
399
  texture_size = gr.Slider(512, 4096, label="Texture Size", value=1024, step=512)
400
 
@@ -420,10 +398,10 @@ if __name__ == "__main__":
420
  tex_rescale_t = gr.Slider(1.0, 6.0, value=3.0, label="Rescale T")
421
 
422
  with gr.Column(scale=2):
423
- gr.Markdown("### 3D Output")
424
-
425
  rerun_output = Rerun(
426
- label="Rerun 3D Viewer",
427
  height=600
428
  )
429
 
@@ -433,9 +411,7 @@ if __name__ == "__main__":
433
  examples=[
434
  ["example-images/A (1).webp"],
435
  ["example-images/A (2).webp"],
436
- ["example-images/A (3).webp"],
437
- ["example-images/A (4).webp"],
438
- ["example-images/A (5).webp"],
439
  ],
440
  inputs=[image_prompt],
441
  label="Image Examples [image-to-3d]"
@@ -445,8 +421,10 @@ if __name__ == "__main__":
445
  examples=[
446
  ["A Cat 3D model"],
447
  ["A realistic Plane 3D"],
448
- ["A cyberpunk Car 3D"],
449
- ["A futuristic Shoe 3D"],
 
 
450
  ],
451
  inputs=[txt_prompt],
452
  label="3D Prompt Examples [text-to-3d]"
@@ -483,8 +461,9 @@ if __name__ == "__main__":
483
  ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
484
  shape_guidance, shape_rescale, shape_steps, shape_rescale_t,
485
  tex_guidance, tex_rescale, tex_steps, tex_rescale_t,
 
486
  ],
487
- outputs=[rerun_output, download_btn],
488
  )
489
 
490
  demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False, show_error=True)
 
4
  import numpy as np
5
  from PIL import Image
6
  import tempfile
 
7
  from typing import *
8
  from datetime import datetime
9
  from pathlib import Path
10
 
11
  from typing import Iterable
12
+ import gradio as gr
13
+ from gradio_client import Client, handle_file
14
  from gradio.themes import Soft
15
  from gradio.themes.utils import colors, fonts, sizes
16
 
17
+ # Import Rerun and the Gradio Component
18
  import rerun as rr
19
  from gradio_rerun import Rerun
20
 
21
+ import spaces
22
+ from diffusers import ZImagePipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Ensure these are installed/available in your environment
25
+ from trellis2.pipelines import Trellis2ImageTo3DPipeline
26
+ import o_voxel
27
+
28
+ # Theme Definition
29
  colors.orange_red = colors.Color(
30
  name="orange_red",
31
  c50="#FFF0E5",
 
94
 
95
  orange_red_theme = OrangeRedTheme()
96
 
97
+ # Environment Config
98
  os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
99
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
100
  os.environ["ATTN_BACKEND"] = "flash_attn_3"
101
+
102
  os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
103
  os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
104
 
 
 
 
 
 
 
 
105
  MAX_SEED = np.iinfo(np.int32).max
106
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
107
 
 
134
 
135
  rmbg_client = Client("briaai/BRIA-RMBG-2.0")
136
 
137
+ # ---------------- Functions ----------------
138
+
139
  def start_session(req: gr.Request):
140
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
141
  os.makedirs(user_dir, exist_ok=True)
 
307
  now = datetime.now()
308
  timestamp = now.strftime("%Y-%m-%dT%H%M%S")
309
  glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
310
+ rrd_path = os.path.join(user_dir, f'trellis_output_{timestamp}.rrd')
311
+
312
  glb.export(glb_path, extension_webp=True)
313
 
314
+ # 4. Log to Rerun
315
+ progress(0.95, desc="Creating Visualization...")
316
 
317
+ # Initialize Rerun - use a unique ID to avoid state leakage
318
+ rr.init(f"trellis_session_{timestamp}", spawn=False)
319
 
320
+ # Log the GLB asset to Rerun
321
+ rr.log("generated_model", rr.Asset3D(path=glb_path))
 
 
 
 
 
 
 
 
 
 
322
 
323
+ # Set up default view coordinates (Trellis is usually Z-up)
324
+ rr.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP)
325
+
326
+ # Save RRD file for Gradio viewer
327
+ rr.save(rrd_path)
328
 
 
 
 
 
329
  # Clean up
330
  torch.cuda.empty_cache()
331
+
332
+ # Return RRD path for display, GLB path for download
333
  return rrd_path, glb_path
334
 
335
  except Exception as e:
 
352
  gr.Markdown("""
353
  **Workflow:**
354
  Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
355
+ **Visualization powered by Rerun.**
356
  """)
357
 
358
  with gr.Row():
 
372
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
373
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
374
 
375
+ #gr.Markdown("**Export Settings**")
376
  decimation_target = gr.Slider(50000, 500000, label="Target Faces", value=150000, step=10000)
377
  texture_size = gr.Slider(512, 4096, label="Texture Size", value=1024, step=512)
378
 
 
398
  tex_rescale_t = gr.Slider(1.0, 6.0, value=3.0, label="Rescale T")
399
 
400
  with gr.Column(scale=2):
401
+ gr.Markdown("### 3D Output (Rerun Viewer)")
402
+ # REPLACED gr.Model3D with Rerun
403
  rerun_output = Rerun(
404
+ label="Rerun Visualization",
405
  height=600
406
  )
407
 
 
411
  examples=[
412
  ["example-images/A (1).webp"],
413
  ["example-images/A (2).webp"],
414
+ # Add more examples if file paths exist in your environment
 
 
415
  ],
416
  inputs=[image_prompt],
417
  label="Image Examples [image-to-3d]"
 
421
  examples=[
422
  ["A Cat 3D model"],
423
  ["A realistic Plane 3D"],
424
+ ["A sports Car 3D"],
425
+ ["A sneaker Shoe 3D"],
426
+ ["A Chair 3D model"],
427
+ ["A House 3D model"],
428
  ],
429
  inputs=[txt_prompt],
430
  label="3D Prompt Examples [text-to-3d]"
 
461
  ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
462
  shape_guidance, shape_rescale, shape_steps, shape_rescale_t,
463
  tex_guidance, tex_rescale, tex_steps, tex_rescale_t,
464
+
465
  ],
466
+ outputs=[rerun_output, download_btn], # Updated to target Rerun output
467
  )
468
 
469
  demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False, show_error=True)