Masrkai commited on
Commit
072bd04
1 Parent(s): b1a2983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -1,38 +1,48 @@
1
  import torch
2
  from diffusers import ShapEPipeline
3
- from diffusers.utils import export_to_gif
4
- import PIL.Image
5
 
6
- def generate_3d_model(prompt, output_path="assistant_3d.gif"):
7
  """
8
- Generate a 3D model using ShapE optimized for CPU usage
9
  """
10
  try:
11
- # Force CPU and reduced precision
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
- # Minimal generation settings to reduce memory usage
19
  outputs = pipe(
20
  prompt,
21
- num_inference_steps=32, # Reduced from default
22
- frame_size=32, # Smaller frame size
23
- guidance_scale=10.0, # Reduced guidance scale
24
  )
25
 
26
- # Ensure we have PIL images
27
- if not isinstance(outputs.images[0], PIL.Image.Image):
28
- images = [PIL.Image.fromarray(img) for img in outputs.images]
29
- else:
30
- images = outputs.images
31
-
32
- # Save as GIF
33
- gif_path = export_to_gif(images, output_path)
34
- print(f"Successfully created GIF at: {gif_path}")
35
- return gif_path
 
 
 
 
 
 
 
 
 
 
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 = "A gentle AI voice assistant constructed from a circle ring and 3 lines that fly alongside the circle" # Simplified prompt
45
  try:
46
- generate_3d_model(prompt)
 
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}")