SeedOfEvil commited on
Commit
50d19ca
·
verified ·
1 Parent(s): 044a0f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -4,31 +4,35 @@ from diffusers import AutoencoderKLWan, WanPipeline
4
  from diffusers.utils import export_to_video
5
  import spaces # ZeroGPU integration
6
 
7
- @spaces.GPU # Request a GPU via ZeroGPU
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
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 desired settings
17
- output = pipe(
 
 
 
 
 
 
18
  prompt=prompt,
19
  negative_prompt=negative_prompt,
20
  height=480, # 480p height
21
- width=832, # Suitable width for 480p
22
- num_frames=81, # Adjust as needed for video length
23
- guidance_scale=5.0 # Recommended for T2V-1.3B model
24
  ).frames[0]
25
 
26
- # Export the generated frames to 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 video generation
32
  iface = gr.Interface(
33
  fn=generate_video,
34
  inputs=[
@@ -41,4 +45,4 @@ iface = gr.Interface(
41
  )
42
 
43
  if __name__ == "__main__":
44
- iface.launch()
 
4
  from diffusers.utils import export_to_video
5
  import spaces # ZeroGPU integration
6
 
7
+ @spaces.GPU # This decorator will request a GPU during initialization
8
+ def load_pipeline():
9
  model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
10
+ print("Loading model. This may take several minutes...")
 
11
  vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
12
  pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
13
  pipe.to("cuda")
14
+ print("Model loaded successfully.")
15
+ return pipe
16
+
17
+ # Preload the model during startup
18
+ PIPELINE = load_pipeline()
19
+
20
+ def generate_video(prompt, negative_prompt=""):
21
+ # Use the globally preloaded PIPELINE
22
+ output = PIPELINE(
23
  prompt=prompt,
24
  negative_prompt=negative_prompt,
25
  height=480, # 480p height
26
+ width=832, # Suitable width for 480p videos
27
+ num_frames=81, # Adjust number of frames for desired video length
28
+ guidance_scale=5.0 # Recommended guidance scale for the 1.3B model
29
  ).frames[0]
30
 
 
31
  video_path = "output.mp4"
32
  export_to_video(output, video_path, fps=15)
33
  return video_path
34
 
35
+ # Create the Gradio interface
36
  iface = gr.Interface(
37
  fn=generate_video,
38
  inputs=[
 
45
  )
46
 
47
  if __name__ == "__main__":
48
+ iface.launch()