Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,159 +1,146 @@
|
|
| 1 |
-
#
|
| 2 |
-
#
|
| 3 |
-
#
|
| 4 |
-
# - diffusers / FluxPipeline o el pipeline que uses para generar la imagen
|
| 5 |
-
# - step1x3d_geometry y step1x3d_texture (pipelines que ya usabas)
|
| 6 |
-
# - trellis si lo usas (opcional)
|
| 7 |
-
# Ajusta nombres de modelos, tokens y paths según tu entorno.
|
| 8 |
-
|
| 9 |
import os
|
| 10 |
-
import
|
|
|
|
|
|
|
| 11 |
import logging
|
| 12 |
-
import shutil
|
| 13 |
-
from typing import Tuple, Union
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
import gradio as gr
|
| 16 |
from PIL import Image
|
| 17 |
-
import
|
| 18 |
-
import
|
| 19 |
-
import imageio
|
| 20 |
-
|
| 21 |
-
# Si tienes decoradores de spaces definidos (como spaces.GPU), impórtalos.
|
| 22 |
-
# from spaces import GPU # si usas spaces.GPU
|
| 23 |
-
import spaces # si lo necesitas por compatibilidad con tus decoradores
|
| 24 |
-
|
| 25 |
-
# Importa tus pipelines Step1X (o los que uses)
|
| 26 |
from step1x3d_geometry.models.pipelines.pipeline import Step1X3DGeometryPipeline
|
| 27 |
-
from step1x3d_texture.pipelines.step1x_3d_texture_synthesis_pipeline import
|
| 28 |
-
Step1X3DTexturePipeline,
|
| 29 |
-
)
|
| 30 |
from step1x3d_geometry.models.pipelines.pipeline_utils import reduce_face, remove_degenerate_face
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Model names (ajusta a los subfolders que usas)
|
| 41 |
-
GEOMETRY_SUBFOLDER = "Step1X-3D-Geometry-Label-1300m"
|
| 42 |
-
TEXTURE_SUBFOLDER = "Step1X-3D-Texture"
|
| 43 |
-
STEP1X_MODEL_REPO = "stepfun-ai/Step1X-3D" # repo base
|
| 44 |
-
# Para text-to-image: usa tu modelo preferido; aquí dejo un placeholder
|
| 45 |
-
IMAGE_GEN_MODEL = "camenduru/FLUX.1-dev-diffusers" # si usas Flux o diffusers
|
| 46 |
-
# Folder para caché/temporales (por sesión)
|
| 47 |
-
TMP_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")
|
| 48 |
-
os.makedirs(TMP_ROOT, exist_ok=True)
|
| 49 |
-
# -----------------------------------
|
| 50 |
|
|
|
|
| 51 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
geometry_model = None
|
| 56 |
-
texture_model = None
|
| 57 |
-
image_gen_pipeline = None
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
try:
|
| 72 |
-
shutil.rmtree(user_dir)
|
| 73 |
-
logger.info(f"[{session_hash}] user dir removed")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
logger.warning(f"[{session_hash}] failed removing user dir: {e}")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
user_dir = os.path.join(TMP_ROOT, session_hash)
|
| 80 |
-
os.makedirs(user_dir, exist_ok=True)
|
| 81 |
-
path = os.path.join(user_dir, name)
|
| 82 |
-
img.save(path)
|
| 83 |
-
return path
|
| 84 |
|
| 85 |
-
|
| 86 |
-
@spaces.GPU # si usas spaces.GPU; si no, puedes quitar
|
| 87 |
-
def generate_image_from_text(
|
| 88 |
-
prompt: str,
|
| 89 |
-
seed: int,
|
| 90 |
-
randomize_seed: bool,
|
| 91 |
-
width: int,
|
| 92 |
-
height: int,
|
| 93 |
-
guidance_scale: float,
|
| 94 |
-
req: gr.Request,
|
| 95 |
-
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
| 96 |
-
) -> Image.Image:
|
| 97 |
-
"""Genera una imagen 2D desde prompt y devuelve PIL.Image"""
|
| 98 |
-
global image_gen_pipeline
|
| 99 |
-
session_hash = str(req.session_hash)
|
| 100 |
-
logger.info(f"[{session_hash}] Generando imagen desde texto: '{prompt[:80]}'")
|
| 101 |
-
if randomize_seed:
|
| 102 |
-
seed = int(np.random.randint(0, np.iinfo(np.int32).max))
|
| 103 |
-
logger.info(f"[{session_hash}] seed aleatorio -> {seed}")
|
| 104 |
-
generator = torch.Generator(device=device).manual_seed(seed) if device == "cuda" else torch.manual_seed(seed)
|
| 105 |
|
| 106 |
-
# Ajusta la llamada según el pipeline que uses (FluxPipeline, DiffusionPipeline, etc.)
|
| 107 |
-
# Ejemplo genérico con DiffusionPipeline (puede necesitar rename de args)
|
| 108 |
-
result = image_gen_pipeline(
|
| 109 |
-
prompt,
|
| 110 |
-
guidance_scale=float(guidance_scale),
|
| 111 |
-
num_inference_steps=8,
|
| 112 |
-
width=int(width),
|
| 113 |
-
height=int(height),
|
| 114 |
-
generator=generator,
|
| 115 |
-
)
|
| 116 |
-
image = result.images[0] if hasattr(result, "images") else result # compat
|
| 117 |
-
# Guarda la imagen en carpeta de sesión
|
| 118 |
-
path = save_pil_image_for_session(image, req, name="generated_2d_image.png")
|
| 119 |
-
logger.info(f"[{session_hash}] imagen guardada en: {path}")
|
| 120 |
-
return image
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
max_facenum,
|
| 129 |
-
symmetry,
|
| 130 |
-
edge_type,
|
| 131 |
-
req: gr.Request,
|
| 132 |
-
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
| 133 |
-
):
|
| 134 |
"""
|
| 135 |
-
Genera
|
| 136 |
-
input_image puede ser path (str) o PIL.Image.
|
| 137 |
-
Devuelve (geometry_preview_path, geometry_path_state)
|
| 138 |
"""
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
symmetry_values = ["x", "asymmetry"]
|
| 155 |
out = geometry_model(
|
| 156 |
-
|
| 157 |
label={"symmetry": symmetry_values[int(symmetry)], "edge_type": edge_type},
|
| 158 |
guidance_scale=float(guidance_scale),
|
| 159 |
octree_resolution=384,
|
|
@@ -162,161 +149,145 @@ def generate_geometry(
|
|
| 162 |
)
|
| 163 |
else:
|
| 164 |
out = geometry_model(
|
| 165 |
-
|
| 166 |
guidance_scale=float(guidance_scale),
|
| 167 |
num_inference_steps=int(inference_steps),
|
| 168 |
max_facenum=int(max_facenum),
|
| 169 |
)
|
| 170 |
|
| 171 |
-
save_name =
|
| 172 |
-
geometry_save_path =
|
| 173 |
geometry_mesh = out.mesh[0]
|
| 174 |
geometry_mesh.export(geometry_save_path)
|
| 175 |
-
|
| 176 |
torch.cuda.empty_cache()
|
| 177 |
-
|
|
|
|
| 178 |
|
| 179 |
-
# Devuelve la ruta para preview (Model3D) y para guardar en el state
|
| 180 |
-
return geometry_save_path, geometry_save_path
|
| 181 |
-
|
| 182 |
-
# ---------- Generar textura a partir de geometría ----------
|
| 183 |
@spaces.GPU(duration=120)
|
| 184 |
-
def generate_texture(
|
| 185 |
-
|
| 186 |
-
session_hash = str(req.session_hash)
|
| 187 |
-
logger.info(f"[{session_hash}] Iniciando texturizado para: {geometry_path}")
|
| 188 |
-
|
| 189 |
if not geometry_path or not os.path.exists(geometry_path):
|
| 190 |
-
raise gr.Error("Por favor, primero genera la geometría
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
img_path = save_pil_image_for_session(input_image, req, name="for_texture.png")
|
| 196 |
-
|
| 197 |
-
# Carga y postprocesado
|
| 198 |
-
import trimesh
|
| 199 |
geometry_mesh = trimesh.load(geometry_path)
|
|
|
|
|
|
|
| 200 |
geometry_mesh = remove_degenerate_face(geometry_mesh)
|
| 201 |
geometry_mesh = reduce_face(geometry_mesh)
|
| 202 |
-
|
| 203 |
-
textured_mesh = texture_model(
|
| 204 |
-
|
| 205 |
-
save_name = os.path.basename(geometry_path).replace(".glb", "")
|
| 206 |
-
textured_save_path =
|
| 207 |
textured_mesh.export(textured_save_path)
|
| 208 |
-
|
| 209 |
torch.cuda.empty_cache()
|
| 210 |
-
|
| 211 |
-
|
| 212 |
return textured_save_path
|
| 213 |
|
| 214 |
-
#
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
gr.Markdown("Flujo: Texto → Generar imagen → Generar geometría → Texturizar")
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
geometry_path_state = gr.State()
|
| 220 |
-
|
| 221 |
-
|
| 222 |
with gr.Row():
|
| 223 |
with gr.Column(scale=2):
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 228 |
-
with gr.Row():
|
| 229 |
-
width = gr.Slider(256, 1024, label="Width", value=512, step=64)
|
| 230 |
-
height = gr.Slider(256, 1024, label="Height", value=512, step=64)
|
| 231 |
-
guidance_scale = gr.Slider(0.0, 10.0, label="Guidance Scale", value=3.5, step=0.1)
|
| 232 |
-
|
| 233 |
-
generate_image_btn = gr.Button("1. Generar Imagen")
|
| 234 |
-
generate_geo_btn = gr.Button("2. Generar Geometría", interactive=False, visible=True)
|
| 235 |
-
generate_tex_btn = gr.Button("3. Generar Textura", interactive=False, visible=False)
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
with gr.Column(scale=3):
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
| 241 |
|
| 242 |
with gr.Column(scale=1):
|
| 243 |
-
gr.
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
#
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
fn=generate_image_from_text,
|
| 257 |
-
inputs=[prompt,
|
| 258 |
-
outputs=[
|
| 259 |
).then(
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
outputs=[
|
| 263 |
)
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
# usamos generated_image (PIL) y enviamos a generate_geometry
|
| 267 |
-
generate_geo_btn.click(
|
| 268 |
fn=generate_geometry,
|
| 269 |
-
inputs=[
|
| 270 |
-
|
| 271 |
-
guidance_geom,
|
| 272 |
-
inference_steps_geom,
|
| 273 |
-
max_facenum,
|
| 274 |
-
symmetry,
|
| 275 |
-
edge_type,
|
| 276 |
-
],
|
| 277 |
-
outputs=[geometry_preview, geometry_path_state],
|
| 278 |
).then(
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
outputs=[
|
| 282 |
)
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
generate_tex_btn.click(
|
| 286 |
fn=generate_texture,
|
| 287 |
-
inputs=[
|
| 288 |
outputs=[textured_preview],
|
| 289 |
)
|
| 290 |
|
| 291 |
-
|
| 292 |
-
if __name__ == "__main__":
|
| 293 |
-
# --------- Inicializar image generation pipeline ----------
|
| 294 |
-
try:
|
| 295 |
-
# Si tienes un pipeline específico (FluxPipeline) reemplaza la línea siguiente
|
| 296 |
-
logger.info("Inicializando pipeline de generación de imágenes...")
|
| 297 |
-
image_gen_pipeline = DiffusionPipeline.from_pretrained(IMAGE_GEN_MODEL, use_auth_token=HUGGINGFACE_TOKEN)
|
| 298 |
-
image_gen_pipeline = image_gen_pipeline.to(device)
|
| 299 |
-
logger.info("Pipeline de imagen cargado.")
|
| 300 |
-
except Exception as e:
|
| 301 |
-
logger.error(f"Error cargando pipeline de imágenes: {e}")
|
| 302 |
-
image_gen_pipeline = None
|
| 303 |
-
|
| 304 |
-
# --------- Inicializar Step1X modelos ----------
|
| 305 |
-
try:
|
| 306 |
-
logger.info("Cargando modelo de geometría Step1X...")
|
| 307 |
-
geometry_model = Step1X3DGeometryPipeline.from_pretrained(STEP1X_MODEL_REPO, subfolder=GEOMETRY_SUBFOLDER).to(device)
|
| 308 |
-
logger.info("Modelo de geometría cargado.")
|
| 309 |
-
except Exception as e:
|
| 310 |
-
logger.error(f"Error cargando modelo de geometría: {e}")
|
| 311 |
-
geometry_model = None
|
| 312 |
-
|
| 313 |
-
try:
|
| 314 |
-
logger.info("Cargando modelo de textura Step1X...")
|
| 315 |
-
texture_model = Step1X3DTexturePipeline.from_pretrained(STEP1X_MODEL_REPO, subfolder=TEXTURE_SUBFOLDER)
|
| 316 |
-
logger.info("Modelo de textura cargado.")
|
| 317 |
-
except Exception as e:
|
| 318 |
-
logger.error(f"Error cargando modelo de textura: {e}")
|
| 319 |
-
texture_model = None
|
| 320 |
-
|
| 321 |
-
# Lanzar app
|
| 322 |
-
demo.launch(show_error=True)
|
|
|
|
| 1 |
+
# ==============================================================================
|
| 2 |
+
# 1. INSTALACIÓN DEL ENTORNO Y DEPENDENCIAS
|
| 3 |
+
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
+
import shlex
|
| 6 |
+
import spaces
|
| 7 |
+
import subprocess
|
| 8 |
import logging
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Configuración del logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - Step1X-3D - %(levelname)s - %(message)s')
|
| 12 |
+
|
| 13 |
+
def install_dependencies():
|
| 14 |
+
"""Instala el toolkit de CUDA y compila las extensiones C++/CUDA necesarias."""
|
| 15 |
+
logging.info("Iniciando la instalación de dependencias...")
|
| 16 |
+
|
| 17 |
+
CUDA_TOOLKIT_URL = "https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda_12.4.0_550.54.14_linux.run"
|
| 18 |
+
CUDA_TOOLKIT_FILE = f"/tmp/{os.path.basename(CUDA_TOOLKIT_URL)}"
|
| 19 |
+
if not os.path.exists("/usr/local/cuda"):
|
| 20 |
+
logging.info("Descargando e instalando CUDA Toolkit...")
|
| 21 |
+
subprocess.call(["wget", "-q", CUDA_TOOLKIT_URL, "-O", CUDA_TOOLKIT_FILE])
|
| 22 |
+
subprocess.call(["chmod", "+x", CUDA_TOOLKIT_FILE])
|
| 23 |
+
subprocess.call([CUDA_TOOLKIT_FILE, "--silent", "--toolkit"])
|
| 24 |
+
else:
|
| 25 |
+
logging.info("CUDA Toolkit ya está instalado.")
|
| 26 |
+
|
| 27 |
+
os.environ["CUDA_HOME"] = "/usr/local/cuda"
|
| 28 |
+
os.environ["PATH"] = f"{os.environ['CUDA_HOME']}/bin:{os.environ['PATH']}"
|
| 29 |
+
os.environ["LD_LIBRARY_PATH"] = f"{os.environ['CUDA_HOME']}/lib:{os.environ.get('LD_LIBRARY_PATH', '')}"
|
| 30 |
+
os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
|
| 31 |
+
|
| 32 |
+
logging.info("Compilando extensiones de renderizado...")
|
| 33 |
+
renderer_path = "/home/user/app/step1x3d_texture/differentiable_renderer/"
|
| 34 |
+
subprocess.run(f"cd {renderer_path} && python setup.py install", shell=True, check=True)
|
| 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 |
+
install_dependencies()
|
| 41 |
+
|
| 42 |
+
import uuid
|
| 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 diffusers import FluxPipeline, FluxTransformer2DModel
|
| 51 |
+
from transformers import T5EncoderModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
from step1x3d_geometry.models.pipelines.pipeline import Step1X3DGeometryPipeline
|
| 53 |
+
from step1x3d_texture.pipelines.step1x_3d_texture_synthesis_pipeline import Step1X3DTexturePipeline
|
|
|
|
|
|
|
| 54 |
from step1x3d_geometry.models.pipelines.pipeline_utils import reduce_face, remove_degenerate_face
|
| 55 |
|
| 56 |
+
# ==============================================================================
|
| 57 |
+
# 2. CONFIGURACIÓN Y CARGA DE MODELOS
|
| 58 |
+
# ==============================================================================
|
| 59 |
|
| 60 |
+
parser = argparse.ArgumentParser()
|
| 61 |
+
parser.add_argument("--geometry_model", type=str, default="Step1X-3D-Geometry-Label-1300m")
|
| 62 |
+
parser.add_argument("--texture_model", type=str, default="Step1X-3D-Texture")
|
| 63 |
+
parser.add_argument("--cache_dir", type=str, default="cache")
|
| 64 |
+
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
os.makedirs(args.cache_dir, exist_ok=True)
|
| 67 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 68 |
+
torch_dtype = torch.bfloat16 # bfloat16 es mejor para FLUX
|
| 69 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 70 |
|
| 71 |
+
logging.info("Cargando modelos... Este proceso puede tardar varios minutos.")
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# --- Carga del modelo FLUX para Texto a Imagen ---
|
| 74 |
+
logging.info("Cargando modelo FLUX.1-dev...")
|
| 75 |
+
flux_pipe = FluxPipeline.from_pretrained(
|
| 76 |
+
"black-forest-labs/FLUX.1-dev",
|
| 77 |
+
torch_dtype=torch_dtype,
|
| 78 |
+
variant="fp16" # Usar fp16 para menor uso de memoria
|
| 79 |
+
)
|
| 80 |
+
flux_pipe.to(device)
|
| 81 |
+
logging.info("Modelo FLUX cargado.")
|
| 82 |
|
| 83 |
+
# --- Carga de Modelos Step1X-3D ---
|
| 84 |
+
logging.info(f"Cargando modelo de geometría: {args.geometry_model}")
|
| 85 |
+
geometry_model = Step1X3DGeometryPipeline.from_pretrained(
|
| 86 |
+
"stepfun-ai/Step1X-3D", subfolder=args.geometry_model
|
| 87 |
+
).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
logging.info(f"Cargando modelo de textura: {args.texture_model}")
|
| 90 |
+
texture_model = Step1X3DTexturePipeline.from_pretrained("stepfun-ai/Step1X-3D", subfolder=args.texture_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
logging.info("Todos los modelos han sido cargados correctamente.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# ==============================================================================
|
| 96 |
+
# 3. FUNCIONES DE GENERACIÓN POR PASOS
|
| 97 |
+
# ==============================================================================
|
| 98 |
+
|
| 99 |
+
@spaces.GPU(duration=60)
|
| 100 |
+
def generate_image_from_text(prompt, negative_prompt, guidance_scale, num_steps, seed, randomize_seed):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
"""
|
| 102 |
+
Paso 0: Genera una imagen 2D a partir de un texto usando FLUX.
|
|
|
|
|
|
|
| 103 |
"""
|
| 104 |
+
if not prompt:
|
| 105 |
+
raise gr.Error("El prompt no puede estar vacío.")
|
| 106 |
+
|
| 107 |
+
if randomize_seed:
|
| 108 |
+
seed = random.randint(0, MAX_SEED)
|
| 109 |
+
|
| 110 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 111 |
+
|
| 112 |
+
logging.info(f"Generando imagen con prompt: '{prompt}', Seed: {seed}")
|
| 113 |
+
|
| 114 |
+
# Añadir modificadores para mejorar la calidad y el estilo 3D
|
| 115 |
+
final_prompt = f"3d model, {prompt}, octane render, professionally rendered, high quality, white background"
|
| 116 |
+
|
| 117 |
+
image = flux_pipe(
|
| 118 |
+
prompt=final_prompt,
|
| 119 |
+
negative_prompt=negative_prompt,
|
| 120 |
+
num_inference_steps=int(num_steps),
|
| 121 |
+
guidance_scale=float(guidance_scale),
|
| 122 |
+
generator=generator,
|
| 123 |
+
).images[0]
|
| 124 |
+
|
| 125 |
+
save_name = str(uuid.uuid4())
|
| 126 |
+
image_save_path = f"{args.cache_dir}/{save_name}_generated_image.png"
|
| 127 |
+
image.save(image_save_path)
|
| 128 |
+
|
| 129 |
+
logging.info(f"Imagen 2D generada y guardada en: {image_save_path}")
|
| 130 |
+
return image_save_path, seed
|
| 131 |
|
| 132 |
+
@spaces.GPU(duration=180)
|
| 133 |
+
def generate_geometry(input_image_path, guidance_scale, inference_steps, max_facenum, symmetry, edge_type):
|
| 134 |
+
"""Paso 1: Genera la geometría a partir de la imagen generada."""
|
| 135 |
+
if not input_image_path or not os.path.exists(input_image_path):
|
| 136 |
+
raise gr.Error("Primero debes generar una imagen a partir del texto.")
|
| 137 |
+
|
| 138 |
+
logging.info(f"Iniciando generación de geometría desde: {os.path.basename(input_image_path)}")
|
| 139 |
+
|
| 140 |
+
if "Label" in args.geometry_model:
|
| 141 |
symmetry_values = ["x", "asymmetry"]
|
| 142 |
out = geometry_model(
|
| 143 |
+
input_image_path,
|
| 144 |
label={"symmetry": symmetry_values[int(symmetry)], "edge_type": edge_type},
|
| 145 |
guidance_scale=float(guidance_scale),
|
| 146 |
octree_resolution=384,
|
|
|
|
| 149 |
)
|
| 150 |
else:
|
| 151 |
out = geometry_model(
|
| 152 |
+
input_image_path,
|
| 153 |
guidance_scale=float(guidance_scale),
|
| 154 |
num_inference_steps=int(inference_steps),
|
| 155 |
max_facenum=int(max_facenum),
|
| 156 |
)
|
| 157 |
|
| 158 |
+
save_name = os.path.basename(input_image_path).replace("_generated_image.png", "")
|
| 159 |
+
geometry_save_path = f"{args.cache_dir}/{save_name}_geometry.glb"
|
| 160 |
geometry_mesh = out.mesh[0]
|
| 161 |
geometry_mesh.export(geometry_save_path)
|
| 162 |
+
|
| 163 |
torch.cuda.empty_cache()
|
| 164 |
+
logging.info(f"Geometría guardada en: {geometry_save_path}")
|
| 165 |
+
return geometry_save_path
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
@spaces.GPU(duration=120)
|
| 168 |
+
def generate_texture(input_image_path, geometry_path):
|
| 169 |
+
"""Paso 2: Aplica la textura a la geometría generada."""
|
|
|
|
|
|
|
|
|
|
| 170 |
if not geometry_path or not os.path.exists(geometry_path):
|
| 171 |
+
raise gr.Error("Por favor, primero genera la geometría.")
|
| 172 |
+
if not input_image_path or not os.path.exists(input_image_path):
|
| 173 |
+
raise gr.Error("Se necesita la imagen generada para el texturizado.")
|
| 174 |
+
|
| 175 |
+
logging.info(f"Iniciando texturizado para la malla: {os.path.basename(geometry_path)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
geometry_mesh = trimesh.load(geometry_path)
|
| 177 |
+
|
| 178 |
+
# Post-procesamiento
|
| 179 |
geometry_mesh = remove_degenerate_face(geometry_mesh)
|
| 180 |
geometry_mesh = reduce_face(geometry_mesh)
|
| 181 |
+
|
| 182 |
+
textured_mesh = texture_model(input_image_path, geometry_mesh)
|
| 183 |
+
|
| 184 |
+
save_name = os.path.basename(geometry_path).replace("_geometry.glb", "")
|
| 185 |
+
textured_save_path = f"{args.cache_dir}/{save_name}_textured.glb"
|
| 186 |
textured_mesh.export(textured_save_path)
|
| 187 |
+
|
| 188 |
torch.cuda.empty_cache()
|
| 189 |
+
logging.info(f"Malla texturizada guardada en: {textured_save_path}")
|
|
|
|
| 190 |
return textured_save_path
|
| 191 |
|
| 192 |
+
# ==============================================================================
|
| 193 |
+
# 4. INTERFAZ DE GRADIO
|
| 194 |
+
# ==============================================================================
|
|
|
|
| 195 |
|
| 196 |
+
with gr.Blocks(title="Step1X-3D: Texto a 3D", css="footer {display: none !important;} a {text-decoration: none !important;}") as demo:
|
| 197 |
+
gr.Markdown("# Step1X-3D: Flujo de Texto a 3D")
|
| 198 |
+
gr.Markdown("Flujo de trabajo en 3 pasos: **0. Texto a Imagen → 1. Generar Geometría → 2. Generar Textura**")
|
| 199 |
+
|
| 200 |
+
# Estados para mantener las rutas de los archivos
|
| 201 |
+
generated_image_path_state = gr.State()
|
| 202 |
geometry_path_state = gr.State()
|
| 203 |
+
|
|
|
|
| 204 |
with gr.Row():
|
| 205 |
with gr.Column(scale=2):
|
| 206 |
+
# --- Panel de Entradas ---
|
| 207 |
+
prompt = gr.Textbox(label="Paso 0: Describe el objeto 3D que quieres crear", value="a detailed, realistic battle axe")
|
| 208 |
+
|
| 209 |
+
with gr.Accordion(label="Opciones Avanzadas", open=False):
|
| 210 |
+
gr.Markdown("### Opciones de Texto a Imagen (Paso 0)")
|
| 211 |
+
neg_prompt = gr.Textbox(label="Negative Prompt (Imagen)", value="blurry, low quality, bad, text, watermark")
|
| 212 |
+
guidance_image = gr.Slider(0.0, 10.0, label="Guidance Scale (Imagen)", value=4.0, step=0.1)
|
| 213 |
+
steps_image = gr.Slider(10, 50, label="Steps (Imagen)", value=28, step=1)
|
| 214 |
+
seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
|
| 215 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
+
gr.Markdown("---")
|
| 218 |
+
gr.Markdown("### Opciones de Generación 3D (Paso 1)")
|
| 219 |
+
guidance_3d = gr.Number(label="Guidance Scale (3D)", value="7.5")
|
| 220 |
+
steps_3d = gr.Slider(label="Inference Steps (3D)", minimum=1, maximum=100, value=50)
|
| 221 |
+
max_facenum = gr.Number(label="Max Face Num", value="200000")
|
| 222 |
+
symmetry = gr.Radio(choices=["symmetry", "asymmetry"], label="Symmetry", value="symmetry", type="index")
|
| 223 |
+
edge_type = gr.Radio(choices=["sharp", "normal", "smooth"], label="Edge Type", value="sharp", type="value")
|
| 224 |
+
|
| 225 |
+
with gr.Row():
|
| 226 |
+
btn_gen_image = gr.Button("0. Generar Imagen", variant="secondary")
|
| 227 |
+
with gr.Row():
|
| 228 |
+
btn_geo = gr.Button("1. Generar Geometría", interactive=False)
|
| 229 |
+
btn_tex = gr.Button("2. Generar Textura", interactive=False)
|
| 230 |
+
|
| 231 |
with gr.Column(scale=3):
|
| 232 |
+
# --- Panel de Salidas ---
|
| 233 |
+
image_preview = gr.Image(label="Resultado de Texto a Imagen", type="filepath", interactive=False, height=400)
|
| 234 |
+
geometry_preview = gr.Model3D(label="Vista Previa de la Geometría", height=400, clear_color=[0.0, 0.0, 0.0, 0.0])
|
| 235 |
+
textured_preview = gr.Model3D(label="Vista Previa del Modelo Texturizado", height=400, clear_color=[0.0, 0.0, 0.0, 0.0])
|
| 236 |
|
| 237 |
with gr.Column(scale=1):
|
| 238 |
+
gr.Examples(
|
| 239 |
+
examples=[
|
| 240 |
+
["a small wooden chest with gold trim"],
|
| 241 |
+
["a futuristic sci-fi pistol"],
|
| 242 |
+
["a cute, chibi-style red dragon"],
|
| 243 |
+
["a slice of pizza with pepperoni and mushrooms"],
|
| 244 |
+
["a classic leather-bound book with a gold clasp"],
|
| 245 |
+
],
|
| 246 |
+
inputs=[prompt], cache_examples=False
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# --- Lógica de la Interfaz ---
|
| 250 |
+
|
| 251 |
+
def on_image_generated(path, current_seed):
|
| 252 |
+
return {
|
| 253 |
+
generated_image_path_state: path,
|
| 254 |
+
btn_geo: gr.update(interactive=True, variant="primary"),
|
| 255 |
+
btn_tex: gr.update(interactive=False),
|
| 256 |
+
geometry_preview: gr.update(value=None),
|
| 257 |
+
textured_preview: gr.update(value=None),
|
| 258 |
+
seed: gr.update(value=int(current_seed)) # Actualiza el slider de la seed
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
def on_geometry_generated(path):
|
| 262 |
+
return {
|
| 263 |
+
geometry_path_state: path,
|
| 264 |
+
btn_tex: gr.update(interactive=True, variant="primary"),
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
btn_gen_image.click(
|
| 268 |
fn=generate_image_from_text,
|
| 269 |
+
inputs=[prompt, neg_prompt, guidance_image, steps_image, seed, randomize_seed],
|
| 270 |
+
outputs=[image_preview, seed]
|
| 271 |
).then(
|
| 272 |
+
fn=on_image_generated,
|
| 273 |
+
inputs=[image_preview, seed],
|
| 274 |
+
outputs=[generated_image_path_state, btn_geo, btn_tex, geometry_preview, textured_preview, seed]
|
| 275 |
)
|
| 276 |
+
|
| 277 |
+
btn_geo.click(
|
|
|
|
|
|
|
| 278 |
fn=generate_geometry,
|
| 279 |
+
inputs=[generated_image_path_state, guidance_3d, steps_3d, max_facenum, symmetry, edge_type],
|
| 280 |
+
outputs=[geometry_preview]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
).then(
|
| 282 |
+
fn=on_geometry_generated,
|
| 283 |
+
inputs=[geometry_preview],
|
| 284 |
+
outputs=[geometry_path_state, btn_tex]
|
| 285 |
)
|
| 286 |
+
|
| 287 |
+
btn_tex.click(
|
|
|
|
| 288 |
fn=generate_texture,
|
| 289 |
+
inputs=[generated_image_path_state, geometry_path_state],
|
| 290 |
outputs=[textured_preview],
|
| 291 |
)
|
| 292 |
|
| 293 |
+
demo.launch(ssr_mode=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|