update app
Browse files
app.py
CHANGED
|
@@ -7,10 +7,20 @@ import tempfile
|
|
| 7 |
from typing import *
|
| 8 |
from datetime import datetime
|
| 9 |
from pathlib import Path
|
| 10 |
-
|
| 11 |
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",
|
|
@@ -83,18 +93,9 @@ orange_red_theme = OrangeRedTheme()
|
|
| 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 |
|
| 90 |
-
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 |
-
|
| 98 |
MAX_SEED = np.iinfo(np.int32).max
|
| 99 |
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
| 100 |
|
|
@@ -145,7 +146,6 @@ def remove_background(input: Image.Image) -> Image.Image:
|
|
| 145 |
return output
|
| 146 |
|
| 147 |
def preprocess_image(input: Image.Image) -> Image.Image:
|
| 148 |
-
"""Preprocess the input image: remove bg, crop, resize."""
|
| 149 |
if input is None:
|
| 150 |
return None
|
| 151 |
|
|
@@ -184,7 +184,6 @@ def get_seed(randomize_seed: bool, seed: int) -> int:
|
|
| 184 |
|
| 185 |
@spaces.GPU
|
| 186 |
def generate_txt2img(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 187 |
-
"""Generate Image using Z-Image Turbo"""
|
| 188 |
if z_pipe is None:
|
| 189 |
raise gr.Error("Z-Image-Turbo model failed to load.")
|
| 190 |
if not prompt.strip():
|
|
@@ -268,15 +267,12 @@ def generate_3d(
|
|
| 268 |
return_latent=True,
|
| 269 |
)
|
| 270 |
|
| 271 |
-
# 2. Process Mesh
|
| 272 |
progress(0.7, desc="Processing Mesh...")
|
| 273 |
mesh = outputs[0]
|
| 274 |
-
mesh.simplify(16777216)
|
| 275 |
|
| 276 |
-
# 3. Export to GLB
|
| 277 |
progress(0.9, desc="Baking Texture & Exporting GLB...")
|
| 278 |
|
| 279 |
-
# Note: We use the latent grid resolution from the pipeline output
|
| 280 |
grid_size = latents[2]
|
| 281 |
|
| 282 |
glb = o_voxel.postprocess.to_glb(
|
|
@@ -299,10 +295,28 @@ def generate_3d(
|
|
| 299 |
timestamp = now.strftime("%Y-%m-%dT%H%M%S")
|
| 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()
|
|
@@ -320,10 +334,11 @@ if __name__ == "__main__":
|
|
| 320 |
os.makedirs(TMP_DIR, exist_ok=True)
|
| 321 |
|
| 322 |
with gr.Blocks(delete_cache=(300, 300)) as demo:
|
| 323 |
-
gr.Markdown("# **TRELLIS.2 (Text-to-3D)**", elem_id="main-title")
|
| 324 |
gr.Markdown("""
|
| 325 |
**Workflow:**
|
| 326 |
Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
|
|
|
|
| 327 |
""")
|
| 328 |
|
| 329 |
with gr.Row():
|
|
@@ -343,7 +358,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 |
|
|
@@ -369,13 +383,10 @@ if __name__ == "__main__":
|
|
| 369 |
tex_rescale_t = gr.Slider(1.0, 6.0, value=3.0, label="Rescale T")
|
| 370 |
|
| 371 |
with gr.Column(scale=2):
|
| 372 |
-
gr.Markdown("### 3D Output")
|
| 373 |
-
|
| 374 |
-
label="
|
| 375 |
-
|
| 376 |
-
clear_color=(0.2, 0.2, 0.2, 1.0),
|
| 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 |
|
|
@@ -561,7 +572,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)
|
|
|
|
| 7 |
from typing import *
|
| 8 |
from datetime import datetime
|
| 9 |
from pathlib import Path
|
|
|
|
| 10 |
from typing import Iterable
|
| 11 |
+
|
| 12 |
+
# Rerun imports
|
| 13 |
+
import rerun as rr
|
| 14 |
+
from gradio_rerun import Rerun
|
| 15 |
+
|
| 16 |
+
import gradio as gr
|
| 17 |
from gradio.themes import Soft
|
| 18 |
from gradio.themes.utils import colors, fonts, sizes
|
| 19 |
+
from gradio_client import Client, handle_file
|
| 20 |
+
import spaces
|
| 21 |
+
from diffusers import ZImagePipeline
|
| 22 |
+
from trellis2.pipelines import Trellis2ImageTo3DPipeline
|
| 23 |
+
import o_voxel
|
| 24 |
|
| 25 |
colors.orange_red = colors.Color(
|
| 26 |
name="orange_red",
|
|
|
|
| 93 |
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
|
| 94 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 95 |
os.environ["ATTN_BACKEND"] = "flash_attn_3"
|
|
|
|
| 96 |
os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
|
| 97 |
os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
MAX_SEED = np.iinfo(np.int32).max
|
| 100 |
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
| 101 |
|
|
|
|
| 146 |
return output
|
| 147 |
|
| 148 |
def preprocess_image(input: Image.Image) -> Image.Image:
|
|
|
|
| 149 |
if input is None:
|
| 150 |
return None
|
| 151 |
|
|
|
|
| 184 |
|
| 185 |
@spaces.GPU
|
| 186 |
def generate_txt2img(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 187 |
if z_pipe is None:
|
| 188 |
raise gr.Error("Z-Image-Turbo model failed to load.")
|
| 189 |
if not prompt.strip():
|
|
|
|
| 267 |
return_latent=True,
|
| 268 |
)
|
| 269 |
|
|
|
|
| 270 |
progress(0.7, desc="Processing Mesh...")
|
| 271 |
mesh = outputs[0]
|
| 272 |
+
mesh.simplify(16777216)
|
| 273 |
|
|
|
|
| 274 |
progress(0.9, desc="Baking Texture & Exporting GLB...")
|
| 275 |
|
|
|
|
| 276 |
grid_size = latents[2]
|
| 277 |
|
| 278 |
glb = o_voxel.postprocess.to_glb(
|
|
|
|
| 295 |
timestamp = now.strftime("%Y-%m-%dT%H%M%S")
|
| 296 |
glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
|
| 297 |
glb.export(glb_path, extension_webp=True)
|
| 298 |
+
|
| 299 |
+
# --- Rerun Visualization Setup ---
|
| 300 |
+
progress(0.95, desc="Creating Rerun Visualization...")
|
| 301 |
+
rr.init("Trellis_3D", spawn=False)
|
| 302 |
+
|
| 303 |
+
# Log the 3D Asset
|
| 304 |
+
rr.log("generated_model", rr.Asset3D(path=glb_path))
|
| 305 |
+
|
| 306 |
+
# Log Axis and View Coordinates (Z-Up)
|
| 307 |
+
rr.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True)
|
| 308 |
+
rr.log("world/xyz", rr.Arrows3D(
|
| 309 |
+
vectors=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
|
| 310 |
+
colors=[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
|
| 311 |
+
labels=["X", "Y", "Z"],
|
| 312 |
+
origins=[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
|
| 313 |
+
))
|
| 314 |
+
|
| 315 |
+
rrd_path = os.path.join(user_dir, f'trellis_output_{timestamp}.rrd')
|
| 316 |
+
rr.save(rrd_path)
|
| 317 |
|
|
|
|
| 318 |
torch.cuda.empty_cache()
|
| 319 |
+
return rrd_path, glb_path
|
| 320 |
|
| 321 |
except Exception as e:
|
| 322 |
torch.cuda.empty_cache()
|
|
|
|
| 334 |
os.makedirs(TMP_DIR, exist_ok=True)
|
| 335 |
|
| 336 |
with gr.Blocks(delete_cache=(300, 300)) as demo:
|
| 337 |
+
gr.Markdown("# **TRELLIS.2 (Text-to-3D) with Rerun**", elem_id="main-title")
|
| 338 |
gr.Markdown("""
|
| 339 |
**Workflow:**
|
| 340 |
Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
|
| 341 |
+
Visualization powered by Rerun.
|
| 342 |
""")
|
| 343 |
|
| 344 |
with gr.Row():
|
|
|
|
| 358 |
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 359 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 360 |
|
|
|
|
| 361 |
decimation_target = gr.Slider(50000, 500000, label="Target Faces", value=150000, step=10000)
|
| 362 |
texture_size = gr.Slider(512, 4096, label="Texture Size", value=1024, step=512)
|
| 363 |
|
|
|
|
| 383 |
tex_rescale_t = gr.Slider(1.0, 6.0, value=3.0, label="Rescale T")
|
| 384 |
|
| 385 |
with gr.Column(scale=2):
|
| 386 |
+
gr.Markdown("### 3D Output (Rerun Viewer)")
|
| 387 |
+
rerun_output = Rerun(
|
| 388 |
+
label="Rerun 3D Visualization",
|
| 389 |
+
height=600
|
|
|
|
|
|
|
|
|
|
| 390 |
)
|
| 391 |
download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
|
| 392 |
|
|
|
|
| 572 |
shape_guidance, shape_rescale, shape_steps, shape_rescale_t,
|
| 573 |
tex_guidance, tex_rescale, tex_steps, tex_rescale_t,
|
| 574 |
],
|
| 575 |
+
outputs=[rerun_output, download_btn],
|
| 576 |
)
|
| 577 |
|
| 578 |
demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False, show_error=True)
|