Hi3DGen / app.py
theglobe1's picture
Upload 3 files
2ae0b22 verified
Raw History Blame Contribute Delete
4.43 kB
# Hi3DGen - SHAPE ONLY (Uniboards, 2026-11-19). One photo -> mesh (GLB + STL) on a plain A10G (24 GB), not ZeroGPU.
# Removed from the original Space: nvdiffrast and diff-gaussian-rasterization (and their start-up build), texturing,
# preview renders (trellis.utils.render_utils), the Blackwell / CUDA 13 shims and the `spaces` decorator.
# Kept: the TRELLIS-normal pipeline (Stable-X/trellis-normal-v0-1) + StableNormal-turbo normal estimator, exactly as Hi3DGen.
# Background removal: rembg with u2net (inside the pipeline) and BiRefNet (inside StableNormal, object mode) - both MIT/Apache.
import os
os.environ.setdefault("ATTN_BACKEND", "xformers") # A10G = sm_86: xformers 0.0.28 works natively
os.environ.setdefault("SPARSE_ATTN_BACKEND", "xformers")
os.environ.setdefault("SPCONV_ALGO", "native")
import datetime
import numpy as np
import torch
import trimesh
import gradio as gr
from trellis.pipelines import TrellisImageTo3DPipeline
MAX_SEED = np.iinfo(np.int32).max
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")
os.makedirs(TMP_DIR, exist_ok=True)
pipeline = TrellisImageTo3DPipeline.from_pretrained("Stable-X/trellis-normal-v0-1")
pipeline.cuda()
# pinned commit of hugoycj/StableNormal (Apache-2.0); weights Stable-X/yoso-normal-v1-8-1 (Apache-2.0)
normal_predictor = torch.hub.load("hugoycj/StableNormal:1b7203c988384cb62286a25865d7354be8fdbbf7", "StableNormal_turbo",
trust_repo=True, yoso_version="yoso-normal-v1-8-1")
@torch.no_grad()
def generate(image, seed, ss_steps, ss_cfg, slat_steps, slat_cfg, target_faces):
if image is None:
raise gr.Error("Upload a photo first.")
seed = int(seed) if int(seed) >= 0 else int(np.random.randint(0, MAX_SEED))
image = pipeline.preprocess_image(image, resolution=1024)
normal = normal_predictor(image, resolution=768, match_input_resolution=True, data_type="object")
out = pipeline.run(normal, seed=seed, formats=["mesh"], preprocess_image=False,
sparse_structure_sampler_params={"steps": int(ss_steps), "cfg_strength": float(ss_cfg)},
slat_sampler_params={"steps": int(slat_steps), "cfg_strength": float(slat_cfg)})
mesh = out["mesh"][0].to_trimesh(transform_pose=True)
faces_before = len(mesh.faces)
if target_faces and int(target_faces) > 0 and faces_before > int(target_faces):
mesh = mesh.simplify_quadric_decimation(face_count=int(target_faces))
stamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
folder = os.path.join(TMP_DIR, stamp)
os.makedirs(folder, exist_ok=True)
glb, stl = os.path.join(folder, f"hi3dgen_{stamp}.glb"), os.path.join(folder, f"hi3dgen_{stamp}.stl")
mesh.export(glb)
mesh.export(stl)
torch.cuda.empty_cache()
info = f"seed {seed}; faces {faces_before:,} -> {len(mesh.faces):,}; vertices {len(mesh.vertices):,}; watertight {mesh.is_watertight}"
return normal, glb, glb, stl, info
with gr.Blocks(title="Hi3DGen shape only") as demo:
gr.Markdown("# Hi3DGen - shape only\nOne photo (one subject) -> 3D mesh. Download GLB or STL.")
with gr.Row():
with gr.Column():
image_in = gr.Image(label="Photo", type="pil", image_mode="RGBA")
with gr.Accordion("Settings", open=False):
seed = gr.Number(label="Seed (-1 = random)", value=0, precision=0)
ss_steps = gr.Slider(1, 50, value=50, step=1, label="Stage 1 sampling steps")
ss_cfg = gr.Slider(0.0, 10.0, value=3.0, step=0.1, label="Stage 1 guidance")
slat_steps = gr.Slider(1, 50, value=6, step=1, label="Stage 2 sampling steps")
slat_cfg = gr.Slider(0.0, 10.0, value=3.0, step=0.1, label="Stage 2 guidance")
target_faces = gr.Slider(0, 2000000, value=0, step=10000, label="Target faces (0 = keep all)")
go = gr.Button("Generate", variant="primary")
with gr.Column():
normal_out = gr.Image(label="Normal map used", type="pil")
model_out = gr.Model3D(label="Mesh")
glb_out = gr.File(label="Download GLB")
stl_out = gr.File(label="Download STL")
info_out = gr.Textbox(label="Info")
go.click(generate, [image_in, seed, ss_steps, ss_cfg, slat_steps, slat_cfg, target_faces],
[normal_out, model_out, glb_out, stl_out, info_out])
demo.queue(max_size=4).launch()