XaronXr commited on
Commit
1025344
·
1 Parent(s): b1f3a03

add handler

Browse files
Files changed (1) hide show
  1. handler.py +80 -0
handler.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile
2
+
3
+ import numpy as np
4
+ import PIL.Image
5
+ import torch
6
+ import trimesh
7
+ from diffusers import ShapEImg2ImgPipeline, ShapEPipeline
8
+ from diffusers.utils import export_to_ply, export_to_gif
9
+
10
+ from typing import Dict, List, Any
11
+
12
+ class EndpointHandler():
13
+ def __init__(self):
14
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+ self.pipe = ShapEPipeline.from_pretrained("openai/shap-e", torch_dtype=torch.float16)
16
+ self.pipe.to(self.device)
17
+
18
+ self.pipe_img = ShapEImg2ImgPipeline.from_pretrained("openai/shap-e-img2img", torch_dtype=torch.float16)
19
+ self.pipe_img.to(self.device)
20
+
21
+ def to_glb(self, ply_path: str) -> str:
22
+ mesh = trimesh.load(ply_path)
23
+ rot = trimesh.transformations.rotation_matrix(-np.pi / 2, [1, 0, 0])
24
+ mesh = mesh.apply_transform(rot)
25
+ rot = trimesh.transformations.rotation_matrix(np.pi, [0, 1, 0])
26
+ mesh = mesh.apply_transform(rot)
27
+ mesh_path = tempfile.NamedTemporaryFile(suffix=".glb", delete=False)
28
+ mesh.export(mesh_path.name, file_type="glb")
29
+ return mesh_path.name
30
+
31
+ def run_text(self, prompt: str, seed: int = 0, guidance_scale: float = 15.0, num_steps: int = 64, output_type: str = "pil") -> str:
32
+ generator = torch.Generator(device=self.device).manual_seed(seed)
33
+
34
+ if output_type=="pil":
35
+ images = self.pipe(
36
+ prompt,
37
+ num_images_per_prompt=4,
38
+ generator=generator,
39
+ guidance_scale=guidance_scale,
40
+ num_inference_steps=num_steps,
41
+ frame_size=64,
42
+ ).images
43
+ gif_path = tempfile.NamedTemporaryFile(suffix=".gif", delete=False, mode="w+b")
44
+ gif_path2 = tempfile.NamedTemporaryFile(suffix=".gif", delete=False, mode="w+b")
45
+ gif_path3 = tempfile.NamedTemporaryFile(suffix=".gif", delete=False, mode="w+b")
46
+ gif_path4 = tempfile.NamedTemporaryFile(suffix=".gif", delete=False, mode="w+b")
47
+ export_to_gif(images[0], gif_path.name)
48
+ export_to_gif(images[1], gif_path2.name)
49
+ export_to_gif(images[2], gif_path3.name)
50
+ export_to_gif(images[3], gif_path4.name)
51
+ return gif_path.name, gif_path2.name, gif_path3.name, gif_path4.name
52
+ else:
53
+ images = self.pipe(
54
+ prompt,
55
+ num_images_per_prompt=1,
56
+ generator=generator,
57
+ guidance_scale=guidance_scale,
58
+ num_inference_steps=num_steps,
59
+ frame_size=64,
60
+ output_type=output_type
61
+ ).images
62
+ ply_path = tempfile.NamedTemporaryFile(suffix=".ply", delete=False, mode="w+b")
63
+ export_to_ply(images[0], ply_path.name)
64
+ return self.to_glb(ply_path.name)
65
+
66
+ def __call__(self, prompt: str, seed: int = 0, guidance_scale: float = 15.0, num_steps: int = 64) -> str:
67
+ generator = torch.Generator(device=self.device).manual_seed(seed)
68
+
69
+ images = self.pipe(
70
+ prompt,
71
+ num_images_per_prompt=1,
72
+ generator=generator,
73
+ guidance_scale=guidance_scale,
74
+ num_inference_steps=num_steps,
75
+ frame_size=64,
76
+ output_type="mesh"
77
+ ).images
78
+ ply_path = tempfile.NamedTemporaryFile(suffix=".ply", delete=False, mode="w+b")
79
+ export_to_ply(images[0], ply_path.name)
80
+ return self.to_glb(ply_path.name)