Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,274 +1,227 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import spaces
|
3 |
-
|
4 |
-
import os
|
5 |
-
import shutil
|
6 |
-
os.environ['TOKENIZERS_PARALLELISM'] = 'true'
|
7 |
-
os.environ['SPCONV_ALGO'] = 'native'
|
8 |
-
from typing import *
|
9 |
-
import torch
|
10 |
-
import numpy as np
|
11 |
-
import imageio
|
12 |
-
from easydict import EasyDict as edict
|
13 |
-
from trellis.pipelines import TrellisTextTo3DPipeline
|
14 |
-
from trellis.representations import Gaussian, MeshExtractResult
|
15 |
-
from trellis.utils import render_utils, postprocessing_utils
|
16 |
-
|
17 |
-
import traceback
|
18 |
-
import sys
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
'
|
41 |
-
|
42 |
-
|
43 |
-
'
|
44 |
-
'
|
45 |
-
},
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
)
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
)
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
generate_btn.click(
|
229 |
-
get_seed,
|
230 |
-
inputs=[randomize_seed, seed],
|
231 |
-
outputs=[seed],
|
232 |
-
).then(
|
233 |
-
text_to_3d,
|
234 |
-
inputs=[text_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
|
235 |
-
outputs=[output_buf, video_output],
|
236 |
-
).then(
|
237 |
-
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
238 |
-
outputs=[extract_glb_btn, extract_gs_btn],
|
239 |
-
)
|
240 |
-
|
241 |
-
video_output.clear(
|
242 |
-
lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),
|
243 |
-
outputs=[extract_glb_btn, extract_gs_btn],
|
244 |
-
)
|
245 |
-
|
246 |
-
extract_glb_btn.click(
|
247 |
-
extract_glb,
|
248 |
-
inputs=[output_buf, mesh_simplify, texture_size],
|
249 |
-
outputs=[model_output, download_glb],
|
250 |
-
).then(
|
251 |
-
lambda: gr.Button(interactive=True),
|
252 |
-
outputs=[download_glb],
|
253 |
-
)
|
254 |
-
|
255 |
-
extract_gs_btn.click(
|
256 |
-
extract_gaussian,
|
257 |
-
inputs=[output_buf],
|
258 |
-
outputs=[model_output, download_gs],
|
259 |
-
).then(
|
260 |
-
lambda: gr.Button(interactive=True),
|
261 |
-
outputs=[download_gs],
|
262 |
-
)
|
263 |
-
|
264 |
-
model_output.clear(
|
265 |
-
lambda: gr.Button(interactive=False),
|
266 |
-
outputs=[download_glb],
|
267 |
-
)
|
268 |
-
|
269 |
-
|
270 |
-
# Launch the Gradio app
|
271 |
-
if __name__ == "__main__":
|
272 |
-
pipeline = TrellisTextTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-text-xlarge")
|
273 |
-
pipeline.cuda()
|
274 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
|
4 |
+
import os
|
5 |
+
import shutil
|
6 |
+
os.environ['TOKENIZERS_PARALLELISM'] = 'true'
|
7 |
+
os.environ['SPCONV_ALGO'] = 'native'
|
8 |
+
from typing import *
|
9 |
+
import torch
|
10 |
+
import numpy as np
|
11 |
+
import imageio
|
12 |
+
from easydict import EasyDict as edict
|
13 |
+
from trellis.pipelines import TrellisTextTo3DPipeline
|
14 |
+
from trellis.representations import Gaussian, MeshExtractResult
|
15 |
+
from trellis.utils import render_utils, postprocessing_utils
|
16 |
+
|
17 |
+
import traceback
|
18 |
+
import sys
|
19 |
+
|
20 |
+
MAX_SEED = np.iinfo(np.int32).max
|
21 |
+
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
22 |
+
os.makedirs(TMP_DIR, exist_ok=True)
|
23 |
+
|
24 |
+
def start_session(req: gr.Request):
|
25 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
26 |
+
os.makedirs(user_dir, exist_ok=True)
|
27 |
+
|
28 |
+
def end_session(req: gr.Request):
|
29 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
30 |
+
shutil.rmtree(user_dir)
|
31 |
+
|
32 |
+
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
33 |
+
return {
|
34 |
+
'gaussian': {
|
35 |
+
**gs.init_params,
|
36 |
+
'_xyz': gs._xyz.cpu().numpy(),
|
37 |
+
'_features_dc': gs._features_dc.cpu().numpy(),
|
38 |
+
'_scaling': gs._scaling.cpu().numpy(),
|
39 |
+
'_rotation': gs._rotation.cpu().numpy(),
|
40 |
+
'_opacity': gs._opacity.cpu().numpy(),
|
41 |
+
},
|
42 |
+
'mesh': {
|
43 |
+
'vertices': mesh.vertices.cpu().numpy(),
|
44 |
+
'faces': mesh.faces.cpu().numpy(),
|
45 |
+
},
|
46 |
+
}
|
47 |
+
|
48 |
+
def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
49 |
+
gs = Gaussian(
|
50 |
+
aabb=state['gaussian']['aabb'],
|
51 |
+
sh_degree=state['gaussian']['sh_degree'],
|
52 |
+
mininum_kernel_size=state['gaussian']['mininum_kernel_size'],
|
53 |
+
scaling_bias=state['gaussian']['scaling_bias'],
|
54 |
+
opacity_bias=state['gaussian']['opacity_bias'],
|
55 |
+
scaling_activation=state['gaussian']['scaling_activation'],
|
56 |
+
)
|
57 |
+
gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')
|
58 |
+
gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')
|
59 |
+
gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')
|
60 |
+
gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')
|
61 |
+
gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')
|
62 |
+
|
63 |
+
mesh = edict(
|
64 |
+
vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),
|
65 |
+
faces=torch.tensor(state['mesh']['faces'], device='cuda'),
|
66 |
+
)
|
67 |
+
|
68 |
+
return gs, mesh
|
69 |
+
|
70 |
+
def get_seed(randomize_seed: bool, seed: int) -> int:
|
71 |
+
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
72 |
+
|
73 |
+
@spaces.GPU
|
74 |
+
def text_to_3d(
|
75 |
+
prompt: str,
|
76 |
+
seed: int,
|
77 |
+
ss_guidance_strength: float,
|
78 |
+
ss_sampling_steps: int,
|
79 |
+
slat_guidance_strength: float,
|
80 |
+
slat_sampling_steps: int,
|
81 |
+
req: gr.Request,
|
82 |
+
) -> Tuple[dict, str]:
|
83 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
84 |
+
outputs = pipeline.run(
|
85 |
+
prompt,
|
86 |
+
seed=seed,
|
87 |
+
formats=["gaussian", "mesh"],
|
88 |
+
sparse_structure_sampler_params={
|
89 |
+
"steps": ss_sampling_steps,
|
90 |
+
"cfg_strength": ss_guidance_strength,
|
91 |
+
},
|
92 |
+
slat_sampler_params={
|
93 |
+
"steps": slat_sampling_steps,
|
94 |
+
"cfg_strength": slat_guidance_strength,
|
95 |
+
},
|
96 |
+
)
|
97 |
+
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
98 |
+
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
99 |
+
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
100 |
+
video_path = os.path.join(user_dir, 'sample.mp4')
|
101 |
+
imageio.mimsave(video_path, video, fps=15)
|
102 |
+
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
103 |
+
torch.cuda.empty_cache()
|
104 |
+
return state, video_path
|
105 |
+
|
106 |
+
@spaces.GPU(duration=90)
|
107 |
+
def extract_glb(
|
108 |
+
state: dict,
|
109 |
+
mesh_simplify: float,
|
110 |
+
texture_size: int,
|
111 |
+
req: gr.Request,
|
112 |
+
) -> Tuple[str, str]:
|
113 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
114 |
+
gs, mesh = unpack_state(state)
|
115 |
+
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
116 |
+
glb_path = os.path.join(user_dir, 'sample.glb')
|
117 |
+
glb.export(glb_path)
|
118 |
+
torch.cuda.empty_cache()
|
119 |
+
return glb_path, glb_path
|
120 |
+
|
121 |
+
@spaces.GPU
|
122 |
+
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
123 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
124 |
+
gs, _ = unpack_state(state)
|
125 |
+
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
126 |
+
gs.save_ply(gaussian_path)
|
127 |
+
torch.cuda.empty_cache()
|
128 |
+
return gaussian_path, gaussian_path
|
129 |
+
|
130 |
+
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
131 |
+
gr.Markdown("""
|
132 |
+
# UTPL - Conversi贸n de Texto a Imagen a objetos 3D usando IA
|
133 |
+
### Tesis: *"Objetos tridimensionales creados por IA: Innovaci贸n en entornos virtuales"*
|
134 |
+
**Autor:** Carlos Vargas
|
135 |
+
**Base t茅cnica:** Adaptaci贸n de [TRELLIS](https://trellis3d.github.io/) y [FLUX](https://huggingface.co/camenduru/FLUX.1-dev-diffusers) (herramientas de c贸digo abierto para generaci贸n 3D)
|
136 |
+
**Prop贸sito educativo:** Demostraciones acad茅micas e Investigaci贸n en modelado 3D autom谩tico
|
137 |
+
""")
|
138 |
+
|
139 |
+
with gr.Row():
|
140 |
+
with gr.Column():
|
141 |
+
text_prompt = gr.Textbox(label="Text Prompt", lines=5)
|
142 |
+
|
143 |
+
with gr.Accordion(label="Generation Settings", open=False):
|
144 |
+
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
145 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
146 |
+
gr.Markdown("Stage 1: Sparse Structure Generation")
|
147 |
+
with gr.Row():
|
148 |
+
ss_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=7.5, step=0.1)
|
149 |
+
ss_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=25, step=1)
|
150 |
+
gr.Markdown("Stage 2: Structured Latent Generation")
|
151 |
+
with gr.Row():
|
152 |
+
slat_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=7.5, step=0.1)
|
153 |
+
slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=25, step=1)
|
154 |
+
|
155 |
+
generate_btn = gr.Button("Generate")
|
156 |
+
|
157 |
+
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
158 |
+
mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
|
159 |
+
texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
|
160 |
+
|
161 |
+
with gr.Row():
|
162 |
+
extract_glb_btn = gr.Button("Extract GLB", interactive=False)
|
163 |
+
extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)
|
164 |
+
gr.Markdown("""
|
165 |
+
*NOTE: Gaussian file can be very large (~50MB), it will take a while to display and download.*
|
166 |
+
""")
|
167 |
+
|
168 |
+
with gr.Column():
|
169 |
+
video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)
|
170 |
+
model_output = gr.Model3D(label="Extracted GLB/Gaussian", height=300)
|
171 |
+
|
172 |
+
with gr.Row():
|
173 |
+
download_glb = gr.DownloadButton(label="Download GLB", interactive=False)
|
174 |
+
download_gs = gr.DownloadButton(label="Download Gaussian", interactive=False)
|
175 |
+
|
176 |
+
output_buf = gr.State()
|
177 |
+
|
178 |
+
# Handlers
|
179 |
+
demo.load(start_session)
|
180 |
+
demo.unload(end_session)
|
181 |
+
|
182 |
+
generate_btn.click(
|
183 |
+
get_seed,
|
184 |
+
inputs=[randomize_seed, seed],
|
185 |
+
outputs=[seed],
|
186 |
+
).then(
|
187 |
+
text_to_3d,
|
188 |
+
inputs=[text_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
|
189 |
+
outputs=[output_buf, video_output],
|
190 |
+
).then(
|
191 |
+
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
192 |
+
outputs=[extract_glb_btn, extract_gs_btn],
|
193 |
+
)
|
194 |
+
|
195 |
+
video_output.clear(
|
196 |
+
lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),
|
197 |
+
outputs=[extract_glb_btn, extract_gs_btn],
|
198 |
+
)
|
199 |
+
|
200 |
+
extract_glb_btn.click(
|
201 |
+
extract_glb,
|
202 |
+
inputs=[output_buf, mesh_simplify, texture_size],
|
203 |
+
outputs=[model_output, download_glb],
|
204 |
+
).then(
|
205 |
+
lambda: gr.Button(interactive=True),
|
206 |
+
outputs=[download_glb],
|
207 |
+
)
|
208 |
+
|
209 |
+
extract_gs_btn.click(
|
210 |
+
extract_gaussian,
|
211 |
+
inputs=[output_buf],
|
212 |
+
outputs=[model_output, download_gs],
|
213 |
+
).then(
|
214 |
+
lambda: gr.Button(interactive=True),
|
215 |
+
outputs=[download_gs],
|
216 |
+
)
|
217 |
+
|
218 |
+
model_output.clear(
|
219 |
+
lambda: gr.Button(interactive=False),
|
220 |
+
outputs=[download_glb],
|
221 |
+
)
|
222 |
+
|
223 |
+
# Launch the Gradio app
|
224 |
+
if __name__ == "__main__":
|
225 |
+
pipeline = TrellisTextTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-text-xlarge")
|
226 |
+
pipeline.cuda()
|
227 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|