Spaces:
Running on Zero
Running on Zero
File size: 7,051 Bytes
a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf a589bff c43e1bf 0798bf7 83cd54d c43e1bf a589bff c43e1bf a589bff c43e1bf 83cd54d a589bff c43e1bf a589bff 83cd54d c43e1bf a589bff 83cd54d c43e1bf a589bff 83cd54d a589bff c43e1bf a589bff c43e1bf a589bff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | """TripoSplat – gradio.Server with custom frontend.
Usage: python app.py
"""
import base64
import os
import subprocess
import tempfile
import time
from pathlib import Path
from uuid import uuid4
import spaces
import torch
from PIL import Image
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
from gradio import Server
from gradio.data_classes import FileData
from triposplat import TripoSplatPipeline
import example_inputs_b64 as _b64
# ----------------------------------------------------------------------------
# Download checkpoints from HuggingFace Hub (VAST-AI/TripoSplat)
# ----------------------------------------------------------------------------
subprocess.run(
[
"hf", "download",
"VAST-AI/TripoSplat",
"--local-dir", "ckpts"
],
check=True,
)
# ----------------------------------------------------------------------------
# Pipeline (loaded once at startup)
# ----------------------------------------------------------------------------
PIPE = TripoSplatPipeline(
ckpt_path = "ckpts/diffusion_models/triposplat_fp16.safetensors",
decoder_path = "ckpts/vae/triposplat_vae_decoder_fp16.safetensors",
dinov3_path = "ckpts/clip_vision/dino_v3_vit_h.safetensors",
flux2_vae_encoder_path = "ckpts/vae/flux2-vae.safetensors",
rmbg_path = "ckpts/background_removal/birefnet.safetensors",
device = "cuda",
)
OUT_ROOT = Path("gradio_outputs").resolve()
OUT_ROOT.mkdir(parents=True, exist_ok=True)
# Decode example images from base64 into a persistent temp directory so that
# the custom frontend can serve them via FastAPI routes.
_EXAMPLES_TMPDIR = tempfile.mkdtemp(prefix="triposplat_examples_")
def _write_example(varname: str, filename: str) -> str:
path = Path(_EXAMPLES_TMPDIR) / filename
path.write_bytes(base64.b64decode(getattr(_b64, varname)))
return str(path)
EXAMPLES = [
{"name": "Creature Butterfly", "file": _write_example("CREATURE_BUTTERFLY", "creature_butterfly.webp")},
{"name": "Building Stone House","file": _write_example("BUILDING_STONE_HOUSE", "building_stone_house.webp")},
{"name": "Vehicle Pirate Ship", "file": _write_example("VEHICLE_PIRATE_SHIP", "vehicle_pirate_ship.webp")},
{"name": "Plant Water Lily", "file": _write_example("PLANT_WATER_LILY", "plant_water_lily.webp")},
]
# ----------------------------------------------------------------------------
# gradio.Server
# ----------------------------------------------------------------------------
app = Server()
# ---- Static pages ----------------------------------------------------------
@app.get("/")
async def homepage():
"""Serve the custom frontend."""
html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html")
with open(html_path, "r", encoding="utf-8") as f:
return HTMLResponse(f.read())
@app.get("/viewer")
async def viewer_page():
"""Serve the Spark.js 3D viewer (loaded inside an iframe)."""
viewer_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"static", "viewer", "viewer.html",
)
with open(viewer_path, "r", encoding="utf-8") as f:
return HTMLResponse(f.read())
# ---- Example images --------------------------------------------------------
@app.get("/api/examples")
async def get_examples():
"""Return a JSON list of example images the frontend can display."""
return JSONResponse([
{"name": ex["name"], "url": f"/api/example/{i}"}
for i, ex in enumerate(EXAMPLES)
])
@app.get("/api/example/{idx}")
async def get_example(idx: int):
"""Serve an individual example image by index."""
if 0 <= idx < len(EXAMPLES):
return FileResponse(EXAMPLES[idx]["file"], media_type="image/webp")
return JSONResponse({"error": "not found"}, status_code=404)
# ----------------------------------------------------------------------------
# GPU pipeline helper
# ----------------------------------------------------------------------------
@spaces.GPU
def _run_pipeline(pil_image, seed, steps, guidance_scale, num_gaussians,
out_dir, output_format):
"""Run the full pipeline (preprocess → encode → sample → decode → save)
in a single GPU acquisition.
All file I/O happens here so the unpicklable Gaussian object never
crosses the ZeroGPU multiprocessing boundary.
"""
t0 = time.time()
prepared = PIPE.preprocess_image(pil_image)
gen = torch.Generator(device=PIPE._device).manual_seed(int(seed))
cond = PIPE.encode_image(prepared, generator=gen)
out = PIPE.sample_latent(
cond,
steps=int(steps),
guidance_scale=float(guidance_scale),
generator=gen,
show_progress=True,
)
gaussian = PIPE.decode_latent(out["latent"], num_gaussians=int(num_gaussians))
gen_dt = time.time() - t0
# Save preprocessed image
prep_path = out_dir / "preprocessed.png"
prepared.save(str(prep_path))
# Save PLY (always needed for the viewer)
ply_path = out_dir / "splat.ply"
gaussian.save_ply(str(ply_path))
# Save in the requested download format
fmt = output_format.lower()
if fmt == "splat":
download_path = out_dir / "splat.splat"
gaussian.save_splat(str(download_path))
else:
download_path = ply_path
n_gaussians = gaussian.get_xyz.shape[0]
# Return only picklable primitives / paths
return str(prep_path), str(ply_path), str(download_path), n_gaussians, gen_dt
# ----------------------------------------------------------------------------
# Main API endpoint (queued via Gradio's engine)
# ----------------------------------------------------------------------------
@app.api()
def generate(
image: FileData,
seed: int = 42,
steps: int = 20,
guidance_scale: float = 3.0,
num_gaussians: int = 262144,
output_format: str = "ply",
) -> tuple[FileData, FileData, FileData, str]:
"""Generate 3D Gaussians from an input image.
Returns (preprocessed_image, ply_file, download_file, info_string).
The frontend receives these as result.data[0..3].
"""
pil_image = Image.open(image["path"]).convert("RGBA")
out_dir = OUT_ROOT / uuid4().hex[:12]
out_dir.mkdir(parents=True, exist_ok=True)
prep_path, ply_path, download_path, n_gaussians, gen_dt = _run_pipeline(
pil_image, seed, steps, guidance_scale, num_gaussians,
out_dir, output_format,
)
info = (
f"{n_gaussians:,} gaussians · "
f"generation: {gen_dt:.1f}s · saved: {Path(download_path).name}"
)
return (
FileData(path=prep_path),
FileData(path=ply_path),
FileData(path=download_path),
info,
)
# ----------------------------------------------------------------------------
# Launch
# ----------------------------------------------------------------------------
if __name__ == "__main__":
app.launch(show_error=True)
|