Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse filesstill working on the high quality download
app.py
CHANGED
@@ -158,7 +158,7 @@ def extract_glb(
|
|
158 |
mesh_simplify: float,
|
159 |
texture_size: int,
|
160 |
req: gr.Request,
|
161 |
-
) -> Tuple[
|
162 |
"""
|
163 |
Extract a GLB file from the 3D model.
|
164 |
|
@@ -169,22 +169,25 @@ def extract_glb(
|
|
169 |
req (gr.Request): Gradio request object.
|
170 |
|
171 |
Returns:
|
172 |
-
Tuple[
|
173 |
"""
|
174 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
175 |
gs, mesh, trial_id = unpack_state(state)
|
176 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
177 |
glb_path = os.path.join(user_dir, f"{trial_id}.glb")
|
178 |
glb.export(glb_path)
|
|
|
|
|
|
|
179 |
torch.cuda.empty_cache()
|
180 |
-
return
|
181 |
|
182 |
# New High-Quality GLB Extraction Function
|
183 |
@spaces.GPU
|
184 |
def extract_glb_high_quality(
|
185 |
state: dict,
|
186 |
req: gr.Request,
|
187 |
-
) -> Tuple[
|
188 |
"""
|
189 |
Extract a high-quality GLB file from the 3D model without polygon reduction.
|
190 |
|
@@ -193,7 +196,7 @@ def extract_glb_high_quality(
|
|
193 |
req (gr.Request): Gradio request object.
|
194 |
|
195 |
Returns:
|
196 |
-
Tuple[
|
197 |
"""
|
198 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
199 |
gs, mesh, trial_id = unpack_state(state)
|
@@ -202,8 +205,11 @@ def extract_glb_high_quality(
|
|
202 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=0.0, texture_size=2048, verbose=False)
|
203 |
glb_path = os.path.join(user_dir, f"{trial_id}_high_quality.glb")
|
204 |
glb.export(glb_path)
|
|
|
|
|
|
|
205 |
torch.cuda.empty_cache()
|
206 |
-
return
|
207 |
|
208 |
# Gradio Interface Definition
|
209 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
@@ -324,8 +330,8 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
324 |
|
325 |
# State Variables
|
326 |
output_buf = gr.State()
|
327 |
-
|
328 |
-
|
329 |
|
330 |
# Example Images
|
331 |
with gr.Row():
|
@@ -377,7 +383,11 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
377 |
extract_glb_btn.click(
|
378 |
extract_glb,
|
379 |
inputs=[output_buf, mesh_simplify, texture_size],
|
380 |
-
outputs=[model_output,
|
|
|
|
|
|
|
|
|
381 |
).then(
|
382 |
lambda: gr.DownloadButton.update(interactive=True),
|
383 |
outputs=[download_glb],
|
@@ -387,10 +397,10 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
387 |
extract_glb_high_quality_btn.click(
|
388 |
extract_glb_high_quality,
|
389 |
inputs=[output_buf],
|
390 |
-
outputs=[model_output,
|
391 |
).then(
|
392 |
-
lambda
|
393 |
-
inputs=[
|
394 |
outputs=[download_high_quality_glb],
|
395 |
).then(
|
396 |
lambda: gr.DownloadButton.update(interactive=True),
|
|
|
158 |
mesh_simplify: float,
|
159 |
texture_size: int,
|
160 |
req: gr.Request,
|
161 |
+
) -> Tuple[dict, bytes]:
|
162 |
"""
|
163 |
Extract a GLB file from the 3D model.
|
164 |
|
|
|
169 |
req (gr.Request): Gradio request object.
|
170 |
|
171 |
Returns:
|
172 |
+
Tuple[dict, bytes]: The model state and the GLB file bytes.
|
173 |
"""
|
174 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
175 |
gs, mesh, trial_id = unpack_state(state)
|
176 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)
|
177 |
glb_path = os.path.join(user_dir, f"{trial_id}.glb")
|
178 |
glb.export(glb_path)
|
179 |
+
# Read the GLB file as bytes
|
180 |
+
with open(glb_path, "rb") as f:
|
181 |
+
glb_bytes = f.read()
|
182 |
torch.cuda.empty_cache()
|
183 |
+
return state, glb_bytes
|
184 |
|
185 |
# New High-Quality GLB Extraction Function
|
186 |
@spaces.GPU
|
187 |
def extract_glb_high_quality(
|
188 |
state: dict,
|
189 |
req: gr.Request,
|
190 |
+
) -> Tuple[dict, bytes]:
|
191 |
"""
|
192 |
Extract a high-quality GLB file from the 3D model without polygon reduction.
|
193 |
|
|
|
196 |
req (gr.Request): Gradio request object.
|
197 |
|
198 |
Returns:
|
199 |
+
Tuple[dict, bytes]: The model state and the high-quality GLB file bytes.
|
200 |
"""
|
201 |
user_dir = os.path.join(TMP_DIR, str(req.session_hash))
|
202 |
gs, mesh, trial_id = unpack_state(state)
|
|
|
205 |
glb = postprocessing_utils.to_glb(gs, mesh, simplify=0.0, texture_size=2048, verbose=False)
|
206 |
glb_path = os.path.join(user_dir, f"{trial_id}_high_quality.glb")
|
207 |
glb.export(glb_path)
|
208 |
+
# Read the GLB file as bytes
|
209 |
+
with open(glb_path, "rb") as f:
|
210 |
+
glb_bytes = f.read()
|
211 |
torch.cuda.empty_cache()
|
212 |
+
return state, glb_bytes
|
213 |
|
214 |
# Gradio Interface Definition
|
215 |
with gr.Blocks(delete_cache=(600, 600)) as demo:
|
|
|
330 |
|
331 |
# State Variables
|
332 |
output_buf = gr.State()
|
333 |
+
glb_bytes_state = gr.State() # For standard GLB
|
334 |
+
glb_high_quality_bytes_state = gr.State() # For high-quality GLB
|
335 |
|
336 |
# Example Images
|
337 |
with gr.Row():
|
|
|
383 |
extract_glb_btn.click(
|
384 |
extract_glb,
|
385 |
inputs=[output_buf, mesh_simplify, texture_size],
|
386 |
+
outputs=[model_output, glb_bytes_state],
|
387 |
+
).then(
|
388 |
+
lambda glb_bytes: (glb_bytes, ),
|
389 |
+
inputs=[glb_bytes_state],
|
390 |
+
outputs=[download_glb],
|
391 |
).then(
|
392 |
lambda: gr.DownloadButton.update(interactive=True),
|
393 |
outputs=[download_glb],
|
|
|
397 |
extract_glb_high_quality_btn.click(
|
398 |
extract_glb_high_quality,
|
399 |
inputs=[output_buf],
|
400 |
+
outputs=[model_output, glb_high_quality_bytes_state],
|
401 |
).then(
|
402 |
+
lambda glb_bytes: (glb_bytes, ),
|
403 |
+
inputs=[glb_high_quality_bytes_state],
|
404 |
outputs=[download_high_quality_glb],
|
405 |
).then(
|
406 |
lambda: gr.DownloadButton.update(interactive=True),
|