meancat commited on
Commit
dc492a2
·
verified ·
1 Parent(s): 60c7875

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -96
app.py CHANGED
@@ -9,10 +9,9 @@ from typing import *
9
  import torch
10
  import numpy as np
11
  import imageio
12
- from easydict import EasyDict as edict
13
  from PIL import Image
14
  from trellis.pipelines import TrellisImageTo3DPipeline
15
- from trellis.representations import Gaussian, MeshExtractResult
16
  from trellis.utils import render_utils, postprocessing_utils
17
 
18
 
@@ -67,44 +66,6 @@ def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image
67
  return processed_images
68
 
69
 
70
- def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
71
- return {
72
- 'gaussian': {
73
- **gs.init_params,
74
- '_xyz': gs._xyz.cpu().numpy(),
75
- '_features_dc': gs._features_dc.cpu().numpy(),
76
- '_scaling': gs._scaling.cpu().numpy(),
77
- '_rotation': gs._rotation.cpu().numpy(),
78
- '_opacity': gs._opacity.cpu().numpy(),
79
- },
80
- 'mesh': {
81
- 'vertices': mesh.vertices.cpu().numpy(),
82
- 'faces': mesh.faces.cpu().numpy(),
83
- },
84
- }
85
-
86
-
87
- def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
88
- gs = Gaussian(
89
- aabb=state['gaussian']['aabb'],
90
- sh_degree=state['gaussian']['sh_degree'],
91
- mininum_kernel_size=state['gaussian']['mininum_kernel_size'],
92
- scaling_bias=state['gaussian']['scaling_bias'],
93
- opacity_bias=state['gaussian']['opacity_bias'],
94
- scaling_activation=state['gaussian']['scaling_activation'],
95
- )
96
- gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')
97
- gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')
98
- gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')
99
- gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')
100
- gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')
101
-
102
- mesh = edict(
103
- vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),
104
- faces=torch.tensor(state['mesh']['faces'], device='cuda'),
105
- )
106
-
107
- return gs, mesh
108
 
109
 
110
  def get_seed(randomize_seed: bool, seed: int) -> int:
@@ -138,7 +99,7 @@ def generate_and_extract_glb(
138
  mesh_simplify: float,
139
  texture_size: int,
140
  req: gr.Request,
141
- ) -> Tuple[dict, str, str, str]:
142
  """
143
  Convert an image to a 3D model and extract GLB file.
144
 
@@ -156,7 +117,6 @@ def generate_and_extract_glb(
156
  texture_size (int): The texture resolution.
157
 
158
  Returns:
159
- dict: The information of the generated 3D model.
160
  str: The path to the video of the 3D model.
161
  str: The path to the extracted GLB file.
162
  str: The path to the extracted GLB file (for download).
@@ -210,35 +170,10 @@ def generate_and_extract_glb(
210
  glb_path = os.path.join(user_dir, 'sample.glb')
211
  glb.export(glb_path)
212
 
213
- # Pack state for optional Gaussian extraction
214
- state = pack_state(gs, mesh)
215
-
216
  torch.cuda.empty_cache()
217
- return state, video_path, glb_path, glb_path
218
 
219
 
220
- @spaces.GPU
221
- def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
222
- """
223
- Extract a Gaussian splatting file from the generated 3D model.
224
-
225
- This function is called when the user clicks "Extract Gaussian" button.
226
- It converts the 3D model state into a .ply file format containing
227
- Gaussian splatting data for advanced 3D applications.
228
-
229
- Args:
230
- state (dict): The state of the generated 3D model containing Gaussian data
231
- req (gr.Request): Gradio request object for session management
232
-
233
- Returns:
234
- Tuple[str, str]: Paths to the extracted Gaussian file (for display and download)
235
- """
236
- user_dir = os.path.join(TMP_DIR, str(req.session_hash))
237
- gs, _ = unpack_state(state)
238
- gaussian_path = os.path.join(user_dir, 'sample.ply')
239
- gs.save_ply(gaussian_path)
240
- torch.cuda.empty_cache()
241
- return gaussian_path, gaussian_path
242
 
243
 
244
  def prepare_multi_example() -> List[Image.Image]:
@@ -284,10 +219,9 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
284
  gr.Markdown("""
285
  ## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
286
  * Upload an image and click "Generate & Extract GLB" to create a 3D asset and automatically extract the GLB file.
287
- * If you want the Gaussian file as well, click "Extract Gaussian" after generation.
288
  * If the image has alpha channel, it will be used as the mask. Otherwise, we use `rembg` to remove the background.
289
 
