Spaces:
Paused
Paused
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() | |