File size: 1,164 Bytes
710d036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from diffusers import AnimateDiffPipeline, DDIMScheduler
from PIL import Image
import tempfile
import imageio

# Use a public AnimateDiff-compatible model (Realistic Vision base)
model_id = "SG161222/Realistic_Vision_V6.0_B1_noVAE"
pipe = AnimateDiffPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")

def generate_video(image, prompt):
    image = image.convert("RGB").resize((512, 512))
    result = pipe(prompt=prompt, image=image, num_frames=16, guidance_scale=7.5)
    frames = result.frames

    video_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
    imageio.mimsave(video_path, frames, fps=8)
    return video_path

gr.Interface(
    fn=generate_video,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Textbox(label="Describe Motion (Prompt)")
    ],
    outputs=gr.Video(label="Generated Video"),
    title="πŸŒ€ Image + Prompt to Animation",
    description="Upload a still image and describe how you want it animated!"
).launch()