File size: 1,254 Bytes
3373062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import gradio as gr
from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import load_image, export_to_video
import torch
import tempfile
import os

# Load the model
pipe = StableVideoDiffusionPipeline.from_pretrained(
    "stabilityai/stable-video-diffusion-img2vid-xt",
    torch_dtype=torch.float16,
    variant="fp16"
)
pipe.enable_model_cpu_offload()  # Optimize for low memory

# Function to generate video
def generate_video(image, num_frames=14, fps=7):
    if image is None:
        return None
    frames = pipe(image, num_frames=num_frames, fps=fps).frames[0]
    with tempfile.TemporaryDirectory() as tmpdirname:
        temp_video_path = os.path.join(tmpdirname, "output.mp4")
        export_to_video(frames, temp_video_path, fps=fps)
        return temp_video_path

# Gradio interface
iface = gr.Interface(
    fn=generate_video,
    inputs=[
        gr.Image(type="pil", label="Upload an Image"),
        gr.Slider(5, 25, value=14, label="Number of Frames"),
        gr.Slider(5, 10, value=7, label="FPS")
    ],
    outputs=gr.Video(label="Generated Video"),
    title="AI Video Generator",
    description="Turn an image into a short video using Stable Video Diffusion!"
)

if __name__ == "__main__":
    iface.launch()