Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,7 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
18 |
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
19 |
os.makedirs(TMP_DIR, exist_ok=True)
|
20 |
|
|
|
21 |
def start_session(req: gr.Request):
|
22 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
23 |
os.makedirs(user_dir, exist_ok=True)
|
@@ -28,11 +29,7 @@ def end_session(req: gr.Request):
|
|
28 |
|
29 |
def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
|
30 |
"""
|
31 |
-
|
32 |
-
Args:
|
33 |
-
images (List[Tuple[Image.Image, str]]): The input images.
|
34 |
-
Returns:
|
35 |
-
List[Image.Image]: The preprocessed images.
|
36 |
"""
|
37 |
images = [image[0] for image in images]
|
38 |
processed_images = [pipeline.preprocess_image(image) for image in images]
|
@@ -75,7 +72,7 @@ def unpack_state(state: dict) -> Tuple[Gaussian, edict]:
|
|
75 |
|
76 |
def get_seed(randomize_seed: bool, seed: int) -> int:
|
77 |
"""
|
78 |
-
|
79 |
"""
|
80 |
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
81 |
|
@@ -91,7 +88,7 @@ def image_to_3d(
|
|
91 |
req: gr.Request,
|
92 |
) -> Tuple[dict, str]:
|
93 |
"""
|
94 |
-
|
95 |
"""
|
96 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
97 |
outputs = pipeline.run_multi_image(
|
@@ -126,7 +123,7 @@ def extract_glb(
|
|
126 |
req: gr.Request,
|
127 |
) -> Tuple[str, str]:
|
128 |
"""
|
129 |
-
|
130 |
"""
|
131 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
132 |
gs, mesh = unpack_state(state)
|
@@ -139,7 +136,7 @@ def extract_glb(
|
|
139 |
@spaces.GPU
|
140 |
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
141 |
"""
|
142 |
-
|
143 |
"""
|
144 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
145 |
gs, _ = unpack_state(state)
|
@@ -150,22 +147,21 @@ def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
|
150 |
|
151 |
def prepare_multi_example() -> List[Tuple[str, str]]:
|
152 |
"""
|
153 |
-
|
154 |
-
Returns:
|
155 |
-
List[Tuple[str, str]]: A list of tuples containing image paths and captions.
|
156 |
"""
|
157 |
multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))
|
158 |
examples = []
|
159 |
for case in multi_case:
|
160 |
case_images = []
|
161 |
-
for i in range(1, 4): #
|
162 |
img_path = f'assets/example_multi_image/{case}_{i}.png'
|
163 |
-
if os.path.exists(img_path): #
|
164 |
case_images.append((img_path, f"View {i}"))
|
165 |
-
if case_images: #
|
166 |
examples.append(case_images)
|
167 |
return examples
|
168 |
|
|
|
169 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
170 |
gr.Markdown("""
|
171 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
@@ -220,18 +216,18 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
220 |
|
221 |
output_buf = gr.State()
|
222 |
|
223 |
-
#
|
224 |
with gr.Row(visible=True) as multiimage_example:
|
225 |
examples_multi = gr.Examples(
|
226 |
examples=prepare_multi_example(),
|
227 |
inputs=[multiimage_prompt],
|
228 |
-
fn=lambda x: x,
|
229 |
outputs=[multiimage_prompt],
|
230 |
run_on_click=True,
|
231 |
examples_per_page=8,
|
232 |
)
|
233 |
|
234 |
-
#
|
235 |
demo.load(start_session)
|
236 |
demo.unload(end_session)
|
237 |
|
@@ -282,12 +278,12 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
282 |
outputs=[download_glb],
|
283 |
)
|
284 |
|
285 |
-
#
|
286 |
if __name__ == "__main__":
|
287 |
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
|
288 |
pipeline.cuda()
|
289 |
try:
|
290 |
-
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8)))
|
291 |
except:
|
292 |
pass
|
293 |
demo.launch(show_error=True)
|
|
|
18 |
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
|
19 |
os.makedirs(TMP_DIR, exist_ok=True)
|
20 |
|
21 |
+
# Funciones auxiliares
|
22 |
def start_session(req: gr.Request):
|
23 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
24 |
os.makedirs(user_dir, exist_ok=True)
|
|
|
29 |
|
30 |
def preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:
|
31 |
"""
|
32 |
+
Preprocesa una lista de imágenes.
|
|
|
|
|
|
|
|
|
33 |
"""
|
34 |
images = [image[0] for image in images]
|
35 |
processed_images = [pipeline.preprocess_image(image) for image in images]
|
|
|
72 |
|
73 |
def get_seed(randomize_seed: bool, seed: int) -> int:
|
74 |
"""
|
75 |
+
Obtiene una semilla aleatoria.
|
76 |
"""
|
77 |
return np.random.randint(0, MAX_SEED) if randomize_seed else seed
|
78 |
|
|
|
88 |
req: gr.Request,
|
89 |
) -> Tuple[dict, str]:
|
90 |
"""
|
91 |
+
Convierte múltiples imágenes en un modelo 3D.
|
92 |
"""
|
93 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
94 |
outputs = pipeline.run_multi_image(
|
|
|
123 |
req: gr.Request,
|
124 |
) -> Tuple[str, str]:
|
125 |
"""
|
126 |
+
Extrae un archivo GLB del modelo 3D.
|
127 |
"""
|
128 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
129 |
gs, mesh = unpack_state(state)
|
|
|
136 |
@spaces.GPU
|
137 |
def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
138 |
"""
|
139 |
+
Extrae un archivo Gaussiano del modelo 3D.
|
140 |
"""
|
141 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
142 |
gs, _ = unpack_state(state)
|
|
|
147 |
|
148 |
def prepare_multi_example() -> List[Tuple[str, str]]:
|
149 |
"""
|
150 |
+
Prepara ejemplos de múltiples imágenes para la galería.
|
|
|
|
|
151 |
"""
|
152 |
multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))
|
153 |
examples = []
|
154 |
for case in multi_case:
|
155 |
case_images = []
|
156 |
+
for i in range(1, 4): # Suponemos 3 vistas por caso
|
157 |
img_path = f'assets/example_multi_image/{case}_{i}.png'
|
158 |
+
if os.path.exists(img_path): # Asegurarse de que la imagen existe
|
159 |
case_images.append((img_path, f"View {i}"))
|
160 |
+
if case_images: # Solo añadir casos con imágenes válidas
|
161 |
examples.append(case_images)
|
162 |
return examples
|
163 |
|
164 |
+
# Interfaz Gradio
|
165 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
166 |
gr.Markdown("""
|
167 |
## Image to 3D Asset with [TRELLIS](https://trellis3d.github.io/)
|
|
|
216 |
|
217 |
output_buf = gr.State()
|
218 |
|
219 |
+
# Ejemplos de imágenes múltiples
|
220 |
with gr.Row(visible=True) as multiimage_example:
|
221 |
examples_multi = gr.Examples(
|
222 |
examples=prepare_multi_example(),
|
223 |
inputs=[multiimage_prompt],
|
224 |
+
fn=lambda x: x, # Pasar la entrada directamente (sin preprocesamiento adicional)
|
225 |
outputs=[multiimage_prompt],
|
226 |
run_on_click=True,
|
227 |
examples_per_page=8,
|
228 |
)
|
229 |
|
230 |
+
# Manejadores
|
231 |
demo.load(start_session)
|
232 |
demo.unload(end_session)
|
233 |
|
|
|
278 |
outputs=[download_glb],
|
279 |
)
|
280 |
|
281 |
+
# Lanzar la aplicación Gradio
|
282 |
if __name__ == "__main__":
|
283 |
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
|
284 |
pipeline.cuda()
|
285 |
try:
|
286 |
+
pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Precargar rembg
|
287 |
except:
|
288 |
pass
|
289 |
demo.launch(show_error=True)
|