Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,123 +1,154 @@
|
|
1 |
import torch
|
2 |
-
from diffusers import UniPCMultistepScheduler
|
3 |
-
from diffusers import WanPipeline, AutoencoderKLWan
|
|
|
4 |
from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
|
|
|
|
|
5 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
6 |
from PIL import Image
|
7 |
import numpy as np
|
|
|
|
|
|
|
8 |
import gradio as gr
|
9 |
import spaces
|
10 |
-
import gc
|
11 |
|
12 |
-
|
|
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
-
|
|
|
15 |
|
16 |
model_id = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
|
17 |
-
print("Loading VAE...")
|
18 |
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
19 |
-
|
20 |
-
print("Loading WanPipeline in bfloat16...")
|
21 |
-
# This will use ZeroGPU/accelerate with meta devices
|
22 |
pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
23 |
-
|
24 |
-
flow_shift = 1.0
|
25 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift)
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
print("Moving pipeline to device (ZeroGPU will handle offloading)...")
|
30 |
pipe.to(device)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
DEFAULT_LORA_NAME = "causvid_lora"
|
36 |
CUSTOM_LORA_NAME = "custom_lora"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
print("Initialization complete. Gradio is starting...")
|
39 |
|
40 |
-
# The decorated function that will run on the GPU. It only does inference.
|
41 |
@spaces.GPU()
|
42 |
-
def generate(prompt, negative_prompt, width, height, num_inference_steps):
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
print("Activating both LoRAs...")
|
83 |
-
pipe.set_adapters([DEFAULT_LORA_NAME, CUSTOM_LORA_NAME], adapter_weights=[1.0, 1.0])
|
84 |
-
else:
|
85 |
-
# If no custom LoRA, just activate the base one.
|
86 |
-
print("Activating base LoRA only.")
|
87 |
-
pipe.set_adapters([DEFAULT_LORA_NAME], adapter_weights=[1.0])
|
88 |
-
|
89 |
-
print("LoRA setup complete. Calling the GPU function...")
|
90 |
-
# Now, call the decorated function to perform the actual generation
|
91 |
-
return generate(prompt, negative_prompt, width, height, num_inference_steps)
|
92 |
-
|
93 |
-
except Exception as e:
|
94 |
-
print(f"ERROR DURING INFERENCE SETUP: {e}")
|
95 |
-
raise gr.Error(f"Failed during LoRA loading or inference: {e}")
|
96 |
-
|
97 |
-
finally:
|
98 |
-
# --- CLEANUP ---
|
99 |
-
# This will run after `generate` has finished.
|
100 |
-
print("Unloading all LoRAs to clean up...")
|
101 |
-
pipe.unload_lora_weights()
|
102 |
-
gc.collect()
|
103 |
-
torch.cuda.empty_cache()
|
104 |
-
print("Cleanup complete.")
|
105 |
-
|
106 |
-
|
107 |
-
# The interface is now pointed at the `call_infer` wrapper function.
|
108 |
iface = gr.Interface(
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
)
|
122 |
|
123 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from diffusers import UniPCMultistepScheduler, FlowMatchEulerDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler
|
3 |
+
from diffusers import WanPipeline, AutoencoderKLWan # Use Wan-specific VAE
|
4 |
+
# from diffusers.hooks import apply_first_block_cache, FirstBlockCacheConfig
|
5 |
from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
|
6 |
+
from diffusers.models import UNetSpatioTemporalConditionModel
|
7 |
+
from transformers import T5EncoderModel, T5Tokenizer
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
from PIL import Image
|
13 |
import numpy as np
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
import gradio as gr
|
18 |
import spaces
|
|
|
19 |
|
20 |
+
|
21 |
+
|
22 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
23 |
+
|
24 |
+
|
25 |
|
26 |
model_id = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
|
|
|
27 |
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
|
|
|
|
|
|
28 |
pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
29 |
+
flow_shift = 1.0 #5.0 1.0 for image, 5.0 for 720P, 3.0 for 480P
|
|
|
30 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift)
|
31 |
|
32 |
+
|
33 |
+
|
|
|
34 |
pipe.to(device)
|
35 |
|
36 |
+
|
37 |
+
|
38 |
+
# Configure DDIMScheduler with a beta schedule
|
39 |
+
# pipe.scheduler = DDIMScheduler.from_config(
|
40 |
+
# pipe.scheduler.config,
|
41 |
+
# beta_start=0.00085, # Starting beta value
|
42 |
+
# beta_end=0.012, # Ending beta value
|
43 |
+
# beta_schedule="linear", # Linear beta schedule (other options: "scaled_linear", "squaredcos_cap_v2")
|
44 |
+
# num_train_timesteps=1000, # Number of timesteps
|
45 |
+
# flow_shift=flow_shift
|
46 |
+
# )
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
# Configure FlowMatchEulerDiscreteScheduler
|
54 |
+
# pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
|
55 |
+
# pipe.scheduler.config,
|
56 |
+
# flow_shift=flow_shift # Retain flow_shift for WanPipeline compatibility
|
57 |
+
# )
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
# --- LoRA State Management ---
|
62 |
+
# Define unique names for our adapters
|
63 |
DEFAULT_LORA_NAME = "causvid_lora"
|
64 |
CUSTOM_LORA_NAME = "custom_lora"
|
65 |
+
# Track which custom LoRA is currently loaded to avoid reloading
|
66 |
+
CURRENTLY_LOADED_CUSTOM_LORA = None
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
# Load the default base LoRA ONCE at startup
|
71 |
+
print("Loading base LoRA...")
|
72 |
+
CAUSVID_LORA_REPO = "Kijai/WanVideo_comfy"
|
73 |
+
CAUSVID_LORA_FILENAME = "Wan21_CausVid_14B_T2V_lora_rank32_v2.safetensors"
|
74 |
+
try:
|
75 |
+
causvid_path = hf_hub_download(repo_id=CAUSVID_LORA_REPO, filename=CAUSVID_LORA_FILENAME)
|
76 |
+
pipe.load_lora_weights(causvid_path, adapter_name=DEFAULT_LORA_NAME)
|
77 |
+
print(f"✅ Default LoRA '{DEFAULT_LORA_NAME}' loaded successfully.")
|
78 |
+
except Exception as e:
|
79 |
+
print(f"⚠️ Default LoRA could not be loaded: {e}")
|
80 |
+
DEFAULT_LORA_NAME = None
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
# print("Initialization complete. Gradio is starting...")
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
|
|
|
93 |
|
|
|
94 |
@spaces.GPU()
|
95 |
+
def generate(prompt, negative_prompt, width=1024, height=1024, num_inference_steps=30, lora_id=None, progress=gr.Progress(track_tqdm=True)):
|
96 |
+
# if lora_id and lora_id.strip() != "":
|
97 |
+
# pipe.unload_lora_weights()
|
98 |
+
# pipe.load_lora_weights(lora_id.strip())
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
#pipe.to("cuda")
|
109 |
+
# apply_first_block_cache(pipe.transformer, FirstBlockCacheConfig(threshold=0.2))
|
110 |
+
apply_cache_on_pipe(
|
111 |
+
pipe,
|
112 |
+
# residual_diff_threshold=0.2,
|
113 |
+
)
|
114 |
+
try:
|
115 |
+
output = pipe(
|
116 |
+
prompt=prompt,
|
117 |
+
negative_prompt=negative_prompt,
|
118 |
+
height=height,
|
119 |
+
width=width,
|
120 |
+
num_frames=1,
|
121 |
+
num_inference_steps=num_inference_steps,
|
122 |
+
guidance_scale=1.0, #5.0
|
123 |
+
)
|
124 |
+
image = output.frames[0][0]
|
125 |
+
image = (image * 255).astype(np.uint8)
|
126 |
+
return Image.fromarray(image)
|
127 |
+
finally:
|
128 |
+
pass
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
iface = gr.Interface(
|
136 |
+
fn=generate,
|
137 |
+
inputs=[
|
138 |
+
gr.Textbox(label="Input prompt"),
|
139 |
+
],
|
140 |
+
additional_inputs = [
|
141 |
+
gr.Textbox(label="Negative prompt", value = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"),
|
142 |
+
gr.Slider(label="Width", minimum=480, maximum=1280, step=16, value=1024),
|
143 |
+
gr.Slider(label="Height", minimum=480, maximum=1280, step=16, value=1024),
|
144 |
+
gr.Slider(minimum=1, maximum=80, step=1, label="Inference Steps", value=10),
|
145 |
+
gr.Textbox(label="LoRA ID"),
|
146 |
+
],
|
147 |
+
outputs=gr.Image(label="output"),
|
148 |
)
|
149 |
|
150 |
+
|
151 |
+
|
152 |
+
iface.launch()
|
153 |
+
|
154 |
+
|