|
import spaces |
|
import torch |
|
from diffusers import CogVideoXImageToVideoPipeline |
|
from diffusers.utils import export_to_video, load_image |
|
import gradio as gr |
|
|
|
pipe = CogVideoXImageToVideoPipeline.from_pretrained( |
|
"THUDM/CogVideoX-5b-I2V", |
|
torch_dtype=torch.bfloat16 |
|
) |
|
|
|
def generate_video(prompt, image): |
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
pipe.to("cpu") |
|
generator = torch.Generator(device=device).manual_seed(42) |
|
|
|
video = pipe( |
|
prompt=prompt, |
|
image=image, |
|
num_videos_per_prompt=1, |
|
num_inference_steps=15, |
|
num_frames=49, |
|
guidance_scale=6, |
|
generator=generator, |
|
).frames[0] |
|
|
|
video_path = "output.mp4" |
|
export_to_video(video, video_path, fps=8) |
|
|
|
return video_path |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Image to Video Generation") |
|
|
|
with gr.Row(): |
|
|
|
prompt_input = gr.Textbox(label="Prompt", value="A little girl is riding a bicycle at high speed. Focused, detailed, realistic.") |
|
|
|
|
|
image_input = gr.Image(label="Upload an Image", type="pil") |
|
|
|
|
|
generate_button = gr.Button("Generate Video") |
|
|
|
|
|
video_output = gr.Video(label="Generated Video") |
|
|
|
|
|
generate_button.click(fn=generate_video, inputs=[prompt_input, image_input], outputs=video_output) |
|
|
|
|
|
demo.launch() |
|
|