Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import AutoencoderKLWan, WanPipeline
|
4 |
+
from diffusers.utils import export_to_video
|
5 |
+
import spaces # ZeroGPU integration
|
6 |
+
|
7 |
+
@spaces.GPU # This decorator requests a GPU when the function is called (ZeroGPU config)
|
8 |
+
def generate_video(prompt, negative_prompt=""):
|
9 |
+
model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
10 |
+
|
11 |
+
# Load the VAE and pipeline with proper data types per model requirements
|
12 |
+
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
13 |
+
pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
14 |
+
pipe.to("cuda")
|
15 |
+
|
16 |
+
# Generate video frames at 480p resolution (480x832) with the desired settings
|
17 |
+
output = pipe(
|
18 |
+
prompt=prompt,
|
19 |
+
negative_prompt=negative_prompt,
|
20 |
+
height=480, # 480p height
|
21 |
+
width=832, # width set to match 480p (adjust as needed)
|
22 |
+
num_frames=81, # Number of frames (adjust for desired video length)
|
23 |
+
guidance_scale=5.0 # Recommended for the 1.3B model
|
24 |
+
).frames[0]
|
25 |
+
|
26 |
+
# Save the generated frames as a video file
|
27 |
+
video_path = "output.mp4"
|
28 |
+
export_to_video(output, video_path, fps=15)
|
29 |
+
return video_path
|
30 |
+
|
31 |
+
# Create a Gradio interface for the video generation
|
32 |
+
iface = gr.Interface(
|
33 |
+
fn=generate_video,
|
34 |
+
inputs=[
|
35 |
+
gr.Textbox(label="Prompt", placeholder="Enter your video prompt here"),
|
36 |
+
gr.Textbox(label="Negative Prompt", placeholder="Optional negative prompt", value="")
|
37 |
+
],
|
38 |
+
outputs=gr.Video(label="Generated Video"),
|
39 |
+
title="Wan2.1-T2V-1.3B Video Generator",
|
40 |
+
description="Generate 480p videos using the Wan2.1-T2V-1.3B diffusers pipeline with ZeroGPU support."
|
41 |
+
)
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
iface.launch()
|