Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from huggingface_hub import snapshot_download
|
| 9 |
+
from accelerate.utils import set_seed
|
| 10 |
+
import trimesh
|
| 11 |
+
|
| 12 |
+
from src.utils.data_utils import get_colored_mesh_composition, export_renderings
|
| 13 |
+
from src.utils.image_utils import prepare_image
|
| 14 |
+
from src.pipelines.pipeline_partcrafter import PartCrafterPipeline
|
| 15 |
+
from src.models.briarmbg import BriaRMBG
|
| 16 |
+
|
| 17 |
+
# Constants
|
| 18 |
+
MAX_NUM_PARTS = 16
|
| 19 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
+
DTYPE = torch.float16
|
| 21 |
+
|
| 22 |
+
# Download and initialize models
|
| 23 |
+
partcrafter_weights_dir = "pretrained_weights/PartCrafter"
|
| 24 |
+
rmbg_weights_dir = "pretrained_weights/RMBG-1.4"
|
| 25 |
+
snapshot_download(repo_id="wgsxm/PartCrafter", local_dir=partcrafter_weights_dir)
|
| 26 |
+
snapshot_download(repo_id="briaai/RMBG-1.4", local_dir=rmbg_weights_dir)
|
| 27 |
+
|
| 28 |
+
rmbg_net = BriaRMBG.from_pretrained(rmbg_weights_dir).to(DEVICE)
|
| 29 |
+
rmbg_net.eval()
|
| 30 |
+
pipe: PartCrafterPipeline = PartCrafterPipeline.from_pretrained(partcrafter_weights_dir).to(DEVICE, DTYPE)
|
| 31 |
+
|
| 32 |
+
@spaces.GPU()
|
| 33 |
+
@torch.no_grad()
|
| 34 |
+
def run_triposg(image: Image.Image,
|
| 35 |
+
num_parts: int,
|
| 36 |
+
seed: int,
|
| 37 |
+
num_tokens: int,
|
| 38 |
+
num_inference_steps: int,
|
| 39 |
+
guidance_scale: float,
|
| 40 |
+
max_num_expanded_coords: float,
|
| 41 |
+
use_flash_decoder: bool,
|
| 42 |
+
rmbg: bool):
|
| 43 |
+
"""
|
| 44 |
+
Generate 3D part meshes from an input image.
|
| 45 |
+
"""
|
| 46 |
+
if rmbg:
|
| 47 |
+
img_pil = prepare_image(image, bg_color=np.array([1.0, 1.0, 1.0]), rmbg_net=rmbg_net)
|
| 48 |
+
else:
|
| 49 |
+
img_pil = image
|
| 50 |
+
|
| 51 |
+
set_seed(seed)
|
| 52 |
+
start_time = time.time()
|
| 53 |
+
outputs = pipe(
|
| 54 |
+
image=[img_pil] * num_parts,
|
| 55 |
+
attention_kwargs={"num_parts": num_parts},
|
| 56 |
+
num_tokens=num_tokens,
|
| 57 |
+
generator=torch.Generator(device=pipe.device).manual_seed(seed),
|
| 58 |
+
num_inference_steps=num_inference_steps,
|
| 59 |
+
guidance_scale=guidance_scale,
|
| 60 |
+
max_num_expanded_coords=max_num_expanded_coords,
|
| 61 |
+
use_flash_decoder=use_flash_decoder,
|
| 62 |
+
).meshes
|
| 63 |
+
duration = time.time() - start_time
|
| 64 |
+
print(f"Generation time: {duration:.2f}s")
|
| 65 |
+
|
| 66 |
+
# Ensure no None outputs
|
| 67 |
+
for i, mesh in enumerate(outputs):
|
| 68 |
+
if mesh is None:
|
| 69 |
+
outputs[i] = trimesh.Trimesh(vertices=[[0,0,0]], faces=[[0,0,0]])
|
| 70 |
+
|
| 71 |
+
# Merge and color
|
| 72 |
+
merged = get_colored_mesh_composition(outputs)
|
| 73 |
+
|
| 74 |
+
# Export meshes and return results
|
| 75 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
| 76 |
+
export_dir = os.path.join("results", timestamp)
|
| 77 |
+
os.makedirs(export_dir, exist_ok=True)
|
| 78 |
+
for idx, mesh in enumerate(outputs):
|
| 79 |
+
mesh.export(os.path.join(export_dir, f"part_{idx:02}.glb"))
|
| 80 |
+
merged.export(os.path.join(export_dir, "object.glb"))
|
| 81 |
+
|
| 82 |
+
return merged, export_dir
|
| 83 |
+
|
| 84 |
+
# Gradio Interface
|
| 85 |
+
def build_demo():
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("# PartCrafter 3D Generation Demo")
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column(scale=1):
|
| 90 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
| 91 |
+
num_parts = gr.Slider(1, MAX_NUM_PARTS, value=4, step=1, label="Number of Parts")
|
| 92 |
+
seed = gr.Number(value=0, label="Random Seed", precision=0)
|
| 93 |
+
num_tokens = gr.Slider(256, 2048, value=1024, step=64, label="Num Tokens")
|
| 94 |
+
num_steps = gr.Slider(1, 100, value=50, step=1, label="Inference Steps")
|
| 95 |
+
guidance = gr.Slider(1.0, 20.0, value=7.0, step=0.1, label="Guidance Scale")
|
| 96 |
+
max_coords = gr.Text(value="1e9", label="Max Expanded Coords")
|
| 97 |
+
flash_decoder = gr.Checkbox(value=False, label="Use Flash Decoder")
|
| 98 |
+
remove_bg = gr.Checkbox(value=False, label="Remove Background (RMBG)")
|
| 99 |
+
run_button = gr.Button("Generate 3D Parts")
|
| 100 |
+
with gr.Column(scale=1):
|
| 101 |
+
output_model = gr.Model3D(label="Merged 3D Object")
|
| 102 |
+
output_dir = gr.Textbox(label="Export Directory")
|
| 103 |
+
|
| 104 |
+
run_button.click(fn=run_triposg,
|
| 105 |
+
inputs=[input_image, num_parts, seed, num_tokens, num_steps,
|
| 106 |
+
guidance, max_coords, flash_decoder, remove_bg],
|
| 107 |
+
outputs=[output_model, output_dir])
|
| 108 |
+
return demo
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
demo = build_demo()
|
| 112 |
+
demo.launch()
|