GenVideo / app.py
hanzla's picture
adapters added
ff629ba
raw
history blame
2.2 kB
import gradio as gr
import torch
from diffusers import AnimateDiffPipeline, DDIMScheduler, MotionAdapter
from diffusers.utils import export_to_gif
from diffusers.utils import export_to_video
import uuid
import spaces
device = "cuda"
adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-2", torch_dtype=torch.float16)
model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
pipe = AnimateDiffPipeline.from_pretrained(model_id, motion_adapter=adapter, torch_dtype=torch.float16).to(device)
pipe.load_lora_weights(
"guoyww/animatediff-motion-lora-zoom-out", adapter_name="zoom-out",
)
pipe.load_lora_weights(
"guoyww/animatediff-motion-lora-zoom-out", adapter_name="pan-left",
)
scheduler = DDIMScheduler.from_pretrained(
model_id,
subfolder="scheduler",
clip_sample=False,
timestep_spacing="linspace",
beta_schedule="linear",
steps_offset=1,
)
pipe.scheduler = scheduler
@spaces.GPU
def generate_video(prompt, guidance_scale, num_inference_steps, num_frames, adapter_choice):
pipe.to(device)
# Set adapters based on user selection
if adapter_choice:
pipe.set_adapters([adapter_choice], adapter_weights=[1.0])
output = pipe(
prompt=prompt,
negative_prompt="bad quality, worse quality",
num_frames=num_frames,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
)
name = str(uuid.uuid4()).replace("-", "")
path = f"/tmp/{name}.mp4"
export_to_video(output.frames[0], path, fps=10)
return path
# Available adapters (replace with your actual adapter names)
adapter_options = ["zoom-out", "pan-left"]
iface = gr.Interface(
fn=generate_video,
inputs=[
gr.Textbox(label="Enter your prompt"),
gr.Slider(minimum=0.5, maximum=10, value=7.5, label="Guidance Scale"),
gr.Slider(minimum=4, maximum=24, step=4, value=4, label="Inference Steps"),
gr.Slider(minimum=16, maximum=64, step=1, value=16, label="Frames"),
gr.Dropdown(choices=adapter_options, label="Select Adapter") # Add dropdown for adapter selection
],
outputs=gr.Video(label="Generated Video"),
)
iface.launch()