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 # Available adapters (replace with your actual adapter names) adapter_options = { "zoom-out":"guoyww/animatediff-motion-lora-zoom-out", "zoom-in":"guoyww/animatediff-motion-lora-zoom-in", "pan-left":"guoyww/animatediff-motion-lora-pan-left", "pan-right":"guoyww/animatediff-motion-lora-pan-right", "roll-clockwise":"guoyww/animatediff-motion-lora-rolling-clockwise", "roll-anticlockwise":"guoyww/animatediff-motion-lora-rolling-anticlockwise", "tilt-up":"guoyww/animatediff-motion-lora-tilt-up", "tilt-down":"guoyww/animatediff-motion-lora-tilt-down" } 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, adapter_choices): pipe.to(device) # Set adapters based on user selection if adapter_choices: for i in range(len(adapter_choices)): adapter_name = adapter_choices[i] pipe.load_lora_weights( adapter_options[adapter_name], adapter_name=adapter_name, ) pipe.set_adapters(adapter_choices, adapter_weights=[1.0] * len(adapter_choices)) print(adapter_choices) output = pipe( prompt=prompt, negative_prompt="bad quality, worse quality", num_frames=16, 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 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.CheckboxGroup(adapter_options.keys(), label="Adapter Choice",type='value'), # Updated for multiple selections ], outputs=gr.Video(label="Generated Video"), ) iface.launch()