Spaces:
Runtime error
Runtime error
Update app.py
Browse filesstill working on the buttons
app.py
CHANGED
@@ -110,9 +110,9 @@ def image_to_3d(
|
|
110 |
slat_guidance_strength: float,
|
111 |
slat_sampling_steps: int,
|
112 |
req: gr.Request,
|
113 |
-
) -> Tuple[dict, str]:
|
114 |
"""
|
115 |
-
Convert an image to a 3D model.
|
116 |
|
117 |
Args:
|
118 |
image (Image.Image): The input image.
|
@@ -124,7 +124,7 @@ def image_to_3d(
|
|
124 |
req (gr.Request): Gradio request object.
|
125 |
|
126 |
Returns:
|
127 |
-
Tuple[dict, str]: The state dictionary
|
128 |
"""
|
129 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
130 |
outputs = pipeline.run(
|
@@ -148,8 +148,20 @@ def image_to_3d(
|
|
148 |
video_path = os.path.join(user_dir, f"{trial_id}.mp4")
|
149 |
imageio.mimsave(video_path, video, fps=15)
|
150 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
torch.cuda.empty_cache()
|
152 |
-
return state, video_path
|
153 |
|
154 |
# Existing GLB Extraction Function
|
155 |
@spaces.GPU
|
@@ -173,7 +185,13 @@ def extract_glb(
|
|
173 |
"""
|
174 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
175 |
gs, mesh, trial_id = unpack_state(state)
|
176 |
-
glb = postprocessing_utils.to_glb(
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
glb_path = os.path.join(user_dir, f"{trial_id}.glb")
|
178 |
glb.export(glb_path)
|
179 |
# Read the GLB file as bytes
|
@@ -202,7 +220,13 @@ def extract_glb_high_quality(
|
|
202 |
gs, mesh, trial_id = unpack_state(state)
|
203 |
# Set simplify to 0.0 to disable polygon reduction
|
204 |
# Set texture_size to 2048 for maximum texture quality
|
205 |
-
glb = postprocessing_utils.to_glb(
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
glb_path = os.path.join(user_dir, f"{trial_id}_high_quality.glb")
|
207 |
glb.export(glb_path)
|
208 |
# Read the GLB file as bytes
|
@@ -215,11 +239,12 @@ def extract_glb_high_quality(
|
|
215 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
216 |
gr.Markdown("""
|
217 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
218 |
-
* Upload an image and click "Generate" to create a 3D asset. If the image has an alpha channel, it will be used as the mask. Otherwise, the background will be removed automatically.
|
219 |
-
*
|
220 |
-
* **
|
|
|
221 |
""")
|
222 |
-
|
223 |
with gr.Row():
|
224 |
with gr.Column():
|
225 |
# Image Input
|
@@ -297,12 +322,12 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
297 |
step=512
|
298 |
)
|
299 |
|
300 |
-
#
|
301 |
-
extract_glb_btn = gr.Button("Extract GLB"
|
302 |
|
303 |
-
#
|
304 |
-
|
305 |
-
|
306 |
with gr.Column():
|
307 |
# Video Output
|
308 |
video_output = gr.Video(
|
@@ -317,15 +342,20 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
317 |
exposure=20.0,
|
318 |
height=300
|
319 |
)
|
320 |
-
#
|
321 |
download_glb = gr.DownloadButton(
|
322 |
-
label="Download GLB"
|
323 |
-
interactive=False # Initially disabled
|
324 |
)
|
325 |
-
# New Download High Quality GLB Button
|
326 |
download_high_quality_glb = gr.DownloadButton(
|
327 |
-
label="Download High Quality GLB"
|
328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
)
|
330 |
|
331 |
# State Variables
|
@@ -373,55 +403,57 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
373 |
slat_guidance_strength,
|
374 |
slat_sampling_steps
|
375 |
],
|
376 |
-
outputs=[output_buf, video_output]
|
377 |
).then(
|
378 |
-
|
379 |
-
|
380 |
-
outputs=[
|
381 |
)
|
382 |
|
383 |
-
#
|
384 |
extract_glb_btn.click(
|
385 |
extract_glb,
|
386 |
inputs=[output_buf, mesh_simplify, texture_size],
|
387 |
-
outputs=[
|
388 |
).then(
|
389 |
-
# Map the GLB bytes to the DownloadButton
|
390 |
-
lambda glb_bytes: (glb_bytes, "model.glb"),
|
391 |
-
inputs=[glb_bytes_state],
|
392 |
outputs=[download_glb],
|
393 |
).then(
|
394 |
-
#
|
395 |
-
lambda:
|
396 |
-
|
|
|
397 |
)
|
398 |
|
399 |
-
#
|
400 |
-
|
401 |
extract_glb_high_quality,
|
402 |
inputs=[output_buf],
|
403 |
-
outputs=[
|
404 |
).then(
|
405 |
-
# Map the high-quality GLB bytes to the DownloadButton
|
406 |
-
lambda glb_bytes: (glb_bytes, "model_high_quality.glb"),
|
407 |
-
inputs=[glb_high_quality_bytes_state],
|
408 |
outputs=[download_high_quality_glb],
|
409 |
).then(
|
410 |
-
#
|
411 |
-
lambda:
|
412 |
-
|
|
|
413 |
)
|
414 |
|
415 |
# Handle Clearing of Video Output
|
416 |
video_output.clear(
|
417 |
-
lambda:
|
418 |
-
outputs=[
|
419 |
)
|
420 |
|
421 |
# Handle Clearing of Model Output
|
422 |
model_output.clear(
|
423 |
-
lambda:
|
424 |
-
outputs=[
|
425 |
)
|
426 |
|
427 |
# Launch the Gradio app
|
|
|
110 |
slat_guidance_strength: float,
|
111 |
slat_sampling_steps: int,
|
112 |
req: gr.Request,
|
113 |
+
) -> Tuple[dict, str, str]:
|
114 |
"""
|
115 |
+
Convert an image to a 3D model and generate a GLB file.
|
116 |
|
117 |
Args:
|
118 |
image (Image.Image): The input image.
|
|
|
124 |
req (gr.Request): Gradio request object.
|
125 |
|
126 |
Returns:
|
127 |
+
Tuple[dict, str, str]: The state dictionary, path to the generated video, and path to the standard GLB file.
|
128 |
"""
|
129 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
130 |
outputs = pipeline.run(
|
|
|
148 |
video_path = os.path.join(user_dir, f"{trial_id}.mp4")
|
149 |
imageio.mimsave(video_path, video, fps=15)
|
150 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
|
151 |
+
|
152 |
+
# Generate standard GLB file with default simplification and texture size
|
153 |
+
glb = postprocessing_utils.to_glb(
|
154 |
+
outputs['gaussian'][0],
|
155 |
+
outputs['mesh'][0],
|
156 |
+
simplify=0.95, # Default simplification
|
157 |
+
texture_size=1024, # Default texture size
|
158 |
+
verbose=False
|
159 |
+
)
|
160 |
+
glb_path = os.path.join(user_dir, f"{trial_id}.glb")
|
161 |
+
glb.export(glb_path)
|
162 |
+
|
163 |
torch.cuda.empty_cache()
|
164 |
+
return state, video_path, glb_path
|
165 |
|
166 |
# Existing GLB Extraction Function
|
167 |
@spaces.GPU
|
|
|
185 |
"""
|
186 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
187 |
gs, mesh, trial_id = unpack_state(state)
|
188 |
+
glb = postprocessing_utils.to_glb(
|
189 |
+
gs,
|
190 |
+
mesh,
|
191 |
+
simplify=mesh_simplify,
|
192 |
+
texture_size=texture_size,
|
193 |
+
verbose=False
|
194 |
+
)
|
195 |
glb_path = os.path.join(user_dir, f"{trial_id}.glb")
|
196 |
glb.export(glb_path)
|
197 |
# Read the GLB file as bytes
|
|
|
220 |
gs, mesh, trial_id = unpack_state(state)
|
221 |
# Set simplify to 0.0 to disable polygon reduction
|
222 |
# Set texture_size to 2048 for maximum texture quality
|
223 |
+
glb = postprocessing_utils.to_glb(
|
224 |
+
gs,
|
225 |
+
mesh,
|
226 |
+
simplify=0.0,
|
227 |
+
texture_size=2048,
|
228 |
+
verbose=False
|
229 |
+
)
|
230 |
glb_path = os.path.join(user_dir, f"{trial_id}_high_quality.glb")
|
231 |
glb.export(glb_path)
|
232 |
# Read the GLB file as bytes
|
|
|
239 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
240 |
gr.Markdown("""
|
241 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
242 |
+
* **Generate:** Upload an image and click "Generate" to create a 3D asset. If the image has an alpha channel, it will be used as the mask. Otherwise, the background will be removed automatically.
|
243 |
+
* **Extract GLB:** If the generated 3D asset is satisfactory, click "Extract GLB" to extract the GLB file based on your chosen settings and download it.
|
244 |
+
* **Download High Quality GLB:** Click this button to download a high-quality GLB file without any polygon reduction and with maximum texture quality.
|
245 |
+
* **Status:** View messages and feedback about your actions below.
|
246 |
""")
|
247 |
+
|
248 |
with gr.Row():
|
249 |
with gr.Column():
|
250 |
# Image Input
|
|
|
322 |
step=512
|
323 |
)
|
324 |
|
325 |
+
# Extract GLB Button
|
326 |
+
extract_glb_btn = gr.Button("Extract GLB")
|
327 |
|
328 |
+
# Download High Quality GLB Button
|
329 |
+
download_glb_high_quality_btn = gr.Button("Download High Quality GLB")
|
330 |
+
|
331 |
with gr.Column():
|
332 |
# Video Output
|
333 |
video_output = gr.Video(
|
|
|
342 |
exposure=20.0,
|
343 |
height=300
|
344 |
)
|
345 |
+
# Download GLB Buttons
|
346 |
download_glb = gr.DownloadButton(
|
347 |
+
label="Download GLB"
|
|
|
348 |
)
|
|
|
349 |
download_high_quality_glb = gr.DownloadButton(
|
350 |
+
label="Download High Quality GLB"
|
351 |
+
)
|
352 |
+
|
353 |
+
# Status Message
|
354 |
+
status_message = gr.Textbox(
|
355 |
+
label="Status",
|
356 |
+
value="Awaiting your action...",
|
357 |
+
interactive=False,
|
358 |
+
lines=2
|
359 |
)
|
360 |
|
361 |
# State Variables
|
|
|
403 |
slat_guidance_strength,
|
404 |
slat_sampling_steps
|
405 |
],
|
406 |
+
outputs=[output_buf, video_output, glb_path := gr.State()]
|
407 |
).then(
|
408 |
+
lambda state, video, glb: "Generation successful! You can now extract and download GLB files.",
|
409 |
+
inputs=[output_buf, video_output, glb_path],
|
410 |
+
outputs=[status_message]
|
411 |
)
|
412 |
|
413 |
+
# Extract GLB Button Click Handler
|
414 |
extract_glb_btn.click(
|
415 |
extract_glb,
|
416 |
inputs=[output_buf, mesh_simplify, texture_size],
|
417 |
+
outputs=[output_buf, glb_bytes_state],
|
418 |
).then(
|
419 |
+
# Map the GLB bytes to the DownloadButton with filename
|
420 |
+
lambda state, glb_bytes: (glb_bytes, "model.glb"),
|
421 |
+
inputs=[output_buf, glb_bytes_state],
|
422 |
outputs=[download_glb],
|
423 |
).then(
|
424 |
+
# Update status message
|
425 |
+
lambda: "GLB extraction successful! Click 'Download GLB' to save your model.",
|
426 |
+
inputs=None,
|
427 |
+
outputs=[status_message]
|
428 |
)
|
429 |
|
430 |
+
# Download High Quality GLB Button Click Handler
|
431 |
+
download_glb_high_quality_btn.click(
|
432 |
extract_glb_high_quality,
|
433 |
inputs=[output_buf],
|
434 |
+
outputs=[output_buf, glb_high_quality_bytes_state],
|
435 |
).then(
|
436 |
+
# Map the high-quality GLB bytes to the DownloadButton with filename
|
437 |
+
lambda state, glb_bytes: (glb_bytes, "model_high_quality.glb"),
|
438 |
+
inputs=[output_buf, glb_high_quality_bytes_state],
|
439 |
outputs=[download_high_quality_glb],
|
440 |
).then(
|
441 |
+
# Update status message
|
442 |
+
lambda: "High-quality GLB extraction successful! Click 'Download High Quality GLB' to save your model.",
|
443 |
+
inputs=None,
|
444 |
+
outputs=[status_message]
|
445 |
)
|
446 |
|
447 |
# Handle Clearing of Video Output
|
448 |
video_output.clear(
|
449 |
+
lambda: "Video output cleared. Please generate a new 3D asset.",
|
450 |
+
outputs=[status_message],
|
451 |
)
|
452 |
|
453 |
# Handle Clearing of Model Output
|
454 |
model_output.clear(
|
455 |
+
lambda: "Model output cleared. Please extract and download GLB files again.",
|
456 |
+
outputs=[status_message],
|
457 |
)
|
458 |
|
459 |
# Launch the Gradio app
|