Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
|
4 |
-
from diffusers import AutoencoderKLWan, WanPipeline
|
5 |
-
from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
|
6 |
-
import os
|
7 |
from uuid import uuid4
|
|
|
|
|
|
|
8 |
|
9 |
-
# Check for available device (CUDA or CPU)
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
print(f"Running on {device}...")
|
12 |
|
13 |
-
# Load
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
pipe.scheduler = scheduler
|
26 |
-
pipe.to(device) # Move model to GPU or CPU based on availability
|
27 |
-
print("Model loaded successfully!")
|
28 |
-
except Exception as e:
|
29 |
-
print(f"Error loading model: {e}")
|
30 |
-
device = "cpu" # Fallback to CPU if model loading fails on GPU
|
31 |
-
pipe.to(device)
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
try:
|
36 |
print(f"Generating video with prompt: {prompt}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
output = pipe(
|
38 |
prompt=prompt,
|
39 |
negative_prompt=negative_prompt,
|
@@ -43,32 +49,27 @@ def generate_video(prompt, negative_prompt="", height=720, width=1280, num_frame
|
|
43 |
guidance_scale=guidance_scale,
|
44 |
).frames[0]
|
45 |
|
46 |
-
|
47 |
-
output_path = os.path.join("outputs", output_filename)
|
48 |
-
os.makedirs("outputs", exist_ok=True)
|
49 |
export_to_video(output, output_path, fps=16)
|
50 |
|
51 |
-
print(f"Video generated
|
52 |
-
return output_path
|
|
|
53 |
except Exception as e:
|
54 |
print(f"Error during video generation: {e}")
|
55 |
return None
|
56 |
|
57 |
-
# Gradio Interface
|
58 |
iface = gr.Interface(
|
59 |
fn=generate_video,
|
60 |
inputs=[
|
61 |
-
gr.Textbox(label="Prompt"),
|
62 |
gr.Textbox(label="Negative Prompt", value=""),
|
63 |
-
gr.Number(label="Height", value=
|
64 |
-
gr.Number(label="Width", value=
|
65 |
gr.Number(label="Number of Frames", value=81),
|
66 |
-
gr.Number(label="Guidance Scale", value=5.0)
|
67 |
],
|
68 |
outputs=gr.File(label="Generated Video"),
|
69 |
-
title="Wan2.1 Video Generator",
|
70 |
-
description="Generate realistic videos from text prompts using the Wan2.1 T2V model.",
|
71 |
-
live=True
|
72 |
)
|
73 |
|
74 |
# Launch Gradio app in API mode
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
import ftfy
|
|
|
|
|
|
|
4 |
from uuid import uuid4
|
5 |
+
from diffusers import WanPipeline, AutoencoderKLWan
|
6 |
+
from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
|
7 |
+
from diffusers.utils import export_to_video
|
8 |
|
|
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
print(f"Running on {device}...")
|
11 |
|
12 |
+
# Load model
|
13 |
+
model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
14 |
+
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
15 |
+
scheduler = UniPCMultistepScheduler(
|
16 |
+
prediction_type='flow_prediction',
|
17 |
+
use_flow_sigmas=True,
|
18 |
+
num_train_timesteps=1000,
|
19 |
+
flow_shift=5.0
|
20 |
+
)
|
21 |
+
pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
22 |
+
pipe.scheduler = scheduler
|
23 |
+
pipe.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
print("Model loaded successfully.")
|
26 |
+
|
27 |
+
def make_divisible_by_16(x):
|
28 |
+
return int(x) - int(x) % 16
|
29 |
+
|
30 |
+
def generate_video(prompt, negative_prompt="", height=480, width=832, num_frames=81, guidance_scale=5.0):
|
31 |
try:
|
32 |
print(f"Generating video with prompt: {prompt}")
|
33 |
+
|
34 |
+
if not prompt:
|
35 |
+
raise ValueError("Prompt must be provided.")
|
36 |
+
|
37 |
+
# Validate and adjust height/width
|
38 |
+
height = make_divisible_by_16(int(height))
|
39 |
+
width = make_divisible_by_16(int(width))
|
40 |
+
num_frames = int(num_frames)
|
41 |
+
guidance_scale = float(guidance_scale)
|
42 |
+
|
43 |
output = pipe(
|
44 |
prompt=prompt,
|
45 |
negative_prompt=negative_prompt,
|
|
|
49 |
guidance_scale=guidance_scale,
|
50 |
).frames[0]
|
51 |
|
52 |
+
output_path = f"{uuid4()}.mp4"
|
|
|
|
|
53 |
export_to_video(output, output_path, fps=16)
|
54 |
|
55 |
+
print(f"Video generated: {output_path}")
|
56 |
+
return output_path
|
57 |
+
|
58 |
except Exception as e:
|
59 |
print(f"Error during video generation: {e}")
|
60 |
return None
|
61 |
|
|
|
62 |
iface = gr.Interface(
|
63 |
fn=generate_video,
|
64 |
inputs=[
|
65 |
+
gr.Textbox(label="Prompt", placeholder="Describe your scene..."),
|
66 |
gr.Textbox(label="Negative Prompt", value=""),
|
67 |
+
gr.Number(label="Height", value=480),
|
68 |
+
gr.Number(label="Width", value=832),
|
69 |
gr.Number(label="Number of Frames", value=81),
|
70 |
+
gr.Number(label="Guidance Scale", value=5.0),
|
71 |
],
|
72 |
outputs=gr.File(label="Generated Video"),
|
|
|
|
|
|
|
73 |
)
|
74 |
|
75 |
# Launch Gradio app in API mode
|