prithivMLmods commited on
Commit
e400692
·
verified ·
1 Parent(s): c226fe7

update app

Browse files
Files changed (1) hide show
  1. app.py +38 -75
app.py CHANGED
@@ -7,26 +7,24 @@ import tempfile
7
  from typing import *
8
  from datetime import datetime
9
  from pathlib import Path
10
- from typing import Iterable
11
 
12
- # Gradio and Rerun Imports
13
- import gradio as gr
14
  from gradio.themes import Soft
15
  from gradio.themes.utils import colors, fonts, sizes
 
 
16
  from gradio_client import Client, handle_file
17
  import spaces
 
 
 
18
  import rerun as rr
19
- from gradio_rerun import Rerun # Requires: pip install gradio-rerun
20
 
21
- # Diffusers / Trellis Imports
22
- from diffusers import ZImagePipeline
23
  from trellis2.pipelines import Trellis2ImageTo3DPipeline
24
  import o_voxel
25
 
26
- # ==========================================
27
- # THEME CONFIGURATION
28
- # ==========================================
29
-
30
  colors.orange_red = colors.Color(
31
  name="orange_red",
32
  c50="#FFF0E5",
@@ -95,15 +93,9 @@ class OrangeRedTheme(Soft):
95
 
96
  orange_red_theme = OrangeRedTheme()
97
 
98
- # ==========================================
99
- # ENVIRONMENT & MODEL SETUP
100
- # ==========================================
101
-
102
  os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
103
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
104
  os.environ["ATTN_BACKEND"] = "flash_attn_3"
105
-
106
- # Adjust path if needed for your environment
107
  os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
108
  os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
109
 
@@ -139,10 +131,6 @@ except Exception as e:
139
 
140
  rmbg_client = Client("briaai/BRIA-RMBG-2.0")
141
 
142
- # ==========================================
143
- # UTILITY FUNCTIONS
144
- # ==========================================
145
-
146
  def start_session(req: gr.Request):
147
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
148
  os.makedirs(user_dir, exist_ok=True)
@@ -161,7 +149,6 @@ def remove_background(input: Image.Image) -> Image.Image:
161
  return output
162
 
163
  def preprocess_image(input: Image.Image) -> Image.Image:
164
- """Preprocess the input image: remove bg, crop, resize."""
165
  if input is None:
166
  return None
167
 
@@ -198,13 +185,8 @@ def preprocess_image(input: Image.Image) -> Image.Image:
198
  def get_seed(randomize_seed: bool, seed: int) -> int:
199
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
200
 
201
- # ==========================================
202
- # GENERATION FUNCTIONS
203
- # ==========================================
204
-
205
  @spaces.GPU
206
  def generate_txt2img(prompt, progress=gr.Progress(track_tqdm=True)):
207
- """Generate Image using Z-Image Turbo"""
208
  if z_pipe is None:
209
  raise gr.Error("Z-Image-Turbo model failed to load.")
210
  if not prompt.strip():
@@ -228,6 +210,30 @@ def generate_txt2img(prompt, progress=gr.Progress(track_tqdm=True)):
228
  except Exception as e:
229
  raise gr.Error(f"Z-Image Generation failed: {str(e)}")
230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  @spaces.GPU(duration=120)
232
  def generate_3d(
233
  image: Image.Image,
@@ -288,12 +294,10 @@ def generate_3d(
288
  return_latent=True,
289
  )
290
 
291
- # 2. Process Mesh
292
  progress(0.7, desc="Processing Mesh...")
293
  mesh = outputs[0]
294
- mesh.simplify(16777216) # Simplify for processing limits
295
 
296
- # 3. Export to GLB
297
  progress(0.9, desc="Baking Texture & Exporting GLB...")
298
 
299
  grid_size = latents[2]
@@ -317,55 +321,17 @@ def generate_3d(
317
  now = datetime.now()
318
  timestamp = now.strftime("%Y-%m-%dT%H%M%S")
319
  glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
320
- rrd_path = os.path.join(user_dir, f'trellis_output_{timestamp}.rrd')
321
-
322
  glb.export(glb_path, extension_webp=True)
323
-
324
- # ---------------------------------------------------------
325
- # RERUN VISUALIZATION LOGIC
326
- # ---------------------------------------------------------
327
- progress(0.95, desc="Creating Rerun Visualization...")
328
 
329
- # Initialize Rerun in standard mode (no spawn, just recording)
330
- rr.init(f"trellis_session_{timestamp}", spawn=False)
331
 
332
- # 1. Setup View Coordinates (Z-Up is standard for 3D web/GLB)
333
- rr.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True)
334
-
335
- # 2. Log Axes (Red=X, Green=Y, Blue=Z)
336
- rr.log(
337
- "world/axes",
338
- rr.Arrows3D(
339
- vectors=[[0.5, 0, 0], [0, 0.5, 0], [0, 0, 0.5]],
340
- colors=[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
341
- origins=[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
342
- labels=["X", "Y", "Z"],
343
- radii=0.015
344
- ),
345
- static=True
346
- )
347
-
348
- # 3. Log the Model (Asset3D)
349
- # Note: TRELLIS outputs are usually normalized.
350
- rr.log("world/model", rr.Asset3D(path=glb_path))
351
-
352
- # 4. Save to RRD for Gradio Viewer
353
- rr.save(rrd_path)
354
-
355
- # Clean up memory
356
  torch.cuda.empty_cache()
357
-
358
- # Return RRD path for visualization and GLB path for download
359
  return rrd_path, glb_path
360
 
361
  except Exception as e:
362
  torch.cuda.empty_cache()
363
  raise gr.Error(f"Generation failed: {str(e)}")
364
 
365
- # ==========================================
366
- # UI LAYOUT
367
- # ==========================================
368
-
369
  css="""
370
  #col-container {
371
  margin: 0 auto;
@@ -378,11 +344,11 @@ if __name__ == "__main__":
378
  os.makedirs(TMP_DIR, exist_ok=True)
379
 
380
  with gr.Blocks(delete_cache=(300, 300)) as demo:
381
- gr.Markdown("# **TRELLIS.2 (Text-to-3D)**", elem_id="main-title")
382
  gr.Markdown("""
383
  **Workflow:**
384
  Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
385
- **Visualization:** Output is displayed using Rerun.
386
  """)
387
 
388
  with gr.Row():
@@ -428,13 +394,10 @@ if __name__ == "__main__":
428
 
429
  with gr.Column(scale=2):
430
  gr.Markdown("### 3D Output (Rerun Viewer)")
431
-
432
- # REPLACED gr.Model3D with Rerun
433
  rerun_output = Rerun(
434
- label="Rerun Visualization",
435
- height=600
436
  )
437
-
438
  download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
439
 
440
  gr.Examples(
 
7
  from typing import *
8
  from datetime import datetime
9
  from pathlib import Path
10
+ import uuid
11
 
12
+ from typing import Iterable
 
13
  from gradio.themes import Soft
14
  from gradio.themes.utils import colors, fonts, sizes
15
+
16
+ import gradio as gr
17
  from gradio_client import Client, handle_file
18
  import spaces
19
+ from diffusers import ZImagePipeline
20
+
21
+ # Rerun Imports
22
  import rerun as rr
23
+ from gradio_rerun import Rerun
24
 
 
 
25
  from trellis2.pipelines import Trellis2ImageTo3DPipeline
26
  import o_voxel
27
 
 
 
 
 
28
  colors.orange_red = colors.Color(
29
  name="orange_red",
30
  c50="#FFF0E5",
 
93
 
94
  orange_red_theme = OrangeRedTheme()
95
 
 
 
 
 
96
  os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
97
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
98
  os.environ["ATTN_BACKEND"] = "flash_attn_3"
 
 
99
  os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
100
  os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
101
 
 
131
 
132
  rmbg_client = Client("briaai/BRIA-RMBG-2.0")
133
 
 
 
 
 
134
  def start_session(req: gr.Request):
135
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
136
  os.makedirs(user_dir, exist_ok=True)
 
149
  return output
150
 
151
  def preprocess_image(input: Image.Image) -> Image.Image:
 
152
  if input is None:
153
  return None
154
 
 
185
  def get_seed(randomize_seed: bool, seed: int) -> int:
186
  return np.random.randint(0, MAX_SEED) if randomize_seed else seed
187
 
 
 
 
 
188
  @spaces.GPU
189
  def generate_txt2img(prompt, progress=gr.Progress(track_tqdm=True)):
 
190
  if z_pipe is None:
191
  raise gr.Error("Z-Image-Turbo model failed to load.")
192
  if not prompt.strip():
 
210
  except Exception as e:
211
  raise gr.Error(f"Z-Image Generation failed: {str(e)}")
212
 
213
+ def create_rerun_rrd(glb_path: str) -> str:
214
+ """Creates a Rerun recording for the generated GLB."""
215
+ rrd_path = glb_path.replace(".glb", ".rrd")
216
+
217
+ rec = rr.new_recording(application_id="trellis_app", recording_id=str(uuid.uuid4()))
218
+
219
+ rec.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True)
220
+
221
+ rec.log(
222
+ "world/axes",
223
+ rr.Arrows3D(
224
+ vectors=[[0.5, 0, 0], [0, 0.5, 0], [0, 0, 0.5]],
225
+ colors=[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
226
+ labels=["X", "Y", "Z"],
227
+ radii=0.02
228
+ ),
229
+ static=True
230
+ )
231
+
232
+ rec.log("world/model", rr.Asset3D(path=glb_path), static=True)
233
+
234
+ rec.save(rrd_path)
235
+ return rrd_path
236
+
237
  @spaces.GPU(duration=120)
238
  def generate_3d(
239
  image: Image.Image,
 
294
  return_latent=True,
295
  )
296
 
 
297
  progress(0.7, desc="Processing Mesh...")
298
  mesh = outputs[0]
299
+ mesh.simplify(16777216)
300
 
 
301
  progress(0.9, desc="Baking Texture & Exporting GLB...")
302
 
303
  grid_size = latents[2]
 
321
  now = datetime.now()
322
  timestamp = now.strftime("%Y-%m-%dT%H%M%S")
323
  glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
 
 
324
  glb.export(glb_path, extension_webp=True)
 
 
 
 
 
325
 
326
+ rrd_path = create_rerun_rrd(glb_path)
 
327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  torch.cuda.empty_cache()
 
 
329
  return rrd_path, glb_path
330
 
331
  except Exception as e:
332
  torch.cuda.empty_cache()
333
  raise gr.Error(f"Generation failed: {str(e)}")
334
 
 
 
 
 
335
  css="""
336
  #col-container {
337
  margin: 0 auto;
 
344
  os.makedirs(TMP_DIR, exist_ok=True)
345
 
346
  with gr.Blocks(delete_cache=(300, 300)) as demo:
347
+ gr.Markdown("# **TRELLIS.2 (Text-to-3D) with Rerun**", elem_id="main-title")
348
  gr.Markdown("""
349
  **Workflow:**
350
  Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
351
+ Visualization powered by Rerun.
352
  """)
353
 
354
  with gr.Row():
 
394
 
395
  with gr.Column(scale=2):
396
  gr.Markdown("### 3D Output (Rerun Viewer)")
 
 
397
  rerun_output = Rerun(
398
+ label="Rerun 3D Viewer",
399
+ height=600,
400
  )
 
401
  download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
402
 
403
  gr.Examples(