Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,46 @@
|
|
1 |
import torch
|
2 |
-
from diffusers import ShapEPipeline
|
3 |
-
import trimesh
|
4 |
import numpy as np
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
try:
|
11 |
-
|
12 |
-
|
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"
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
|
|
2 |
import numpy as np
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
+
import streamlit as st
|
5 |
|
6 |
+
# Load the ShapE pipeline on CPU
|
7 |
+
pipeline = DiffusionPipeline.from_pretrained(
|
8 |
+
"openai/shap-e",
|
9 |
+
torch_dtype=torch.float32,
|
10 |
+
trust_remote_code=True,
|
11 |
+
custom_pipeline="openai/shap-e", # Assuming it works with custom_pipeline param
|
12 |
+
).to("cpu")
|
13 |
+
|
14 |
+
# Define the function to generate and save a 3D model
|
15 |
+
def generate_3d_model(prompt, output_path="/tmp/output.ply"):
|
16 |
+
# Run the pipeline with a text prompt
|
17 |
+
result = pipeline(prompt, None)
|
18 |
+
|
19 |
+
# Try to save the result as a 3D model
|
20 |
try:
|
21 |
+
pipeline.save_ply(result, output_path)
|
22 |
+
print(f"Model saved to {output_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
return output_path
|
|
|
24 |
except Exception as e:
|
25 |
+
print(f"Failed to save model: {e}")
|
26 |
+
return None
|
27 |
|
28 |
+
# Streamlit interface
|
29 |
+
st.title("3D Model Generator")
|
30 |
+
prompt = st.text_input("Enter a prompt to generate a 3D model:", "a cat statue")
|
31 |
+
|
32 |
+
if st.button("Generate Model"):
|
33 |
+
with st.spinner("Generating model..."):
|
34 |
+
model_path = generate_3d_model(prompt)
|
35 |
+
if model_path:
|
36 |
+
st.success("Model generated successfully!")
|
37 |
+
# Display download link
|
38 |
+
with open(model_path, "rb") as file:
|
39 |
+
st.download_button(
|
40 |
+
label="Download 3D Model",
|
41 |
+
data=file,
|
42 |
+
file_name="generated_model.ply",
|
43 |
+
mime="application/octet-stream"
|
44 |
+
)
|
45 |
+
else:
|
46 |
+
st.error("Model generation failed.")
|