dannyboy84 commited on
Commit
3373062
·
verified ·
1 Parent(s): 7cd26b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import StableVideoDiffusionPipeline
3
+ from diffusers.utils import load_image, export_to_video
4
+ import torch
5
+ import tempfile
6
+ import os
7
+
8
+ # Load the model
9
+ pipe = StableVideoDiffusionPipeline.from_pretrained(
10
+ "stabilityai/stable-video-diffusion-img2vid-xt",
11
+ torch_dtype=torch.float16,
12
+ variant="fp16"
13
+ )
14
+ pipe.enable_model_cpu_offload() # Optimize for low memory
15
+
16
+ # Function to generate video
17
+ def generate_video(image, num_frames=14, fps=7):
18
+ if image is None:
19
+ return None
20
+ frames = pipe(image, num_frames=num_frames, fps=fps).frames[0]
21
+ with tempfile.TemporaryDirectory() as tmpdirname:
22
+ temp_video_path = os.path.join(tmpdirname, "output.mp4")
23
+ export_to_video(frames, temp_video_path, fps=fps)
24
+ return temp_video_path
25
+
26
+ # Gradio interface
27
+ iface = gr.Interface(
28
+ fn=generate_video,
29
+ inputs=[
30
+ gr.Image(type="pil", label="Upload an Image"),
31
+ gr.Slider(5, 25, value=14, label="Number of Frames"),
32
+ gr.Slider(5, 10, value=7, label="FPS")
33
+ ],
34
+ outputs=gr.Video(label="Generated Video"),
35
+ title="AI Video Generator",
36
+ description="Turn an image into a short video using Stable Video Diffusion!"
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ iface.launch()