Masrkai commited on
Commit
0d36818
·
verified ·
1 Parent(s): 6a8a2e6

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +35 -46
model.py CHANGED
@@ -1,50 +1,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
- )
 
1
+ import torch
2
+ from diffusers import DiffusionPipeline
3
+ import trimesh
4
+ import numpy as np
5
  from PIL import Image
6
+ from io import BytesIO
 
7
 
8
+ def load_pipeline():
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
+ def generate_3d_model(pipe, prompt, output_path="output.obj", guidance_scale=7.5, num_inference_steps=32):
17
+ """
18
+ Generate a 3D model from the prompt and save it in a Blender-compatible format (.obj).
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
+ def convert_to_gif(images, gif_path="output.gif"):
33
+ """
34
+ Convert a list of images into a GIF.
35
+ """
36
+ images[0].save(
37
+ gif_path, save_all=True, append_images=images[1:], loop=0, duration=100
38
+ )
39
+ return gif_path