File size: 1,474 Bytes
bb5bd61
e7c0b45
 
 
 
 
 
 
 
 
bb5bd61
e7c0b45
 
 
 
 
 
 
 
 
 
 
bb5bd61
 
 
e7c0b45
 
 
 
 
bb5bd61
e7c0b45
 
 
 
 
 
 
 
bb5bd61
e7c0b45
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import torch
import gradio as gr
from diffusers import AutoencoderKLWan, WanPipeline
from diffusers.utils import export_to_video

# ---- MODEL SETUP ----

# Model ID on Hugging Face
MODEL_ID = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"

# Load the pipeline and VAE
def load_model():
    print("Loading model... (this may take a while)")
    vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
    pipe = WanPipeline.from_pretrained(MODEL_ID, vae=vae, torch_dtype=torch.float16)
    pipe = pipe.to("cuda")  # Ensure using GPU
    return pipe

video_pipe = load_model()

# ---- GENERATION FUNCTION ----

def generate_video(prompt):
    try:
        # Text to video generation
        result = video_pipe(
            prompt=prompt,
            num_frames=24,
            guidance_scale=7.5
        )

        # Convert list of PIL frames to MP4
        frames = result.frames
        output_path = "generated_video.mp4"
        export_to_video(frames, output_path, fps=8)

        return output_path

    except Exception as e:
        return f"Error: {str(e)}"

# ---- GRADIO UI ----

with gr.Blocks() as app:
    gr.Markdown("# 🧠 Text‑to‑Video with Wan2.1‑T2V")
    prompt = gr.Textbox(label="Enter your video prompt", placeholder="e.g. A dragon flying over mountains")
    btn = gr.Button("Generate Video")
    video_output = gr.Video()

    btn.click(fn=generate_video, inputs=prompt, outputs=video_output)

app.launch()