Spaces:
Sleeping
Sleeping
Update app.py
Browse filesupdate the end file_path
app.py
CHANGED
|
@@ -17,8 +17,10 @@ from trellis.utils import render_utils, postprocessing_utils
|
|
| 17 |
|
| 18 |
|
| 19 |
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
-
|
| 21 |
-
os.
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def start_session(req: gr.Request):
|
|
@@ -128,6 +130,7 @@ def get_seed(randomize_seed: bool, seed: int) -> int:
|
|
| 128 |
def generate_and_extract_glb(
|
| 129 |
image: Image.Image,
|
| 130 |
multiimages: List[Tuple[Image.Image, str]],
|
|
|
|
| 131 |
seed: int,
|
| 132 |
ss_guidance_strength: float,
|
| 133 |
ss_sampling_steps: int,
|
|
@@ -137,33 +140,25 @@ def generate_and_extract_glb(
|
|
| 137 |
mesh_simplify: float,
|
| 138 |
texture_size: int,
|
| 139 |
req: gr.Request,
|
| 140 |
-
) ->
|
|
|
|
|
|
|
|
|
|
| 141 |
"""
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
seed (int): The random seed.
|
| 149 |
-
ss_guidance_strength (float): The guidance strength for sparse structure generation.
|
| 150 |
-
ss_sampling_steps (int): The number of sampling steps for sparse structure generation.
|
| 151 |
-
slat_guidance_strength (float): The guidance strength for structured latent generation.
|
| 152 |
-
slat_sampling_steps (int): The number of sampling steps for structured latent generation.
|
| 153 |
-
multiimage_algo (Literal["multidiffusion", "stochastic"]): The algorithm for multi-image generation.
|
| 154 |
-
mesh_simplify (float): The mesh simplification factor.
|
| 155 |
-
texture_size (int): The texture resolution.
|
| 156 |
|
| 157 |
-
Returns:
|
| 158 |
-
dict: The information of the generated 3D model.
|
| 159 |
-
str: The path to the video of the 3D model.
|
| 160 |
-
str: The path to the extracted GLB file.
|
| 161 |
-
str: The path to the extracted GLB file (for download).
|
| 162 |
-
"""
|
| 163 |
-
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
| 164 |
-
|
| 165 |
# Generate 3D model
|
| 166 |
-
if
|
| 167 |
outputs = pipeline.run(
|
| 168 |
image,
|
| 169 |
seed=seed,
|
|
@@ -180,7 +175,7 @@ def generate_and_extract_glb(
|
|
| 180 |
)
|
| 181 |
else:
|
| 182 |
outputs = pipeline.run_multi_image(
|
| 183 |
-
[
|
| 184 |
seed=seed,
|
| 185 |
formats=["gaussian", "mesh"],
|
| 186 |
preprocess_image=False,
|
|
@@ -194,26 +189,23 @@ def generate_and_extract_glb(
|
|
| 194 |
},
|
| 195 |
mode=multiimage_algo,
|
| 196 |
)
|
| 197 |
-
|
| 198 |
-
#
|
| 199 |
-
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 200 |
-
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 201 |
-
video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
|
| 202 |
-
video_path = os.path.join(user_dir, 'sample.mp4')
|
| 203 |
-
imageio.mimsave(video_path, video, fps=15)
|
| 204 |
-
|
| 205 |
-
# Extract GLB
|
| 206 |
gs = outputs['gaussian'][0]
|
| 207 |
mesh = outputs['mesh'][0]
|
| 208 |
-
glb = postprocessing_utils.to_glb(
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
glb.export(glb_path)
|
| 211 |
-
|
| 212 |
-
# Pack state for optional Gaussian extraction
|
| 213 |
-
state = pack_state(gs, mesh)
|
| 214 |
-
|
| 215 |
torch.cuda.empty_cache()
|
| 216 |
-
|
|
|
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
@spaces.GPU
|
|
@@ -390,7 +382,7 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 390 |
).then(
|
| 391 |
generate_and_extract_glb,
|
| 392 |
inputs=[image_prompt, multiimage_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo, mesh_simplify, texture_size],
|
| 393 |
-
outputs=[
|
| 394 |
).then(
|
| 395 |
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
| 396 |
outputs=[extract_gs_btn, download_glb],
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
+
# Custom save directory for GLB files
|
| 21 |
+
CUSTOM_SAVE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'outputs')
|
| 22 |
+
os.makedirs(CUSTOM_SAVE_DIR, exist_ok=True)
|
| 23 |
+
|
| 24 |
|
| 25 |
|
| 26 |
def start_session(req: gr.Request):
|
|
|
|
| 130 |
def generate_and_extract_glb(
|
| 131 |
image: Image.Image,
|
| 132 |
multiimages: List[Tuple[Image.Image, str]],
|
| 133 |
+
is_multiimage: bool,
|
| 134 |
seed: int,
|
| 135 |
ss_guidance_strength: float,
|
| 136 |
ss_sampling_steps: int,
|
|
|
|
| 140 |
mesh_simplify: float,
|
| 141 |
texture_size: int,
|
| 142 |
req: gr.Request,
|
| 143 |
+
) -> str:
|
| 144 |
+
"""
|
| 145 |
+
Generate the 3D model and save only the GLB file to a custom folder.
|
| 146 |
+
The GLB filename matches the input image filename (or first image if multi-image).
|
| 147 |
"""
|
| 148 |
+
# Derive base filename from the first image in the batch
|
| 149 |
+
if not is_multiimage:
|
| 150 |
+
base_filename = getattr(image, "filename", None) or "generated_model"
|
| 151 |
+
else:
|
| 152 |
+
# Use the first uploaded image filename if available
|
| 153 |
+
base_filename = getattr(multiimages[0][0], "filename", None) or "generated_model"
|
| 154 |
|
| 155 |
+
# Strip directories and extension to get a clean name
|
| 156 |
+
base_name = os.path.splitext(os.path.basename(base_filename))[0]
|
| 157 |
+
glb_filename = f"{base_name}.glb"
|
| 158 |
+
glb_path = os.path.join(CUSTOM_SAVE_DIR, glb_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
# Generate 3D model
|
| 161 |
+
if not is_multiimage:
|
| 162 |
outputs = pipeline.run(
|
| 163 |
image,
|
| 164 |
seed=seed,
|
|
|
|
| 175 |
)
|
| 176 |
else:
|
| 177 |
outputs = pipeline.run_multi_image(
|
| 178 |
+
[img[0] for img in multiimages],
|
| 179 |
seed=seed,
|
| 180 |
formats=["gaussian", "mesh"],
|
| 181 |
preprocess_image=False,
|
|
|
|
| 189 |
},
|
| 190 |
mode=multiimage_algo,
|
| 191 |
)
|
| 192 |
+
|
| 193 |
+
# Export GLB file to custom folder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
gs = outputs['gaussian'][0]
|
| 195 |
mesh = outputs['mesh'][0]
|
| 196 |
+
glb = postprocessing_utils.to_glb(
|
| 197 |
+
gs,
|
| 198 |
+
mesh,
|
| 199 |
+
simplify=mesh_simplify,
|
| 200 |
+
texture_size=texture_size,
|
| 201 |
+
verbose=False
|
| 202 |
+
)
|
| 203 |
glb.export(glb_path)
|
| 204 |
+
|
|
|
|
|
|
|
|
|
|
| 205 |
torch.cuda.empty_cache()
|
| 206 |
+
print(f"[Saved GLB] {glb_path}")
|
| 207 |
+
return glb_path
|
| 208 |
+
|
| 209 |
|
| 210 |
|
| 211 |
@spaces.GPU
|
|
|
|
| 382 |
).then(
|
| 383 |
generate_and_extract_glb,
|
| 384 |
inputs=[image_prompt, multiimage_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo, mesh_simplify, texture_size],
|
| 385 |
+
outputs=[download_glb],
|
| 386 |
).then(
|
| 387 |
lambda: tuple([gr.Button(interactive=True), gr.Button(interactive=True)]),
|
| 388 |
outputs=[extract_gs_btn, download_glb],
|