update app
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import torch
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import tempfile
|
|
|
|
| 7 |
from typing import *
|
| 8 |
from datetime import datetime
|
| 9 |
from pathlib import Path
|
|
@@ -12,6 +13,10 @@ from typing import Iterable
|
|
| 12 |
from gradio.themes import Soft
|
| 13 |
from gradio.themes.utils import colors, fonts, sizes
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
colors.orange_red = colors.Color(
|
| 16 |
name="orange_red",
|
| 17 |
c50="#FFF0E5",
|
|
@@ -80,10 +85,10 @@ class OrangeRedTheme(Soft):
|
|
| 80 |
|
| 81 |
orange_red_theme = OrangeRedTheme()
|
| 82 |
|
|
|
|
| 83 |
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
|
| 84 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 85 |
os.environ["ATTN_BACKEND"] = "flash_attn_3"
|
| 86 |
-
|
| 87 |
os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
|
| 88 |
os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
|
| 89 |
|
|
@@ -91,7 +96,6 @@ import gradio as gr
|
|
| 91 |
from gradio_client import Client, handle_file
|
| 92 |
import spaces
|
| 93 |
from diffusers import ZImagePipeline
|
| 94 |
-
|
| 95 |
from trellis2.pipelines import Trellis2ImageTo3DPipeline
|
| 96 |
import o_voxel
|
| 97 |
|
|
@@ -300,9 +304,32 @@ def generate_3d(
|
|
| 300 |
glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
|
| 301 |
glb.export(glb_path, extension_webp=True)
|
| 302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
# Clean up
|
| 304 |
torch.cuda.empty_cache()
|
| 305 |
-
return
|
| 306 |
|
| 307 |
except Exception as e:
|
| 308 |
torch.cuda.empty_cache()
|
|
@@ -343,7 +370,6 @@ if __name__ == "__main__":
|
|
| 343 |
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 344 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 345 |
|
| 346 |
-
#gr.Markdown("**Export Settings**")
|
| 347 |
decimation_target = gr.Slider(50000, 500000, label="Target Faces", value=150000, step=10000)
|
| 348 |
texture_size = gr.Slider(512, 4096, label="Texture Size", value=1024, step=512)
|
| 349 |
|
|
@@ -370,13 +396,13 @@ if __name__ == "__main__":
|
|
| 370 |
|
| 371 |
with gr.Column(scale=2):
|
| 372 |
gr.Markdown("### 3D Output")
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
height=600
|
| 378 |
-
interactive=False # Changed to False to hide upload area
|
| 379 |
)
|
|
|
|
| 380 |
download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
|
| 381 |
|
| 382 |
gr.Examples(
|
|
@@ -386,72 +412,6 @@ if __name__ == "__main__":
|
|
| 386 |
["example-images/A (3).webp"],
|
| 387 |
["example-images/A (4).webp"],
|
| 388 |
["example-images/A (5).webp"],
|
| 389 |
-
["example-images/A (6).webp"],
|
| 390 |
-
["example-images/A (7).webp"],
|
| 391 |
-
["example-images/A (8).webp"],
|
| 392 |
-
["example-images/A (9).webp"],
|
| 393 |
-
["example-images/A (10).webp"],
|
| 394 |
-
["example-images/A (11).webp"],
|
| 395 |
-
["example-images/A (12).webp"],
|
| 396 |
-
["example-images/A (13).webp"],
|
| 397 |
-
["example-images/A (14).webp"],
|
| 398 |
-
["example-images/A (15).webp"],
|
| 399 |
-
["example-images/A (16).webp"],
|
| 400 |
-
["example-images/A (17).webp"],
|
| 401 |
-
["example-images/A (18).webp"],
|
| 402 |
-
["example-images/A (19).webp"],
|
| 403 |
-
["example-images/A (20).webp"],
|
| 404 |
-
["example-images/A (21).webp"],
|
| 405 |
-
["example-images/A (22).webp"],
|
| 406 |
-
["example-images/A (23).webp"],
|
| 407 |
-
["example-images/A (24).webp"],
|
| 408 |
-
["example-images/A (25).webp"],
|
| 409 |
-
["example-images/A (26).webp"],
|
| 410 |
-
["example-images/A (27).webp"],
|
| 411 |
-
["example-images/A (28).webp"],
|
| 412 |
-
["example-images/A (29).webp"],
|
| 413 |
-
["example-images/A (30).webp"],
|
| 414 |
-
["example-images/A (31).webp"],
|
| 415 |
-
["example-images/A (32).webp"],
|
| 416 |
-
["example-images/A (33).webp"],
|
| 417 |
-
["example-images/A (34).webp"],
|
| 418 |
-
["example-images/A (35).webp"],
|
| 419 |
-
["example-images/A (36).webp"],
|
| 420 |
-
["example-images/A (37).webp"],
|
| 421 |
-
["example-images/A (38).webp"],
|
| 422 |
-
["example-images/A (39).webp"],
|
| 423 |
-
["example-images/A (40).webp"],
|
| 424 |
-
["example-images/A (41).webp"],
|
| 425 |
-
["example-images/A (42).webp"],
|
| 426 |
-
["example-images/A (43).webp"],
|
| 427 |
-
["example-images/A (44).webp"],
|
| 428 |
-
["example-images/A (45).webp"],
|
| 429 |
-
["example-images/A (46).webp"],
|
| 430 |
-
["example-images/A (47).webp"],
|
| 431 |
-
["example-images/A (48).webp"],
|
| 432 |
-
["example-images/A (49).webp"],
|
| 433 |
-
["example-images/A (50).webp"],
|
| 434 |
-
["example-images/A (51).webp"],
|
| 435 |
-
["example-images/A (52).webp"],
|
| 436 |
-
["example-images/A (53).webp"],
|
| 437 |
-
["example-images/A (54).webp"],
|
| 438 |
-
["example-images/A (55).webp"],
|
| 439 |
-
["example-images/A (56).webp"],
|
| 440 |
-
["example-images/A (57).webp"],
|
| 441 |
-
["example-images/A (58).webp"],
|
| 442 |
-
["example-images/A (59).webp"],
|
| 443 |
-
["example-images/A (60).webp"],
|
| 444 |
-
["example-images/A (61).webp"],
|
| 445 |
-
["example-images/A (62).webp"],
|
| 446 |
-
["example-images/A (63).webp"],
|
| 447 |
-
["example-images/A (64).webp"],
|
| 448 |
-
["example-images/A (65).webp"],
|
| 449 |
-
["example-images/A (66).webp"],
|
| 450 |
-
["example-images/A (67).webp"],
|
| 451 |
-
["example-images/A (68).webp"],
|
| 452 |
-
["example-images/A (69).webp"],
|
| 453 |
-
["example-images/A (70).webp"],
|
| 454 |
-
["example-images/A (71).webp"],
|
| 455 |
],
|
| 456 |
inputs=[image_prompt],
|
| 457 |
label="Image Examples [image-to-3d]"
|
|
@@ -460,70 +420,9 @@ if __name__ == "__main__":
|
|
| 460 |
gr.Examples(
|
| 461 |
examples=[
|
| 462 |
["A Cat 3D model"],
|
| 463 |
-
["A realistic Cat 3D model"],
|
| 464 |
-
["A cartoon Cat 3D model"],
|
| 465 |
-
["A low poly Cat 3D"],
|
| 466 |
-
["A cyberpunk Cat 3D"],
|
| 467 |
-
["A robotic Cat 3D"],
|
| 468 |
-
["A fluffy Cat 3D"],
|
| 469 |
-
["A fantasy Cat 3D creature"],
|
| 470 |
-
["A stylized Cat 3D"],
|
| 471 |
-
["A Cat 3D sculpture"],
|
| 472 |
-
|
| 473 |
-
["A Plane 3D model"],
|
| 474 |
-
["A commercial Plane 3D"],
|
| 475 |
-
["A fighter jet Plane 3D"],
|
| 476 |
-
["A low poly Plane 3D"],
|
| 477 |
-
["A vintage Plane 3D"],
|
| 478 |
-
["A futuristic Plane 3D"],
|
| 479 |
-
["A cargo Plane 3D"],
|
| 480 |
-
["A private jet Plane 3D"],
|
| 481 |
-
["A toy Plane 3D"],
|
| 482 |
["A realistic Plane 3D"],
|
| 483 |
-
|
| 484 |
-
["A Car 3D model"],
|
| 485 |
-
["A sports Car 3D"],
|
| 486 |
-
["A luxury Car 3D"],
|
| 487 |
-
["A low poly Car 3D"],
|
| 488 |
-
["A racing Car 3D"],
|
| 489 |
["A cyberpunk Car 3D"],
|
| 490 |
-
["A vintage Car 3D"],
|
| 491 |
-
["A futuristic Car 3D"],
|
| 492 |
-
["A SUV Car 3D"],
|
| 493 |
-
["A electric Car 3D"],
|
| 494 |
-
|
| 495 |
-
["A Shoe 3D model"],
|
| 496 |
-
["A sneaker Shoe 3D"],
|
| 497 |
-
["A running Shoe 3D"],
|
| 498 |
-
["A leather Shoe 3D"],
|
| 499 |
-
["A high heel Shoe 3D"],
|
| 500 |
-
["A boot Shoe 3D"],
|
| 501 |
-
["A low poly Shoe 3D"],
|
| 502 |
["A futuristic Shoe 3D"],
|
| 503 |
-
["A sports Shoe 3D"],
|
| 504 |
-
["A casual Shoe 3D"],
|
| 505 |
-
|
| 506 |
-
["A Chair 3D model"],
|
| 507 |
-
["A Table 3D model"],
|
| 508 |
-
["A Sofa 3D model"],
|
| 509 |
-
["A Lamp 3D model"],
|
| 510 |
-
["A Watch 3D model"],
|
| 511 |
-
["A Backpack 3D model"],
|
| 512 |
-
["A Drone 3D model"],
|
| 513 |
-
["A Robot 3D model"],
|
| 514 |
-
["A Smartphone 3D model"],
|
| 515 |
-
["A Headphones 3D model"],
|
| 516 |
-
|
| 517 |
-
["A House 3D model"],
|
| 518 |
-
["A Skyscraper 3D model"],
|
| 519 |
-
["A Bridge 3D model"],
|
| 520 |
-
["A Castle 3D model"],
|
| 521 |
-
["A Spaceship 3D model"],
|
| 522 |
-
["A Rocket 3D model"],
|
| 523 |
-
["A Satellite 3D model"],
|
| 524 |
-
["A Tank 3D model"],
|
| 525 |
-
["A Motorcycle 3D model"],
|
| 526 |
-
["A Bicycle 3D model"]
|
| 527 |
],
|
| 528 |
inputs=[txt_prompt],
|
| 529 |
label="3D Prompt Examples [text-to-3d]"
|
|
@@ -561,7 +460,7 @@ if __name__ == "__main__":
|
|
| 561 |
shape_guidance, shape_rescale, shape_steps, shape_rescale_t,
|
| 562 |
tex_guidance, tex_rescale, tex_steps, tex_rescale_t,
|
| 563 |
],
|
| 564 |
-
outputs=[
|
| 565 |
)
|
| 566 |
|
| 567 |
demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False, show_error=True)
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import tempfile
|
| 7 |
+
import uuid
|
| 8 |
from typing import *
|
| 9 |
from datetime import datetime
|
| 10 |
from pathlib import Path
|
|
|
|
| 13 |
from gradio.themes import Soft
|
| 14 |
from gradio.themes.utils import colors, fonts, sizes
|
| 15 |
|
| 16 |
+
import rerun as rr
|
| 17 |
+
from gradio_rerun import Rerun
|
| 18 |
+
|
| 19 |
+
# --- Theme Configuration ---
|
| 20 |
colors.orange_red = colors.Color(
|
| 21 |
name="orange_red",
|
| 22 |
c50="#FFF0E5",
|
|
|
|
| 85 |
|
| 86 |
orange_red_theme = OrangeRedTheme()
|
| 87 |
|
| 88 |
+
# --- Environment Setup ---
|
| 89 |
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
|
| 90 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 91 |
os.environ["ATTN_BACKEND"] = "flash_attn_3"
|
|
|
|
| 92 |
os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
|
| 93 |
os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
|
| 94 |
|
|
|
|
| 96 |
from gradio_client import Client, handle_file
|
| 97 |
import spaces
|
| 98 |
from diffusers import ZImagePipeline
|
|
|
|
| 99 |
from trellis2.pipelines import Trellis2ImageTo3DPipeline
|
| 100 |
import o_voxel
|
| 101 |
|
|
|
|
| 304 |
glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
|
| 305 |
glb.export(glb_path, extension_webp=True)
|
| 306 |
|
| 307 |
+
# --- Rerun Visualization Logic ---
|
| 308 |
+
progress(0.95, desc="Creating Rerun Visualization...")
|
| 309 |
+
|
| 310 |
+
run_id = str(uuid.uuid4())
|
| 311 |
+
|
| 312 |
+
# Create a new Rerun recording
|
| 313 |
+
rec = rr.new_recording(application_id="TRELLIS-3D-Viewer", recording_id=run_id)
|
| 314 |
+
|
| 315 |
+
# Read the GLB data to embed it in the RRD
|
| 316 |
+
# This ensures the web viewer can see it without needing local file access
|
| 317 |
+
with open(glb_path, 'rb') as f:
|
| 318 |
+
glb_data = f.read()
|
| 319 |
+
|
| 320 |
+
# Log the GLB asset to Rerun
|
| 321 |
+
rec.log("generated_model", rr.Asset3D(blob=glb_data, media_type=rr.MediaType.GLB), static=True)
|
| 322 |
+
|
| 323 |
+
# Also log the input image for context
|
| 324 |
+
rec.log("input_image", rr.Image(np.array(image)), static=True)
|
| 325 |
+
|
| 326 |
+
# Save the Rerun recording (.rrd)
|
| 327 |
+
rrd_path = os.path.join(user_dir, f'trellis_output_{timestamp}.rrd')
|
| 328 |
+
rec.save(rrd_path)
|
| 329 |
+
|
| 330 |
# Clean up
|
| 331 |
torch.cuda.empty_cache()
|
| 332 |
+
return rrd_path, glb_path
|
| 333 |
|
| 334 |
except Exception as e:
|
| 335 |
torch.cuda.empty_cache()
|
|
|
|
| 370 |
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 371 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 372 |
|
|
|
|
| 373 |
decimation_target = gr.Slider(50000, 500000, label="Target Faces", value=150000, step=10000)
|
| 374 |
texture_size = gr.Slider(512, 4096, label="Texture Size", value=1024, step=512)
|
| 375 |
|
|
|
|
| 396 |
|
| 397 |
with gr.Column(scale=2):
|
| 398 |
gr.Markdown("### 3D Output")
|
| 399 |
+
|
| 400 |
+
# Replaced gr.Model3D with Rerun component
|
| 401 |
+
rerun_output = Rerun(
|
| 402 |
+
label="Rerun 3D Viewer",
|
| 403 |
+
height=600
|
|
|
|
| 404 |
)
|
| 405 |
+
|
| 406 |
download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
|
| 407 |
|
| 408 |
gr.Examples(
|
|
|
|
| 412 |
["example-images/A (3).webp"],
|
| 413 |
["example-images/A (4).webp"],
|
| 414 |
["example-images/A (5).webp"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
],
|
| 416 |
inputs=[image_prompt],
|
| 417 |
label="Image Examples [image-to-3d]"
|
|
|
|
| 420 |
gr.Examples(
|
| 421 |
examples=[
|
| 422 |
["A Cat 3D model"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
["A realistic Plane 3D"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
["A cyberpunk Car 3D"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
["A futuristic Shoe 3D"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
],
|
| 427 |
inputs=[txt_prompt],
|
| 428 |
label="3D Prompt Examples [text-to-3d]"
|
|
|
|
| 460 |
shape_guidance, shape_rescale, shape_steps, shape_rescale_t,
|
| 461 |
tex_guidance, tex_rescale, tex_steps, tex_rescale_t,
|
| 462 |
],
|
| 463 |
+
outputs=[rerun_output, download_btn],
|
| 464 |
)
|
| 465 |
|
| 466 |
demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False, show_error=True)
|