markury commited on
Commit
3b2e319
·
1 Parent(s): 887231d

debug: peft

Browse files
Files changed (2) hide show
  1. app.py +46 -148
  2. requirements.txt +1 -4
app.py CHANGED
@@ -1,30 +1,10 @@
1
- import sys
2
- import subprocess
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
16
  from diffusers.utils import export_to_video
17
-
18
- # Now try to import the specific components
19
- try:
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,21 +18,7 @@ SCHEDULER_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,
58
  prompt,
@@ -68,119 +34,52 @@ def generate_video(
68
  num_inference_steps,
69
  output_fps
70
  ):
71
- """Generate a video using the Wan model and provided parameters"""
72
- try:
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]
86
- print(f"Using scheduler: {scheduler_type} with flow_shift: {flow_shift}")
87
-
88
- if scheduler_type == "UniPCMultistepScheduler":
89
- pipe.scheduler = scheduler_class.from_config(
90
- pipe.scheduler.config,
91
- prediction_type="flow_prediction",
92
- use_flow_sigmas=True,
93
- flow_shift=flow_shift
94
- )
95
- else:
96
- pipe.scheduler = scheduler_class(shift=flow_shift)
97
-
98
- # Move to GPU
99
- print("Moving model to GPU")
100
- pipe.to("cuda")
101
-
102
- # Enable CPU offload for low VRAM
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,
151
- "width": width,
152
- "num_frames": num_frames,
153
- "guidance_scale": guidance_scale,
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
-
173
- return temp_file
174
- except Exception as e:
175
- import traceback
176
- error_trace = traceback.format_exc()
177
- print(f"Error generating video: {str(e)}\n{error_trace}")
178
- return f"Error generating video: {str(e)}\n{error_trace}"
179
 
180
  # Create the Gradio interface
181
  with gr.Blocks() as demo:
182
  gr.Markdown("# Wan Video Generation with ZeroGPU")
183
- gr.Markdown("Generate high-quality videos using the Wan model with optional LoRA adaptations.")
184
 
185
  with gr.Row():
186
  with gr.Column(scale=1):
@@ -309,7 +208,6 @@ with gr.Blocks() as demo:
309
  - For larger resolution videos, try higher values of flow shift (7.0-12.0)
310
  - Number of frames should be of the form 4k+1 (e.g., 49, 81, 65)
311
  - The model is memory intensive, so adjust resolution according to available VRAM
312
- - LoRA ID should be a Hugging Face repository containing safetensors files
313
  """)
314
 
315
  demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
  import gradio as gr
3
  import spaces
4
  from diffusers.utils import export_to_video
5
+ from diffusers import AutoencoderKLWan, WanPipeline
6
+ from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
7
+ from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler
 
 
 
 
 
 
 
 
8
 
9
  # Define model options
10
  MODEL_OPTIONS = {
 
18
  "FlowMatchEulerDiscreteScheduler": FlowMatchEulerDiscreteScheduler
19
  }
20
 
21
+ @spaces.GPU(duration=300)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def generate_video(
23
  model_choice,
24
  prompt,
 
34
  num_inference_steps,
35
  output_fps
36
  ):
37
+ # Get model ID from selection
38
+ model_id = MODEL_OPTIONS[model_choice]
39
+
40
+ # Load model
41
+ vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
42
+ pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
43
+
44
+ # Set scheduler
45
+ if scheduler_type == "UniPCMultistepScheduler":
46
+ pipe.scheduler = UniPCMultistepScheduler.from_config(
47
+ pipe.scheduler.config,
48
+ flow_shift=flow_shift
49
+ )
50
+ else:
51
+ pipe.scheduler = FlowMatchEulerDiscreteScheduler(shift=flow_shift)
52
+
53
+ # Move to GPU
54
+ pipe.to("cuda")
55
+
56
+ # Load LoRA weights if provided
57
+ if lora_id and lora_id.strip():
58
+ pipe.load_lora_weights(lora_id)
59
+
60
+ # Enable CPU offload for low VRAM
61
+ pipe.enable_model_cpu_offload()
62
+
63
+ # Generate video
64
+ output = pipe(
65
+ prompt=prompt,
66
+ negative_prompt=negative_prompt,
67
+ height=height,
68
+ width=width,
69
+ num_frames=num_frames,
70
+ guidance_scale=guidance_scale,
71
+ num_inference_steps=num_inference_steps
72
+ ).frames[0]
73
+
74
+ # Export to video
75
+ temp_file = "output.mp4"
76
+ export_to_video(output, temp_file, fps=output_fps)
77
+
78
+ return temp_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Create the Gradio interface
81
  with gr.Blocks() as demo:
82
  gr.Markdown("# Wan Video Generation with ZeroGPU")
 
83
 
84
  with gr.Row():
85
  with gr.Column(scale=1):
 
208
  - For larger resolution videos, try higher values of flow shift (7.0-12.0)
209
  - Number of frames should be of the form 4k+1 (e.g., 49, 81, 65)
210
  - The model is memory intensive, so adjust resolution according to available VRAM
 
211
  """)
212
 
213
  demo.launch()
requirements.txt CHANGED
@@ -7,7 +7,4 @@ ftfy>=6.1.3
7
  einops>=0.7.0
8
  imageio>=2.31.6
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
 
7
  einops>=0.7.0
8
  imageio>=2.31.6
9
  imageio-ffmpeg>=0.4.9
10
+ peft==0.7.1