Spaces:
Running on Zero
Running on Zero
mcp-compatible
#2
by victor HF Staff - opened
- README.md +1 -1
- app.py +70 -182
- requirements.txt +10 -3
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🏢
|
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
app.py
CHANGED
|
@@ -1,114 +1,16 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ['SPCONV_ALGO'] = 'native'
|
| 3 |
-
os.environ['ATTN_BACKEND'] = 'xformers'
|
| 4 |
-
os.environ['SPARSE_ATTN'] = 'xformers'
|
| 5 |
-
|
| 6 |
-
import subprocess, sys, tempfile, ctypes
|
| 7 |
-
|
| 8 |
-
try:
|
| 9 |
-
import gradio_litmodel3d # noqa: F401
|
| 10 |
-
except ImportError:
|
| 11 |
-
subprocess.check_call(
|
| 12 |
-
[sys.executable, "-m", "pip", "install", "--no-deps",
|
| 13 |
-
"gradio_litmodel3d==0.0.1"],
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
import spaces
|
| 17 |
-
|
| 18 |
-
CUDA_HOME = "/cuda-image/usr/local/cuda-13.0"
|
| 19 |
-
CUDA_LIBDIR = os.path.join(CUDA_HOME, "lib64")
|
| 20 |
-
REPO_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
@spaces.GPU(duration=600)
|
| 24 |
-
def first_gpu_setup():
|
| 25 |
-
need_nvdiffrast = need_dgr = False
|
| 26 |
-
try:
|
| 27 |
-
import nvdiffrast # noqa: F401
|
| 28 |
-
except ImportError:
|
| 29 |
-
need_nvdiffrast = True
|
| 30 |
-
try:
|
| 31 |
-
import diff_gaussian_rasterization # noqa: F401
|
| 32 |
-
except ImportError:
|
| 33 |
-
need_dgr = True
|
| 34 |
-
if not (need_nvdiffrast or need_dgr):
|
| 35 |
-
print("CUDA extensions already installed; skipping build.")
|
| 36 |
-
return
|
| 37 |
-
|
| 38 |
-
if not os.path.exists(os.path.join(CUDA_HOME, "bin", "nvcc")):
|
| 39 |
-
raise RuntimeError(
|
| 40 |
-
f"nvcc not found at {CUDA_HOME}/bin/nvcc on the ZeroGPU worker. "
|
| 41 |
-
"The new-hardware CUDA path may have moved; please update CUDA_HOME."
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
patch_dir = tempfile.mkdtemp(prefix="torch_cuda_patch_")
|
| 45 |
-
with open(os.path.join(patch_dir, "sitecustomize.py"), "w") as f:
|
| 46 |
-
f.write(
|
| 47 |
-
"try:\n"
|
| 48 |
-
" import torch.utils.cpp_extension as _c\n"
|
| 49 |
-
" _c._check_cuda_version = lambda *a, **k: None\n"
|
| 50 |
-
"except Exception:\n"
|
| 51 |
-
" pass\n"
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
env = os.environ.copy()
|
| 55 |
-
env["CUDA_HOME"] = CUDA_HOME
|
| 56 |
-
env["CUDA_PATH"] = CUDA_HOME
|
| 57 |
-
env["PATH"] = os.path.join(CUDA_HOME, "bin") + os.pathsep + env.get("PATH", "")
|
| 58 |
-
env["PYTHONPATH"] = patch_dir + os.pathsep + env.get("PYTHONPATH", "")
|
| 59 |
-
env["TORCH_CUDA_ARCH_LIST"] = "12.0"
|
| 60 |
-
|
| 61 |
-
subprocess.check_call(
|
| 62 |
-
[sys.executable, "-m", "pip", "install", "--no-deps",
|
| 63 |
-
"setuptools", "wheel", "ninja"],
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
if need_nvdiffrast:
|
| 67 |
-
subprocess.check_call(
|
| 68 |
-
[sys.executable, "-m", "pip", "install", "--no-build-isolation",
|
| 69 |
-
os.path.join(REPO_DIR, "extensions", "nvdiffrast")],
|
| 70 |
-
env=env,
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
if need_dgr:
|
| 74 |
-
mip_dir = tempfile.mkdtemp(prefix="mip_splatting_")
|
| 75 |
-
subprocess.check_call(
|
| 76 |
-
["git", "clone", "--recursive", "--depth=1",
|
| 77 |
-
"https://github.com/autonomousvision/mip-splatting.git", mip_dir],
|
| 78 |
-
)
|
| 79 |
-
subprocess.check_call(
|
| 80 |
-
[sys.executable, "-m", "pip", "install", "--no-build-isolation",
|
| 81 |
-
os.path.join(mip_dir, "submodules", "diff-gaussian-rasterization")],
|
| 82 |
-
env=env,
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
first_gpu_setup()
|
| 87 |
-
|
| 88 |
-
ctypes.CDLL(os.path.join(CUDA_LIBDIR, "libcudart.so.13"), mode=ctypes.RTLD_GLOBAL)
|
| 89 |
-
os.environ["LD_LIBRARY_PATH"] = CUDA_LIBDIR + os.pathsep + os.environ.get("LD_LIBRARY_PATH", "")
|
| 90 |
-
|
| 91 |
import gradio as gr
|
|
|
|
| 92 |
from gradio_litmodel3d import LitModel3D
|
| 93 |
|
|
|
|
| 94 |
import shutil
|
|
|
|
| 95 |
from typing import *
|
| 96 |
import torch
|
| 97 |
import numpy as np
|
| 98 |
import imageio
|
| 99 |
from easydict import EasyDict as edict
|
| 100 |
from PIL import Image
|
| 101 |
-
|
| 102 |
-
# xformers on Blackwell (sm_120) picks Flash-Attn-3 (Hopper-only) and crashes
|
| 103 |
-
# with "invalid argument". Force it to use Cutlass kernels instead.
|
| 104 |
-
import xformers.ops as _xops
|
| 105 |
-
_orig_mea = _xops.memory_efficient_attention
|
| 106 |
-
_cutlass_op = (_xops.fmha.cutlass.FwOp, _xops.fmha.cutlass.BwOp)
|
| 107 |
-
def _mea_cutlass(*args, **kwargs):
|
| 108 |
-
kwargs.setdefault("op", _cutlass_op)
|
| 109 |
-
return _orig_mea(*args, **kwargs)
|
| 110 |
-
_xops.memory_efficient_attention = _mea_cutlass
|
| 111 |
-
|
| 112 |
from trellis.pipelines import TrellisImageTo3DPipeline
|
| 113 |
from trellis.representations import Gaussian, MeshExtractResult
|
| 114 |
from trellis.utils import render_utils, postprocessing_utils
|
|
@@ -131,17 +33,13 @@ def end_session(req: gr.Request):
|
|
| 131 |
|
| 132 |
def preprocess_image(image: Image.Image) -> Image.Image:
|
| 133 |
"""
|
| 134 |
-
Preprocess the input image
|
| 135 |
-
|
| 136 |
-
This function is called when a user uploads an image or selects an example.
|
| 137 |
-
It applies background removal and other preprocessing steps necessary for
|
| 138 |
-
optimal 3D model generation.
|
| 139 |
|
| 140 |
Args:
|
| 141 |
-
image (Image.Image): The input image
|
| 142 |
|
| 143 |
Returns:
|
| 144 |
-
Image.Image: The preprocessed image
|
| 145 |
"""
|
| 146 |
processed_image = pipeline.preprocess_image(image)
|
| 147 |
return processed_image
|
|
@@ -149,16 +47,13 @@ def preprocess_image(image: Image.Image) -> Image.Image:
|
|
| 149 |
|
| 150 |
def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
|
| 151 |
"""
|
| 152 |
-
Preprocess a list of input images
|
| 153 |
-
|
| 154 |
-
This function is called when users upload multiple images in the gallery.
|
| 155 |
-
It processes each image to prepare them for the multi-image 3D generation pipeline.
|
| 156 |
|
| 157 |
Args:
|
| 158 |
-
images (List[Tuple[Image.Image, str]]): The input images
|
| 159 |
|
| 160 |
Returns:
|
| 161 |
-
List[Image.Image]: The preprocessed images
|
| 162 |
"""
|
| 163 |
images = [image[0] for image in images]
|
| 164 |
processed_images = [pipeline.preprocess_image(image) for image in images]
|
|
@@ -207,23 +102,13 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
|
| 207 |
|
| 208 |
def get_seed(randomize_seed: bool, seed: int) -> int:
|
| 209 |
"""
|
| 210 |
-
Get the random seed
|
| 211 |
-
|
| 212 |
-
This function is called by the generate button to determine whether to use
|
| 213 |
-
a random seed or the user-specified seed value.
|
| 214 |
-
|
| 215 |
-
Args:
|
| 216 |
-
randomize_seed (bool): Whether to generate a random seed
|
| 217 |
-
seed (int): The user-specified seed value
|
| 218 |
-
|
| 219 |
-
Returns:
|
| 220 |
-
int: The seed to use for generation
|
| 221 |
"""
|
| 222 |
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
| 223 |
|
| 224 |
|
| 225 |
-
@spaces.GPU
|
| 226 |
-
def
|
| 227 |
image: Image.Image,
|
| 228 |
multiimages: List[Tuple[Image.Image, str]],
|
| 229 |
is_multiimage: bool,
|
|
@@ -233,12 +118,10 @@ def generate_and_extract_glb(
|
|
| 233 |
slat_guidance_strength: float,
|
| 234 |
slat_sampling_steps: int,
|
| 235 |
multiimage_algo: Literal["multidiffusion", "stochastic"],
|
| 236 |
-
mesh_simplify: float,
|
| 237 |
-
texture_size: int,
|
| 238 |
req: gr.Request,
|
| 239 |
-
) -> Tuple[dict, str
|
| 240 |
"""
|
| 241 |
-
Convert an image to a 3D model
|
| 242 |
|
| 243 |
Args:
|
| 244 |
image (Image.Image): The input image.
|
|
@@ -250,18 +133,12 @@ def generate_and_extract_glb(
|
|
| 250 |
slat_guidance_strength (float): The guidance strength for structured latent generation.
|
| 251 |
slat_sampling_steps (int): The number of sampling steps for structured latent generation.
|
| 252 |
multiimage_algo (Literal["multidiffusion", "stochastic"]): The algorithm for multi-image generation.
|
| 253 |
-
mesh_simplify (float): The mesh simplification factor.
|
| 254 |
-
texture_size (int): The texture resolution.
|
| 255 |
|
| 256 |
Returns:
|
| 257 |
dict: The information of the generated 3D model.
|
| 258 |
str: The path to the video of the 3D model.
|
| 259 |
-
str: The path to the extracted GLB file.
|
| 260 |
-
str: The path to the extracted GLB file (for download).
|
| 261 |
"""
|
| 262 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 263 |
-
|
| 264 |
-
# Generate 3D model
|
| 265 |
if not is_multiimage:
|
| 266 |
outputs = pipeline.run(
|
| 267 |
image,
|
|
@@ -293,43 +170,53 @@ def generate_and_extract_glb(
|
|
| 293 |
},
|
| 294 |
mode=multiimage_algo,
|
| 295 |
)
|
| 296 |
-
|
| 297 |
-
# Render video
|
| 298 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 299 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 300 |
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
| 301 |
video_path = os.path.join(user_dir, 'sample.mp4')
|
| 302 |
imageio.mimsave(video_path, video, fps=15)
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
| 308 |
glb_path = os.path.join(user_dir, 'sample.glb')
|
| 309 |
glb.export(glb_path)
|
| 310 |
-
|
| 311 |
-
# Pack state for optional Gaussian extraction
|
| 312 |
-
state = pack_state(gs, mesh)
|
| 313 |
-
|
| 314 |
torch.cuda.empty_cache()
|
| 315 |
-
return
|
| 316 |
|
| 317 |
|
| 318 |
@spaces.GPU
|
| 319 |
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
| 320 |
"""
|
| 321 |
-
Extract a Gaussian
|
| 322 |
-
|
| 323 |
-
This function is called when the user clicks "Extract Gaussian" button.
|
| 324 |
-
It converts the 3D model state into a .ply file format containing
|
| 325 |
-
Gaussian splatting data for advanced 3D applications.
|
| 326 |
|
| 327 |
Args:
|
| 328 |
-
state (dict): The state of the generated 3D model
|
| 329 |
-
req (gr.Request): Gradio request object for session management
|
| 330 |
|
| 331 |
Returns:
|
| 332 |
-
|
| 333 |
"""
|
| 334 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 335 |
gs, _ = unpack_state(state)
|
|
@@ -355,17 +242,7 @@ def prepare_multi_example() -> List[Image.Image]:
|
|
| 355 |
|
| 356 |
def split_image(image: Image.Image) -> List[Image.Image]:
|
| 357 |
"""
|
| 358 |
-
Split
|
| 359 |
-
|
| 360 |
-
This function is called when users select multi-image examples that contain
|
| 361 |
-
multiple views in a single concatenated image. It automatically splits them
|
| 362 |
-
based on alpha channel boundaries and preprocesses each view.
|
| 363 |
-
|
| 364 |
-
Args:
|
| 365 |
-
image (Image.Image): A concatenated image containing multiple views
|
| 366 |
-
|
| 367 |
-
Returns:
|
| 368 |
-
List[Image.Image]: List of individual preprocessed view images
|
| 369 |
"""
|
| 370 |
image = np.array(image)
|
| 371 |
alpha = image[..., 3]
|
|
@@ -381,9 +258,8 @@ def split_image(image: Image.Image) -> List[Image.Image]:
|
|
| 381 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
| 382 |
gr.Markdown("""
|
| 383 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
| 384 |
-
* Upload an image and click "Generate
|
| 385 |
-
* If you
|
| 386 |
-
* If the image has alpha channel, it will be used as the mask. Otherwise, we use `rembg` to remove the background.
|
| 387 |
|
| 388 |
✨New: 1) Experimental multi-image support. 2) Gaussian file extraction.
|
| 389 |
""")
|
|
@@ -413,13 +289,16 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 413 |
slat_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=3.0, step=0.1)
|
| 414 |
slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
| 415 |
multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
|
|
|
|
|
|
|
| 416 |
|
| 417 |
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
| 418 |
mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
|
| 419 |
texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
|
|
|
| 423 |
gr.Markdown("""
|
| 424 |
*NOTE: Gaussian file can be very large (~50MB), it will take a while to display and download.*
|
| 425 |
""")
|
|
@@ -487,17 +366,26 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 487 |
inputs=[randomize_seed, seed],
|
| 488 |
outputs=[seed],
|
| 489 |
).then(
|
| 490 |
-
|
| 491 |
-
inputs=[image_prompt, multiimage_prompt, is_multiimage, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo
|
| 492 |
-
outputs=[output_buf, video_output
|
| 493 |
).then(
|
| 494 |
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
| 495 |
-
outputs=[
|
| 496 |
)
|
| 497 |
|
| 498 |
video_output.clear(
|
| 499 |
-
lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)
|
| 500 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
)
|
| 502 |
|
| 503 |
extract_gs_btn.click(
|
|
@@ -510,8 +398,8 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 510 |
)
|
| 511 |
|
| 512 |
model_output.clear(
|
| 513 |
-
lambda:
|
| 514 |
-
outputs=[download_glb
|
| 515 |
)
|
| 516 |
|
| 517 |
|
|
@@ -523,4 +411,4 @@ if __name__ == "__main__":
|
|
| 523 |
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Preload rembg
|
| 524 |
except:
|
| 525 |
pass
|
| 526 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
from gradio_litmodel3d import LitModel3D
|
| 4 |
|
| 5 |
+
import os
|
| 6 |
import shutil
|
| 7 |
+
os.environ['SPCONV_ALGO'] = 'native'
|
| 8 |
from typing import *
|
| 9 |
import torch
|
| 10 |
import numpy as np
|
| 11 |
import imageio
|
| 12 |
from easydict import EasyDict as edict
|
| 13 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
from trellis.pipelines import TrellisImageTo3DPipeline
|
| 15 |
from trellis.representations import Gaussian, MeshExtractResult
|
| 16 |
from trellis.utils import render_utils, postprocessing_utils
|
|
|
|
| 33 |
|
| 34 |
def preprocess_image(image: Image.Image) -> Image.Image:
|
| 35 |
"""
|
| 36 |
+
Preprocess the input image.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
Args:
|
| 39 |
+
image (Image.Image): The input image.
|
| 40 |
|
| 41 |
Returns:
|
| 42 |
+
Image.Image: The preprocessed image.
|
| 43 |
"""
|
| 44 |
processed_image = pipeline.preprocess_image(image)
|
| 45 |
return processed_image
|
|
|
|
| 47 |
|
| 48 |
def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
|
| 49 |
"""
|
| 50 |
+
Preprocess a list of input images.
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
Args:
|
| 53 |
+
images (List[Tuple[Image.Image, str]]): The input images.
|
| 54 |
|
| 55 |
Returns:
|
| 56 |
+
List[Image.Image]: The preprocessed images.
|
| 57 |
"""
|
| 58 |
images = [image[0] for image in images]
|
| 59 |
processed_images = [pipeline.preprocess_image(image) for image in images]
|
|
|
|
| 102 |
|
| 103 |
def get_seed(randomize_seed: bool, seed: int) -> int:
|
| 104 |
"""
|
| 105 |
+
Get the random seed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
"""
|
| 107 |
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
| 108 |
|
| 109 |
|
| 110 |
+
@spaces.GPU
|
| 111 |
+
def image_to_3d(
|
| 112 |
image: Image.Image,
|
| 113 |
multiimages: List[Tuple[Image.Image, str]],
|
| 114 |
is_multiimage: bool,
|
|
|
|
| 118 |
slat_guidance_strength: float,
|
| 119 |
slat_sampling_steps: int,
|
| 120 |
multiimage_algo: Literal["multidiffusion", "stochastic"],
|
|
|
|
|
|
|
| 121 |
req: gr.Request,
|
| 122 |
+
) -> Tuple[dict, str]:
|
| 123 |
"""
|
| 124 |
+
Convert an image to a 3D model.
|
| 125 |
|
| 126 |
Args:
|
| 127 |
image (Image.Image): The input image.
|
|
|
|
| 133 |
slat_guidance_strength (float): The guidance strength for structured latent generation.
|
| 134 |
slat_sampling_steps (int): The number of sampling steps for structured latent generation.
|
| 135 |
multiimage_algo (Literal["multidiffusion", "stochastic"]): The algorithm for multi-image generation.
|
|
|
|
|
|
|
| 136 |
|
| 137 |
Returns:
|
| 138 |
dict: The information of the generated 3D model.
|
| 139 |
str: The path to the video of the 3D model.
|
|
|
|
|
|
|
| 140 |
"""
|
| 141 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
|
|
|
|
|
|
| 142 |
if not is_multiimage:
|
| 143 |
outputs = pipeline.run(
|
| 144 |
image,
|
|
|
|
| 170 |
},
|
| 171 |
mode=multiimage_algo,
|
| 172 |
)
|
|
|
|
|
|
|
| 173 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 174 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 175 |
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
| 176 |
video_path = os.path.join(user_dir, 'sample.mp4')
|
| 177 |
imageio.mimsave(video_path, video, fps=15)
|
| 178 |
+
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
| 179 |
+
torch.cuda.empty_cache()
|
| 180 |
+
return state, video_path
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
@spaces.GPU(duration=90)
|
| 184 |
+
def extract_glb(
|
| 185 |
+
state: dict,
|
| 186 |
+
mesh_simplify: float,
|
| 187 |
+
texture_size: int,
|
| 188 |
+
req: gr.Request,
|
| 189 |
+
) -> Tuple[str, str]:
|
| 190 |
+
"""
|
| 191 |
+
Extract a GLB file from the 3D model.
|
| 192 |
+
|
| 193 |
+
Args:
|
| 194 |
+
state (dict): The state of the generated 3D model.
|
| 195 |
+
mesh_simplify (float): The mesh simplification factor.
|
| 196 |
+
texture_size (int): The texture resolution.
|
| 197 |
+
|
| 198 |
+
Returns:
|
| 199 |
+
str: The path to the extracted GLB file.
|
| 200 |
+
"""
|
| 201 |
+
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 202 |
+
gs, mesh = unpack_state(state)
|
| 203 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
| 204 |
glb_path = os.path.join(user_dir, 'sample.glb')
|
| 205 |
glb.export(glb_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
torch.cuda.empty_cache()
|
| 207 |
+
return glb_path, glb_path
|
| 208 |
|
| 209 |
|
| 210 |
@spaces.GPU
|
| 211 |
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
| 212 |
"""
|
| 213 |
+
Extract a Gaussian file from the 3D model.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
Args:
|
| 216 |
+
state (dict): The state of the generated 3D model.
|
|
|
|
| 217 |
|
| 218 |
Returns:
|
| 219 |
+
str: The path to the extracted Gaussian file.
|
| 220 |
"""
|
| 221 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 222 |
gs, _ = unpack_state(state)
|
|
|
|
| 242 |
|
| 243 |
def split_image(image: Image.Image) -> List[Image.Image]:
|
| 244 |
"""
|
| 245 |
+
Split an image into multiple views.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
"""
|
| 247 |
image = np.array(image)
|
| 248 |
alpha = image[..., 3]
|
|
|
|
| 258 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
| 259 |
gr.Markdown("""
|
| 260 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
| 261 |
+
* Upload an image and click "Generate" to create a 3D asset. If the image has alpha channel, it be used as the mask. Otherwise, we use `rembg` to remove the background.
|
| 262 |
+
* If you find the generated 3D asset satisfactory, click "Extract GLB" to extract the GLB file and download it.
|
|
|
|
| 263 |
|
| 264 |
✨New: 1) Experimental multi-image support. 2) Gaussian file extraction.
|
| 265 |
""")
|
|
|
|
| 289 |
slat_guidance_strength = gr.Slider(0.0, 10.0, label="Guidance Strength", value=3.0, step=0.1)
|
| 290 |
slat_sampling_steps = gr.Slider(1, 50, label="Sampling Steps", value=12, step=1)
|
| 291 |
multiimage_algo = gr.Radio(["stochastic", "multidiffusion"], label="Multi-image Algorithm", value="stochastic")
|
| 292 |
+
|
| 293 |
+
generate_btn = gr.Button("Generate")
|
| 294 |
|
| 295 |
with gr.Accordion(label="GLB Extraction Settings", open=False):
|
| 296 |
mesh_simplify = gr.Slider(0.9, 0.98, label="Simplify", value=0.95, step=0.01)
|
| 297 |
texture_size = gr.Slider(512, 2048, label="Texture Size", value=1024, step=512)
|
| 298 |
+
|
| 299 |
+
with gr.Row():
|
| 300 |
+
extract_glb_btn = gr.Button("Extract GLB", interactive=False)
|
| 301 |
+
extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)
|
| 302 |
gr.Markdown("""
|
| 303 |
*NOTE: Gaussian file can be very large (~50MB), it will take a while to display and download.*
|
| 304 |
""")
|
|
|
|
| 366 |
inputs=[randomize_seed, seed],
|
| 367 |
outputs=[seed],
|
| 368 |
).then(
|
| 369 |
+
image_to_3d,
|
| 370 |
+
inputs=[image_prompt, multiimage_prompt, is_multiimage, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo],
|
| 371 |
+
outputs=[output_buf, video_output],
|
| 372 |
).then(
|
| 373 |
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
| 374 |
+
outputs=[extract_glb_btn, extract_gs_btn],
|
| 375 |
)
|
| 376 |
|
| 377 |
video_output.clear(
|
| 378 |
+
lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),
|
| 379 |
+
outputs=[extract_glb_btn, extract_gs_btn],
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
extract_glb_btn.click(
|
| 383 |
+
extract_glb,
|
| 384 |
+
inputs=[output_buf, mesh_simplify, texture_size],
|
| 385 |
+
outputs=[model_output, download_glb],
|
| 386 |
+
).then(
|
| 387 |
+
lambda: gr.Button(interactive=True),
|
| 388 |
+
outputs=[download_glb],
|
| 389 |
)
|
| 390 |
|
| 391 |
extract_gs_btn.click(
|
|
|
|
| 398 |
)
|
| 399 |
|
| 400 |
model_output.clear(
|
| 401 |
+
lambda: gr.Button(interactive=False),
|
| 402 |
+
outputs=[download_glb],
|
| 403 |
)
|
| 404 |
|
| 405 |
|
|
|
|
| 411 |
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Preload rembg
|
| 412 |
except:
|
| 413 |
pass
|
| 414 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
pillow==10.4.0
|
| 4 |
imageio==2.36.1
|
| 5 |
imageio-ffmpeg==0.5.1
|
|
@@ -15,6 +17,11 @@ pyvista==0.44.2
|
|
| 15 |
pymeshfix==0.17.0
|
| 16 |
igraph==0.11.8
|
| 17 |
git+https://github.com/EasternJournalist/utils3d.git@9a4eb15e4021b67b12c460c7057d642626897ec8
|
| 18 |
-
xformers
|
| 19 |
spconv-cu120==2.3.6
|
| 20 |
transformers==4.46.3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cu121
|
| 2 |
+
|
| 3 |
+
torch==2.4.0
|
| 4 |
+
torchvision==0.19.0
|
| 5 |
pillow==10.4.0
|
| 6 |
imageio==2.36.1
|
| 7 |
imageio-ffmpeg==0.5.1
|
|
|
|
| 17 |
pymeshfix==0.17.0
|
| 18 |
igraph==0.11.8
|
| 19 |
git+https://github.com/EasternJournalist/utils3d.git@9a4eb15e4021b67b12c460c7057d642626897ec8
|
| 20 |
+
xformers==0.0.27.post2
|
| 21 |
spconv-cu120==2.3.6
|
| 22 |
transformers==4.46.3
|
| 23 |
+
gradio_litmodel3d==0.0.1
|
| 24 |
+
pydantic==2.10.6
|
| 25 |
+
https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.0.post2/flash_attn-2.7.0.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
|
| 26 |
+
https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl?download=true
|
| 27 |
+
https://huggingface.co/spaces/JeffreyXiang/TRELLIS/resolve/main/wheels/nvdiffrast-0.3.3-cp310-cp310-linux_x86_64.whl?download=true
|