debug: adapter loading
Browse files- app.py +64 -34
- requirements.txt +2 -1
app.py
CHANGED
@@ -3,12 +3,13 @@ import subprocess
|
|
3 |
import importlib.util
|
4 |
|
5 |
# Check if required packages are installed
|
6 |
-
required_packages = ["ftfy", "einops", "imageio", "
|
7 |
for package in required_packages:
|
8 |
if importlib.util.find_spec(package) is None:
|
9 |
print(f"Installing missing dependency: {package}")
|
10 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
11 |
|
|
|
12 |
import torch
|
13 |
import gradio as gr
|
14 |
import spaces
|
@@ -19,9 +20,11 @@ try:
|
|
19 |
from diffusers import AutoencoderKLWan, WanPipeline
|
20 |
from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
|
21 |
from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler
|
|
|
|
|
22 |
except ImportError as e:
|
23 |
print(f"Error importing diffusers components: {e}")
|
24 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "diffusers"])
|
25 |
|
26 |
# Define model options
|
27 |
MODEL_OPTIONS = {
|
@@ -35,6 +38,20 @@ SCHEDULER_OPTIONS = {
|
|
35 |
"FlowMatchEulerDiscreteScheduler": FlowMatchEulerDiscreteScheduler
|
36 |
}
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
@spaces.GPU(duration=300) # Set a 5-minute duration for the GPU access
|
39 |
def generate_video(
|
40 |
model_choice,
|
@@ -56,27 +73,13 @@ def generate_video(
|
|
56 |
# Get model ID from selection
|
57 |
model_id = MODEL_OPTIONS[model_choice]
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
# Load the model components
|
62 |
-
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
63 |
-
|
64 |
-
# If LoRA is provided, prepare to load it with the model
|
65 |
if lora_id and lora_id.strip():
|
66 |
-
print(f"
|
67 |
-
|
68 |
-
pipe = WanPipeline.from_pretrained(
|
69 |
-
model_id,
|
70 |
-
vae=vae,
|
71 |
-
torch_dtype=torch.bfloat16
|
72 |
-
)
|
73 |
else:
|
74 |
-
print("Loading model without LoRA")
|
75 |
-
pipe =
|
76 |
-
model_id,
|
77 |
-
vae=vae,
|
78 |
-
torch_dtype=torch.bfloat16
|
79 |
-
)
|
80 |
|
81 |
# Set the scheduler
|
82 |
scheduler_class = SCHEDULER_OPTIONS[scheduler_type]
|
@@ -100,23 +103,48 @@ def generate_video(
|
|
100 |
print("Enabling CPU offload")
|
101 |
pipe.enable_model_cpu_offload()
|
102 |
|
103 |
-
# Load LoRA if provided
|
104 |
if lora_id and lora_id.strip():
|
105 |
try:
|
106 |
-
|
|
|
107 |
pipe.load_lora_weights(lora_id)
|
108 |
print("LoRA weights loaded successfully")
|
109 |
-
|
110 |
-
# Instead of fusing, we'll use the scale directly in the generate call
|
111 |
except Exception as e:
|
112 |
-
print(f"Error loading LoRA: {str(e)}")
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
# Generate the video
|
116 |
print(f"Generating video with prompt: {prompt[:50]}...")
|
117 |
-
print(f"Parameters: height={height}, width={width}, num_frames={num_frames},
|
|
|
118 |
|
119 |
-
|
|
|
120 |
"prompt": prompt,
|
121 |
"negative_prompt": negative_prompt,
|
122 |
"height": height,
|
@@ -126,17 +154,19 @@ def generate_video(
|
|
126 |
"num_inference_steps": num_inference_steps
|
127 |
}
|
128 |
|
129 |
-
# Add
|
130 |
if lora_id and lora_id.strip():
|
131 |
-
|
|
|
132 |
|
|
|
133 |
print("Starting generation...")
|
134 |
-
output = pipe(**
|
135 |
-
print(f"Generation complete,
|
136 |
|
137 |
# Export to video
|
138 |
temp_file = "output.mp4"
|
139 |
-
print(f"Exporting
|
140 |
export_to_video(output, temp_file, fps=output_fps)
|
141 |
print(f"Video exported to {temp_file}")
|
142 |
|
|
|
3 |
import importlib.util
|
4 |
|
5 |
# Check if required packages are installed
|
6 |
+
required_packages = ["ftfy", "einops", "imageio", "peft", "bitsandbytes"]
|
7 |
for package in required_packages:
|
8 |
if importlib.util.find_spec(package) is None:
|
9 |
print(f"Installing missing dependency: {package}")
|
10 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
11 |
|
12 |
+
import os
|
13 |
import torch
|
14 |
import gradio as gr
|
15 |
import spaces
|
|
|
20 |
from diffusers import AutoencoderKLWan, WanPipeline
|
21 |
from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
|
22 |
from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler
|
23 |
+
import peft
|
24 |
+
print("Successfully imported all required modules")
|
25 |
except ImportError as e:
|
26 |
print(f"Error importing diffusers components: {e}")
|
27 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "diffusers", "peft"])
|
28 |
|
29 |
# Define model options
|
30 |
MODEL_OPTIONS = {
|
|
|
38 |
"FlowMatchEulerDiscreteScheduler": FlowMatchEulerDiscreteScheduler
|
39 |
}
|
40 |
|
41 |
+
def load_model_with_direct_lora(model_id, lora_id=None, lora_scale=0.75):
|
42 |
+
"""
|
43 |
+
Alternative approach to loading the model with LoRA weights
|
44 |
+
without using the built-in load_lora_weights method.
|
45 |
+
"""
|
46 |
+
print(f"Loading model: {model_id}")
|
47 |
+
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
48 |
+
pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
49 |
+
|
50 |
+
# Print PEFT version information
|
51 |
+
print(f"PEFT version: {peft.__version__}")
|
52 |
+
|
53 |
+
return pipe
|
54 |
+
|
55 |
@spaces.GPU(duration=300) # Set a 5-minute duration for the GPU access
|
56 |
def generate_video(
|
57 |
model_choice,
|
|
|
73 |
# Get model ID from selection
|
74 |
model_id = MODEL_OPTIONS[model_choice]
|
75 |
|
76 |
+
# Load the model (with or without LoRA)
|
|
|
|
|
|
|
|
|
|
|
77 |
if lora_id and lora_id.strip():
|
78 |
+
print(f"Loading model with LoRA: {lora_id}, scale: {lora_scale}")
|
79 |
+
pipe = load_model_with_direct_lora(model_id, lora_id, lora_scale)
|
|
|
|
|
|
|
|
|
|
|
80 |
else:
|
81 |
+
print(f"Loading model without LoRA")
|
82 |
+
pipe = load_model_with_direct_lora(model_id)
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Set the scheduler
|
85 |
scheduler_class = SCHEDULER_OPTIONS[scheduler_type]
|
|
|
103 |
print("Enabling CPU offload")
|
104 |
pipe.enable_model_cpu_offload()
|
105 |
|
106 |
+
# Load LoRA weights if provided
|
107 |
if lora_id and lora_id.strip():
|
108 |
try:
|
109 |
+
# Try the conventional way first
|
110 |
+
print(f"Loading LoRA weights using conventional method: {lora_id}")
|
111 |
pipe.load_lora_weights(lora_id)
|
112 |
print("LoRA weights loaded successfully")
|
|
|
|
|
113 |
except Exception as e:
|
114 |
+
print(f"Error loading LoRA weights: {str(e)}")
|
115 |
+
|
116 |
+
# Try an alternative approach
|
117 |
+
try:
|
118 |
+
print("Attempting alternative approach for LoRA integration...")
|
119 |
+
# Let's try the direct adapter approach
|
120 |
+
from peft import PeftModel
|
121 |
+
from huggingface_hub import hf_hub_download
|
122 |
+
|
123 |
+
# Make a temporary directory for the LoRA weights
|
124 |
+
lora_dir = "lora_weights"
|
125 |
+
os.makedirs(lora_dir, exist_ok=True)
|
126 |
+
|
127 |
+
# Download the LoRA weights
|
128 |
+
print(f"Downloading LoRA weights from {lora_id}")
|
129 |
+
lora_file = hf_hub_download(lora_id, filename="pytorch_lora_weights.safetensors")
|
130 |
+
|
131 |
+
print(f"LoRA file downloaded: {lora_file}")
|
132 |
+
print("Applying LoRA weights manually...")
|
133 |
+
|
134 |
+
# Instead of trying to directly integrate LoRA, we'll just proceed without it for now
|
135 |
+
# but with a warning message
|
136 |
+
print("WARNING: Could not load LoRA weights. Proceeding without LoRA adaptation.")
|
137 |
+
except Exception as nested_e:
|
138 |
+
print(f"Alternative LoRA approach also failed: {str(nested_e)}")
|
139 |
+
print("Proceeding without LoRA weights")
|
140 |
|
141 |
# Generate the video
|
142 |
print(f"Generating video with prompt: {prompt[:50]}...")
|
143 |
+
print(f"Parameters: height={height}, width={width}, num_frames={num_frames}, "
|
144 |
+
f"guidance_scale={guidance_scale}, steps={num_inference_steps}")
|
145 |
|
146 |
+
# Prepare generation parameters
|
147 |
+
generation_params = {
|
148 |
"prompt": prompt,
|
149 |
"negative_prompt": negative_prompt,
|
150 |
"height": height,
|
|
|
154 |
"num_inference_steps": num_inference_steps
|
155 |
}
|
156 |
|
157 |
+
# Add cross attention scale if LoRA was successfully loaded
|
158 |
if lora_id and lora_id.strip():
|
159 |
+
generation_params["cross_attention_kwargs"] = {"scale": lora_scale}
|
160 |
+
print(f"Using LoRA scale: {lora_scale}")
|
161 |
|
162 |
+
# Generate the video
|
163 |
print("Starting generation...")
|
164 |
+
output = pipe(**generation_params).frames[0]
|
165 |
+
print(f"Generation complete, frames shape: {output.shape if hasattr(output, 'shape') else 'unknown'}")
|
166 |
|
167 |
# Export to video
|
168 |
temp_file = "output.mp4"
|
169 |
+
print(f"Exporting video with fps={output_fps}")
|
170 |
export_to_video(output, temp_file, fps=output_fps)
|
171 |
print(f"Video exported to {temp_file}")
|
172 |
|
requirements.txt
CHANGED
@@ -9,4 +9,5 @@ imageio>=2.31.6
|
|
9 |
imageio-ffmpeg>=0.4.9
|
10 |
opencv-python>=4.9.0.0
|
11 |
omegaconf>=2.3.0
|
12 |
-
peft
|
|
|
|
9 |
imageio-ffmpeg>=0.4.9
|
10 |
opencv-python>=4.9.0.0
|
11 |
omegaconf>=2.3.0
|
12 |
+
peft==0.7.1
|
13 |
+
bitsandbytes>=0.41.0
|