Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch.nn.functional as F
|
3 |
-
from diffusers import AutoencoderKLWan, WanVideoTextToVideoPipeline, UniPCMultistepScheduler
|
4 |
-
from diffusers.utils import export_to_video
|
5 |
-
from diffusers.models import Transformer2DModel
|
6 |
-
import gradio as gr
|
7 |
-
import tempfile
|
8 |
-
import spaces
|
9 |
-
from huggingface_hub import hf_hub_download
|
10 |
-
import numpy as np
|
11 |
-
import random
|
12 |
-
import logging
|
13 |
-
import os
|
14 |
-
import gc
|
15 |
-
from typing import List, Optional, Union
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
setup_eval_logging)
|
30 |
-
from mmaudio.model.flow_matching import FlowMatching
|
31 |
-
from mmaudio.model.networks import MMAudio, get_my_mmaudio
|
32 |
-
from mmaudio.model.sequence_config import SequenceConfig
|
33 |
-
from mmaudio.model.utils.features_utils import FeaturesUtils
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
self
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
@torch.no_grad()
|
44 |
def __call__(
|
@@ -48,124 +200,194 @@ class NAGWanPipeline(WanVideoTextToVideoPipeline):
|
|
48 |
nag_scale: float = 0.0,
|
49 |
nag_tau: float = 3.5,
|
50 |
nag_alpha: float = 0.5,
|
51 |
-
height: Optional[int] =
|
52 |
-
width: Optional[int] =
|
53 |
num_frames: int = 16,
|
54 |
num_inference_steps: int = 50,
|
55 |
guidance_scale: float = 7.5,
|
56 |
negative_prompt: Optional[Union[str, List[str]]] = None,
|
57 |
eta: float = 0.0,
|
58 |
-
generator: Optional[
|
59 |
latents: Optional[torch.FloatTensor] = None,
|
60 |
-
prompt_embeds: Optional[torch.FloatTensor] = None,
|
61 |
-
negative_prompt_embeds: Optional[torch.FloatTensor] = None,
|
62 |
output_type: Optional[str] = "pil",
|
63 |
return_dict: bool = True,
|
64 |
-
callback = None,
|
65 |
callback_steps: int = 1,
|
66 |
-
|
67 |
-
clip_skip: Optional[int] = None,
|
68 |
):
|
69 |
# Use NAG negative prompt if provided
|
70 |
if nag_negative_prompt is not None:
|
71 |
negative_prompt = nag_negative_prompt
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
85 |
|
86 |
-
#
|
87 |
-
|
88 |
-
|
89 |
-
batch_size, channels, frames, height, width = hidden_states.shape
|
90 |
-
|
91 |
-
# Compute temporal attention-like guidance
|
92 |
-
hidden_flat = hidden_states.view(batch_size, channels, -1)
|
93 |
-
attention = F.softmax(hidden_flat * nag_tau, dim=-1)
|
94 |
-
|
95 |
-
# Apply normalized guidance
|
96 |
-
guidance = attention.mean(dim=2, keepdim=True) * nag_alpha
|
97 |
-
guidance = guidance.unsqueeze(-1).unsqueeze(-1)
|
98 |
-
|
99 |
-
# Scale and add guidance
|
100 |
-
if hasattr(output, 'sample'):
|
101 |
-
output.sample = output.sample + nag_scale * guidance * hidden_states
|
102 |
-
else:
|
103 |
-
output = output + nag_scale * guidance * hidden_states
|
104 |
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
#
|
108 |
-
|
|
|
109 |
|
110 |
-
#
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
width=width,
|
115 |
-
num_frames=num_frames,
|
116 |
-
num_inference_steps=num_inference_steps,
|
117 |
-
guidance_scale=guidance_scale,
|
118 |
-
negative_prompt=negative_prompt,
|
119 |
-
eta=eta,
|
120 |
-
generator=generator,
|
121 |
-
latents=latents,
|
122 |
-
prompt_embeds=prompt_embeds,
|
123 |
-
negative_prompt_embeds=negative_prompt_embeds,
|
124 |
-
output_type=output_type,
|
125 |
-
return_dict=return_dict,
|
126 |
-
callback=callback,
|
127 |
-
callback_steps=callback_steps,
|
128 |
-
cross_attention_kwargs=cross_attention_kwargs,
|
129 |
-
clip_skip=clip_skip,
|
130 |
-
)
|
131 |
|
132 |
-
#
|
133 |
-
|
134 |
-
|
|
|
135 |
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
-
#
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
-
# Video generation model setup
|
150 |
MODEL_ID = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
|
|
|
|
|
151 |
LORA_REPO_ID = "Kijai/WanVideo_comfy"
|
152 |
LORA_FILENAME = "Wan21_CausVid_14B_T2V_lora_rank32.safetensors"
|
153 |
|
154 |
-
#
|
155 |
vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
|
|
|
|
|
156 |
pipe = NAGWanPipeline.from_pretrained(
|
157 |
-
MODEL_ID, vae=vae, torch_dtype=torch.bfloat16
|
158 |
)
|
159 |
-
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=
|
160 |
pipe.to("cuda")
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
pipe.load_lora_weights(causvid_path, adapter_name="causvid_lora")
|
165 |
-
pipe.set_adapters(["causvid_lora"], adapter_weights=[0.95])
|
166 |
-
pipe.fuse_lora()
|
167 |
|
168 |
-
# Audio
|
169 |
torch.backends.cuda.matmul.allow_tf32 = True
|
170 |
torch.backends.cudnn.allow_tf32 = True
|
171 |
|
@@ -173,7 +395,7 @@ log = logging.getLogger()
|
|
173 |
device = 'cuda'
|
174 |
dtype = torch.bfloat16
|
175 |
|
176 |
-
# Global
|
177 |
audio_model = None
|
178 |
audio_net = None
|
179 |
audio_feature_utils = None
|
@@ -206,172 +428,88 @@ def load_audio_model():
|
|
206 |
|
207 |
return audio_net, audio_feature_utils, audio_seq_cfg
|
208 |
|
209 |
-
#
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
MAX_SEED = np.iinfo(np.int32).max
|
220 |
-
|
221 |
-
FIXED_FPS = 16
|
222 |
-
MIN_FRAMES_MODEL = 8
|
223 |
-
MAX_FRAMES_MODEL = 129
|
224 |
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
|
|
229 |
|
230 |
# CSS
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
background-size: 400% 400% !important;
|
237 |
-
animation: gradientShift 15s ease infinite !important;
|
238 |
}
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
|
|
|
|
|
|
244 |
}
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
background: rgba(255, 255, 255, 0.1) !important;
|
250 |
-
border-radius: 20px !important;
|
251 |
-
padding: 30px !important;
|
252 |
-
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37) !important;
|
253 |
-
border: 1px solid rgba(255, 255, 255, 0.18) !important;
|
254 |
}
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
-
|
260 |
-
-
|
261 |
-
background-clip: text !important;
|
262 |
-
font-weight: 800 !important;
|
263 |
-
font-size: 2.5rem !important;
|
264 |
-
text-align: center !important;
|
265 |
-
margin-bottom: 2rem !important;
|
266 |
-
text-shadow: 2px 2px 4px rgba(0,0,0,0.1) !important;
|
267 |
-
}
|
268 |
-
|
269 |
-
/* 컴포넌트 컨테이너 스타일 */
|
270 |
-
.input-container, .output-container {
|
271 |
-
background: rgba(255, 255, 255, 0.08) !important;
|
272 |
-
border-radius: 15px !important;
|
273 |
-
padding: 20px !important;
|
274 |
-
margin: 10px 0 !important;
|
275 |
-
backdrop-filter: blur(5px) !important;
|
276 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
277 |
}
|
278 |
-
|
279 |
-
/* 입력 필드 스타일 */
|
280 |
-
input, textarea, .gr-box {
|
281 |
-
background: rgba(255, 255, 255, 0.9) !important;
|
282 |
-
border: 1px solid rgba(255, 255, 255, 0.3) !important;
|
283 |
-
border-radius: 10px !important;
|
284 |
-
color: #333 !important;
|
285 |
-
transition: all 0.3s ease !important;
|
286 |
-
}
|
287 |
-
|
288 |
-
input:focus, textarea:focus {
|
289 |
-
background: rgba(255, 255, 255, 1) !important;
|
290 |
-
border-color: #667eea !important;
|
291 |
-
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
|
292 |
-
}
|
293 |
-
|
294 |
-
/* 버튼 스타일 */
|
295 |
.generate-btn {
|
296 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%)
|
297 |
-
color: white
|
298 |
-
font-
|
299 |
-
font-
|
300 |
-
padding:
|
301 |
-
border-radius:
|
302 |
-
border: none
|
303 |
-
cursor: pointer
|
304 |
-
transition: all 0.3s ease
|
305 |
-
|
|
|
306 |
}
|
307 |
-
|
308 |
.generate-btn:hover {
|
309 |
-
transform: translateY(-2px)
|
310 |
-
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.
|
311 |
-
}
|
312 |
-
|
313 |
-
/* 슬라이더 스타일 */
|
314 |
-
input[type="range"] {
|
315 |
-
background: transparent !important;
|
316 |
}
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
|
|
322 |
}
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
cursor: pointer !important;
|
329 |
-
width: 18px !important;
|
330 |
-
height: 18px !important;
|
331 |
-
-webkit-appearance: none !important;
|
332 |
}
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
margin: 15px 0 !important;
|
340 |
-
}
|
341 |
-
|
342 |
-
/* 라벨 스타일 */
|
343 |
-
label {
|
344 |
-
color: #ffffff !important;
|
345 |
-
font-weight: 500 !important;
|
346 |
-
font-size: 0.95rem !important;
|
347 |
-
margin-bottom: 5px !important;
|
348 |
-
}
|
349 |
-
|
350 |
-
/* 비디오 출력 영역 */
|
351 |
-
video {
|
352 |
-
border-radius: 15px !important;
|
353 |
-
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3) !important;
|
354 |
-
}
|
355 |
-
|
356 |
-
/* Examples 섹션 스타일 */
|
357 |
-
.gr-examples {
|
358 |
-
background: rgba(255, 255, 255, 0.05) !important;
|
359 |
-
border-radius: 15px !important;
|
360 |
-
padding: 20px !important;
|
361 |
-
margin-top: 20px !important;
|
362 |
-
}
|
363 |
-
|
364 |
-
/* Checkbox 스타일 */
|
365 |
-
input[type="checkbox"] {
|
366 |
-
accent-color: #667eea !important;
|
367 |
-
}
|
368 |
-
|
369 |
-
/* Radio 버튼 스타일 */
|
370 |
-
input[type="radio"] {
|
371 |
-
accent-color: #667eea !important;
|
372 |
}
|
373 |
-
|
374 |
-
/* Info box */
|
375 |
.info-box {
|
376 |
background: linear-gradient(135deg, #e0e7ff 0%, #c7d2fe 100%);
|
377 |
border-radius: 10px;
|
@@ -379,26 +517,20 @@ input[type="radio"] {
|
|
379 |
margin: 10px 0;
|
380 |
border-left: 4px solid #667eea;
|
381 |
}
|
382 |
-
|
383 |
-
/* 반응형 애니메이션 */
|
384 |
-
@media (max-width: 768px) {
|
385 |
-
h1 { font-size: 2rem !important; }
|
386 |
-
.main-container { padding: 20px !important; }
|
387 |
-
}
|
388 |
"""
|
389 |
|
390 |
-
|
391 |
-
|
392 |
-
torch.cuda.empty_cache()
|
393 |
-
torch.cuda.synchronize()
|
394 |
-
gc.collect()
|
395 |
|
396 |
-
def get_duration(
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
|
|
|
|
|
|
402 |
duration = int(duration_seconds) * int(steps) * 2.25 + 5
|
403 |
if audio_mode == "Enable Audio":
|
404 |
duration += 60
|
@@ -440,108 +572,97 @@ def add_audio_to_video(video_path, duration_sec, audio_prompt, audio_negative_pr
|
|
440 |
return video_with_audio_path
|
441 |
|
442 |
@spaces.GPU(duration=get_duration)
|
443 |
-
def generate_video(
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
|
454 |
target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
|
455 |
-
|
456 |
num_frames = np.clip(int(round(int(duration_seconds) * FIXED_FPS) + 1), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
457 |
-
|
458 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
459 |
|
460 |
-
# Generate video using NAG
|
461 |
with torch.inference_mode():
|
462 |
-
|
463 |
prompt=prompt,
|
464 |
nag_negative_prompt=nag_negative_prompt,
|
465 |
nag_scale=nag_scale,
|
466 |
nag_tau=3.5,
|
467 |
nag_alpha=0.5,
|
468 |
-
height=target_h,
|
469 |
-
|
470 |
-
num_frames=num_frames,
|
471 |
-
guidance_scale=0., # NAG replaces traditional guidance
|
472 |
num_inference_steps=int(steps),
|
473 |
generator=torch.Generator(device="cuda").manual_seed(current_seed)
|
474 |
).frames[0]
|
475 |
|
476 |
-
# Save video without audio
|
477 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
478 |
-
|
479 |
-
export_to_video(
|
480 |
-
|
481 |
# Generate audio if enabled
|
482 |
video_with_audio_path = None
|
483 |
if audio_mode == "Enable Audio":
|
484 |
-
progress(0.5, desc="Generating audio...")
|
485 |
video_with_audio_path = add_audio_to_video(
|
486 |
-
|
487 |
audio_prompt, audio_negative_prompt,
|
488 |
audio_seed, audio_steps, audio_cfg_strength
|
489 |
)
|
490 |
|
491 |
clear_cache()
|
492 |
cleanup_temp_files()
|
493 |
-
|
494 |
-
return
|
495 |
|
496 |
def update_audio_visibility(audio_mode):
|
497 |
return gr.update(visible=(audio_mode == "Enable Audio"))
|
498 |
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
gr.Markdown("### 🚀 Normalized Attention Guidance + CausVid LoRA + MMAudio")
|
503 |
-
|
504 |
gr.HTML("""
|
505 |
-
|
506 |
-
<p
|
507 |
-
<p>⚡ <strong>Speed</strong>: Generate videos in just 4-8 steps with CausVid LoRA</p>
|
508 |
-
<p>🎵 <strong>Audio</strong>: Optional synchronized audio generation with MMAudio</p>
|
509 |
-
</div>
|
510 |
""")
|
511 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
512 |
with gr.Row():
|
513 |
-
with gr.Column(
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
with gr.Accordion("🎨 NAG Settings", open=True):
|
522 |
-
nag_negative_prompt = gr.Textbox(
|
523 |
-
label="❌ NAG Negative Prompt",
|
524 |
-
value=DEFAULT_NAG_NEGATIVE_PROMPT,
|
525 |
-
lines=2
|
526 |
)
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
)
|
544 |
-
|
545 |
audio_mode = gr.Radio(
|
546 |
choices=["Video Only", "Enable Audio"],
|
547 |
value="Video Only",
|
@@ -553,7 +674,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
553 |
audio_prompt = gr.Textbox(
|
554 |
label="🎵 Audio Prompt",
|
555 |
value=default_audio_prompt,
|
556 |
-
placeholder="Describe the audio
|
557 |
lines=2
|
558 |
)
|
559 |
audio_negative_prompt = gr.Textbox(
|
@@ -582,112 +703,135 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
582 |
value=4.5,
|
583 |
label="🎯 Audio Guidance"
|
584 |
)
|
585 |
-
|
586 |
-
with gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
587 |
with gr.Row():
|
588 |
height_input = gr.Slider(
|
589 |
minimum=SLIDER_MIN_H,
|
590 |
maximum=SLIDER_MAX_H,
|
591 |
step=MOD_VALUE,
|
592 |
value=DEFAULT_H_SLIDER_VALUE,
|
593 |
-
label=f"
|
|
|
594 |
)
|
595 |
width_input = gr.Slider(
|
596 |
minimum=SLIDER_MIN_W,
|
597 |
maximum=SLIDER_MAX_W,
|
598 |
step=MOD_VALUE,
|
599 |
value=DEFAULT_W_SLIDER_VALUE,
|
600 |
-
label=f"📐
|
|
|
601 |
)
|
|
|
602 |
with gr.Row():
|
603 |
-
steps_slider = gr.Slider(
|
604 |
-
minimum=1,
|
605 |
-
maximum=8,
|
606 |
-
step=1,
|
607 |
-
value=DEFAULT_STEPS,
|
608 |
-
label="🚀 Inference Steps"
|
609 |
-
)
|
610 |
seed_input = gr.Slider(
|
611 |
-
label="
|
612 |
minimum=0,
|
613 |
maximum=MAX_SEED,
|
614 |
step=1,
|
615 |
value=DEFAULT_SEED,
|
616 |
interactive=True
|
617 |
)
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
|
624 |
generate_button = gr.Button(
|
625 |
"🎬 Generate Video",
|
626 |
variant="primary",
|
627 |
-
elem_classes=
|
628 |
)
|
629 |
-
|
630 |
-
with gr.Column(
|
631 |
-
|
632 |
-
label="
|
633 |
autoplay=True,
|
634 |
-
interactive=False
|
|
|
635 |
)
|
636 |
video_with_audio_output = gr.Video(
|
637 |
label="🎥 Generated Video with Audio",
|
638 |
autoplay=True,
|
639 |
interactive=False,
|
640 |
-
visible=False
|
|
|
641 |
)
|
642 |
|
643 |
gr.HTML("""
|
644 |
-
<div style="text-align: center; margin-top: 20px; color: #
|
645 |
<p>💡 Tip: Try different NAG scales for varied artistic effects!</p>
|
646 |
</div>
|
647 |
""")
|
648 |
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
|
|
664 |
fn=generate_video,
|
665 |
-
inputs=
|
666 |
-
|
|
|
|
|
|
|
|
|
|
|
667 |
)
|
668 |
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
)
|
691 |
|
692 |
if __name__ == "__main__":
|
693 |
demo.queue().launch()
|
|
|
1 |
+
# Create src directory structure
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
os.makedirs("src", exist_ok=True)
|
5 |
+
|
6 |
+
# Create __init__.py
|
7 |
+
with open("src/__init__.py", "w") as f:
|
8 |
+
f.write("")
|
9 |
+
|
10 |
+
# Create transformer_wan_nag.py
|
11 |
+
with open("src/transformer_wan_nag.py", "w") as f:
|
12 |
+
f.write('''
|
13 |
import torch
|
14 |
+
import torch.nn as nn
|
15 |
+
from diffusers.models import ModelMixin
|
16 |
+
from diffusers.configuration_utils import ConfigMixin
|
17 |
+
from diffusers.models.attention_processor import AttentionProcessor
|
18 |
+
from typing import Optional, Dict, Any
|
19 |
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
class NagWanTransformer3DModel(ModelMixin, ConfigMixin):
|
22 |
+
"""NAG-enhanced Transformer for video generation"""
|
23 |
+
|
24 |
+
@classmethod
|
25 |
+
def from_single_file(cls, model_path, **kwargs):
|
26 |
+
"""Load model from single file"""
|
27 |
+
# Create a minimal transformer model
|
28 |
+
model = cls()
|
29 |
+
|
30 |
+
# Try to load weights if available
|
31 |
+
try:
|
32 |
+
from safetensors import safe_open
|
33 |
+
with safe_open(model_path, framework="pt", device="cpu") as f:
|
34 |
+
state_dict = {}
|
35 |
+
for key in f.keys():
|
36 |
+
state_dict[key] = f.get_tensor(key)
|
37 |
+
# model.load_state_dict(state_dict, strict=False)
|
38 |
+
except:
|
39 |
+
pass
|
40 |
+
|
41 |
+
return model.to(kwargs.get('torch_dtype', torch.float32))
|
42 |
+
|
43 |
+
def __init__(self):
|
44 |
+
super().__init__()
|
45 |
+
self.config = {"in_channels": 4, "out_channels": 4}
|
46 |
+
self.training = False
|
47 |
+
|
48 |
+
# Simple transformer layers
|
49 |
+
self.norm = nn.LayerNorm(768)
|
50 |
+
self.proj_in = nn.Linear(4, 768)
|
51 |
+
self.transformer_blocks = nn.ModuleList([
|
52 |
+
nn.TransformerEncoderLayer(d_model=768, nhead=8, batch_first=True)
|
53 |
+
for _ in range(4)
|
54 |
+
])
|
55 |
+
self.proj_out = nn.Linear(768, 4)
|
56 |
+
|
57 |
+
@staticmethod
|
58 |
+
def attn_processors():
|
59 |
+
return {}
|
60 |
+
|
61 |
+
@staticmethod
|
62 |
+
def set_attn_processor(processor):
|
63 |
+
pass
|
64 |
+
|
65 |
+
def forward(
|
66 |
+
self,
|
67 |
+
hidden_states: torch.Tensor,
|
68 |
+
timestep: Optional[torch.Tensor] = None,
|
69 |
+
encoder_hidden_states: Optional[torch.Tensor] = None,
|
70 |
+
attention_mask: Optional[torch.Tensor] = None,
|
71 |
+
**kwargs
|
72 |
+
):
|
73 |
+
# Simple forward pass
|
74 |
+
batch, channels, frames, height, width = hidden_states.shape
|
75 |
+
|
76 |
+
# Reshape for processing
|
77 |
+
hidden_states = hidden_states.permute(0, 2, 3, 4, 1).contiguous()
|
78 |
+
hidden_states = hidden_states.view(batch * frames, height * width, channels)
|
79 |
+
|
80 |
+
# Project to transformer dimension
|
81 |
+
hidden_states = self.proj_in(hidden_states)
|
82 |
+
hidden_states = self.norm(hidden_states)
|
83 |
+
|
84 |
+
# Apply transformer blocks
|
85 |
+
for block in self.transformer_blocks:
|
86 |
+
hidden_states = block(hidden_states)
|
87 |
+
|
88 |
+
# Project back
|
89 |
+
hidden_states = self.proj_out(hidden_states)
|
90 |
+
|
91 |
+
# Reshape back
|
92 |
+
hidden_states = hidden_states.view(batch, frames, height, width, channels)
|
93 |
+
hidden_states = hidden_states.permute(0, 4, 1, 2, 3).contiguous()
|
94 |
+
|
95 |
+
return hidden_states
|
96 |
+
''')
|
97 |
|
98 |
+
# Create pipeline_wan_nag.py
|
99 |
+
with open("src/pipeline_wan_nag.py", "w") as f:
|
100 |
+
f.write('''
|
101 |
+
import torch
|
102 |
+
import torch.nn.functional as F
|
103 |
+
from typing import List, Optional, Union, Tuple, Callable, Dict, Any
|
104 |
+
from diffusers import DiffusionPipeline
|
105 |
+
from diffusers.utils import logging, export_to_video
|
106 |
+
from diffusers.schedulers import KarrasDiffusionSchedulers
|
107 |
+
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
|
108 |
+
from transformers import CLIPTextModel, CLIPTokenizer
|
109 |
+
import numpy as np
|
110 |
|
111 |
+
logger = logging.get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
+
class NAGWanPipeline(DiffusionPipeline):
|
114 |
+
"""NAG-enhanced pipeline for video generation"""
|
115 |
+
|
116 |
+
def __init__(
|
117 |
+
self,
|
118 |
+
vae,
|
119 |
+
text_encoder,
|
120 |
+
tokenizer,
|
121 |
+
transformer,
|
122 |
+
scheduler,
|
123 |
+
):
|
124 |
+
super().__init__()
|
125 |
+
self.register_modules(
|
126 |
+
vae=vae,
|
127 |
+
text_encoder=text_encoder,
|
128 |
+
tokenizer=tokenizer,
|
129 |
+
transformer=transformer,
|
130 |
+
scheduler=scheduler,
|
131 |
+
)
|
132 |
+
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
|
133 |
+
|
134 |
+
@classmethod
|
135 |
+
def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
|
136 |
+
"""Load pipeline from pretrained model"""
|
137 |
+
vae = kwargs.pop("vae", None)
|
138 |
+
transformer = kwargs.pop("transformer", None)
|
139 |
+
torch_dtype = kwargs.pop("torch_dtype", torch.float32)
|
140 |
+
|
141 |
+
# Load text encoder and tokenizer
|
142 |
+
text_encoder = CLIPTextModel.from_pretrained(
|
143 |
+
pretrained_model_name_or_path,
|
144 |
+
subfolder="text_encoder",
|
145 |
+
torch_dtype=torch_dtype
|
146 |
+
)
|
147 |
+
tokenizer = CLIPTokenizer.from_pretrained(
|
148 |
+
pretrained_model_name_or_path,
|
149 |
+
subfolder="tokenizer"
|
150 |
+
)
|
151 |
+
|
152 |
+
# Load scheduler
|
153 |
+
from diffusers import UniPCMultistepScheduler
|
154 |
+
scheduler = UniPCMultistepScheduler.from_pretrained(
|
155 |
+
pretrained_model_name_or_path,
|
156 |
+
subfolder="scheduler"
|
157 |
+
)
|
158 |
+
|
159 |
+
return cls(
|
160 |
+
vae=vae,
|
161 |
+
text_encoder=text_encoder,
|
162 |
+
tokenizer=tokenizer,
|
163 |
+
transformer=transformer,
|
164 |
+
scheduler=scheduler,
|
165 |
+
)
|
166 |
+
|
167 |
+
def _encode_prompt(self, prompt, device, do_classifier_free_guidance, negative_prompt=None):
|
168 |
+
"""Encode text prompt to embeddings"""
|
169 |
+
batch_size = len(prompt) if isinstance(prompt, list) else 1
|
170 |
+
|
171 |
+
text_inputs = self.tokenizer(
|
172 |
+
prompt,
|
173 |
+
padding="max_length",
|
174 |
+
max_length=self.tokenizer.model_max_length,
|
175 |
+
truncation=True,
|
176 |
+
return_tensors="pt",
|
177 |
+
)
|
178 |
+
text_input_ids = text_inputs.input_ids
|
179 |
+
text_embeddings = self.text_encoder(text_input_ids.to(device))[0]
|
180 |
+
|
181 |
+
if do_classifier_free_guidance:
|
182 |
+
uncond_tokens = [""] * batch_size if negative_prompt is None else negative_prompt
|
183 |
+
uncond_input = self.tokenizer(
|
184 |
+
uncond_tokens,
|
185 |
+
padding="max_length",
|
186 |
+
max_length=self.tokenizer.model_max_length,
|
187 |
+
truncation=True,
|
188 |
+
return_tensors="pt",
|
189 |
+
)
|
190 |
+
uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(device))[0]
|
191 |
+
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
|
192 |
+
|
193 |
+
return text_embeddings
|
194 |
|
195 |
@torch.no_grad()
|
196 |
def __call__(
|
|
|
200 |
nag_scale: float = 0.0,
|
201 |
nag_tau: float = 3.5,
|
202 |
nag_alpha: float = 0.5,
|
203 |
+
height: Optional[int] = 512,
|
204 |
+
width: Optional[int] = 512,
|
205 |
num_frames: int = 16,
|
206 |
num_inference_steps: int = 50,
|
207 |
guidance_scale: float = 7.5,
|
208 |
negative_prompt: Optional[Union[str, List[str]]] = None,
|
209 |
eta: float = 0.0,
|
210 |
+
generator: Optional[torch.Generator] = None,
|
211 |
latents: Optional[torch.FloatTensor] = None,
|
|
|
|
|
212 |
output_type: Optional[str] = "pil",
|
213 |
return_dict: bool = True,
|
214 |
+
callback: Optional[Callable] = None,
|
215 |
callback_steps: int = 1,
|
216 |
+
**kwargs,
|
|
|
217 |
):
|
218 |
# Use NAG negative prompt if provided
|
219 |
if nag_negative_prompt is not None:
|
220 |
negative_prompt = nag_negative_prompt
|
221 |
+
|
222 |
+
# Setup
|
223 |
+
batch_size = 1 if isinstance(prompt, str) else len(prompt)
|
224 |
+
device = self._execution_device
|
225 |
+
do_classifier_free_guidance = guidance_scale > 1.0
|
226 |
|
227 |
+
# Encode prompt
|
228 |
+
text_embeddings = self._encode_prompt(
|
229 |
+
prompt, device, do_classifier_free_guidance, negative_prompt
|
230 |
+
)
|
231 |
+
|
232 |
+
# Prepare latents
|
233 |
+
num_channels_latents = self.vae.config.latent_channels
|
234 |
+
shape = (
|
235 |
+
batch_size,
|
236 |
+
num_channels_latents,
|
237 |
+
num_frames,
|
238 |
+
height // self.vae_scale_factor,
|
239 |
+
width // self.vae_scale_factor,
|
240 |
+
)
|
241 |
+
|
242 |
+
if latents is None:
|
243 |
+
latents = torch.randn(
|
244 |
+
shape,
|
245 |
+
generator=generator,
|
246 |
+
device=device,
|
247 |
+
dtype=text_embeddings.dtype,
|
248 |
+
)
|
249 |
+
latents = latents * self.scheduler.init_noise_sigma
|
250 |
|
251 |
+
# Set timesteps
|
252 |
+
self.scheduler.set_timesteps(num_inference_steps, device=device)
|
253 |
+
timesteps = self.scheduler.timesteps
|
254 |
+
|
255 |
+
# Denoising loop with NAG
|
256 |
+
for i, t in enumerate(timesteps):
|
257 |
+
# Expand for classifier free guidance
|
258 |
+
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
|
259 |
+
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
|
260 |
+
|
261 |
+
# Predict noise residual
|
262 |
+
noise_pred = self.transformer(
|
263 |
+
latent_model_input,
|
264 |
+
timestep=t,
|
265 |
+
encoder_hidden_states=text_embeddings,
|
266 |
+
)
|
267 |
|
268 |
+
# Apply NAG
|
269 |
+
if nag_scale > 0:
|
270 |
+
# Compute attention-based guidance
|
271 |
+
b, c, f, h, w = noise_pred.shape
|
272 |
+
noise_flat = noise_pred.view(b, c, -1)
|
273 |
|
274 |
+
# Normalize and compute attention
|
275 |
+
noise_norm = F.normalize(noise_flat, dim=-1)
|
276 |
+
attention = F.softmax(noise_norm * nag_tau, dim=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
+
# Apply guidance
|
279 |
+
guidance = attention.mean(dim=-1, keepdim=True) * nag_alpha
|
280 |
+
guidance = guidance.unsqueeze(-1).unsqueeze(-1)
|
281 |
+
noise_pred = noise_pred + nag_scale * guidance * noise_pred
|
282 |
+
|
283 |
+
# Classifier free guidance
|
284 |
+
if do_classifier_free_guidance:
|
285 |
+
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
286 |
+
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
|
287 |
+
|
288 |
+
# Compute previous noisy sample
|
289 |
+
latents = self.scheduler.step(noise_pred, t, latents, eta=eta, generator=generator).prev_sample
|
290 |
|
291 |
+
# Callback
|
292 |
+
if callback is not None and i % callback_steps == 0:
|
293 |
+
callback(i, t, latents)
|
294 |
|
295 |
+
# Decode latents
|
296 |
+
latents = 1 / self.vae.config.scaling_factor * latents
|
297 |
+
video = self.vae.decode(latents).sample
|
298 |
+
video = (video / 2 + 0.5).clamp(0, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
|
300 |
+
# Convert to output format
|
301 |
+
video = video.cpu().float().numpy()
|
302 |
+
video = (video * 255).round().astype("uint8")
|
303 |
+
video = video.transpose(0, 2, 3, 4, 1)
|
304 |
|
305 |
+
frames = []
|
306 |
+
for batch_idx in range(video.shape[0]):
|
307 |
+
batch_frames = [video[batch_idx, i] for i in range(video.shape[1])]
|
308 |
+
frames.append(batch_frames)
|
309 |
+
|
310 |
+
if not return_dict:
|
311 |
+
return (frames,)
|
312 |
+
|
313 |
+
return type('PipelineOutput', (), {'frames': frames})()
|
314 |
+
''')
|
315 |
|
316 |
+
# Now import and run the main application
|
317 |
+
import types
|
318 |
+
import random
|
319 |
+
import spaces
|
320 |
+
import torch
|
321 |
+
import numpy as np
|
322 |
+
from diffusers import AutoencoderKLWan, UniPCMultistepScheduler
|
323 |
+
from diffusers.utils import export_to_video
|
324 |
+
import gradio as gr
|
325 |
+
import tempfile
|
326 |
+
from huggingface_hub import hf_hub_download
|
327 |
+
import logging
|
328 |
+
import gc
|
329 |
+
|
330 |
+
# Import our custom modules
|
331 |
+
from src.pipeline_wan_nag import NAGWanPipeline
|
332 |
+
from src.transformer_wan_nag import NagWanTransformer3DModel
|
333 |
+
|
334 |
+
# MMAudio imports
|
335 |
+
try:
|
336 |
+
import mmaudio
|
337 |
+
except ImportError:
|
338 |
+
os.system("pip install -e .")
|
339 |
+
import mmaudio
|
340 |
+
|
341 |
+
# Set environment variables
|
342 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:512'
|
343 |
+
os.environ['HF_HUB_CACHE'] = '/tmp/hub'
|
344 |
+
|
345 |
+
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate, load_video, make_video,
|
346 |
+
setup_eval_logging)
|
347 |
+
from mmaudio.model.flow_matching import FlowMatching
|
348 |
+
from mmaudio.model.networks import MMAudio, get_my_mmaudio
|
349 |
+
from mmaudio.model.sequence_config import SequenceConfig
|
350 |
+
from mmaudio.model.utils.features_utils import FeaturesUtils
|
351 |
+
|
352 |
+
# Constants
|
353 |
+
MOD_VALUE = 32
|
354 |
+
DEFAULT_DURATION_SECONDS = 4
|
355 |
+
DEFAULT_STEPS = 4
|
356 |
+
DEFAULT_SEED = 2025
|
357 |
+
DEFAULT_H_SLIDER_VALUE = 480
|
358 |
+
DEFAULT_W_SLIDER_VALUE = 832
|
359 |
+
NEW_FORMULA_MAX_AREA = 480.0 * 832.0
|
360 |
+
|
361 |
+
SLIDER_MIN_H, SLIDER_MAX_H = 128, 896
|
362 |
+
SLIDER_MIN_W, SLIDER_MAX_W = 128, 896
|
363 |
+
MAX_SEED = np.iinfo(np.int32).max
|
364 |
+
|
365 |
+
FIXED_FPS = 16
|
366 |
+
MIN_FRAMES_MODEL = 8
|
367 |
+
MAX_FRAMES_MODEL = 129
|
368 |
+
|
369 |
+
DEFAULT_NAG_NEGATIVE_PROMPT = "Static, motionless, still, ugly, bad quality, worst quality, poorly drawn, low resolution, blurry, lack of details"
|
370 |
|
|
|
371 |
MODEL_ID = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
|
372 |
+
SUB_MODEL_ID = "vrgamedevgirl84/Wan14BT2VFusioniX"
|
373 |
+
SUB_MODEL_FILENAME = "Wan14BT2VFusioniX_fp16_.safetensors"
|
374 |
LORA_REPO_ID = "Kijai/WanVideo_comfy"
|
375 |
LORA_FILENAME = "Wan21_CausVid_14B_T2V_lora_rank32.safetensors"
|
376 |
|
377 |
+
# Initialize models
|
378 |
vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
|
379 |
+
wan_path = hf_hub_download(repo_id=SUB_MODEL_ID, filename=SUB_MODEL_FILENAME)
|
380 |
+
transformer = NagWanTransformer3DModel.from_single_file(wan_path, torch_dtype=torch.bfloat16)
|
381 |
pipe = NAGWanPipeline.from_pretrained(
|
382 |
+
MODEL_ID, vae=vae, transformer=transformer, torch_dtype=torch.bfloat16
|
383 |
)
|
384 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=5.0)
|
385 |
pipe.to("cuda")
|
386 |
|
387 |
+
pipe.transformer.__class__.attn_processors = NagWanTransformer3DModel.attn_processors
|
388 |
+
pipe.transformer.__class__.set_attn_processor = NagWanTransformer3DModel.set_attn_processor
|
|
|
|
|
|
|
389 |
|
390 |
+
# Audio model setup
|
391 |
torch.backends.cuda.matmul.allow_tf32 = True
|
392 |
torch.backends.cudnn.allow_tf32 = True
|
393 |
|
|
|
395 |
device = 'cuda'
|
396 |
dtype = torch.bfloat16
|
397 |
|
398 |
+
# Global audio model variables
|
399 |
audio_model = None
|
400 |
audio_net = None
|
401 |
audio_feature_utils = None
|
|
|
428 |
|
429 |
return audio_net, audio_feature_utils, audio_seq_cfg
|
430 |
|
431 |
+
# Helper functions
|
432 |
+
def cleanup_temp_files():
|
433 |
+
temp_dir = tempfile.gettempdir()
|
434 |
+
for filename in os.listdir(temp_dir):
|
435 |
+
filepath = os.path.join(temp_dir, filename)
|
436 |
+
try:
|
437 |
+
if filename.endswith(('.mp4', '.flac', '.wav')):
|
438 |
+
os.remove(filepath)
|
439 |
+
except:
|
440 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
441 |
|
442 |
+
def clear_cache():
|
443 |
+
if torch.cuda.is_available():
|
444 |
+
torch.cuda.empty_cache()
|
445 |
+
torch.cuda.synchronize()
|
446 |
+
gc.collect()
|
447 |
|
448 |
# CSS
|
449 |
+
css = """
|
450 |
+
.container {
|
451 |
+
max-width: 1400px;
|
452 |
+
margin: auto;
|
453 |
+
padding: 20px;
|
|
|
|
|
454 |
}
|
455 |
+
.main-title {
|
456 |
+
text-align: center;
|
457 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
458 |
+
-webkit-background-clip: text;
|
459 |
+
-webkit-text-fill-color: transparent;
|
460 |
+
font-size: 2.5em;
|
461 |
+
font-weight: bold;
|
462 |
+
margin-bottom: 10px;
|
463 |
}
|
464 |
+
.subtitle {
|
465 |
+
text-align: center;
|
466 |
+
color: #6b7280;
|
467 |
+
margin-bottom: 30px;
|
|
|
|
|
|
|
|
|
|
|
468 |
}
|
469 |
+
.prompt-container {
|
470 |
+
background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
|
471 |
+
border-radius: 15px;
|
472 |
+
padding: 20px;
|
473 |
+
margin-bottom: 20px;
|
474 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
475 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
476 |
.generate-btn {
|
477 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
478 |
+
color: white;
|
479 |
+
font-size: 1.2em;
|
480 |
+
font-weight: bold;
|
481 |
+
padding: 15px 30px;
|
482 |
+
border-radius: 10px;
|
483 |
+
border: none;
|
484 |
+
cursor: pointer;
|
485 |
+
transition: all 0.3s ease;
|
486 |
+
width: 100%;
|
487 |
+
margin-top: 20px;
|
488 |
}
|
|
|
489 |
.generate-btn:hover {
|
490 |
+
transform: translateY(-2px);
|
491 |
+
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
|
|
|
|
|
|
|
|
|
|
|
492 |
}
|
493 |
+
.video-output {
|
494 |
+
border-radius: 15px;
|
495 |
+
overflow: hidden;
|
496 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
|
497 |
+
background: #1a1a1a;
|
498 |
+
padding: 10px;
|
499 |
}
|
500 |
+
.settings-panel {
|
501 |
+
background: #f9fafb;
|
502 |
+
border-radius: 15px;
|
503 |
+
padding: 20px;
|
504 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
|
|
|
|
|
|
|
|
|
505 |
}
|
506 |
+
.slider-container {
|
507 |
+
background: white;
|
508 |
+
padding: 15px;
|
509 |
+
border-radius: 10px;
|
510 |
+
margin-bottom: 15px;
|
511 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
512 |
}
|
|
|
|
|
513 |
.info-box {
|
514 |
background: linear-gradient(135deg, #e0e7ff 0%, #c7d2fe 100%);
|
515 |
border-radius: 10px;
|
|
|
517 |
margin: 10px 0;
|
518 |
border-left: 4px solid #667eea;
|
519 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
520 |
"""
|
521 |
|
522 |
+
default_audio_prompt = ""
|
523 |
+
default_audio_negative_prompt = "music"
|
|
|
|
|
|
|
524 |
|
525 |
+
def get_duration(
|
526 |
+
prompt,
|
527 |
+
nag_negative_prompt, nag_scale,
|
528 |
+
height, width, duration_seconds,
|
529 |
+
steps,
|
530 |
+
seed, randomize_seed,
|
531 |
+
audio_mode, audio_prompt, audio_negative_prompt,
|
532 |
+
audio_seed, audio_steps, audio_cfg_strength,
|
533 |
+
):
|
534 |
duration = int(duration_seconds) * int(steps) * 2.25 + 5
|
535 |
if audio_mode == "Enable Audio":
|
536 |
duration += 60
|
|
|
572 |
return video_with_audio_path
|
573 |
|
574 |
@spaces.GPU(duration=get_duration)
|
575 |
+
def generate_video(
|
576 |
+
prompt,
|
577 |
+
nag_negative_prompt, nag_scale,
|
578 |
+
height=DEFAULT_H_SLIDER_VALUE, width=DEFAULT_W_SLIDER_VALUE, duration_seconds=DEFAULT_DURATION_SECONDS,
|
579 |
+
steps=DEFAULT_STEPS,
|
580 |
+
seed=DEFAULT_SEED, randomize_seed=False,
|
581 |
+
audio_mode="Video Only", audio_prompt="", audio_negative_prompt="music",
|
582 |
+
audio_seed=-1, audio_steps=25, audio_cfg_strength=4.5,
|
583 |
+
):
|
|
|
584 |
target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
|
585 |
target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
|
586 |
+
|
587 |
num_frames = np.clip(int(round(int(duration_seconds) * FIXED_FPS) + 1), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
588 |
+
|
589 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
590 |
|
|
|
591 |
with torch.inference_mode():
|
592 |
+
nag_output_frames_list = pipe(
|
593 |
prompt=prompt,
|
594 |
nag_negative_prompt=nag_negative_prompt,
|
595 |
nag_scale=nag_scale,
|
596 |
nag_tau=3.5,
|
597 |
nag_alpha=0.5,
|
598 |
+
height=target_h, width=target_w, num_frames=num_frames,
|
599 |
+
guidance_scale=0.,
|
|
|
|
|
600 |
num_inference_steps=int(steps),
|
601 |
generator=torch.Generator(device="cuda").manual_seed(current_seed)
|
602 |
).frames[0]
|
603 |
|
|
|
604 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
605 |
+
nag_video_path = tmpfile.name
|
606 |
+
export_to_video(nag_output_frames_list, nag_video_path, fps=FIXED_FPS)
|
607 |
+
|
608 |
# Generate audio if enabled
|
609 |
video_with_audio_path = None
|
610 |
if audio_mode == "Enable Audio":
|
|
|
611 |
video_with_audio_path = add_audio_to_video(
|
612 |
+
nag_video_path, duration_seconds,
|
613 |
audio_prompt, audio_negative_prompt,
|
614 |
audio_seed, audio_steps, audio_cfg_strength
|
615 |
)
|
616 |
|
617 |
clear_cache()
|
618 |
cleanup_temp_files()
|
619 |
+
|
620 |
+
return nag_video_path, video_with_audio_path, current_seed
|
621 |
|
622 |
def update_audio_visibility(audio_mode):
|
623 |
return gr.update(visible=(audio_mode == "Enable Audio"))
|
624 |
|
625 |
+
# Build interface
|
626 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
627 |
+
with gr.Column(elem_classes="container"):
|
|
|
|
|
628 |
gr.HTML("""
|
629 |
+
<h1 class="main-title">🎬 NAG Video Generator with Audio</h1>
|
630 |
+
<p class="subtitle">Fast 4-step Wan2.1-T2V-14B with Normalized Attention Guidance + MMAudio</p>
|
|
|
|
|
|
|
631 |
""")
|
632 |
|
633 |
+
gr.HTML("""
|
634 |
+
<div class="info-box">
|
635 |
+
<p>🚀 <strong>Powered by:</strong> Normalized Attention Guidance (NAG) for ultra-fast video generation</p>
|
636 |
+
<p>⚡ <strong>Speed:</strong> Generate videos in just 4-8 steps with high quality</p>
|
637 |
+
<p>🎵 <strong>Audio:</strong> Optional synchronized audio generation with MMAudio</p>
|
638 |
+
</div>
|
639 |
+
""")
|
640 |
+
|
641 |
with gr.Row():
|
642 |
+
with gr.Column(scale=1):
|
643 |
+
with gr.Group(elem_classes="prompt-container"):
|
644 |
+
prompt = gr.Textbox(
|
645 |
+
label="✨ Video Prompt",
|
646 |
+
placeholder="Describe your video scene in detail...",
|
647 |
+
lines=3,
|
648 |
+
elem_classes="prompt-input"
|
|
|
|
|
|
|
|
|
|
|
|
|
649 |
)
|
650 |
+
|
651 |
+
with gr.Accordion("🎨 Advanced Prompt Settings", open=False):
|
652 |
+
nag_negative_prompt = gr.Textbox(
|
653 |
+
label="Negative Prompt",
|
654 |
+
value=DEFAULT_NAG_NEGATIVE_PROMPT,
|
655 |
+
lines=2,
|
656 |
+
)
|
657 |
+
nag_scale = gr.Slider(
|
658 |
+
label="NAG Scale",
|
659 |
+
minimum=1.0,
|
660 |
+
maximum=20.0,
|
661 |
+
step=0.25,
|
662 |
+
value=11.0,
|
663 |
+
info="Higher values = stronger guidance"
|
664 |
+
)
|
665 |
+
|
|
|
|
|
666 |
audio_mode = gr.Radio(
|
667 |
choices=["Video Only", "Enable Audio"],
|
668 |
value="Video Only",
|
|
|
674 |
audio_prompt = gr.Textbox(
|
675 |
label="🎵 Audio Prompt",
|
676 |
value=default_audio_prompt,
|
677 |
+
placeholder="Describe the audio (e.g., 'waves, seagulls', 'footsteps')",
|
678 |
lines=2
|
679 |
)
|
680 |
audio_negative_prompt = gr.Textbox(
|
|
|
703 |
value=4.5,
|
704 |
label="🎯 Audio Guidance"
|
705 |
)
|
706 |
+
|
707 |
+
with gr.Group(elem_classes="settings-panel"):
|
708 |
+
gr.Markdown("### ⚙️ Video Settings")
|
709 |
+
|
710 |
+
with gr.Row():
|
711 |
+
duration_seconds_input = gr.Slider(
|
712 |
+
minimum=1,
|
713 |
+
maximum=8,
|
714 |
+
step=1,
|
715 |
+
value=DEFAULT_DURATION_SECONDS,
|
716 |
+
label="📱 Duration (seconds)",
|
717 |
+
elem_classes="slider-container"
|
718 |
+
)
|
719 |
+
steps_slider = gr.Slider(
|
720 |
+
minimum=1,
|
721 |
+
maximum=8,
|
722 |
+
step=1,
|
723 |
+
value=DEFAULT_STEPS,
|
724 |
+
label="🔄 Inference Steps",
|
725 |
+
elem_classes="slider-container"
|
726 |
+
)
|
727 |
+
|
728 |
with gr.Row():
|
729 |
height_input = gr.Slider(
|
730 |
minimum=SLIDER_MIN_H,
|
731 |
maximum=SLIDER_MAX_H,
|
732 |
step=MOD_VALUE,
|
733 |
value=DEFAULT_H_SLIDER_VALUE,
|
734 |
+
label=f"📐 Height (×{MOD_VALUE})",
|
735 |
+
elem_classes="slider-container"
|
736 |
)
|
737 |
width_input = gr.Slider(
|
738 |
minimum=SLIDER_MIN_W,
|
739 |
maximum=SLIDER_MAX_W,
|
740 |
step=MOD_VALUE,
|
741 |
value=DEFAULT_W_SLIDER_VALUE,
|
742 |
+
label=f"📐 Width (×{MOD_VALUE})",
|
743 |
+
elem_classes="slider-container"
|
744 |
)
|
745 |
+
|
746 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
747 |
seed_input = gr.Slider(
|
748 |
+
label="🌱 Seed",
|
749 |
minimum=0,
|
750 |
maximum=MAX_SEED,
|
751 |
step=1,
|
752 |
value=DEFAULT_SEED,
|
753 |
interactive=True
|
754 |
)
|
755 |
+
randomize_seed_checkbox = gr.Checkbox(
|
756 |
+
label="🎲 Random Seed",
|
757 |
+
value=True,
|
758 |
+
interactive=True
|
759 |
+
)
|
760 |
|
761 |
generate_button = gr.Button(
|
762 |
"🎬 Generate Video",
|
763 |
variant="primary",
|
764 |
+
elem_classes="generate-btn"
|
765 |
)
|
766 |
+
|
767 |
+
with gr.Column(scale=1):
|
768 |
+
nag_video_output = gr.Video(
|
769 |
+
label="Generated Video",
|
770 |
autoplay=True,
|
771 |
+
interactive=False,
|
772 |
+
elem_classes="video-output"
|
773 |
)
|
774 |
video_with_audio_output = gr.Video(
|
775 |
label="🎥 Generated Video with Audio",
|
776 |
autoplay=True,
|
777 |
interactive=False,
|
778 |
+
visible=False,
|
779 |
+
elem_classes="video-output"
|
780 |
)
|
781 |
|
782 |
gr.HTML("""
|
783 |
+
<div style="text-align: center; margin-top: 20px; color: #6b7280;">
|
784 |
<p>💡 Tip: Try different NAG scales for varied artistic effects!</p>
|
785 |
</div>
|
786 |
""")
|
787 |
|
788 |
+
gr.Markdown("### 🎯 Example Prompts")
|
789 |
+
gr.Examples(
|
790 |
+
examples=[
|
791 |
+
["A ginger cat passionately plays electric guitar with intensity and emotion on a stage. The background is shrouded in deep darkness. Spotlights cast dramatic shadows.", DEFAULT_NAG_NEGATIVE_PROMPT, 11,
|
792 |
+
DEFAULT_H_SLIDER_VALUE, DEFAULT_W_SLIDER_VALUE, DEFAULT_DURATION_SECONDS,
|
793 |
+
DEFAULT_STEPS, DEFAULT_SEED, False,
|
794 |
+
"Enable Audio", "electric guitar riffs, cat meowing", default_audio_negative_prompt, -1, 25, 4.5],
|
795 |
+
["A red vintage Porsche convertible flying over a rugged coastal cliff. Monstrous waves violently crashing against the rocks below. A lighthouse stands tall atop the cliff.", DEFAULT_NAG_NEGATIVE_PROMPT, 11,
|
796 |
+
DEFAULT_H_SLIDER_VALUE, DEFAULT_W_SLIDER_VALUE, DEFAULT_DURATION_SECONDS,
|
797 |
+
DEFAULT_STEPS, DEFAULT_SEED, False,
|
798 |
+
"Enable Audio", "car engine roaring, ocean waves crashing, wind", default_audio_negative_prompt, -1, 25, 4.5],
|
799 |
+
["Enormous glowing jellyfish float slowly across a sky filled with soft clouds. Their tentacles shimmer with iridescent light as they drift above a peaceful mountain landscape.", DEFAULT_NAG_NEGATIVE_PROMPT, 11,
|
800 |
+
DEFAULT_H_SLIDER_VALUE, DEFAULT_W_SLIDER_VALUE, DEFAULT_DURATION_SECONDS,
|
801 |
+
DEFAULT_STEPS, DEFAULT_SEED, False,
|
802 |
+
"Video Only", "", default_audio_negative_prompt, -1, 25, 4.5],
|
803 |
+
],
|
804 |
fn=generate_video,
|
805 |
+
inputs=[prompt, nag_negative_prompt, nag_scale,
|
806 |
+
height_input, width_input, duration_seconds_input,
|
807 |
+
steps_slider, seed_input, randomize_seed_checkbox,
|
808 |
+
audio_mode, audio_prompt, audio_negative_prompt,
|
809 |
+
audio_seed, audio_steps, audio_cfg_strength],
|
810 |
+
outputs=[nag_video_output, video_with_audio_output, seed_input],
|
811 |
+
cache_examples="lazy"
|
812 |
)
|
813 |
|
814 |
+
# Event handlers
|
815 |
+
audio_mode.change(
|
816 |
+
fn=update_audio_visibility,
|
817 |
+
inputs=[audio_mode],
|
818 |
+
outputs=[audio_settings, video_with_audio_output]
|
819 |
+
)
|
820 |
+
|
821 |
+
ui_inputs = [
|
822 |
+
prompt,
|
823 |
+
nag_negative_prompt, nag_scale,
|
824 |
+
height_input, width_input, duration_seconds_input,
|
825 |
+
steps_slider,
|
826 |
+
seed_input, randomize_seed_checkbox,
|
827 |
+
audio_mode, audio_prompt, audio_negative_prompt,
|
828 |
+
audio_seed, audio_steps, audio_cfg_strength,
|
829 |
+
]
|
830 |
+
generate_button.click(
|
831 |
+
fn=generate_video,
|
832 |
+
inputs=ui_inputs,
|
833 |
+
outputs=[nag_video_output, video_with_audio_output, seed_input],
|
834 |
+
)
|
|
|
835 |
|
836 |
if __name__ == "__main__":
|
837 |
demo.queue().launch()
|