Masrkai commited on
Commit
32d89da
·
verified ·
1 Parent(s): c2a06bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -25
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 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}")
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__":