File size: 1,934 Bytes
683afc3
 
 
 
 
92fa744
 
 
 
 
683afc3
92fa744
 
 
 
683afc3
 
 
 
 
 
 
 
 
92fa744
683afc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
import torch
from diffusers import AnimateDiffPipeline, DDIMScheduler, MotionAdapter
from diffusers.utils import export_to_gif

# Set device to CPU
device = torch.device("cpu")

# Load the motion adapter on CPU
adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-2", torch_dtype=torch.float32).to(device)
model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
pipe = AnimateDiffPipeline.from_pretrained(
    model_id, motion_adapter=adapter, torch_dtype=torch.float32
).to(device)

scheduler = DDIMScheduler.from_pretrained(
    model_id,
    subfolder="scheduler",
    clip_sample=False,
    timestep_spacing="linspace",
    beta_schedule="linear",
    steps_offset=1,
)
pipe.scheduler = scheduler

pipe.enable_vae_slicing()

# Define the animation function
def generate_animation(prompt, negative_prompt, num_frames, guidance_scale, num_inference_steps):
    output = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        num_frames=num_frames,
        guidance_scale=guidance_scale,
        num_inference_steps=num_inference_steps,
        generator=torch.Generator("cpu").manual_seed(42),
    )
    frames = output.frames[0]
    gif_path = "animation.gif"
    export_to_gif(frames, gif_path)
    return gif_path

# Gradio Interface
iface = gr.Interface(
    fn=generate_animation,
    inputs=[
        gr.Textbox(value="masterpiece, best quality, highly detailed...", label="Prompt"),
        gr.Textbox(value="bad quality, worse quality", label="Negative Prompt"),
        gr.Slider(1, 24, value=16, label="Number of Frames"),
        gr.Slider(1.0, 10.0, value=7.5, step=0.1, label="Guidance Scale"),
        gr.Slider(1, 50, value=25, label="Inference Steps"),
    ],
    outputs=gr.Image(label="Generated Animation"),
    title="Animated Stable Diffusion",
    description="Generate animations based on your prompt using Stable Diffusion.",
)

iface.launch()