Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,280 +1,214 @@
|
|
| 1 |
-
# ==============================================================================
|
| 2 |
-
# 1. INSTALACIÓN DEL ENTORNO Y DEPENDENCIAS
|
| 3 |
-
# ==============================================================================
|
| 4 |
import os
|
| 5 |
import shlex
|
| 6 |
import spaces
|
| 7 |
import subprocess
|
| 8 |
-
import
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
if not os.path.exists("/usr/local/cuda"):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
else:
|
| 25 |
-
|
| 26 |
|
|
|
|
| 27 |
os.environ["CUDA_HOME"] = "/usr/local/cuda"
|
| 28 |
os.environ["PATH"] = f"{os.environ['CUDA_HOME']}/bin:{os.environ['PATH']}"
|
| 29 |
-
|
|
|
|
| 30 |
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
subprocess.run(shlex.split("pip install custom_rasterizer-0.1-cp310-cp310-linux_x86_64.whl"), check=True)
|
| 36 |
-
|
| 37 |
-
logging.info("Instalación completada.")
|
| 38 |
os.system('nvcc -V')
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
import torch
|
| 44 |
import trimesh
|
| 45 |
-
import argparse
|
| 46 |
-
import random
|
| 47 |
-
import numpy as np
|
| 48 |
import gradio as gr
|
| 49 |
-
from PIL import Image
|
| 50 |
-
from huggingface_hub import hf_hub_download
|
| 51 |
-
from diffusers import FluxPipeline, FluxTransformer2DModel, GGUFQuantizationConfig
|
| 52 |
-
from transformers import T5EncoderModel, BitsAndBytesConfig as BitsAndBytesConfigTF
|
| 53 |
-
from step1x3d_geometry.models.pipelines.pipeline import Step1X3DGeometryPipeline
|
| 54 |
from step1x3d_texture.pipelines.step1x_3d_texture_synthesis_pipeline import Step1X3DTexturePipeline
|
| 55 |
from step1x3d_geometry.models.pipelines.pipeline_utils import reduce_face, remove_degenerate_face
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
#
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
parser = argparse.ArgumentParser()
|
| 62 |
-
parser.add_argument("--geometry_model", type=str, default="Step1X-3D-Geometry-Label-1300m")
|
| 63 |
parser.add_argument("--texture_model", type=str, default="Step1X-3D-Texture")
|
| 64 |
parser.add_argument("--cache_dir", type=str, default="cache")
|
| 65 |
args = parser.parse_args()
|
| 66 |
|
| 67 |
os.makedirs(args.cache_dir, exist_ok=True)
|
| 68 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 69 |
-
torch_dtype = torch.bfloat16
|
| 70 |
MAX_SEED = np.iinfo(np.int32).max
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
logging.info(f"Cargando modelo de geometría: {args.geometry_model}")
|
| 76 |
-
geometry_model = Step1X3DGeometryPipeline.from_pretrained(
|
| 77 |
-
"stepfun-ai/Step1X-3D", subfolder=args.geometry_model
|
| 78 |
-
).to(device)
|
| 79 |
|
| 80 |
-
|
| 81 |
texture_model = Step1X3DTexturePipeline.from_pretrained("stepfun-ai/Step1X-3D", subfolder=args.texture_model)
|
|
|
|
| 82 |
|
| 83 |
-
# --- Carga de Modelo FLUX para Texto-a-Imagen ---
|
| 84 |
-
logging.info("Cargando modelo FLUX para Texto-a-Imagen...")
|
| 85 |
-
single_file_base_model = "camenduru/FLUX.1-dev-diffusers"
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
flux_filename = "hyperflux_00001_.q8_0.gguf"
|
| 91 |
-
logging.info(f"Descargando {flux_filename} desde {flux_repo_id}...")
|
| 92 |
-
downloaded_flux_path = hf_hub_download(repo_id=flux_repo_id, filename=flux_filename)
|
| 93 |
-
logging.info(f"Archivo GGUF descargado en: {downloaded_flux_path}")
|
| 94 |
-
# --- FIN DE LA CORRECCIÓN ---
|
| 95 |
-
|
| 96 |
-
quantization_config_tf = BitsAndBytesConfigTF(load_in_8bit=True, bnb_8bit_compute_dtype=torch_dtype)
|
| 97 |
-
text_encoder_2 = T5EncoderModel.from_pretrained(single_file_base_model, subfolder="text_encoder_2", torch_dtype=torch_dtype, quantization_config=quantization_config_tf)
|
| 98 |
-
|
| 99 |
-
# Usar la ruta local descargada
|
| 100 |
-
transformer = FluxTransformer2DModel.from_single_file(downloaded_flux_path, subfolder="transformer", quantization_config=GGUFQuantizationConfig(compute_dtype=torch_dtype), torch_dtype=torch_dtype, config=single_file_base_model)
|
| 101 |
-
flux_pipeline = FluxPipeline.from_pretrained(single_file_base_model, transformer=transformer, text_encoder_2=text_encoder_2, torch_dtype=torch_dtype)
|
| 102 |
-
flux_pipeline.to(device)
|
| 103 |
-
logging.info("Todos los modelos han sido cargados correctamente.")
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# ==============================================================================
|
| 107 |
-
# 3. FUNCIONES DE GENERACIÓN POR PASOS (Sin cambios)
|
| 108 |
-
# ==============================================================================
|
| 109 |
-
|
| 110 |
-
@spaces.GPU(duration=60)
|
| 111 |
-
def generate_image_from_text(prompt, seed, randomize_seed, guidance_scale, num_steps):
|
| 112 |
-
"""Paso 0: Genera una imagen 2D a partir de un prompt de texto usando FLUX."""
|
| 113 |
-
if not prompt:
|
| 114 |
-
raise gr.Error("El prompt de texto no puede estar vacío.")
|
| 115 |
|
|
|
|
|
|
|
| 116 |
if randomize_seed:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
logging.info(f"Generando imagen con prompt: '{prompt}' y seed: {seed}")
|
| 120 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
| 121 |
-
|
| 122 |
-
full_prompt = f"professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting, on a white background"
|
| 123 |
-
negative_prompt = "ugly, deformed, noisy, low poly, blurry, painting, photo, text, watermark"
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
-
image_save_path = f"{args.cache_dir}/{save_name}_t2i.png"
|
| 137 |
-
image.save(image_save_path)
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
@spaces.GPU(duration=180)
|
| 143 |
-
def generate_geometry(input_image_path, guidance_scale, inference_steps, max_facenum, symmetry, edge_type):
|
| 144 |
-
"""Paso 1: Genera la geometría a partir de la imagen generada."""
|
| 145 |
-
if not input_image_path or not os.path.exists(input_image_path):
|
| 146 |
-
raise gr.Error("Primero debes generar una imagen desde el texto.")
|
| 147 |
-
|
| 148 |
-
logging.info(f"Iniciando generación de geometría desde: {os.path.basename(input_image_path)}")
|
| 149 |
|
| 150 |
-
|
| 151 |
-
symmetry_values = ["x", "asymmetry"]
|
| 152 |
-
out = geometry_model(
|
| 153 |
-
input_image_path,
|
| 154 |
-
label={"symmetry": symmetry_values[int(symmetry)], "edge_type": edge_type},
|
| 155 |
-
guidance_scale=float(guidance_scale),
|
| 156 |
-
octree_resolution=384,
|
| 157 |
-
max_facenum=int(max_facenum),
|
| 158 |
-
num_inference_steps=int(inference_steps),
|
| 159 |
-
)
|
| 160 |
-
else:
|
| 161 |
-
out = geometry_model(
|
| 162 |
-
input_image_path,
|
| 163 |
-
guidance_scale=float(guidance_scale),
|
| 164 |
-
num_inference_steps=int(inference_steps),
|
| 165 |
-
max_facenum=int(max_facenum),
|
| 166 |
-
)
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
torch.cuda.empty_cache()
|
| 174 |
-
logging.info(f"Geometría guardada en: {geometry_save_path}")
|
| 175 |
-
return geometry_save_path
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
if not geometry_path or not os.path.exists(geometry_path):
|
| 181 |
-
raise gr.Error("Por favor, primero genera la geometría.")
|
| 182 |
-
if not input_image_path or not os.path.exists(input_image_path):
|
| 183 |
-
raise gr.Error("Se necesita la imagen generada para el texturizado.")
|
| 184 |
-
|
| 185 |
-
logging.info(f"Iniciando texturizado para la malla: {os.path.basename(geometry_path)}")
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
save_name =
|
| 193 |
-
textured_save_path = f"{args.cache_dir}/{save_name}
|
| 194 |
textured_mesh.export(textured_save_path)
|
| 195 |
|
| 196 |
torch.cuda.empty_cache()
|
| 197 |
-
|
|
|
|
| 198 |
return textured_save_path
|
| 199 |
|
| 200 |
-
# ==============================================================================
|
| 201 |
-
# 4. INTERFAZ DE GRADIO (Sin cambios)
|
| 202 |
-
# ==============================================================================
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
generated_image_path_state = gr.State()
|
| 209 |
-
geometry_path_state = gr.State()
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
with gr.Row():
|
| 212 |
with gr.Column(scale=2):
|
| 213 |
-
|
|
|
|
| 214 |
|
| 215 |
-
with gr.Accordion("
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
| 220 |
|
| 221 |
-
|
| 222 |
-
guidance_3d = gr.Number(label="Guidance Scale (3D)", value="7.5")
|
| 223 |
-
steps_3d = gr.Slider(label="Inference Steps (3D)", minimum=1, maximum=100, value=50)
|
| 224 |
-
max_facenum = gr.Number(label="Max Face Num", value="200000")
|
| 225 |
-
symmetry = gr.Radio(choices=["symmetry", "asymmetry"], label="Symmetry", value="symmetry", type="index")
|
| 226 |
-
edge_type = gr.Radio(choices=["sharp", "normal", "smooth"], label="Edge Type", value="sharp", type="value")
|
| 227 |
-
|
| 228 |
-
with gr.Row():
|
| 229 |
-
btn_t2i = gr.Button("0. Generate Image", variant="secondary")
|
| 230 |
-
with gr.Row():
|
| 231 |
-
btn_geo = gr.Button("1. Generate Geometry", interactive=False)
|
| 232 |
-
btn_tex = gr.Button("2. Generate Texture", interactive=False)
|
| 233 |
|
| 234 |
with gr.Column(scale=3):
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
return {
|
| 250 |
-
geometry_path_state: path,
|
| 251 |
-
btn_tex: gr.update(interactive=True, variant="primary"),
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
btn_t2i.click(
|
| 255 |
-
fn=generate_image_from_text,
|
| 256 |
-
inputs=[prompt, seed, randomize_seed, guidance_t2i, steps_t2i],
|
| 257 |
-
outputs=[generated_image_preview]
|
| 258 |
-
).then(
|
| 259 |
-
fn=on_image_generated,
|
| 260 |
-
inputs=[generated_image_preview],
|
| 261 |
-
outputs=[generated_image_path_state, btn_geo, btn_tex, geometry_preview, textured_preview]
|
| 262 |
-
)
|
| 263 |
-
|
| 264 |
-
btn_geo.click(
|
| 265 |
-
fn=generate_geometry,
|
| 266 |
-
inputs=[generated_image_path_state, guidance_3d, steps_3d, max_facenum, symmetry, edge_type],
|
| 267 |
-
outputs=[geometry_preview]
|
| 268 |
-
).then(
|
| 269 |
-
fn=on_geometry_generated,
|
| 270 |
-
inputs=[geometry_preview],
|
| 271 |
-
outputs=[geometry_path_state, btn_tex]
|
| 272 |
-
)
|
| 273 |
-
|
| 274 |
-
btn_tex.click(
|
| 275 |
-
fn=generate_texture,
|
| 276 |
-
inputs=[generated_image_path_state, geometry_path_state],
|
| 277 |
-
outputs=[textured_preview],
|
| 278 |
)
|
| 279 |
|
|
|
|
| 280 |
demo.launch(ssr_mode=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import shlex
|
| 3 |
import spaces
|
| 4 |
import subprocess
|
| 5 |
+
import argparse
|
| 6 |
+
import uuid
|
| 7 |
+
import random
|
| 8 |
+
import numpy as np
|
| 9 |
|
| 10 |
+
# --------------------------------------------------------------------------
|
| 11 |
+
# BLOQUE 1: INSTALACIÓN DEL ENTORNO Y DEPENDENCIAS
|
| 12 |
+
# Esta sección DEBE ejecutarse en su totalidad ANTES de importar torch o gradio.
|
| 13 |
+
# --------------------------------------------------------------------------
|
| 14 |
|
| 15 |
+
@spaces.GPU(duration=120)
|
| 16 |
+
def setup_environment():
|
| 17 |
+
"""
|
| 18 |
+
Prepara todo el entorno necesario, instalando CUDA y compilando las extensiones.
|
| 19 |
+
Esta función se ejecuta una vez al inicio del Space.
|
| 20 |
+
"""
|
| 21 |
+
print("--- INICIANDO CONFIGURACIÓN DEL ENTORNO ---")
|
| 22 |
+
|
| 23 |
+
# 1. Instalar CUDA Toolkit
|
| 24 |
if not os.path.exists("/usr/local/cuda"):
|
| 25 |
+
print("Instalando CUDA Toolkit...")
|
| 26 |
+
CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda_12.4.0_550.54.14_linux.run"
|
| 27 |
+
CUDA_TOOLKIT_FILE = f"/tmp/{os.path.basename(CUDA_TOOLKIT_URL)}"
|
| 28 |
+
|
| 29 |
+
subprocess.run(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE], check=True)
|
| 30 |
+
subprocess.run(["chmod", "+x", CUDA_TOOLKIT_FILE], check=True)
|
| 31 |
+
# Se añade --override para evitar problemas en algunos entornos de HF Spaces
|
| 32 |
+
subprocess.run([CUDA_TOOLKIT_FILE, "--silent", "--toolkit", "--override"], check=True)
|
| 33 |
+
print("CUDA Toolkit instalado.")
|
| 34 |
else:
|
| 35 |
+
print("CUDA Toolkit ya está instalado.")
|
| 36 |
|
| 37 |
+
# 2. Configurar variables de entorno
|
| 38 |
os.environ["CUDA_HOME"] = "/usr/local/cuda"
|
| 39 |
os.environ["PATH"] = f"{os.environ['CUDA_HOME']}/bin:{os.environ['PATH']}"
|
| 40 |
+
# La ruta correcta en la mayoría de las distribuciones de Linux es lib64
|
| 41 |
+
os.environ["LD_LIBRARY_PATH"] = f"{os.environ['CUDA_HOME']}/lib64:{os.environ.get('LD_LIBRARY_PATH', '')}"
|
| 42 |
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
|
| 43 |
+
print("Variables de entorno de CUDA configuradas.")
|
| 44 |
+
|
| 45 |
+
# 3. Verificar NVCC
|
| 46 |
+
print("Verificando versión de NVCC:")
|
|
|
|
|
|
|
|
|
|
| 47 |
os.system('nvcc -V')
|
| 48 |
|
| 49 |
+
# 4. Compilar extensiones C++/CUDA
|
| 50 |
+
print("Compilando extensión de renderizador diferenciable...")
|
| 51 |
+
try:
|
| 52 |
+
subprocess.run(
|
| 53 |
+
"cd /home/user/app/step1x3d_texture/differentiable_renderer/ && python setup.py install",
|
| 54 |
+
shell=True, check=True, capture_output=True, text=True
|
| 55 |
+
)
|
| 56 |
+
print("Renderizador diferenciable compilado.")
|
| 57 |
+
except subprocess.CalledProcessError as e:
|
| 58 |
+
print(f"ERROR al compilar el renderizador diferenciable:\n{e.stderr}")
|
| 59 |
+
raise RuntimeError("Fallo crítico en la compilación del renderizador.")
|
| 60 |
|
| 61 |
+
print("Instalando rasterizador personalizado...")
|
| 62 |
+
try:
|
| 63 |
+
subprocess.run(shlex.split("pip install custom_rasterizer-0.1-cp310-cp310-linux_x86_64.whl"), check=True)
|
| 64 |
+
print("Rasterizador personalizado instalado.")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"ERROR al instalar el rasterizador personalizado: {e}")
|
| 67 |
+
raise RuntimeError("Fallo crítico en la instalación del rasterizador.")
|
| 68 |
+
|
| 69 |
+
print("--- CONFIGURACIÓN DEL ENTORNO FINALIZADA ---")
|
| 70 |
+
|
| 71 |
+
# Ejecutar la configuración del entorno antes de cualquier otra cosa
|
| 72 |
+
setup_environment()
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# --------------------------------------------------------------------------
|
| 76 |
+
# BLOQUE 2: IMPORTACIONES DE LA APLICACIÓN Y LÓGICA PRINCIPAL
|
| 77 |
+
# Todas las importaciones que dependen de CUDA se hacen AHORA.
|
| 78 |
+
# --------------------------------------------------------------------------
|
| 79 |
import torch
|
| 80 |
import trimesh
|
|
|
|
|
|
|
|
|
|
| 81 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
from step1x3d_texture.pipelines.step1x_3d_texture_synthesis_pipeline import Step1X3DTexturePipeline
|
| 83 |
from step1x3d_geometry.models.pipelines.pipeline_utils import reduce_face, remove_degenerate_face
|
| 84 |
|
| 85 |
+
# --------------------------------------------------------------------------
|
| 86 |
+
# 3. CONFIGURACIÓN Y CARGA DEL MODELO DE TEXTURA
|
| 87 |
+
# --------------------------------------------------------------------------
|
|
|
|
| 88 |
parser = argparse.ArgumentParser()
|
|
|
|
| 89 |
parser.add_argument("--texture_model", type=str, default="Step1X-3D-Texture")
|
| 90 |
parser.add_argument("--cache_dir", type=str, default="cache")
|
| 91 |
args = parser.parse_args()
|
| 92 |
|
| 93 |
os.makedirs(args.cache_dir, exist_ok=True)
|
| 94 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 95 |
MAX_SEED = np.iinfo(np.int32).max
|
| 96 |
|
| 97 |
+
print(f"Dispositivo detectado: {device}")
|
| 98 |
+
if not torch.cuda.is_available():
|
| 99 |
+
raise RuntimeError("CUDA no está disponible para PyTorch. La aplicación no puede continuar.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
print(f"Cargando modelo de textura: {args.texture_model}...")
|
| 102 |
texture_model = Step1X3DTexturePipeline.from_pretrained("stepfun-ai/Step1X-3D", subfolder=args.texture_model)
|
| 103 |
+
print("Modelo de textura cargado y listo.")
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
# --------------------------------------------------------------------------
|
| 107 |
+
# 4. FUNCIÓN DE GENERACIÓN DE TEXTURA
|
| 108 |
+
# --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
def get_random_seed(randomize_seed, seed):
|
| 111 |
+
"""Genera una semilla aleatoria si se solicita."""
|
| 112 |
if randomize_seed:
|
| 113 |
+
return random.randint(0, MAX_SEED)
|
| 114 |
+
return int(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
@spaces.GPU(duration=180)
|
| 117 |
+
def generate_texture_for_user_mesh(
|
| 118 |
+
input_image_path,
|
| 119 |
+
input_mesh_path,
|
| 120 |
+
guidance_scale,
|
| 121 |
+
inference_steps,
|
| 122 |
+
seed,
|
| 123 |
+
randomize_seed,
|
| 124 |
+
reference_conditioning_scale,
|
| 125 |
+
progress=gr.Progress(track_tqdm=True)
|
| 126 |
+
):
|
| 127 |
+
"""
|
| 128 |
+
Función principal que genera la textura para un modelo 3D proporcionado por el usuario.
|
| 129 |
+
"""
|
| 130 |
+
if input_image_path is None:
|
| 131 |
+
raise gr.Error("Por favor, sube una imagen de referencia para empezar.")
|
| 132 |
+
if input_mesh_path is None:
|
| 133 |
+
raise gr.Error("Por favor, sube un modelo 3D (.glb o .obj) para texturizar.")
|
| 134 |
+
|
| 135 |
+
print("Iniciando generación de textura para el modelo del usuario...")
|
| 136 |
|
| 137 |
+
seed = get_random_seed(randomize_seed, seed)
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
texture_model.config.guidance_scale = float(guidance_scale)
|
| 140 |
+
texture_model.config.num_inference_steps = int(inference_steps)
|
| 141 |
+
texture_model.config.reference_conditioning_scale = float(reference_conditioning_scale)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
print(f"Parámetros: Pasos={inference_steps}, Escala Guía={guidance_scale}, Semilla={seed}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
print(f"Cargando malla desde: {input_mesh_path}")
|
| 146 |
+
try:
|
| 147 |
+
user_mesh = trimesh.load(input_mesh_path, force='mesh')
|
| 148 |
+
except Exception as e:
|
| 149 |
+
raise gr.Error(f"No se pudo cargar el archivo del modelo 3D. Error: {e}")
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
print("Pre-procesando la malla...")
|
| 152 |
+
user_mesh = remove_degenerate_face(user_mesh)
|
| 153 |
+
user_mesh = reduce_face(user_mesh)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
print("Ejecutando el pipeline de texturizado de Step1X-3D...")
|
| 156 |
+
textured_mesh = texture_model(
|
| 157 |
+
image=input_image_path,
|
| 158 |
+
mesh=user_mesh,
|
| 159 |
+
remove_bg=True,
|
| 160 |
+
seed=seed
|
| 161 |
+
)
|
| 162 |
|
| 163 |
+
save_name = str(uuid.uuid4())
|
| 164 |
+
textured_save_path = f"{args.cache_dir}/{save_name}-textured.glb"
|
| 165 |
textured_mesh.export(textured_save_path)
|
| 166 |
|
| 167 |
torch.cuda.empty_cache()
|
| 168 |
+
print(f"Malla texturizada guardada en: {textured_save_path}")
|
| 169 |
+
|
| 170 |
return textured_save_path
|
| 171 |
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
# --------------------------------------------------------------------------
|
| 174 |
+
# 5. INTERFAZ DE USUARIO CON GRADIO
|
| 175 |
+
# --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
with gr.Blocks(title="Step1X-3D Texture Generator") as demo:
|
| 178 |
+
gr.Markdown("# Step1X-3D: Generador de Texturas para Mallas Personalizadas")
|
| 179 |
+
gr.Markdown("### Sube tu propia imagen y modelo 3D (`.glb`, `.obj`) para generar una textura de alta calidad.")
|
| 180 |
+
|
| 181 |
with gr.Row():
|
| 182 |
with gr.Column(scale=2):
|
| 183 |
+
input_image = gr.Image(label="Imagen de Referencia", type="filepath", sources=["upload"])
|
| 184 |
+
input_mesh = gr.Model3D(label="Modelo 3D de Entrada (.glb, .obj)")
|
| 185 |
|
| 186 |
+
with gr.Accordion(label="Parámetros Avanzados", open=True):
|
| 187 |
+
guidance_scale = gr.Slider(label="Guidance Scale (Fidelidad a la imagen)", minimum=1.0, maximum=10.0, value=3.0, step=0.1)
|
| 188 |
+
inference_steps = gr.Slider(label="Pasos de Inferencia (Calidad vs. Velocidad)", minimum=10, maximum=100, value=50, step=1)
|
| 189 |
+
reference_conditioning_scale = gr.Slider(label="Escala de Condicionamiento de Imagen", minimum=0.0, maximum=2.0, step=0.1, value=1.0)
|
| 190 |
+
with gr.Row():
|
| 191 |
+
seed = gr.Slider(label="Semilla (Seed)", minimum=0, maximum=MAX_SEED, step=1, value=2024, interactive=True)
|
| 192 |
+
randomize_seed = gr.Checkbox(label="Aleatoria", value=True)
|
| 193 |
|
| 194 |
+
btn_generate = gr.Button("✨ Generar Textura", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
with gr.Column(scale=3):
|
| 197 |
+
output_model = gr.Model3D(label="Resultado: Modelo Texturizado", height=600, clear_color=[0.0, 0.0, 0.0, 0.0])
|
| 198 |
+
|
| 199 |
+
btn_generate.click(
|
| 200 |
+
fn=generate_texture_for_user_mesh,
|
| 201 |
+
inputs=[
|
| 202 |
+
input_image,
|
| 203 |
+
input_mesh,
|
| 204 |
+
guidance_scale,
|
| 205 |
+
inference_steps,
|
| 206 |
+
seed,
|
| 207 |
+
randomize_seed,
|
| 208 |
+
reference_conditioning_scale,
|
| 209 |
+
],
|
| 210 |
+
outputs=[output_model]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
+
# Lanza la aplicación (share=True no es necesario en HF Spaces)
|
| 214 |
demo.launch(ssr_mode=False)
|