update app
Browse files
app.py
CHANGED
|
@@ -12,18 +12,6 @@ from typing import Iterable
|
|
| 12 |
from gradio.themes import Soft
|
| 13 |
from gradio.themes.utils import colors, fonts, sizes
|
| 14 |
|
| 15 |
-
import gradio as gr
|
| 16 |
-
from gradio_client import Client, handle_file
|
| 17 |
-
import spaces
|
| 18 |
-
from diffusers import ZImagePipeline
|
| 19 |
-
|
| 20 |
-
# Rerun imports
|
| 21 |
-
import rerun as rr
|
| 22 |
-
from gradio_rerun import Rerun
|
| 23 |
-
|
| 24 |
-
from trellis2.pipelines import Trellis2ImageTo3DPipeline
|
| 25 |
-
import o_voxel
|
| 26 |
-
|
| 27 |
colors.orange_red = colors.Color(
|
| 28 |
name="orange_red",
|
| 29 |
c50="#FFF0E5",
|
|
@@ -99,10 +87,19 @@ os.environ["ATTN_BACKEND"] = "flash_attn_3"
|
|
| 99 |
os.environ["FLEX_GEMM_AUTOTUNE_CACHE_PATH"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'autotune_cache.json')
|
| 100 |
os.environ["FLEX_GEMM_AUTOTUNER_VERBOSE"] = '1'
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
print("Loading Z-Image-Turbo...")
|
| 107 |
try:
|
| 108 |
z_pipe = ZImagePipeline.from_pretrained(
|
|
@@ -148,6 +145,7 @@ def remove_background(input: Image.Image) -> Image.Image:
|
|
| 148 |
return output
|
| 149 |
|
| 150 |
def preprocess_image(input: Image.Image) -> Image.Image:
|
|
|
|
| 151 |
if input is None:
|
| 152 |
return None
|
| 153 |
|
|
@@ -186,6 +184,7 @@ def get_seed(randomize_seed: bool, seed: int) -> int:
|
|
| 186 |
|
| 187 |
@spaces.GPU
|
| 188 |
def generate_txt2img(prompt, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 189 |
if z_pipe is None:
|
| 190 |
raise gr.Error("Z-Image-Turbo model failed to load.")
|
| 191 |
if not prompt.strip():
|
|
@@ -269,12 +268,15 @@ def generate_3d(
|
|
| 269 |
return_latent=True,
|
| 270 |
)
|
| 271 |
|
|
|
|
| 272 |
progress(0.7, desc="Processing Mesh...")
|
| 273 |
mesh = outputs[0]
|
| 274 |
-
mesh.simplify(16777216)
|
| 275 |
|
|
|
|
| 276 |
progress(0.9, desc="Baking Texture & Exporting GLB...")
|
| 277 |
|
|
|
|
| 278 |
grid_size = latents[2]
|
| 279 |
|
| 280 |
glb = o_voxel.postprocess.to_glb(
|
|
@@ -297,28 +299,10 @@ def generate_3d(
|
|
| 297 |
timestamp = now.strftime("%Y-%m-%dT%H%M%S")
|
| 298 |
glb_path = os.path.join(user_dir, f'trellis_output_{timestamp}.glb')
|
| 299 |
glb.export(glb_path, extension_webp=True)
|
| 300 |
-
|
| 301 |
-
# Rerun Visualization Logic
|
| 302 |
-
progress(0.95, desc="Preparing Rerun Visualization...")
|
| 303 |
-
rrd_path = os.path.join(user_dir, f'trellis_output_{timestamp}.rrd')
|
| 304 |
-
|
| 305 |
-
rec = rr.new_recording(application_id="TRELLIS_Viewer", recording_id=str(req.session_hash))
|
| 306 |
-
|
| 307 |
-
# Set view coordinates (Z-Up)
|
| 308 |
-
rec.log("world", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True)
|
| 309 |
-
|
| 310 |
-
# Log visual reference axis
|
| 311 |
-
rec.log("world/axis/x", rr.Arrows3D(vectors=[[0.5, 0, 0]], colors=[[255, 0, 0]], radii=[0.02]), static=True)
|
| 312 |
-
rec.log("world/axis/y", rr.Arrows3D(vectors=[[0, 0.5, 0]], colors=[[0, 255, 0]], radii=[0.02]), static=True)
|
| 313 |
-
rec.log("world/axis/z", rr.Arrows3D(vectors=[[0, 0, 0.5]], colors=[[0, 0, 255]], radii=[0.02]), static=True)
|
| 314 |
-
|
| 315 |
-
# Log the 3D asset
|
| 316 |
-
rec.log("world/model", rr.Asset3D(path=glb_path), static=True)
|
| 317 |
-
|
| 318 |
-
rec.save(rrd_path)
|
| 319 |
|
|
|
|
| 320 |
torch.cuda.empty_cache()
|
| 321 |
-
return
|
| 322 |
|
| 323 |
except Exception as e:
|
| 324 |
torch.cuda.empty_cache()
|
|
@@ -359,6 +343,7 @@ if __name__ == "__main__":
|
|
| 359 |
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 360 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 361 |
|
|
|
|
| 362 |
decimation_target = gr.Slider(50000, 500000, label="Target Faces", value=150000, step=10000)
|
| 363 |
texture_size = gr.Slider(512, 4096, label="Texture Size", value=1024, step=512)
|
| 364 |
|
|
@@ -385,13 +370,13 @@ if __name__ == "__main__":
|
|
| 385 |
|
| 386 |
with gr.Column(scale=2):
|
| 387 |
gr.Markdown("### 3D Output")
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
height=600,
|
|
|
|
| 393 |
)
|
| 394 |
-
|
| 395 |
download_btn = gr.DownloadButton(label="3.Download GLB File", variant="primary")
|
| 396 |
|
| 397 |
gr.Examples(
|
|
@@ -575,9 +560,8 @@ if __name__ == "__main__":
|
|
| 575 |
ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
|
| 576 |
shape_guidance, shape_rescale, shape_steps, shape_rescale_t,
|
| 577 |
tex_guidance, tex_rescale, tex_steps, tex_rescale_t,
|
| 578 |
-
|
| 579 |
],
|
| 580 |
-
outputs=[
|
| 581 |
)
|
| 582 |
|
| 583 |
demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False, show_error=True)
|
|
|
|
| 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",
|
|
|
|
| 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 |
|
| 101 |
print("Initializing models...")
|
| 102 |
+
|
| 103 |
print("Loading Z-Image-Turbo...")
|
| 104 |
try:
|
| 105 |
z_pipe = ZImagePipeline.from_pretrained(
|
|
|
|
| 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 |
|
| 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 |
return_latent=True,
|
| 269 |
)
|
| 270 |
|
| 271 |
+
# 2. Process Mesh
|
| 272 |
progress(0.7, desc="Processing Mesh...")
|
| 273 |
mesh = outputs[0]
|
| 274 |
+
mesh.simplify(16777216) # Simplify for processing limits
|
| 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 |
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 glb_path, glb_path
|
| 306 |
|
| 307 |
except Exception as e:
|
| 308 |
torch.cuda.empty_cache()
|
|
|
|
| 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 |
|
| 371 |
with gr.Column(scale=2):
|
| 372 |
gr.Markdown("### 3D Output")
|
| 373 |
+
glb_output = gr.Model3D(
|
| 374 |
+
label="Generated GLB",
|
| 375 |
+
display_mode="solid",
|
| 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 |
|
| 382 |
gr.Examples(
|
|
|
|
| 560 |
ss_guidance_strength, ss_guidance_rescale, ss_sampling_steps, ss_rescale_t,
|
| 561 |
shape_guidance, shape_rescale, shape_steps, shape_rescale_t,
|
| 562 |
tex_guidance, tex_rescale, tex_steps, tex_rescale_t,
|
|
|
|
| 563 |
],
|
| 564 |
+
outputs=[glb_output, download_btn],
|
| 565 |
)
|
| 566 |
|
| 567 |
demo.launch(theme=orange_red_theme, css=css, mcp_server=True, ssr_mode=False, show_error=True)
|