update app
Browse files
app.py
CHANGED
|
@@ -7,24 +7,23 @@ import tempfile
|
|
| 7 |
from typing import *
|
| 8 |
from datetime import datetime
|
| 9 |
from pathlib import Path
|
| 10 |
-
import uuid
|
| 11 |
-
|
| 12 |
from typing import Iterable
|
| 13 |
-
from gradio.themes import Soft
|
| 14 |
-
from gradio.themes.utils import colors, fonts, sizes
|
| 15 |
|
| 16 |
import gradio as gr
|
| 17 |
from gradio_client import Client, handle_file
|
|
|
|
|
|
|
|
|
|
| 18 |
import spaces
|
| 19 |
from diffusers import ZImagePipeline
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Rerun
|
| 22 |
import rerun as rr
|
| 23 |
from gradio_rerun import Rerun
|
| 24 |
|
| 25 |
-
|
| 26 |
-
import o_voxel
|
| 27 |
-
|
| 28 |
colors.orange_red = colors.Color(
|
| 29 |
name="orange_red",
|
| 30 |
c50="#FFF0E5",
|
|
@@ -93,6 +92,7 @@ class OrangeRedTheme(Soft):
|
|
| 93 |
|
| 94 |
orange_red_theme = OrangeRedTheme()
|
| 95 |
|
|
|
|
| 96 |
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
|
| 97 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 98 |
os.environ["ATTN_BACKEND"] = "flash_attn_3"
|
|
@@ -102,9 +102,8 @@ os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
|
|
| 102 |
MAX_SEED = np.iinfo(np.int32).max
|
| 103 |
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
| 104 |
|
|
|
|
| 105 |
print("Initializing models...")
|
| 106 |
-
|
| 107 |
-
print("Loading Z-Image-Turbo...")
|
| 108 |
try:
|
| 109 |
z_pipe = ZImagePipeline.from_pretrained(
|
| 110 |
"Tongyi-MAI/Z-Image-Turbo",
|
|
@@ -118,7 +117,6 @@ except Exception as e:
|
|
| 118 |
print(f"Failed to load Z-Image-Turbo: {e}")
|
| 119 |
z_pipe = None
|
| 120 |
|
| 121 |
-
print("Loading TRELLIS.2...")
|
| 122 |
try:
|
| 123 |
trellis_pipeline = Trellis2ImageTo3DPipeline.from_pretrained('microsoft/TRELLIS.2-4B')
|
| 124 |
trellis_pipeline.rembg_model = None
|
|
@@ -210,30 +208,6 @@ def generate_txt2img(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
| 210 |
except Exception as e:
|
| 211 |
raise gr.Error(f"Z-Image Generation failed: {str(e)}")
|
| 212 |
|
| 213 |
-
def create_rerun_rrd(glb_path: str) -> str:
|
| 214 |
-
"""Creates a Rerun recording for the generated GLB."""
|
| 215 |
-
rrd_path = glb_path.replace(".glb", ".rrd")
|
| 216 |
-
|
| 217 |
-
rec = rr.new_recording(application_id="trellis_app", recording_id=str(uuid.uuid4()))
|
| 218 |
-
|
| 219 |
-
rec.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True)
|
| 220 |
-
|
| 221 |
-
rec.log(
|
| 222 |
-
"world/axes",
|
| 223 |
-
rr.Arrows3D(
|
| 224 |
-
vectors=[[0.5, 0, 0], [0, 0.5, 0], [0, 0, 0.5]],
|
| 225 |
-
colors=[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
|
| 226 |
-
labels=["X", "Y", "Z"],
|
| 227 |
-
radii=0.02
|
| 228 |
-
),
|
| 229 |
-
static=True
|
| 230 |
-
)
|
| 231 |
-
|
| 232 |
-
rec.log("world/model", rr.Asset3D(path=glb_path), static=True)
|
| 233 |
-
|
| 234 |
-
rec.save(rrd_path)
|
| 235 |
-
return rrd_path
|
| 236 |
-
|
| 237 |
@spaces.GPU(duration=120)
|
| 238 |
def generate_3d(
|
| 239 |
image: Image.Image,
|
|
@@ -296,7 +270,7 @@ def generate_3d(
|
|
| 296 |
|
| 297 |
progress(0.7, desc="Processing Mesh...")
|
| 298 |
mesh = outputs[0]
|
| 299 |
-
mesh.simplify(16777216)
|
| 300 |
|
| 301 |
progress(0.9, desc="Baking Texture & Exporting GLB...")
|
| 302 |
|
|
@@ -322,8 +296,29 @@ def generate_3d(
|
|
| 322 |
timestamp = now.strftime("%Y-%m-%dT%H%M%S")
|
| 323 |
glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
|
| 324 |
glb.export(glb_path, extension_webp=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
-
|
|
|
|
| 327 |
|
| 328 |
torch.cuda.empty_cache()
|
| 329 |
return rrd_path, glb_path
|
|
@@ -344,11 +339,11 @@ if __name__ == "__main__":
|
|
| 344 |
os.makedirs(TMP_DIR, exist_ok=True)
|
| 345 |
|
| 346 |
with gr.Blocks(delete_cache=(300, 300)) as demo:
|
| 347 |
-
gr.Markdown("# **TRELLIS.2 (Text-to-3D)
|
| 348 |
gr.Markdown("""
|
| 349 |
**Workflow:**
|
| 350 |
Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
|
| 351 |
-
|
| 352 |
""")
|
| 353 |
|
| 354 |
with gr.Row():
|
|
@@ -394,9 +389,10 @@ if __name__ == "__main__":
|
|
| 394 |
|
| 395 |
with gr.Column(scale=2):
|
| 396 |
gr.Markdown("### 3D Output (Rerun Viewer)")
|
|
|
|
| 397 |
rerun_output = Rerun(
|
| 398 |
-
label="Rerun 3D Viewer",
|
| 399 |
-
height=600
|
| 400 |
)
|
| 401 |
download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
|
| 402 |
|
|
|
|
| 7 |
from typing import *
|
| 8 |
from datetime import datetime
|
| 9 |
from pathlib import Path
|
|
|
|
|
|
|
| 10 |
from typing import Iterable
|
|
|
|
|
|
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
from gradio_client import Client, handle_file
|
| 14 |
+
from gradio.themes import Soft
|
| 15 |
+
from gradio.themes.utils import colors, fonts, sizes
|
| 16 |
+
|
| 17 |
import spaces
|
| 18 |
from diffusers import ZImagePipeline
|
| 19 |
+
from trellis2.pipelines import Trellis2ImageTo3DPipeline
|
| 20 |
+
import o_voxel
|
| 21 |
|
| 22 |
+
# Rerun imports
|
| 23 |
import rerun as rr
|
| 24 |
from gradio_rerun import Rerun
|
| 25 |
|
| 26 |
+
# Theme Configuration
|
|
|
|
|
|
|
| 27 |
colors.orange_red = colors.Color(
|
| 28 |
name="orange_red",
|
| 29 |
c50="#FFF0E5",
|
|
|
|
| 92 |
|
| 93 |
orange_red_theme = OrangeRedTheme()
|
| 94 |
|
| 95 |
+
# Environment Config
|
| 96 |
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = '1'
|
| 97 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 98 |
os.environ["ATTN_BACKEND"] = "flash_attn_3"
|
|
|
|
| 102 |
MAX_SEED = np.iinfo(np.int32).max
|
| 103 |
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
| 104 |
|
| 105 |
+
# Model Loading
|
| 106 |
print("Initializing models...")
|
|
|
|
|
|
|
| 107 |
try:
|
| 108 |
z_pipe = ZImagePipeline.from_pretrained(
|
| 109 |
"Tongyi-MAI/Z-Image-Turbo",
|
|
|
|
| 117 |
print(f"Failed to load Z-Image-Turbo: {e}")
|
| 118 |
z_pipe = None
|
| 119 |
|
|
|
|
| 120 |
try:
|
| 121 |
trellis_pipeline = Trellis2ImageTo3DPipeline.from_pretrained('microsoft/TRELLIS.2-4B')
|
| 122 |
trellis_pipeline.rembg_model = None
|
|
|
|
| 208 |
except Exception as e:
|
| 209 |
raise gr.Error(f"Z-Image Generation failed: {str(e)}")
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
@spaces.GPU(duration=120)
|
| 212 |
def generate_3d(
|
| 213 |
image: Image.Image,
|
|
|
|
| 270 |
|
| 271 |
progress(0.7, desc="Processing Mesh...")
|
| 272 |
mesh = outputs[0]
|
| 273 |
+
mesh.simplify(16777216)
|
| 274 |
|
| 275 |
progress(0.9, desc="Baking Texture & Exporting GLB...")
|
| 276 |
|
|
|
|
| 296 |
timestamp = now.strftime("%Y-%m-%dT%H%M%S")
|
| 297 |
glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
|
| 298 |
glb.export(glb_path, extension_webp=True)
|
| 299 |
+
|
| 300 |
+
# --- RERUN LOGGING ---
|
| 301 |
+
progress(0.95, desc="Creating Rerun Visualization...")
|
| 302 |
+
rrd_path = os.path.join(user_dir, f'trellis_output_{timestamp}.rrd')
|
| 303 |
+
|
| 304 |
+
# Initialize Rerun
|
| 305 |
+
rr.init(f"Trellis_Session_{timestamp}", spawn=False)
|
| 306 |
+
|
| 307 |
+
# Setup View Coordinates (Z-UP is standard for Trellis)
|
| 308 |
+
rr.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True)
|
| 309 |
+
|
| 310 |
+
# Add Reference Axis
|
| 311 |
+
rr.log("world/axis", rr.Arrows3D(
|
| 312 |
+
vectors=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
|
| 313 |
+
colors=[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
|
| 314 |
+
radii=0.02
|
| 315 |
+
), static=True)
|
| 316 |
+
|
| 317 |
+
# Log the generated 3D model
|
| 318 |
+
rr.log("world/model", rr.Asset3D(path=glb_path))
|
| 319 |
|
| 320 |
+
# Save to RRD file for Gradio
|
| 321 |
+
rr.save(rrd_path)
|
| 322 |
|
| 323 |
torch.cuda.empty_cache()
|
| 324 |
return rrd_path, glb_path
|
|
|
|
| 339 |
os.makedirs(TMP_DIR, exist_ok=True)
|
| 340 |
|
| 341 |
with gr.Blocks(delete_cache=(300, 300)) as demo:
|
| 342 |
+
gr.Markdown("# **TRELLIS.2 (Text-to-3D)**", elem_id="main-title")
|
| 343 |
gr.Markdown("""
|
| 344 |
**Workflow:**
|
| 345 |
Generate a 3D asset directly by converting Text-to-Image → 3D or Image-to-3D, powered by TRELLIS.2 and Z-Image-Turbo.
|
| 346 |
+
**Visualized with Rerun.**
|
| 347 |
""")
|
| 348 |
|
| 349 |
with gr.Row():
|
|
|
|
| 389 |
|
| 390 |
with gr.Column(scale=2):
|
| 391 |
gr.Markdown("### 3D Output (Rerun Viewer)")
|
| 392 |
+
# Replaced Model3D with Rerun Component
|
| 393 |
rerun_output = Rerun(
|
| 394 |
+
label="Rerun 3D Viewer",
|
| 395 |
+
height=600
|
| 396 |
)
|
| 397 |
download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
|
| 398 |
|