Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import os
|
|
| 2 |
import shlex
|
| 3 |
import spaces
|
| 4 |
import subprocess
|
|
|
|
|
|
|
| 5 |
def install_cuda_toolkit():
|
| 6 |
CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda_12.4.0_550.54.14_linux.run"
|
| 7 |
CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL)
|
|
@@ -16,6 +18,7 @@ def install_cuda_toolkit():
|
|
| 16 |
"" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"],
|
| 17 |
)
|
| 18 |
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
|
|
|
|
| 19 |
install_cuda_toolkit()
|
| 20 |
os.system("pip list | grep torch")
|
| 21 |
os.system('nvcc -V')
|
|
@@ -23,6 +26,7 @@ print("cd /home/user/app/step1x3d_texture/differentiable_renderer/ && python set
|
|
| 23 |
os.system("cd /home/user/app/step1x3d_texture/differentiable_renderer/ && python setup.py install")
|
| 24 |
|
| 25 |
subprocess.run(shlex.split("pip install custom_rasterizer-0.1-cp310-cp310-linux_x86_64.whl"), check=True)
|
|
|
|
| 26 |
import time
|
| 27 |
import uuid
|
| 28 |
import torch
|
|
@@ -36,7 +40,7 @@ from step1x3d_texture.pipelines.step1x_3d_texture_synthesis_pipeline import (
|
|
| 36 |
)
|
| 37 |
from step1x3d_geometry.models.pipelines.pipeline_utils import reduce_face, remove_degenerate_face
|
| 38 |
|
| 39 |
-
|
| 40 |
parser = argparse.ArgumentParser()
|
| 41 |
parser.add_argument(
|
| 42 |
"--geometry_model", type=str, default="Step1X-3D-Geometry-Label-1300m"
|
|
@@ -56,11 +60,16 @@ geometry_model = Step1X3DGeometryPipeline.from_pretrained(
|
|
| 56 |
texture_model = Step1X3DTexturePipeline.from_pretrained("stepfun-ai/Step1X-3D", subfolder=args.texture_model)
|
| 57 |
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
input_image_path, guidance_scale, inference_steps, max_facenum, symmetry, edge_type
|
| 62 |
):
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
if "Label" in args.geometry_model:
|
| 65 |
symmetry_values = ["x", "asymmetry"]
|
| 66 |
out = geometry_model(
|
|
@@ -80,24 +89,52 @@ def generate_func(
|
|
| 80 |
)
|
| 81 |
|
| 82 |
save_name = str(uuid.uuid4())
|
| 83 |
-
print(save_name)
|
| 84 |
geometry_save_path = f"{args.cache_dir}/{save_name}.glb"
|
| 85 |
geometry_mesh = out.mesh[0]
|
| 86 |
geometry_mesh.export(geometry_save_path)
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
geometry_mesh = remove_degenerate_face(geometry_mesh)
|
| 89 |
geometry_mesh = reduce_face(geometry_mesh)
|
|
|
|
|
|
|
| 90 |
textured_mesh = texture_model(input_image_path, geometry_mesh)
|
|
|
|
|
|
|
| 91 |
textured_save_path = f"{args.cache_dir}/{save_name}-textured.glb"
|
| 92 |
textured_mesh.export(textured_save_path)
|
| 93 |
-
|
| 94 |
torch.cuda.empty_cache()
|
| 95 |
-
print("
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
|
|
|
| 98 |
|
| 99 |
with gr.Blocks(title="Step1X-3D demo") as demo:
|
| 100 |
gr.Markdown("# Step1X-3D")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column(scale=2):
|
| 103 |
input_image = gr.Image(label="Image", type="filepath")
|
|
@@ -118,10 +155,15 @@ with gr.Blocks(title="Step1X-3D demo") as demo:
|
|
| 118 |
value="sharp",
|
| 119 |
type="value",
|
| 120 |
)
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
with gr.Column(scale=4):
|
| 123 |
-
textured_preview = gr.Model3D(label="Textured", height=380)
|
| 124 |
-
geometry_preview = gr.Model3D(label="Geometry", height=380)
|
|
|
|
| 125 |
with gr.Column(scale=1):
|
| 126 |
gr.Examples(
|
| 127 |
examples=[
|
|
@@ -138,8 +180,11 @@ with gr.Blocks(title="Step1X-3D demo") as demo:
|
|
| 138 |
cache_examples=False,
|
| 139 |
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
inputs=[
|
| 144 |
input_image,
|
| 145 |
guidance_scale,
|
|
@@ -148,7 +193,21 @@ with gr.Blocks(title="Step1X-3D demo") as demo:
|
|
| 148 |
symmetry,
|
| 149 |
edge_type,
|
| 150 |
],
|
| 151 |
-
outputs=[geometry_preview,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
-
demo.launch(ssr_mode=False)
|
|
|
|
| 2 |
import shlex
|
| 3 |
import spaces
|
| 4 |
import subprocess
|
| 5 |
+
|
| 6 |
+
# --- Instalaci贸n del Entorno (sin cambios) ---
|
| 7 |
def install_cuda_toolkit():
|
| 8 |
CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda_12.4.0_550.54.14_linux.run"
|
| 9 |
CUDA_TOOLKIT_FILE = "/tmp/%s" % os.path.basename(CUDA_TOOLKIT_URL)
|
|
|
|
| 18 |
"" if "LD_LIBRARY_PATH" not in os.environ else os.environ["LD_LIBRARY_PATH"],
|
| 19 |
)
|
| 20 |
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
|
| 21 |
+
|
| 22 |
install_cuda_toolkit()
|
| 23 |
os.system("pip list | grep torch")
|
| 24 |
os.system('nvcc -V')
|
|
|
|
| 26 |
os.system("cd /home/user/app/step1x3d_texture/differentiable_renderer/ && python setup.py install")
|
| 27 |
|
| 28 |
subprocess.run(shlex.split("pip install custom_rasterizer-0.1-cp310-cp310-linux_x86_64.whl"), check=True)
|
| 29 |
+
|
| 30 |
import time
|
| 31 |
import uuid
|
| 32 |
import torch
|
|
|
|
| 40 |
)
|
| 41 |
from step1x3d_geometry.models.pipelines.pipeline_utils import reduce_face, remove_degenerate_face
|
| 42 |
|
| 43 |
+
# --- Configuraci贸n y Carga de Modelos (sin cambios) ---
|
| 44 |
parser = argparse.ArgumentParser()
|
| 45 |
parser.add_argument(
|
| 46 |
"--geometry_model", type=str, default="Step1X-3D-Geometry-Label-1300m"
|
|
|
|
| 60 |
texture_model = Step1X3DTexturePipeline.from_pretrained("stepfun-ai/Step1X-3D", subfolder=args.texture_model)
|
| 61 |
|
| 62 |
|
| 63 |
+
# --- NUEVAS FUNCIONES DE GENERACI脫N SEPARADAS ---
|
| 64 |
+
|
| 65 |
+
@spaces.GPU(duration=180)
|
| 66 |
+
def generate_geometry(
|
| 67 |
input_image_path, guidance_scale, inference_steps, max_facenum, symmetry, edge_type
|
| 68 |
):
|
| 69 |
+
"""
|
| 70 |
+
Funci贸n que genera 煤nicamente la geometr铆a.
|
| 71 |
+
"""
|
| 72 |
+
print("Iniciando generaci贸n de geometr铆a...")
|
| 73 |
if "Label" in args.geometry_model:
|
| 74 |
symmetry_values = ["x", "asymmetry"]
|
| 75 |
out = geometry_model(
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
save_name = str(uuid.uuid4())
|
|
|
|
| 92 |
geometry_save_path = f"{args.cache_dir}/{save_name}.glb"
|
| 93 |
geometry_mesh = out.mesh[0]
|
| 94 |
geometry_mesh.export(geometry_save_path)
|
| 95 |
+
|
| 96 |
+
torch.cuda.empty_cache()
|
| 97 |
+
print(f"Geometr铆a guardada en: {geometry_save_path}")
|
| 98 |
+
|
| 99 |
+
# Devuelve la ruta para mostrar y para guardar en el estado
|
| 100 |
+
return geometry_save_path, geometry_save_path
|
| 101 |
+
|
| 102 |
+
@spaces.GPU(duration=120)
|
| 103 |
+
def generate_texture(input_image_path, geometry_path):
|
| 104 |
+
"""
|
| 105 |
+
Funci贸n que aplica la textura a una geometr铆a existente.
|
| 106 |
+
"""
|
| 107 |
+
if not geometry_path or not os.path.exists(geometry_path):
|
| 108 |
+
raise gr.Error("Por favor, primero genera la geometr铆a antes de texturizar.")
|
| 109 |
+
|
| 110 |
+
print(f"Iniciando texturizado para la malla: {geometry_path}")
|
| 111 |
+
geometry_mesh = trimesh.load(geometry_path)
|
| 112 |
+
|
| 113 |
+
# Post-procesamiento de la geometr铆a antes de texturizar
|
| 114 |
geometry_mesh = remove_degenerate_face(geometry_mesh)
|
| 115 |
geometry_mesh = reduce_face(geometry_mesh)
|
| 116 |
+
|
| 117 |
+
# Aplicar textura
|
| 118 |
textured_mesh = texture_model(input_image_path, geometry_mesh)
|
| 119 |
+
|
| 120 |
+
save_name = os.path.basename(geometry_path).replace(".glb", "")
|
| 121 |
textured_save_path = f"{args.cache_dir}/{save_name}-textured.glb"
|
| 122 |
textured_mesh.export(textured_save_path)
|
| 123 |
+
|
| 124 |
torch.cuda.empty_cache()
|
| 125 |
+
print(f"Malla texturizada guardada en: {textured_save_path}")
|
| 126 |
+
|
| 127 |
+
return textured_save_path
|
| 128 |
+
|
| 129 |
|
| 130 |
+
# --- NUEVA INTERFAZ DE GRADIO ---
|
| 131 |
|
| 132 |
with gr.Blocks(title="Step1X-3D demo") as demo:
|
| 133 |
gr.Markdown("# Step1X-3D")
|
| 134 |
+
|
| 135 |
+
# Estado para almacenar la ruta de la geometr铆a generada
|
| 136 |
+
geometry_path_state = gr.State()
|
| 137 |
+
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column(scale=2):
|
| 140 |
input_image = gr.Image(label="Image", type="filepath")
|
|
|
|
| 155 |
value="sharp",
|
| 156 |
type="value",
|
| 157 |
)
|
| 158 |
+
|
| 159 |
+
with gr.Row():
|
| 160 |
+
btn_geo = gr.Button("1. Generate Geometry")
|
| 161 |
+
btn_tex = gr.Button("2. Generate Texture", visible=False)
|
| 162 |
+
|
| 163 |
with gr.Column(scale=4):
|
| 164 |
+
textured_preview = gr.Model3D(label="Textured Model", height=380, clear_color=[0.0, 0.0, 0.0, 0.0])
|
| 165 |
+
geometry_preview = gr.Model3D(label="Geometry-only Model", height=380, clear_color=[0.0, 0.0, 0.0, 0.0])
|
| 166 |
+
|
| 167 |
with gr.Column(scale=1):
|
| 168 |
gr.Examples(
|
| 169 |
examples=[
|
|
|
|
| 180 |
cache_examples=False,
|
| 181 |
)
|
| 182 |
|
| 183 |
+
# L贸gica de los botones
|
| 184 |
+
|
| 185 |
+
# Al hacer clic en el bot贸n de geometr铆a
|
| 186 |
+
btn_geo.click(
|
| 187 |
+
fn=generate_geometry,
|
| 188 |
inputs=[
|
| 189 |
input_image,
|
| 190 |
guidance_scale,
|
|
|
|
| 193 |
symmetry,
|
| 194 |
edge_type,
|
| 195 |
],
|
| 196 |
+
outputs=[geometry_preview, geometry_path_state]
|
| 197 |
+
).then(
|
| 198 |
+
# Cuando la geometr铆a termine, actualiza la interfaz
|
| 199 |
+
fn=lambda: {
|
| 200 |
+
btn_tex: gr.update(visible=True), # Muestra el bot贸n de texturizado
|
| 201 |
+
textured_preview: gr.update(value=None) # Limpia la vista previa de la textura anterior
|
| 202 |
+
},
|
| 203 |
+
outputs=[btn_tex, textured_preview]
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Al hacer clic en el bot贸n de textura
|
| 207 |
+
btn_tex.click(
|
| 208 |
+
fn=generate_texture,
|
| 209 |
+
inputs=[input_image, geometry_path_state],
|
| 210 |
+
outputs=[textured_preview],
|
| 211 |
)
|
| 212 |
|
| 213 |
+
demo.launch(ssr_mode=False)
|