Update app.py
Browse files
app.py
CHANGED
|
@@ -8,47 +8,45 @@ def generate_3d_model(prompt, output_path="assistant_3d.obj"):
|
|
| 8 |
Generate a 3D model using ShapE and export it in a Blender-compatible format
|
| 9 |
"""
|
| 10 |
try:
|
| 11 |
-
# Load pipeline
|
| 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
|
| 19 |
outputs = pipe(
|
| 20 |
prompt=prompt,
|
| 21 |
-
num_inference_steps=8, # Reduced
|
| 22 |
-
guidance_scale=5.0
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
faces = outputs["faces"][0].detach().cpu().numpy()
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
mesh_obj = trimesh.Trimesh(vertices=vertices, faces=faces, process=True)
|
| 31 |
-
|
| 32 |
-
# Export model in desired format with robust handling for compatibility
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
print(f"
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
mesh_obj.export(backup_path)
|
| 44 |
-
print(f"Exported backup model to: {backup_path}")
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return output_path
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Error during generation: {e}")
|
| 50 |
-
print(f"Error type: {type(e)}")
|
| 51 |
-
print(f"Full error details: {str(e)}")
|
| 52 |
raise
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
|
|
|
| 8 |
Generate a 3D model using ShapE and export it in a Blender-compatible format
|
| 9 |
"""
|
| 10 |
try:
|
| 11 |
+
# Load pipeline optimized for CPU usage
|
| 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 model
|
| 19 |
outputs = pipe(
|
| 20 |
prompt=prompt,
|
| 21 |
+
num_inference_steps=8, # Reduced for CPU efficiency
|
| 22 |
+
guidance_scale=5.0 # Lower guidance to conserve resources
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Check output structure for debugging
|
| 26 |
+
print("Output structure:", outputs.keys())
|
|
|
|
| 27 |
|
| 28 |
+
# Attempt to extract vertices and faces
|
|
|
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
+
vertices = outputs["vertices"][0].detach().cpu().numpy()
|
| 31 |
+
faces = outputs["faces"][0].detach().cpu().numpy()
|
| 32 |
+
except KeyError as ke:
|
| 33 |
+
print(f"Key error: {ke}")
|
| 34 |
+
print(f"Available keys in output: {list(outputs.keys())}")
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# Construct the 3D mesh object
|
| 38 |
+
mesh_obj = trimesh.Trimesh(vertices=vertices, faces=faces, process=True)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Export mesh object
|
| 41 |
+
if output_path.endswith('.obj'):
|
| 42 |
+
mesh_obj.export(output_path, include_normals=True)
|
| 43 |
+
else:
|
| 44 |
+
mesh_obj.export(output_path)
|
| 45 |
+
print(f"Successfully exported 3D model to: {output_path}")
|
| 46 |
return output_path
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Error during generation: {e}")
|
|
|
|
|
|
|
| 50 |
raise
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|