290
- ✨New: 1) Experimental multi-image support. 2) Gaussian file extraction.
291
  """)
292
 
293
  with gr.Row():
@@ -317,25 +251,18 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
317
  multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
318
 
319
  with gr.Accordion(label="GLB Extraction Settings", open=False):
320
- mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
321
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
322
 
323
  generate_btn = gr.Button("Generate & Extract GLB", variant="primary")
324
- extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)
325
- gr.Markdown("""
326
- *NOTE: Gaussian file can be very large (~50MB), it will take a while to display and download.*
327
- """)
328
 
329
  with gr.Column():
330
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
331
- model_output = LitModel3D(label="Extracted GLB/Gaussian", exposure=10.0, height=300)
332
 
333
- with gr.Row():
334
- download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
335
- download_gs = gr.DownloadButton(label="Download Gaussian", interactive=False)
336
 
337
  is_multiimage = gr.State(False)
338
- output_buf = gr.State()
339
 
340
  # Example images at the bottom of the page
341
  with gr.Row() as single_image_example:
@@ -391,29 +318,20 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
391
  ).then(
392
  generate_and_extract_glb,
393
  inputs=[image_prompt, multiimage_prompt, is_multiimage, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo, mesh_simplify, texture_size],
394
- outputs=[output_buf, video_output, model_output, download_glb],
395
  ).then(
396
- lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
397
- outputs=[extract_gs_btn, download_glb],
398
  )
399
 
400
  video_output.clear(
401
- lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False), gr.Button(interactive=False)]),
402
- outputs=[extract_gs_btn, download_glb, download_gs],
403
- )
404
-
405
- extract_gs_btn.click(
406
- extract_gaussian,
407
- inputs=[output_buf],
408
- outputs=[model_output, download_gs],
409
- ).then(
410
- lambda: gr.Button(interactive=True),
411
- outputs=[download_gs],
412
  )
413
 
414
  model_output.clear(
415
- lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),
416
- outputs=[download_glb, download_gs],
417
  )
418
 
419
 
 
9
  import torch
10
  import numpy as np
11
  import imageio
 
12
  from PIL import Image
13
  from trellis.pipelines import TrellisImageTo3DPipeline
14
+ from trellis.representations import MeshExtractResult
15
  from trellis.utils import render_utils, postprocessing_utils
16
 
17
 
 
66
  return processed_images
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
 
71
  def get_seed(randomize_seed: bool, seed: int) -> int:
 
99
  mesh_simplify: float,
100
  texture_size: int,
101
  req: gr.Request,
102
+ ) -> Tuple[str, str, str]:
103
  """
104
  Convert an image to a 3D model and extract GLB file.
105
 
 
117
  texture_size (int): The texture resolution.
118
 
119
  Returns:
 
120
  str: The path to the video of the 3D model.
121
  str: The path to the extracted GLB file.
122
  str: The path to the extracted GLB file (for download).
 
170
  glb_path = os.path.join(user_dir, 'sample.glb')
171
  glb.export(glb_path)
172
 
 
 
 
173
  torch.cuda.empty_cache()
174
+ return video_path, glb_path, glb_path
175
 
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
 
179
  def prepare_multi_example() -> List[Image.Image]:
 
219
  gr.Markdown("""
220
  ## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
221
  * Upload an image and click "Generate & Extract GLB" to create a 3D asset and automatically extract the GLB file.
 
222
  * If the image has alpha channel, it will be used as the mask. Otherwise, we use `rembg` to remove the background.
223
 
224
+ ✨New: Experimental multi-image support.
225
  """)
226
 
227
  with gr.Row():
 
251
  multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
252
 
253
  with gr.Accordion(label="GLB Extraction Settings", open=False):
254
+ mesh_simplify = gr.Slider(0.3, 0.98, label="Simplify", value=0.95, step=0.01)
255
  texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
256
 
257
  generate_btn = gr.Button("Generate & Extract GLB", variant="primary")
 
 
 
 
258
 
259
  with gr.Column():
260
  video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
261
+ model_output = LitModel3D(label="Extracted GLB", exposure=10.0, height=300)
262
 
263
+ download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
 
 
264
 
265
  is_multiimage = gr.State(False)
 
266
 
267
  # Example images at the bottom of the page
268
  with gr.Row() as single_image_example:
 
318
  ).then(
319
  generate_and_extract_glb,
320
  inputs=[image_prompt, multiimage_prompt, is_multiimage, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo, mesh_simplify, texture_size],
321
+ outputs=[video_output, model_output, download_glb],
322
  ).then(
323
+ lambda: gr.Button(interactive=True),
324
+ outputs=[download_glb],
325
  )
326
 
327
  video_output.clear(
328
+ lambda: gr.Button(interactive=False),
329
+ outputs=[download_glb],
 
 
 
 
 
 
 
 
 
330
  )
331
 
332
  model_output.clear(
333
+ lambda: gr.Button(interactive=False),
334
+ outputs=[download_glb],
335
  )
336
 
337