Update model.py
Browse files
model.py
CHANGED
@@ -1,39 +1,50 @@
|
|
1 |
-
import
|
2 |
-
from diffusers import DiffusionPipeline
|
3 |
-
import trimesh
|
4 |
-
import numpy as np
|
5 |
from PIL import Image
|
6 |
-
from
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
Load the stable-zero123 model pipeline from Hugging Face.
|
11 |
-
"""
|
12 |
-
ckpt_id = "stabilityai/stable-zero123"
|
13 |
-
pipe = DiffusionPipeline.from_pretrained(ckpt_id, torch_dtype=torch.float32).to("cpu")
|
14 |
-
return pipe
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
# Generate the model output
|
21 |
-
outputs = pipe(prompt=prompt, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps)
|
22 |
-
|
23 |
-
# Extract mesh data if the output structure allows
|
24 |
-
vertices = outputs["vertices"][0].detach().cpu().numpy()
|
25 |
-
faces = outputs["faces"][0].detach().cpu().numpy()
|
26 |
-
|
27 |
-
# Create and save the mesh using trimesh
|
28 |
-
mesh = trimesh.Trimesh(vertices=vertices, faces=faces, process=True)
|
29 |
-
mesh.export(output_path)
|
30 |
-
return output_path
|
31 |
|
32 |
-
|
33 |
-
""
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
|
|
|
|
|
|
2 |
from PIL import Image
|
3 |
+
from model import load_pipeline, generate_3d_model, convert_to_gif
|
4 |
+
import os
|
5 |
|
6 |
+
# Load pipeline
|
7 |
+
pipe = load_pipeline()
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
st.title("3D Model Generator with GIF Export")
|
10 |
+
prompt = st.text_input("Enter a prompt for the 3D model:", "a shark")
|
11 |
+
output_obj_path = "generated_3d_model.obj"
|
12 |
+
output_gif_path = "generated_3d_model.gif"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
if st.button("Generate 3D Model and Convert to GIF"):
|
15 |
+
with st.spinner("Generating 3D model..."):
|
16 |
+
obj_path = generate_3d_model(pipe, prompt, output_path=output_obj_path)
|
17 |
+
|
18 |
+
# Load the 3D model in trimesh for rendering as images
|
19 |
+
mesh = trimesh.load(obj_path)
|
20 |
+
images = []
|
21 |
+
|
22 |
+
# Rotate and render views for GIF
|
23 |
+
for angle in range(0, 360, 30): # Capture images every 30 degrees
|
24 |
+
scene = mesh.scene()
|
25 |
+
scene.set_camera(angles=(angle, angle))
|
26 |
+
data = scene.save_image(resolution=(256, 256))
|
27 |
+
images.append(Image.open(BytesIO(data)))
|
28 |
+
|
29 |
+
# Convert to GIF
|
30 |
+
gif_path = convert_to_gif(images, gif_path=output_gif_path)
|
31 |
+
|
32 |
+
# Display GIF
|
33 |
+
st.image(gif_path, caption="Generated 3D Model GIF")
|
34 |
+
|
35 |
+
# Provide download options
|
36 |
+
with open(gif_path, "rb") as file:
|
37 |
+
st.download_button(
|
38 |
+
label="Download GIF",
|
39 |
+
data=file,
|
40 |
+
file_name="generated_3d_model.gif",
|
41 |
+
mime="image/gif"
|
42 |
+
)
|
43 |
+
|
44 |
+
with open(obj_path, "rb") as file:
|
45 |
+
st.download_button(
|
46 |
+
label="Download 3D Model (.obj)",
|
47 |
+
data=file,
|
48 |
+
file_name="generated_3d_model.obj",
|
49 |
+
mime="application/octet-stream"
|
50 |
+
)
|