Spaces:
Build error
Build error
File size: 7,082 Bytes
b481005 f0a21d7 b481005 d9905a4 edefd31 d9905a4 e31ed5b d9905a4 e31ed5b f0a21d7 b481005 e31ed5b b481005 f0a21d7 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 f0a21d7 e31ed5b f0a21d7 e31ed5b f0a21d7 e31ed5b f0a21d7 e31ed5b f0a21d7 e31ed5b d9905a4 f0a21d7 b481005 e31ed5b f0a21d7 e31ed5b d9905a4 e31ed5b d9905a4 e31ed5b b481005 f0a21d7 b481005 e31ed5b f0a21d7 e31ed5b f0a21d7 b481005 e31ed5b f0a21d7 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 f0a21d7 b481005 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 e31ed5b f0a21d7 e31ed5b f0a21d7 e31ed5b f0a21d7 e31ed5b f0a21d7 e31ed5b f0a21d7 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 e31ed5b b481005 f0a21d7 e31ed5b | 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | import sys
import tempfile
import subprocess
from functools import lru_cache
import gradio as gr
import numpy as np
import spaces
import torch
from PIL import Image
from diffusers import DiffusionPipeline
MODEL_ID = "WasabiOctopus/LGM"
INPUT_SIZE = 256
RASTERIZER_WHEEL = (
"https://huggingface.co/spaces/dylanebert/LGM-mini/resolve/main/wheel/"
"diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl"
)
def install_runtime_dependencies() -> None:
"""
LGM needs diff_gaussian_rasterization.
The original LGM demo installs a prebuilt wheel at runtime.
"""
try:
import diff_gaussian_rasterization # noqa: F401
except Exception:
subprocess.run(
[sys.executable, "-m", "pip", "install", RASTERIZER_WHEEL],
check=True,
)
def get_device_and_dtype():
if torch.cuda.is_available():
return "cuda", torch.float16
return "cpu", torch.float32
@lru_cache(maxsize=1)
def load_pipeline():
install_runtime_dependencies()
device, dtype = get_device_and_dtype()
pipe = DiffusionPipeline.from_pretrained(
MODEL_ID,
custom_pipeline=MODEL_ID,
torch_dtype=dtype,
trust_remote_code=True,
)
pipe = pipe.to(device)
if hasattr(pipe, "enable_attention_slicing"):
pipe.enable_attention_slicing()
return pipe
def center_pad_to_square(image: Image.Image, size: int = INPUT_SIZE) -> Image.Image:
image = image.convert("RGBA")
background = Image.new("RGBA", image.size, (255, 255, 255, 255))
image = Image.alpha_composite(background, image).convert("RGB")
image.thumbnail((size, size), Image.Resampling.LANCZOS)
canvas = Image.new("RGB", (size, size), (255, 255, 255))
left = (size - image.width) // 2
top = (size - image.height) // 2
canvas.paste(image, (left, top))
return canvas
def preprocess_image(image: Image.Image) -> np.ndarray:
if image is None:
raise gr.Error("Please upload a single object image first.")
image = center_pad_to_square(image, INPUT_SIZE)
image = np.asarray(image, dtype=np.float32) / 255.0
return image
@spaces.GPU(duration=120)
def run(image, guidance_scale, num_inference_steps, elevation):
input_image = preprocess_image(image)
pipe = load_pipeline()
device, _ = get_device_and_dtype()
if device == "cuda":
torch.cuda.empty_cache()
with torch.inference_mode():
splat = pipe(
"",
input_image,
guidance_scale=float(guidance_scale),
num_inference_steps=int(num_inference_steps),
elevation=int(elevation),
)
with tempfile.NamedTemporaryFile(delete=False, suffix=".ply") as f:
output_path = f.name
pipe.save_ply(splat, output_path)
return output_path
CUSTOM_CSS = """
#title-block {
text-align: center;
padding: 24px 12px 12px 12px;
}
#title-block h1 {
font-size: 42px;
margin-bottom: 8px;
}
#title-block p {
font-size: 17px;
opacity: 0.86;
}
.tip-box {
border-radius: 16px;
padding: 14px 16px;
background: rgba(127, 127, 127, 0.08);
}
"""
with gr.Blocks(
theme=gr.themes.Soft(
primary_hue="purple",
secondary_hue="blue",
neutral_hue="slate",
),
css=CUSTOM_CSS,
) as demo:
gr.HTML(
"""
<div id="title-block">
<h1>🐙 WasabiOctopus / LGM Tiny</h1>
<p><b>Fast single-image to 3D Gaussian asset generation</b></p>
<p>
Upload a clean single-object image and generate a 3D Gaussian asset powered by LGM.
</p>
</div>
"""
)
with gr.Row():
with gr.Column(scale=1):
image_input = gr.Image(
type="pil",
label="Input Image",
image_mode="RGBA",
height=360,
)
with gr.Accordion("Generation Settings", open=True):
guidance_input = gr.Slider(
minimum=1.0,
maximum=10.0,
value=5.0,
step=0.5,
label="Guidance Scale",
info="Higher values follow the image condition more strongly.",
)
steps_input = gr.Slider(
minimum=10,
maximum=50,
value=30,
step=1,
label="Inference Steps",
info="More steps may improve quality but increase runtime.",
)
elevation_input = gr.Slider(
minimum=-30,
maximum=30,
value=0,
step=1,
label="Elevation",
info="Adjust the assumed camera elevation of the input image.",
)
run_button = gr.Button("🚀 Generate 3D Asset", variant="primary")
gr.HTML(
"""
<div class="tip-box">
<b>Tips for better results</b>
<ul>
<li>Use a single centered object.</li>
<li>Use a clean or transparent background.</li>
<li>Front-view or slightly angled images usually work best.</li>
<li>Avoid tiny structures, heavy occlusion, and reflective surfaces.</li>
</ul>
</div>
"""
)
gr.Examples(
examples=[
[
"https://huggingface.co/datasets/dylanebert/iso3d/resolve/main/jpg@512/a_cat_statue.jpg",
5.0,
30,
0,
],
],
inputs=[
image_input,
guidance_input,
steps_input,
elevation_input,
],
cache_examples=False,
)
with gr.Column(scale=1):
model_output = gr.Model3D(
label="Generated 3D Asset",
height=520,
)
gr.Markdown(
"""
### About this Space
This demo runs **WasabiOctopus/LGM**, a Diffusers-compatible LGM pipeline for fast single-image to 3D Gaussian asset generation.
**Model:** [WasabiOctopus/LGM](https://huggingface.co/WasabiOctopus/LGM)
**Original method:** [LGM: Large Multi-View Gaussian Model](https://arxiv.org/abs/2402.05054)
The output is a `.ply` 3D Gaussian asset that can be previewed directly in the browser.
"""
)
run_button.click(
fn=run,
inputs=[
image_input,
guidance_input,
steps_input,
elevation_input,
],
outputs=model_output,
)
demo.queue(max_size=10).launch() |