Spaces:
Runtime error
Runtime error
Logging
Browse files
app.py
CHANGED
|
@@ -13,6 +13,10 @@ from trellis.pipelines import TrellisImageTo3DPipeline
|
|
| 13 |
from trellis.representations import Gaussian, MeshExtractResult
|
| 14 |
from trellis.utils import render_utils, postprocessing_utils
|
| 15 |
import pydantic
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
print(pydantic.__version__)
|
| 17 |
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
|
@@ -20,15 +24,29 @@ TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
|
| 20 |
os.makedirs(TMP_DIR, exist_ok=True)
|
| 21 |
|
| 22 |
def start_session(req: gr.Request):
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
os.makedirs(user_dir, exist_ok=True)
|
| 25 |
|
| 26 |
def end_session(req: gr.Request):
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def preprocess_image(image: Image.Image) -> Image.Image:
|
|
|
|
| 31 |
processed_image = pipeline.preprocess_image(image)
|
|
|
|
| 32 |
return processed_image
|
| 33 |
|
| 34 |
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
|
@@ -70,7 +88,9 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
|
| 70 |
return gs, mesh
|
| 71 |
|
| 72 |
def get_seed(randomize_seed: bool, seed: int) -> int:
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
@spaces.GPU
|
| 76 |
def image_to_3d(
|
|
@@ -82,7 +102,10 @@ def image_to_3d(
|
|
| 82 |
slat_sampling_steps: int,
|
| 83 |
req: gr.Request,
|
| 84 |
) -> Tuple[dict, str]:
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
outputs = pipeline.run(
|
| 87 |
image,
|
| 88 |
seed=seed,
|
|
@@ -98,16 +121,17 @@ def image_to_3d(
|
|
| 98 |
},
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 102 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 103 |
-
|
| 104 |
-
# Solo usamos el video de color, eliminamos la concatenación
|
| 105 |
video = video
|
| 106 |
|
| 107 |
video_path = os.path.join(user_dir, 'sample.mp4')
|
| 108 |
imageio.mimsave(video_path, video, fps=15)
|
| 109 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
| 110 |
torch.cuda.empty_cache()
|
|
|
|
| 111 |
return state, video_path
|
| 112 |
|
| 113 |
@spaces.GPU(duration=90)
|
|
@@ -117,12 +141,17 @@ def extract_glb(
|
|
| 117 |
texture_size: int,
|
| 118 |
req: gr.Request,
|
| 119 |
) -> Tuple[str, str]:
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
gs, mesh = unpack_state(state)
|
| 122 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
| 123 |
glb_path = os.path.join(user_dir, 'sample.glb')
|
| 124 |
glb.export(glb_path)
|
|
|
|
| 125 |
torch.cuda.empty_cache()
|
|
|
|
| 126 |
return glb_path, glb_path
|
| 127 |
|
| 128 |
def split_image(image: Image.Image) -> List[Image.Image]:
|
|
@@ -183,7 +212,6 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
| 183 |
|
| 184 |
extract_glb_btn = gr.Button("Export GLB", interactive=False, size="lg")
|
| 185 |
|
| 186 |
-
# Right column (Outputs)
|
| 187 |
with gr.Column(scale=3, min_width=600):
|
| 188 |
with gr.Group():
|
| 189 |
video_output = gr.Video(
|
|
|
|
| 13 |
from trellis.representations import Gaussian, MeshExtractResult
|
| 14 |
from trellis.utils import render_utils, postprocessing_utils
|
| 15 |
import pydantic
|
| 16 |
+
import logging
|
| 17 |
+
|
| 18 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - HF_SPACE - %(levelname)s - %(message)s')
|
| 19 |
+
|
| 20 |
print(pydantic.__version__)
|
| 21 |
|
| 22 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 24 |
os.makedirs(TMP_DIR, exist_ok=True)
|
| 25 |
|
| 26 |
def start_session(req: gr.Request):
|
| 27 |
+
session_hash = str(req.session_hash)
|
| 28 |
+
user_dir = os.path.join(TMP_DIR, session_hash)
|
| 29 |
+
logging.info(f"START SESSION: Creando directorio para la sesión {session_hash} en {user_dir}")
|
| 30 |
os.makedirs(user_dir, exist_ok=True)
|
| 31 |
|
| 32 |
def end_session(req: gr.Request):
|
| 33 |
+
session_hash = str(req.session_hash)
|
| 34 |
+
user_dir = os.path.join(TMP_DIR, session_hash)
|
| 35 |
+
logging.info(f"END SESSION: Intentando eliminar el directorio de la sesión {session_hash} en {user_dir}")
|
| 36 |
+
# Hacemos la eliminación más robusta.
|
| 37 |
+
if os.path.exists(user_dir):
|
| 38 |
+
try:
|
| 39 |
+
shutil.rmtree(user_dir)
|
| 40 |
+
logging.info(f"Directorio de la sesión {session_hash} eliminado correctamente.")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logging.error(f"Error al eliminar el directorio de la sesión {session_hash}: {e}")
|
| 43 |
+
else:
|
| 44 |
+
logging.warning(f"El directorio de la sesión {session_hash} no fue encontrado al intentar eliminarlo. Es posible que ya haya sido limpiado.")
|
| 45 |
|
| 46 |
def preprocess_image(image: Image.Image) -> Image.Image:
|
| 47 |
+
logging.info("Preprocesando imagen...")
|
| 48 |
processed_image = pipeline.preprocess_image(image)
|
| 49 |
+
logging.info("Imagen preprocesada correctamente.")
|
| 50 |
return processed_image
|
| 51 |
|
| 52 |
def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
|
|
|
| 88 |
return gs, mesh
|
| 89 |
|
| 90 |
def get_seed(randomize_seed: bool, seed: int) -> int:
|
| 91 |
+
new_seed = np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
| 92 |
+
logging.info(f"Usando seed: {new_seed}")
|
| 93 |
+
return new_seed
|
| 94 |
|
| 95 |
@spaces.GPU
|
| 96 |
def image_to_3d(
|
|
|
|
| 102 |
slat_sampling_steps: int,
|
| 103 |
req: gr.Request,
|
| 104 |
) -> Tuple[dict, str]:
|
| 105 |
+
session_hash = str(req.session_hash)
|
| 106 |
+
logging.info(f"[{session_hash}] Iniciando image_to_3d...")
|
| 107 |
+
user_dir = os.path.join(TMP_DIR, session_hash)
|
| 108 |
+
|
| 109 |
outputs = pipeline.run(
|
| 110 |
image,
|
| 111 |
seed=seed,
|
|
|
|
| 121 |
},
|
| 122 |
)
|
| 123 |
|
| 124 |
+
logging.info(f"[{session_hash}] Generación del modelo completada. Renderizando video...")
|
| 125 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 126 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
| 127 |
+
|
|
|
|
| 128 |
video = video
|
| 129 |
|
| 130 |
video_path = os.path.join(user_dir, 'sample.mp4')
|
| 131 |
imageio.mimsave(video_path, video, fps=15)
|
| 132 |
state = pack_state(outputs['gaussian'][0], outputs['mesh'][0])
|
| 133 |
torch.cuda.empty_cache()
|
| 134 |
+
logging.info(f"[{session_hash}] Video renderizado y estado empaquetado. Devolviendo: {video_path}")
|
| 135 |
return state, video_path
|
| 136 |
|
| 137 |
@spaces.GPU(duration=90)
|
|
|
|
| 141 |
texture_size: int,
|
| 142 |
req: gr.Request,
|
| 143 |
) -> Tuple[str, str]:
|
| 144 |
+
session_hash = str(req.session_hash)
|
| 145 |
+
logging.info(f"[{session_hash}] Iniciando extract_glb...")
|
| 146 |
+
user_dir = os.path.join(TMP_DIR, session_hash)
|
| 147 |
+
|
| 148 |
gs, mesh = unpack_state(state)
|
| 149 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
| 150 |
glb_path = os.path.join(user_dir, 'sample.glb')
|
| 151 |
glb.export(glb_path)
|
| 152 |
+
|
| 153 |
torch.cuda.empty_cache()
|
| 154 |
+
logging.info(f"[{session_hash}] GLB extraído. Devolviendo: {glb_path}")
|
| 155 |
return glb_path, glb_path
|
| 156 |
|
| 157 |
def split_image(image: Image.Image) -> List[Image.Image]:
|
|
|
|
| 212 |
|
| 213 |
extract_glb_btn = gr.Button("Export GLB", interactive=False, size="lg")
|
| 214 |
|
|
|
|
| 215 |
with gr.Column(scale=3, min_width=600):
|
| 216 |
with gr.Group():
|
| 217 |
video_output = gr.Video(
|