Update app.py
Browse files
app.py
CHANGED
@@ -4,30 +4,36 @@ from diffusers import AutoencoderKLWan, WanPipeline
|
|
4 |
from diffusers.utils import export_to_video
|
5 |
import spaces # ZeroGPU integration
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
if PIPELINE is None:
|
14 |
-
model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
15 |
-
print("Loading model in worker process (moving to GPU)...")
|
16 |
-
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
17 |
-
PIPELINE = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
18 |
-
PIPELINE.to("cuda")
|
19 |
-
print("Model loaded on GPU.")
|
20 |
-
return PIPELINE
|
21 |
|
22 |
-
@spaces.GPU
|
23 |
def generate_video(prompt, negative_prompt=""):
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
prompt=prompt,
|
27 |
negative_prompt=negative_prompt,
|
28 |
height=480, # 480p height
|
29 |
width=832, # Suitable width for 480p videos
|
30 |
-
num_frames=81, # Adjust
|
31 |
guidance_scale=5.0 # Recommended guidance scale for the 1.3B model
|
32 |
).frames[0]
|
33 |
|
|
|
4 |
from diffusers.utils import export_to_video
|
5 |
import spaces # ZeroGPU integration
|
6 |
|
7 |
+
def load_pipeline_on_cpu():
|
8 |
+
model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
9 |
+
print("Preloading model on CPU...")
|
10 |
+
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
11 |
+
pipeline_cpu = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
12 |
+
# Remain on CPU for now
|
13 |
+
print("Model preloaded on CPU.")
|
14 |
+
return pipeline_cpu
|
15 |
|
16 |
+
# Load the model on CPU during container initialization.
|
17 |
+
PIPELINE_CPU = load_pipeline_on_cpu()
|
18 |
+
PIPELINE_GPU = None # Will hold the GPU-loaded pipeline after the first request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
@spaces.GPU # This ensures GPU is only initialized in the request worker process
|
21 |
def generate_video(prompt, negative_prompt=""):
|
22 |
+
global PIPELINE_GPU
|
23 |
+
# Move to GPU on first request if not already done.
|
24 |
+
if PIPELINE_GPU is None:
|
25 |
+
print("Moving model to GPU...")
|
26 |
+
PIPELINE_GPU = PIPELINE_CPU.to("cuda")
|
27 |
+
print("Model moved to GPU.")
|
28 |
+
pipeline_gpu = PIPELINE_GPU
|
29 |
+
|
30 |
+
# Generate video frames at 480p resolution
|
31 |
+
output = pipeline_gpu(
|
32 |
prompt=prompt,
|
33 |
negative_prompt=negative_prompt,
|
34 |
height=480, # 480p height
|
35 |
width=832, # Suitable width for 480p videos
|
36 |
+
num_frames=81, # Adjust for desired video length
|
37 |
guidance_scale=5.0 # Recommended guidance scale for the 1.3B model
|
38 |
).frames[0]
|
39 |
|