Sushantkas commited on
Commit
1d7f1a4
·
1 Parent(s): 221df0b

Updated code model loading code completed

Browse files
Files changed (2) hide show
  1. app.py +60 -0
  2. requirements.txt +5 -0
app.py CHANGED
@@ -1,4 +1,64 @@
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def greet(name):
4
  return "Hello " + name + "!!"
 
1
+ import spaces
2
  import gradio as gr
3
+ import torch
4
+ import numpy as np
5
+ from diffusers import AutoencoderKLWan, WanImageToVideoPipeline
6
+ from diffusers.utils import export_to_video, load_image
7
+ from transformers import CLIPVisionModel
8
+
9
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+
11
+ ## Loading Encoder
12
+ model_id = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
13
+ print(f"Using video Model: {model_id}")
14
+
15
+
16
+ print("###### Loading image encoder ######")
17
+
18
+ image_encoder = CLIPVisionModel.from_pretrained(
19
+ model_id, subfolder="image_encoder", torch_dtype=torch.float32
20
+ )
21
+
22
+
23
+ print("###### Loading VAE encoder ######")
24
+ vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
25
+ pipe = WanImageToVideoPipeline.from_pretrained(
26
+ model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16
27
+ )
28
+
29
+ try:
30
+ pipe.to(device)
31
+ print(f"Model Loaded in {device}")
32
+ except:
33
+ print(f"Model loading on {device} failed as trying alternate method")
34
+ try:
35
+ pipe.to("cuda")
36
+ print("Model Loaded in cuda")
37
+ except:
38
+ print(f"Model loading on cuda also failed")
39
+
40
+ try:
41
+ pipe.enable_model_cpu_offload()
42
+ print("Model CPU Offload Completed")
43
+ except:
44
+ print("Model CPU Offload failed")
45
+
46
+
47
+ try:
48
+ print("Enabling vae Slicing")
49
+ pipe.enable_vae_slicing()
50
+ print("VAE Slicing Enabled")
51
+ except:
52
+ print("Model VAE Slicing Failed")
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
 
63
  def greet(name):
64
  return "Hello " + name + "!!"
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ diffusers
2
+ transformers
3
+ accelerate
4
+ torch
5
+ torchvision