Masrkai commited on
Commit
377ce24
·
verified ·
1 Parent(s): 083ed22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -28
app.py CHANGED
@@ -8,45 +8,37 @@ 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 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 fixed dimensions
19
  outputs = pipe(
20
- prompt,
21
- num_inference_steps=16,
22
- guidance_scale=7.5,
23
- # Using standard dimensions that work with the model
24
- frame_size=32, # Changed from 24 to standard size
25
  )
26
 
27
- # Extract mesh - using the correct accessor
28
- vertices = outputs.vertices[0].detach().cpu().numpy()
29
- faces = outputs.faces[0].detach().cpu().numpy()
30
 
31
- # Create trimesh object with explicit vertex and face arrays
32
- mesh_obj = trimesh.Trimesh(
33
- vertices=vertices,
34
- faces=faces,
35
- process=True # Enable post-processing to fix any mesh issues
36
- )
37
 
38
- # Export with error checking
39
  try:
40
  if output_path.endswith('.obj'):
41
  mesh_obj.export(output_path, include_normals=True)
42
- elif output_path.endswith('.glb'):
43
- mesh_obj.export(output_path)
44
- elif output_path.endswith('.stl'):
45
  mesh_obj.export(output_path)
46
  print(f"Successfully exported 3D model to: {output_path}")
47
  except Exception as export_error:
48
  print(f"Error during export: {export_error}")
49
- # Try alternate export format if primary fails
50
  backup_path = output_path.rsplit('.', 1)[0] + '.ply'
51
  mesh_obj.export(backup_path)
52
  print(f"Exported backup model to: {backup_path}")
@@ -57,16 +49,11 @@ def generate_3d_model(prompt, output_path="assistant_3d.obj"):
57
  print(f"Error during generation: {e}")
58
  print(f"Error type: {type(e)}")
59
  print(f"Full error details: {str(e)}")
60
-
61
- # Additional debug information
62
- if hasattr(outputs, 'shape'):
63
- print(f"Output shape: {outputs.shape}")
64
  raise
65
 
66
  if __name__ == "__main__":
67
- # Using a very specific prompt for better results
68
  prompt = "a simple 3D ring, perfect circle, clean geometry"
69
  try:
70
  generate_3d_model(prompt, "assistant_3d.obj")
71
  except Exception as e:
72
- print(f"Generation failed: {e}")
 
8
  Generate a 3D model using ShapE and export it in a Blender-compatible format
9
  """
10
  try:
11
+ # Load pipeline with memory constraints suitable for CPU-only usage
12
  pipe = ShapEPipeline.from_pretrained(
13
  "openai/shap-e",
14
  torch_dtype=torch.float32,
15
+ low_cpu_mem_usage=True # Minimize CPU memory usage
16
  ).to("cpu")
17
 
18
+ # Generate with reduced inference steps and guidance scale for CPU
19
  outputs = pipe(
20
+ prompt=prompt,
21
+ num_inference_steps=8, # Reduced steps for CPU speed
22
+ guidance_scale=5.0, # Lower guidance scale to save memory
 
 
23
  )
24
 
25
+ # Extract vertices and faces for the 3D mesh generation
26
+ vertices = outputs["vertices"][0].detach().cpu().numpy()
27
+ faces = outputs["faces"][0].detach().cpu().numpy()
28
 
29
+ # Create the trimesh object with error handling
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
  if output_path.endswith('.obj'):
35
  mesh_obj.export(output_path, include_normals=True)
36
+ else:
 
 
37
  mesh_obj.export(output_path)
38
  print(f"Successfully exported 3D model to: {output_path}")
39
  except Exception as export_error:
40
  print(f"Error during export: {export_error}")
41
+ # Use a fallback format in case of export failure
42
  backup_path = output_path.rsplit('.', 1)[0] + '.ply'
43
  mesh_obj.export(backup_path)
44
  print(f"Exported backup model to: {backup_path}")
 
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__":
 
55
  prompt = "a simple 3D ring, perfect circle, clean geometry"
56
  try:
57
  generate_3d_model(prompt, "assistant_3d.obj")
58
  except Exception as e:
59
+ print(f"Generation failed: {e}")