Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,48 @@
|
|
| 1 |
import torch
|
| 2 |
from diffusers import ShapEPipeline
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
def generate_3d_model(prompt, output_path="assistant_3d.
|
| 7 |
"""
|
| 8 |
-
Generate a 3D model using ShapE
|
| 9 |
"""
|
| 10 |
try:
|
| 11 |
-
#
|
| 12 |
pipe = ShapEPipeline.from_pretrained(
|
| 13 |
"openai/shap-e",
|
| 14 |
torch_dtype=torch.float32,
|
| 15 |
low_cpu_mem_usage=True
|
| 16 |
).to("cpu")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
outputs = pipe(
|
| 20 |
prompt,
|
| 21 |
-
num_inference_steps=
|
| 22 |
-
frame_size=
|
| 23 |
-
guidance_scale=
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Error during generation: {e}")
|
|
@@ -41,8 +51,9 @@ def generate_3d_model(prompt, output_path="assistant_3d.gif"):
|
|
| 41 |
raise
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
prompt = "
|
| 45 |
try:
|
| 46 |
-
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Generation failed: {e}")
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import ShapEPipeline
|
| 3 |
+
import trimesh
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
def generate_3d_model(prompt, output_path="assistant_3d.obj"):
|
| 7 |
"""
|
| 8 |
+
Generate a 3D model using ShapE and export it in a Blender-compatible format
|
| 9 |
"""
|
| 10 |
try:
|
| 11 |
+
# Load pipeline with minimal settings
|
| 12 |
pipe = ShapEPipeline.from_pretrained(
|
| 13 |
"openai/shap-e",
|
| 14 |
torch_dtype=torch.float32,
|
| 15 |
low_cpu_mem_usage=True
|
| 16 |
).to("cpu")
|
| 17 |
|
| 18 |
+
# Generate with minimal parameters
|
| 19 |
outputs = pipe(
|
| 20 |
prompt,
|
| 21 |
+
num_inference_steps=16,
|
| 22 |
+
frame_size=24,
|
| 23 |
+
guidance_scale=7.5
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Get the mesh data
|
| 27 |
+
mesh = outputs.meshes[0] # Get the first mesh
|
| 28 |
+
|
| 29 |
+
# Convert to vertices and faces format
|
| 30 |
+
verts = mesh.verts.detach().cpu().numpy()
|
| 31 |
+
faces = mesh.faces.detach().cpu().numpy()
|
| 32 |
+
|
| 33 |
+
# Create trimesh object
|
| 34 |
+
mesh_obj = trimesh.Trimesh(vertices=verts, faces=faces)
|
| 35 |
+
|
| 36 |
+
# Export in desired format
|
| 37 |
+
if output_path.endswith('.obj'):
|
| 38 |
+
mesh_obj.export(output_path)
|
| 39 |
+
elif output_path.endswith('.glb'):
|
| 40 |
+
mesh_obj.export(output_path)
|
| 41 |
+
elif output_path.endswith('.stl'):
|
| 42 |
+
mesh_obj.export(output_path)
|
| 43 |
+
|
| 44 |
+
print(f"Successfully exported 3D model to: {output_path}")
|
| 45 |
+
return output_path
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Error during generation: {e}")
|
|
|
|
| 51 |
raise
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
prompt = "a simple ring"
|
| 55 |
try:
|
| 56 |
+
# You can change the extension to .glb or .stl as needed
|
| 57 |
+
generate_3d_model(prompt, "assistant_3d.obj")
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Generation failed: {e}")
|