Update app.py
Browse files
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
|
| 19 |
outputs = pipe(
|
| 20 |
prompt,
|
| 21 |
num_inference_steps=16,
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
# Export
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 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}")
|