File size: 1,639 Bytes
024adaf
b9f7861
4e84c36
3c6bbec
 
4e84c36
b9f7861
e0a1d8c
024adaf
e0a1d8c
024adaf
 
e0a1d8c
3c6bbec
 
 
3d550a9
3c6bbec
 
 
 
 
 
 
 
 
 
 
 
 
e0a1d8c
4e84c36
2530e97
024adaf
 
4e84c36
024adaf
3c6bbec
e0a1d8c
4e84c36
e0a1d8c
4e84c36
 
3c6bbec
4e84c36
 
 
 
 
 
 
 
 
3c6bbec
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
import os
import gradio as gr
import torch
import ftfy
import spaces
from diffusers import DiffusionPipeline

# Read token and optional model override from environment
token = os.environ.get("HUGGINGFACE_TOKEN")
if not token:
    raise ValueError("Environment variable HUGGINGFACE_TOKEN is not set.")

# Use the Diffusers-ready model repository by default
model_id = os.environ.get("WAN_MODEL_ID", "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers")

@spaces.GPU  # GPU is only activated when this function is called
def generate_video(image, prompt, num_frames=16, steps=25, guidance_scale=7.5):
    torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

    # Load pipeline inside the GPU-allocated function
    pipe = DiffusionPipeline.from_pretrained(
        model_id,
        torch_dtype=torch_dtype,
        trust_remote_code=True,
        use_auth_token=token
    ).to("cuda")

    pipe.enable_attention_slicing()

    # Generate video
    output = pipe(
        prompt=prompt,
        image=image,
        num_inference_steps=steps,
        guidance_scale=guidance_scale,
        num_frames=num_frames
    )

    return output.videos

# Gradio UI
def main():
    with gr.Blocks() as demo:
        gr.Markdown("# Wan2.1 Image-to-Video Demo (ZeroGPU Edition)")
        with gr.Row():
            img_in = gr.Image(type="pil", label="Input Image")
            txt_p = gr.Textbox(label="Prompt")
        btn = gr.Button("Generate Video")
        out = gr.Video(label="Generated Video")
        btn.click(fn=generate_video, inputs=[img_in, txt_p], outputs=out)
    return demo

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