redkits commited on
Commit
77a6e14
·
verified ·
1 Parent(s): 06440eb

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. handler.py +58 -0
  2. requirements.txt +7 -0
handler.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ import torch
4
+ from diffusers import AutoencoderKLWan, WanPipeline
5
+ from diffusers.utils import export_to_video
6
+
7
+ class EndpointHandler:
8
+ def __init__(self, path=""):
9
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ self.dtype = torch.bfloat16
11
+
12
+ # Load VAE and pipeline from the diffusers-format repo
13
+ vae = AutoencoderKLWan.from_pretrained(
14
+ "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
15
+ subfolder="vae",
16
+ torch_dtype=torch.float32,
17
+ )
18
+ self.pipe = WanPipeline.from_pretrained(
19
+ "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
20
+ vae=vae,
21
+ torch_dtype=self.dtype,
22
+ )
23
+ self.pipe.to(self.device)
24
+
25
+ def __call__(self, data):
26
+ prompt = data.pop("inputs", "")
27
+ params = data.pop("parameters", {})
28
+
29
+ num_frames = params.get("num_frames", 49)
30
+ height = params.get("height", 480)
31
+ width = params.get("width", 832)
32
+ num_inference_steps = params.get("num_inference_steps", 30)
33
+ fps = params.get("fps", 24)
34
+ guidance_scale = params.get("guidance_scale", 5.0)
35
+ negative_prompt = params.get("negative_prompt", "low quality, blurry, distorted")
36
+
37
+ frames = self.pipe(
38
+ prompt=prompt,
39
+ negative_prompt=negative_prompt,
40
+ num_frames=num_frames,
41
+ height=height,
42
+ width=width,
43
+ num_inference_steps=num_inference_steps,
44
+ guidance_scale=guidance_scale,
45
+ ).frames[0]
46
+
47
+ # Export to video in memory
48
+ buf = io.BytesIO()
49
+ export_to_video(frames, buf, fps=fps)
50
+ video_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
51
+
52
+ duration = len(frames) / fps
53
+ return {
54
+ "video": video_b64,
55
+ "format": "mp4",
56
+ "frames": len(frames),
57
+ "duration": duration,
58
+ }
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/diffusers.git
2
+ transformers>=4.51.0
3
+ accelerate>=0.34.0
4
+ torch>=2.1.0
5
+ sentencepiece
6
+ protobuf
7
+ imageio[ffmpeg]