File size: 2,031 Bytes
4b2e644
1e350c6
e7c6544
 
 
 
 
 
 
 
 
 
 
 
 
 
4b2e644
e7c6544
 
 
 
 
4b2e644
 
e7c6544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
import spaces
import torch
import gradio as gr
import time
from diffusers import CogVideoXImageToVideoPipeline
from diffusers.utils import export_to_video, load_image

# Load model once
pipe = CogVideoXImageToVideoPipeline.from_pretrained(
    "THUDM/CogVideoX1.5-5B-I2V",
    torch_dtype=torch.bfloat16
)
pipe.enable_sequential_cpu_offload()
pipe.vae.enable_tiling()
pipe.vae.enable_slicing()

@spaces.GPU(duration=120)
def generate_video(image, prompt):
    if image is None:
        raise gr.Error("Please upload an input image")
    if not prompt:
        raise gr.Error("Please enter a text prompt")

    pipe.to('cuda')
    
    # Load uploaded image
    input_image = load_image(image)
    
    # Generate video
    video_frames = pipe(
        prompt=prompt,
        image=input_image,
        num_videos_per_prompt=1,
        num_inference_steps=50,
        num_frames=81,
        guidance_scale=6,
        generator=torch.Generator(device="cuda").manual_seed(42),
    ).frames[0]
    
    # Save to temporary file
    output_path = f"output_{int(time.time())}.mp4"
    export_to_video(video_frames, output_path, fps=8)
    
    return output_path

with gr.Blocks(title="CogVideoX Image-to-Video") as demo:
    gr.Markdown("# πŸŽ₯ CogVideoX Image-to-Video Generation")
    gr.Markdown("Transform images into videos using AI! Upload an image and enter a description to generate a video.")
    
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(label="Input Image", type="filepath")
            prompt_input = gr.Textbox(label="Prompt", placeholder="Describe the video you want to generate...")
            submit_btn = gr.Button("Generate Video")
        
        with gr.Column():
            video_output = gr.Video(label="Generated Video")
    gr.Examples(examples=examples, inputs=[image_input, prompt_input])
    
    submit_btn.click(
        fn=generate_video,
        inputs=[image_input, prompt_input],
        outputs=video_output,
    )

if __name__ == "__main__":
    demo.launch()