Spaces:
Runtime error

File size: 1,673 Bytes
61db242
 
 
b16e5f9
61db242
b16e5f9
 
 
 
 
 
 
eb13683
b16e5f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61db242
 
 
b16e5f9
 
 
 
 
 
 
 
 
61db242
 
b16e5f9
 
 
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
68
69
70
71
72
73
74
75
import gradio as gr
import spaces
import torch
import tempfile

from diffusers import AutoencoderKLWan, WanTransformer3DModel, WanPipeline
from diffusers.utils import export_to_video


MODEL_ID = "Wan-AI/Wan2.2-TI2V-5B"


@spaces.GPU(duration=60)
def generate_video(prompt):
    if not prompt or not prompt.strip():
        raise gr.Error("Please enter a prompt.")

    if not torch.cuda.is_available():
        raise gr.Error("CUDA GPU is not available.")

    try:
        pipe = WanPipeline.from_pretrained(
            MODEL_ID,
            torch_dtype=torch.bfloat16
        )

        pipe.enable_model_cpu_offload()

        result = pipe(
            prompt=prompt,
            height=480,
            width=832,
            num_frames=49,
            num_inference_steps=20,
            guidance_scale=5.0
        )

        frames = result.frames[0]

        output_file = tempfile.NamedTemporaryFile(
            suffix=".mp4",
            delete=False
        ).name

        export_to_video(
            frames,
            output_file,
            fps=16
        )

        del pipe
        torch.cuda.empty_cache()

        return output_file

    except Exception as e:
        torch.cuda.empty_cache()
        raise gr.Error(f"Generation failed: {str(e)}")


demo = gr.Interface(
    fn=generate_video,
    inputs=gr.Textbox(
        label="Prompt",
        lines=5,
        placeholder="Describe the video you want to generate..."
    ),
    outputs=gr.Video(label="Generated Video"),
    title="Wan 2.2 Video Generator",
    description="Experimental Wan2.2-TI2V-5B generation using Hugging Face ZeroGPU."
)


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