Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import os
|
3 |
import shutil
|
4 |
os.environ['SPCONV_ALGO'] = 'native'
|
@@ -12,7 +15,6 @@ from PIL import Image
|
|
12 |
from trellis.pipelines import TrellisImageTo3DPipeline
|
13 |
from trellis.representations import Gaussian, MeshExtractResult
|
14 |
from trellis.utils import render_utils, postprocessing_utils
|
15 |
-
from gradio_litmodel3d import LitModel3D
|
16 |
|
17 |
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
@@ -89,6 +91,7 @@ def get_seed(randomize_seed: bool, seed: int) -> int:
|
|
89 |
"""
|
90 |
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
91 |
|
|
|
92 |
def image_to_3d(
|
93 |
image: Image.Image,
|
94 |
seed: int,
|
@@ -123,22 +126,23 @@ def image_to_3d(
|
|
123 |
video_path = os.path.join(user_dir, f"{trial_id}.mp4")
|
124 |
imageio.mimsave(video_path, video, fps=15)
|
125 |
|
126 |
-
#
|
127 |
glb = postprocessing_utils.to_glb(
|
128 |
outputs['gaussian'][0],
|
129 |
outputs['mesh'][0],
|
130 |
simplify=0.0, # No simplification
|
131 |
fill_holes=True,
|
132 |
fill_holes_max_size=0.04,
|
133 |
-
texture_size=2048, # Maximum texture
|
134 |
verbose=False
|
135 |
)
|
136 |
full_glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
|
137 |
glb.export(full_glb_path)
|
138 |
|
139 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
|
140 |
-
return state, video_path, full_glb_path
|
141 |
|
|
|
142 |
def extract_glb(
|
143 |
state: dict,
|
144 |
mesh_simplify: float,
|
@@ -159,7 +163,8 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
159 |
gr.Markdown("""
|
160 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
161 |
* Upload an image and click "Generate" to create a 3D asset. If the image has alpha channel, it be used as the mask. Otherwise, we use `rembg` to remove the background.
|
162 |
-
*
|
|
|
163 |
""")
|
164 |
|
165 |
with gr.Row():
|
@@ -226,7 +231,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
226 |
).then(
|
227 |
image_to_3d,
|
228 |
inputs=[image_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
|
229 |
-
outputs=[output_buf, video_output, download_full],
|
230 |
).then(
|
231 |
lambda: [gr.Button(interactive=True), gr.Button(interactive=True), gr.Button(interactive=False)],
|
232 |
outputs=[download_full, extract_glb_btn, download_reduced],
|
@@ -245,7 +250,7 @@ if __name__ == "__main__":
|
|
245 |
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
|
246 |
pipeline.cuda()
|
247 |
try:
|
248 |
-
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8)))
|
249 |
except:
|
250 |
pass
|
251 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from gradio_litmodel3d import LitModel3D
|
4 |
+
|
5 |
import os
|
6 |
import shutil
|
7 |
os.environ['SPCONV_ALGO'] = 'native'
|
|
|
15 |
from trellis.pipelines import TrellisImageTo3DPipeline
|
16 |
from trellis.representations import Gaussian, MeshExtractResult
|
17 |
from trellis.utils import render_utils, postprocessing_utils
|
|
|
18 |
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
91 |
"""
|
92 |
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
93 |
|
94 |
+
@spaces.GPU
|
95 |
def image_to_3d(
|
96 |
image: Image.Image,
|
97 |
seed: int,
|
|
|
126 |
video_path = os.path.join(user_dir, f"{trial_id}.mp4")
|
127 |
imageio.mimsave(video_path, video, fps=15)
|
128 |
|
129 |
+
# Save full quality GLB
|
130 |
glb = postprocessing_utils.to_glb(
|
131 |
outputs['gaussian'][0],
|
132 |
outputs['mesh'][0],
|
133 |
simplify=0.0, # No simplification
|
134 |
fill_holes=True,
|
135 |
fill_holes_max_size=0.04,
|
136 |
+
texture_size=2048, # Maximum texture size
|
137 |
verbose=False
|
138 |
)
|
139 |
full_glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
|
140 |
glb.export(full_glb_path)
|
141 |
|
142 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
|
143 |
+
return state, video_path, model_output, full_glb_path
|
144 |
|
145 |
+
@spaces.GPU
|
146 |
def extract_glb(
|
147 |
state: dict,
|
148 |
mesh_simplify: float,
|
|
|
163 |
gr.Markdown("""
|
164 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
165 |
* Upload an image and click "Generate" to create a 3D asset. If the image has alpha channel, it be used as the mask. Otherwise, we use `rembg` to remove the background.
|
166 |
+
* The full-quality GLB will be available immediately after generation.
|
167 |
+
* If you need a reduced size version, use the GLB Extraction Settings below.
|
168 |
""")
|
169 |
|
170 |
with gr.Row():
|
|
|
231 |
).then(
|
232 |
image_to_3d,
|
233 |
inputs=[image_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
|
234 |
+
outputs=[output_buf, video_output, model_output, download_full],
|
235 |
).then(
|
236 |
lambda: [gr.Button(interactive=True), gr.Button(interactive=True), gr.Button(interactive=False)],
|
237 |
outputs=[download_full, extract_glb_btn, download_reduced],
|
|
|
250 |
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
|
251 |
pipeline.cuda()
|
252 |
try:
|
253 |
+
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Preload rembg
|
254 |
except:
|
255 |
pass
|
256 |
demo.launch()
|