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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -15,45 +15,58 @@ def generate_3d_model(prompt, output_path="assistant_3d.obj"):
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}")
49
  print(f"Error type: {type(e)}")
50
  print(f"Full error details: {str(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}")
 
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}")
53
 
 
54
  return output_path
55
 
56
  except Exception as e:
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}")