Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import logging
|
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
import torch
|
| 10 |
import numpy as np
|
|
@@ -15,19 +16,42 @@ from diffusers import AutoModel
|
|
| 15 |
import gradio as gr
|
| 16 |
import tempfile
|
| 17 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
from src.pipeline_wan_nag import NAGWanPipeline
|
| 20 |
from src.transformer_wan_nag import NagWanTransformer3DModel
|
| 21 |
|
| 22 |
# MMAudio imports
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
except ImportError:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate as mmaudio_generate,
|
| 30 |
-
|
| 31 |
from mmaudio.model.flow_matching import FlowMatching
|
| 32 |
from mmaudio.model.networks import MMAudio, get_my_mmaudio
|
| 33 |
from mmaudio.model.sequence_config import SequenceConfig
|
|
@@ -51,7 +75,7 @@ MIN_FRAMES_MODEL = 8
|
|
| 51 |
MAX_FRAMES_MODEL = 129
|
| 52 |
|
| 53 |
DEFAULT_NAG_NEGATIVE_PROMPT = "Static, motionless, still, ugly, bad quality, worst quality, poorly drawn, low resolution, blurry, lack of details"
|
| 54 |
-
DEFAULT_AUDIO_NEGATIVE_PROMPT = "music"
|
| 55 |
|
| 56 |
# NAG Model Settings
|
| 57 |
MODEL_ID = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
|
|
@@ -72,421 +96,500 @@ setup_eval_logging()
|
|
| 72 |
|
| 73 |
# Initialize NAG Video Model
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
except Exception as e:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
# Initialize MMAudio Model
|
| 93 |
def get_mmaudio_model() -> tuple[MMAudio, FeaturesUtils, SequenceConfig]:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
try:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
except Exception as e:
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Audio generation function
|
| 118 |
@torch.inference_mode()
|
| 119 |
-
def add_audio_to_video(video_path, prompt, audio_negative_prompt, audio_steps, audio_cfg_strength, duration):
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
# Combined generation function
|
| 159 |
def get_duration(prompt, nag_negative_prompt, nag_scale, height, width, duration_seconds,
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
|
| 167 |
@spaces.GPU(duration=get_duration)
|
| 168 |
def generate_video_with_audio(
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
| 176 |
):
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
| 236 |
|
| 237 |
# Example generation function - simplified
|
| 238 |
def set_example(prompt, nag_negative_prompt, nag_scale):
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
| 255 |
|
| 256 |
# Examples with audio descriptions
|
| 257 |
examples = [
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
]
|
| 262 |
|
| 263 |
# CSS styling - Fixed for better layout
|
| 264 |
css = """
|
| 265 |
/* Right column - video output */
|
| 266 |
.video-output {
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
}
|
| 274 |
|
| 275 |
/* Ensure video container is responsive */
|
| 276 |
.video-output video {
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
}
|
| 283 |
|
| 284 |
/* Remove any overlay or background from video container */
|
| 285 |
.video-output > div {
|
| 286 |
-
|
| 287 |
-
|
| 288 |
}
|
| 289 |
|
| 290 |
/* Remove gradio's default video player overlay */
|
| 291 |
.video-output .wrap {
|
| 292 |
-
|
| 293 |
}
|
| 294 |
|
| 295 |
/* Ensure no gray overlay on video controls */
|
| 296 |
.video-output video::-webkit-media-controls-enclosure {
|
| 297 |
-
|
| 298 |
}
|
| 299 |
"""
|
| 300 |
|
| 301 |
# Gradio interface - Fixed structure
|
| 302 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
if __name__ == "__main__":
|
| 492 |
-
|
|
|
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
| 7 |
from datetime import datetime
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
import torch
|
| 11 |
import numpy as np
|
|
|
|
| 16 |
import gradio as gr
|
| 17 |
import tempfile
|
| 18 |
from huggingface_hub import hf_hub_download
|
| 19 |
+
import traceback
|
| 20 |
+
|
| 21 |
+
# Patch for scaled_dot_product_attention to fix enable_gqa issue
|
| 22 |
+
import torch.nn.functional as F
|
| 23 |
+
|
| 24 |
+
original_sdpa = F.scaled_dot_product_attention
|
| 25 |
+
|
| 26 |
+
def patched_scaled_dot_product_attention(query, key, value, attn_mask=None, dropout_p=0.0, is_causal=False, scale=None, enable_gqa=None):
|
| 27 |
+
# enable_gqa νλΌλ―Έν°λ₯Ό 무μνκ³ λλ¨Έμ§ νλΌλ―Έν°λ§ μ λ¬
|
| 28 |
+
kwargs = {}
|
| 29 |
+
if attn_mask is not None:
|
| 30 |
+
kwargs['attn_mask'] = attn_mask
|
| 31 |
+
if dropout_p != 0.0:
|
| 32 |
+
kwargs['dropout_p'] = dropout_p
|
| 33 |
+
if is_causal:
|
| 34 |
+
kwargs['is_causal'] = is_causal
|
| 35 |
+
if scale is not None:
|
| 36 |
+
kwargs['scale'] = scale
|
| 37 |
+
|
| 38 |
+
return original_sdpa(query, key, value, **kwargs)
|
| 39 |
+
|
| 40 |
+
# ν¨μΉ μ μ©
|
| 41 |
+
F.scaled_dot_product_attention = patched_scaled_dot_product_attention
|
| 42 |
|
| 43 |
from src.pipeline_wan_nag import NAGWanPipeline
|
| 44 |
from src.transformer_wan_nag import NagWanTransformer3DModel
|
| 45 |
|
| 46 |
# MMAudio imports
|
| 47 |
try:
|
| 48 |
+
import mmaudio
|
| 49 |
except ImportError:
|
| 50 |
+
os.system("pip install -e .")
|
| 51 |
+
import mmaudio
|
| 52 |
|
| 53 |
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate as mmaudio_generate,
|
| 54 |
+
load_video, make_video, setup_eval_logging)
|
| 55 |
from mmaudio.model.flow_matching import FlowMatching
|
| 56 |
from mmaudio.model.networks import MMAudio, get_my_mmaudio
|
| 57 |
from mmaudio.model.sequence_config import SequenceConfig
|
|
|
|
| 75 |
MAX_FRAMES_MODEL = 129
|
| 76 |
|
| 77 |
DEFAULT_NAG_NEGATIVE_PROMPT = "Static, motionless, still, ugly, bad quality, worst quality, poorly drawn, low resolution, blurry, lack of details"
|
| 78 |
+
DEFAULT_AUDIO_NEGATIVE_PROMPT = "music, speech, voice, singing, narration"
|
| 79 |
|
| 80 |
# NAG Model Settings
|
| 81 |
MODEL_ID = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
|
|
|
|
| 96 |
|
| 97 |
# Initialize NAG Video Model
|
| 98 |
try:
|
| 99 |
+
vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
|
| 100 |
+
wan_path = hf_hub_download(repo_id=SUB_MODEL_ID, filename=SUB_MODEL_FILENAME)
|
| 101 |
+
transformer = NagWanTransformer3DModel.from_single_file(wan_path, torch_dtype=torch.bfloat16)
|
| 102 |
+
pipe = NAGWanPipeline.from_pretrained(
|
| 103 |
+
MODEL_ID, vae=vae, transformer=transformer, torch_dtype=torch.bfloat16
|
| 104 |
+
)
|
| 105 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=5.0)
|
| 106 |
+
pipe.to("cuda")
|
| 107 |
+
|
| 108 |
+
pipe.transformer.__class__.attn_processors = NagWanTransformer3DModel.attn_processors
|
| 109 |
+
pipe.transformer.__class__.set_attn_processor = NagWanTransformer3DModel.set_attn_processor
|
| 110 |
+
pipe.transformer.__class__.forward = NagWanTransformer3DModel.forward
|
| 111 |
+
print("NAG Video Model loaded successfully!")
|
| 112 |
except Exception as e:
|
| 113 |
+
print(f"Error loading NAG Video Model: {e}")
|
| 114 |
+
pipe = None
|
| 115 |
|
| 116 |
# Initialize MMAudio Model
|
| 117 |
def get_mmaudio_model() -> tuple[MMAudio, FeaturesUtils, SequenceConfig]:
|
| 118 |
+
seq_cfg = audio_model_config.seq_cfg
|
| 119 |
+
|
| 120 |
+
net: MMAudio = get_my_mmaudio(audio_model_config.model_name).to(device, dtype).eval()
|
| 121 |
+
net.load_weights(torch.load(audio_model_config.model_path, map_location=device, weights_only=True))
|
| 122 |
+
log.info(f'Loaded MMAudio weights from {audio_model_config.model_path}')
|
| 123 |
+
|
| 124 |
+
feature_utils = FeaturesUtils(tod_vae_ckpt=audio_model_config.vae_path,
|
| 125 |
+
synchformer_ckpt=audio_model_config.synchformer_ckpt,
|
| 126 |
+
enable_conditions=True,
|
| 127 |
+
mode=audio_model_config.mode,
|
| 128 |
+
bigvgan_vocoder_ckpt=audio_model_config.bigvgan_16k_path,
|
| 129 |
+
need_vae_encoder=False)
|
| 130 |
+
feature_utils = feature_utils.to(device, dtype).eval()
|
| 131 |
+
|
| 132 |
+
return net, feature_utils, seq_cfg
|
| 133 |
|
| 134 |
try:
|
| 135 |
+
audio_net, audio_feature_utils, audio_seq_cfg = get_mmaudio_model()
|
| 136 |
+
print("MMAudio Model loaded successfully!")
|
| 137 |
except Exception as e:
|
| 138 |
+
print(f"Error loading MMAudio Model: {e}")
|
| 139 |
+
audio_net = None
|
| 140 |
+
|
| 141 |
+
# λΉλμ€ ν둬ννΈλ₯Ό μ€λμ€ ν둬ννΈλ‘ λ³ννλ ν¨μ
|
| 142 |
+
def extract_audio_description(video_prompt):
|
| 143 |
+
"""λΉλμ€ ν둬ννΈμμ μ€λμ€ κ΄λ ¨ μ€λͺ
μΆμΆ/λ³ν"""
|
| 144 |
+
|
| 145 |
+
# ν€μλ λ§€ν
|
| 146 |
+
audio_keywords = {
|
| 147 |
+
'car': 'car engine sound, vehicle noise',
|
| 148 |
+
'porsche': 'sports car engine roar, exhaust sound',
|
| 149 |
+
'guitar': 'electric guitar playing, guitar music',
|
| 150 |
+
'concert': 'crowd cheering, live music, applause',
|
| 151 |
+
'motorcycle': 'motorcycle engine sound, motor rumble',
|
| 152 |
+
'highway': 'traffic noise, road ambience',
|
| 153 |
+
'rain': 'rain sounds, water drops',
|
| 154 |
+
'wind': 'wind blowing sound',
|
| 155 |
+
'ocean': 'ocean waves, water sounds',
|
| 156 |
+
'city': 'urban ambience, city traffic sounds',
|
| 157 |
+
'singer': 'singing voice, vocals',
|
| 158 |
+
'crowd': 'crowd noise, people talking',
|
| 159 |
+
'flames': 'fire crackling sound',
|
| 160 |
+
'pyro': 'fire whoosh, flame burst sound',
|
| 161 |
+
'explosion': 'explosion sound, blast',
|
| 162 |
+
'countryside': 'nature ambience, birds chirping',
|
| 163 |
+
'wheat fields': 'wind through grass, rural ambience',
|
| 164 |
+
'engine': 'motor sound, mechanical noise',
|
| 165 |
+
'flat-six engine': 'sports car engine sound',
|
| 166 |
+
'roaring': 'loud engine roar',
|
| 167 |
+
'thunderous': 'loud booming sound',
|
| 168 |
+
'child': 'children playing sounds',
|
| 169 |
+
'running': 'footsteps sound',
|
| 170 |
+
'woman': 'ambient sounds',
|
| 171 |
+
'phone': 'subtle electronic ambience',
|
| 172 |
+
'advertisement': 'modern ambient sounds'
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
# κ°λ¨ν ν€μλ κΈ°λ° λ³ν
|
| 176 |
+
audio_descriptions = []
|
| 177 |
+
lower_prompt = video_prompt.lower()
|
| 178 |
+
|
| 179 |
+
for key, value in audio_keywords.items():
|
| 180 |
+
if key in lower_prompt:
|
| 181 |
+
audio_descriptions.append(value)
|
| 182 |
+
|
| 183 |
+
# κΈ°λ³Έκ° μ€μ
|
| 184 |
+
if not audio_descriptions:
|
| 185 |
+
# ν둬ννΈμ λͺ
μμ μΈ μ€λμ€ μ€λͺ
μ΄ μλμ§ νμΈ
|
| 186 |
+
if 'sound' in lower_prompt or 'audio' in lower_prompt or 'noise' in lower_prompt:
|
| 187 |
+
# ν둬ννΈμμ μ€λμ€ κ΄λ ¨ λΆλΆλ§ μΆμΆ
|
| 188 |
+
audio_pattern = r'([^.]*(?:sound|audio|noise|music|voice|roar|rumble)[^.]*)'
|
| 189 |
+
matches = re.findall(audio_pattern, lower_prompt, re.IGNORECASE)
|
| 190 |
+
if matches:
|
| 191 |
+
return ', '.join(matches)
|
| 192 |
+
|
| 193 |
+
# κΈ°λ³Έ ambient sound
|
| 194 |
+
return "ambient environmental sounds matching the scene"
|
| 195 |
+
|
| 196 |
+
return ', '.join(audio_descriptions)
|
| 197 |
|
| 198 |
# Audio generation function
|
| 199 |
@torch.inference_mode()
|
| 200 |
+
def add_audio_to_video(video_path, prompt, audio_custom_prompt, audio_negative_prompt, audio_steps, audio_cfg_strength, duration):
|
| 201 |
+
"""Generate and add audio to video using MMAudio"""
|
| 202 |
+
if audio_net is None:
|
| 203 |
+
print("MMAudio model not loaded, returning video without audio")
|
| 204 |
+
return video_path
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
# 컀μ€ν
μ€λμ€ ν둬ννΈκ° μμΌλ©΄ μ¬μ©, μμΌλ©΄ λΉλμ€ ν둬ννΈμμ μΆμΆ
|
| 208 |
+
if audio_custom_prompt and audio_custom_prompt.strip():
|
| 209 |
+
audio_prompt = audio_custom_prompt.strip()
|
| 210 |
+
else:
|
| 211 |
+
audio_prompt = extract_audio_description(prompt)
|
| 212 |
+
|
| 213 |
+
print(f"Original prompt: {prompt}")
|
| 214 |
+
print(f"Audio prompt: {audio_prompt}")
|
| 215 |
+
|
| 216 |
+
rng = torch.Generator(device=device)
|
| 217 |
+
rng.manual_seed(random.randint(0, 2**32 - 1)) # λ λͺ
νν λλ€ μλ
|
| 218 |
+
fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=audio_steps)
|
| 219 |
+
|
| 220 |
+
video_info = load_video(video_path, duration)
|
| 221 |
+
clip_frames = video_info.clip_frames
|
| 222 |
+
sync_frames = video_info.sync_frames
|
| 223 |
+
duration = video_info.duration_sec
|
| 224 |
+
clip_frames = clip_frames.unsqueeze(0)
|
| 225 |
+
sync_frames = sync_frames.unsqueeze(0)
|
| 226 |
+
audio_seq_cfg.duration = duration
|
| 227 |
+
audio_net.update_seq_lengths(audio_seq_cfg.latent_seq_len, audio_seq_cfg.clip_seq_len, audio_seq_cfg.sync_seq_len)
|
| 228 |
+
|
| 229 |
+
# ν₯μλ λ€κ±°ν°λΈ ν둬ννΈ
|
| 230 |
+
enhanced_negative = f"{audio_negative_prompt}, distortion, static noise, silence, random beeps"
|
| 231 |
+
|
| 232 |
+
audios = mmaudio_generate(clip_frames,
|
| 233 |
+
sync_frames, [audio_prompt], # λ³νλ μ€λμ€ ν둬ννΈ μ¬μ©
|
| 234 |
+
negative_text=[enhanced_negative],
|
| 235 |
+
feature_utils=audio_feature_utils,
|
| 236 |
+
net=audio_net,
|
| 237 |
+
fm=fm,
|
| 238 |
+
rng=rng,
|
| 239 |
+
cfg_strength=audio_cfg_strength)
|
| 240 |
+
audio = audios.float().cpu()[0]
|
| 241 |
+
|
| 242 |
+
# Create video with audio
|
| 243 |
+
video_with_audio_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
|
| 244 |
+
make_video(video_info, video_with_audio_path, audio, sampling_rate=audio_seq_cfg.sampling_rate)
|
| 245 |
+
|
| 246 |
+
return video_with_audio_path
|
| 247 |
+
except Exception as e:
|
| 248 |
+
print(f"Error in audio generation: {e}")
|
| 249 |
+
traceback.print_exc()
|
| 250 |
+
return video_path
|
| 251 |
|
| 252 |
# Combined generation function
|
| 253 |
def get_duration(prompt, nag_negative_prompt, nag_scale, height, width, duration_seconds,
|
| 254 |
+
steps, seed, randomize_seed, enable_audio, audio_custom_prompt,
|
| 255 |
+
audio_negative_prompt, audio_steps, audio_cfg_strength):
|
| 256 |
+
# Calculate total duration including audio processing if enabled
|
| 257 |
+
video_duration = int(duration_seconds) * int(steps) * 2.25 + 5
|
| 258 |
+
audio_duration = 30 if enable_audio else 0 # Additional time for audio processing
|
| 259 |
+
return video_duration + audio_duration
|
| 260 |
|
| 261 |
@spaces.GPU(duration=get_duration)
|
| 262 |
def generate_video_with_audio(
|
| 263 |
+
prompt,
|
| 264 |
+
nag_negative_prompt, nag_scale,
|
| 265 |
+
height=DEFAULT_H_SLIDER_VALUE, width=DEFAULT_W_SLIDER_VALUE, duration_seconds=DEFAULT_DURATION_SECONDS,
|
| 266 |
+
steps=DEFAULT_STEPS,
|
| 267 |
+
seed=DEFAULT_SEED, randomize_seed=False,
|
| 268 |
+
enable_audio=True, audio_custom_prompt="",
|
| 269 |
+
audio_negative_prompt=DEFAULT_AUDIO_NEGATIVE_PROMPT,
|
| 270 |
+
audio_steps=30, audio_cfg_strength=4.5,
|
| 271 |
):
|
| 272 |
+
if pipe is None:
|
| 273 |
+
return None, DEFAULT_SEED
|
| 274 |
+
|
| 275 |
+
try:
|
| 276 |
+
# Generate video first
|
| 277 |
+
target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
|
| 278 |
+
target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
|
| 279 |
+
|
| 280 |
+
num_frames = np.clip(int(round(int(duration_seconds) * FIXED_FPS) + 1), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
| 281 |
+
|
| 282 |
+
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 283 |
+
|
| 284 |
+
print(f"Generating video with: prompt='{prompt}', resolution={target_w}x{target_h}, frames={num_frames}")
|
| 285 |
+
|
| 286 |
+
with torch.inference_mode():
|
| 287 |
+
nag_output_frames_list = pipe(
|
| 288 |
+
prompt=prompt,
|
| 289 |
+
nag_negative_prompt=nag_negative_prompt,
|
| 290 |
+
nag_scale=nag_scale,
|
| 291 |
+
nag_tau=3.5,
|
| 292 |
+
nag_alpha=0.5,
|
| 293 |
+
height=target_h, width=target_w, num_frames=num_frames,
|
| 294 |
+
guidance_scale=0.,
|
| 295 |
+
num_inference_steps=int(steps),
|
| 296 |
+
generator=torch.Generator(device="cuda").manual_seed(current_seed)
|
| 297 |
+
).frames[0]
|
| 298 |
+
|
| 299 |
+
# Save initial video without audio
|
| 300 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 301 |
+
temp_video_path = tmpfile.name
|
| 302 |
+
export_to_video(nag_output_frames_list, temp_video_path, fps=FIXED_FPS)
|
| 303 |
+
print(f"Video saved to: {temp_video_path}")
|
| 304 |
+
|
| 305 |
+
# Add audio if enabled
|
| 306 |
+
if enable_audio:
|
| 307 |
+
try:
|
| 308 |
+
print("Adding audio to video...")
|
| 309 |
+
final_video_path = add_audio_to_video(
|
| 310 |
+
temp_video_path,
|
| 311 |
+
prompt,
|
| 312 |
+
audio_custom_prompt,
|
| 313 |
+
audio_negative_prompt,
|
| 314 |
+
audio_steps,
|
| 315 |
+
audio_cfg_strength,
|
| 316 |
+
duration_seconds
|
| 317 |
+
)
|
| 318 |
+
# Clean up temp video
|
| 319 |
+
if os.path.exists(temp_video_path) and final_video_path != temp_video_path:
|
| 320 |
+
os.remove(temp_video_path)
|
| 321 |
+
print(f"Final video with audio: {final_video_path}")
|
| 322 |
+
except Exception as e:
|
| 323 |
+
log.error(f"Audio generation failed: {e}")
|
| 324 |
+
final_video_path = temp_video_path
|
| 325 |
+
else:
|
| 326 |
+
final_video_path = temp_video_path
|
| 327 |
+
|
| 328 |
+
return final_video_path, current_seed
|
| 329 |
+
except Exception as e:
|
| 330 |
+
print(f"Error in video generation: {e}")
|
| 331 |
+
return None, current_seed
|
| 332 |
|
| 333 |
# Example generation function - simplified
|
| 334 |
def set_example(prompt, nag_negative_prompt, nag_scale):
|
| 335 |
+
"""Set example values in the UI without triggering generation"""
|
| 336 |
+
return (
|
| 337 |
+
prompt,
|
| 338 |
+
nag_negative_prompt,
|
| 339 |
+
nag_scale,
|
| 340 |
+
DEFAULT_H_SLIDER_VALUE,
|
| 341 |
+
DEFAULT_W_SLIDER_VALUE,
|
| 342 |
+
DEFAULT_DURATION_SECONDS,
|
| 343 |
+
DEFAULT_STEPS,
|
| 344 |
+
DEFAULT_SEED,
|
| 345 |
+
True, # randomize_seed
|
| 346 |
+
True, # enable_audio
|
| 347 |
+
"", # audio_custom_prompt
|
| 348 |
+
DEFAULT_AUDIO_NEGATIVE_PROMPT,
|
| 349 |
+
30, # audio_steps
|
| 350 |
+
4.5 # audio_cfg_strength
|
| 351 |
+
)
|
| 352 |
|
| 353 |
# Examples with audio descriptions
|
| 354 |
examples = [
|
| 355 |
+
["Midnight highway outside a neon-lit city. A black 1973 Porsche 911 Carrera RS speeds at 120 km/h. Inside, a stylish singer-guitarist sings while driving, vintage sunburst guitar on the passenger seat. Sodium streetlights streak over the hood; RGB panels shift magenta to blue on the driver. Camera: drone dive, Russian-arm low wheel shot, interior gimbal, FPV barrel roll, overhead spiral. Neo-noir palette, rain-slick asphalt reflections, roaring flat-six engine blended with live guitar.", DEFAULT_NAG_NEGATIVE_PROMPT, 11],
|
| 356 |
+
["Arena rock concert packed with 20 000 fans. A flamboyant lead guitarist in leather jacket and mirrored aviators shreds a cherry-red Flying V on a thrust stage. Pyro flames shoot up on every downbeat, COβ jets burst behind. Moving-head spotlights swirl teal and amber, follow-spots rim-light the guitarist's hair. Steadicam 360-orbit, crane shot rising over crowd, ultra-slow-motion pick attack at 1 000 fps. Film-grain teal-orange grade, thunderous crowd roar mixes with screaming guitar solo.", DEFAULT_NAG_NEGATIVE_PROMPT, 11],
|
| 357 |
+
["Golden-hour countryside road winding through rolling wheat fields. A man and woman ride a vintage cafΓ©-racer motorcycle, hair and scarf fluttering in the warm breeze. Drone chase shot reveals endless patchwork farmland; low slider along rear wheel captures dust trail. Sun-flare back-lights the riders, lens blooms on highlights. Soft acoustic rock underscore; engine rumble mixed at β8 dB. Warm pastel color grade, gentle film-grain for nostalgic vibe.", DEFAULT_NAG_NEGATIVE_PROMPT, 11],
|
| 358 |
]
|
| 359 |
|
| 360 |
# CSS styling - Fixed for better layout
|
| 361 |
css = """
|
| 362 |
/* Right column - video output */
|
| 363 |
.video-output {
|
| 364 |
+
border-radius: 15px;
|
| 365 |
+
overflow: hidden;
|
| 366 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
|
| 367 |
+
width: 100% !important;
|
| 368 |
+
height: auto !important;
|
| 369 |
+
min-height: 400px;
|
| 370 |
}
|
| 371 |
|
| 372 |
/* Ensure video container is responsive */
|
| 373 |
.video-output video {
|
| 374 |
+
width: 100% !important;
|
| 375 |
+
height: auto !important;
|
| 376 |
+
max-height: 600px;
|
| 377 |
+
object-fit: contain;
|
| 378 |
+
display: block;
|
| 379 |
}
|
| 380 |
|
| 381 |
/* Remove any overlay or background from video container */
|
| 382 |
.video-output > div {
|
| 383 |
+
background: transparent !important;
|
| 384 |
+
padding: 0 !important;
|
| 385 |
}
|
| 386 |
|
| 387 |
/* Remove gradio's default video player overlay */
|
| 388 |
.video-output .wrap {
|
| 389 |
+
background: transparent !important;
|
| 390 |
}
|
| 391 |
|
| 392 |
/* Ensure no gray overlay on video controls */
|
| 393 |
.video-output video::-webkit-media-controls-enclosure {
|
| 394 |
+
background: transparent;
|
| 395 |
}
|
| 396 |
"""
|
| 397 |
|
| 398 |
# Gradio interface - Fixed structure
|
| 399 |
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 400 |
+
gr.HTML("""
|
| 401 |
+
<div class="container">
|
| 402 |
+
<h1 class="main-title">π¬ VEO3 Free</h1>
|
| 403 |
+
<p class="subtitle">Wan2.1-T2V-14B + Fast 4-step with NAG + Automatic Audio Generation</p>
|
| 404 |
+
</div>
|
| 405 |
+
""")
|
| 406 |
+
|
| 407 |
+
gr.HTML("""
|
| 408 |
+
<div class='container' style='display:flex; justify-content:center; gap:12px; margin-bottom: 20px;'>
|
| 409 |
+
<a href="https://huggingface.co/spaces/openfree/Best-AI" target="_blank">
|
| 410 |
+
<img src="https://img.shields.io/static/v1?label=OpenFree&message=BEST%20AI%20Services&color=%230000ff&labelColor=%23000080&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="OpenFree badge">
|
| 411 |
+
</a>
|
| 412 |
+
|
| 413 |
+
<a href="https://discord.gg/openfreeai" target="_blank">
|
| 414 |
+
<img src="https://img.shields.io/static/v1?label=Discord&message=Openfree%20AI&color=%230000ff&labelColor=%23800080&logo=discord&logoColor=white&style=for-the-badge" alt="Discord badge">
|
| 415 |
+
</a>
|
| 416 |
+
</div>
|
| 417 |
+
""")
|
| 418 |
+
|
| 419 |
+
with gr.Row(equal_height=True):
|
| 420 |
+
with gr.Column(scale=5):
|
| 421 |
+
with gr.Group(elem_classes="prompt-container"):
|
| 422 |
+
prompt = gr.Textbox(
|
| 423 |
+
label="β¨ Video Prompt (also used for audio generation)",
|
| 424 |
+
placeholder="Describe your video scene in detail...",
|
| 425 |
+
lines=3,
|
| 426 |
+
elem_classes="prompt-input"
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
with gr.Accordion("π¨ Advanced Video Settings", open=False):
|
| 430 |
+
nag_negative_prompt = gr.Textbox(
|
| 431 |
+
label="Video Negative Prompt",
|
| 432 |
+
value=DEFAULT_NAG_NEGATIVE_PROMPT,
|
| 433 |
+
lines=2,
|
| 434 |
+
)
|
| 435 |
+
nag_scale = gr.Slider(
|
| 436 |
+
label="NAG Scale",
|
| 437 |
+
minimum=1.0,
|
| 438 |
+
maximum=20.0,
|
| 439 |
+
step=0.25,
|
| 440 |
+
value=11.0,
|
| 441 |
+
info="Higher values = stronger guidance"
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
with gr.Group(elem_classes="settings-panel"):
|
| 445 |
+
gr.Markdown("### βοΈ Video Settings")
|
| 446 |
+
|
| 447 |
+
with gr.Row():
|
| 448 |
+
duration_seconds_input = gr.Slider(
|
| 449 |
+
minimum=1,
|
| 450 |
+
maximum=8,
|
| 451 |
+
step=1,
|
| 452 |
+
value=DEFAULT_DURATION_SECONDS,
|
| 453 |
+
label="π± Duration (seconds)",
|
| 454 |
+
elem_classes="slider-container"
|
| 455 |
+
)
|
| 456 |
+
steps_slider = gr.Slider(
|
| 457 |
+
minimum=1,
|
| 458 |
+
maximum=8,
|
| 459 |
+
step=1,
|
| 460 |
+
value=DEFAULT_STEPS,
|
| 461 |
+
label="π Inference Steps",
|
| 462 |
+
elem_classes="slider-container"
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
with gr.Row():
|
| 466 |
+
height_input = gr.Slider(
|
| 467 |
+
minimum=SLIDER_MIN_H,
|
| 468 |
+
maximum=SLIDER_MAX_H,
|
| 469 |
+
step=MOD_VALUE,
|
| 470 |
+
value=DEFAULT_H_SLIDER_VALUE,
|
| 471 |
+
label=f"π Height (Γ{MOD_VALUE})",
|
| 472 |
+
elem_classes="slider-container"
|
| 473 |
+
)
|
| 474 |
+
width_input = gr.Slider(
|
| 475 |
+
minimum=SLIDER_MIN_W,
|
| 476 |
+
maximum=SLIDER_MAX_W,
|
| 477 |
+
step=MOD_VALUE,
|
| 478 |
+
value=DEFAULT_W_SLIDER_VALUE,
|
| 479 |
+
label=f"π Width (Γ{MOD_VALUE})",
|
| 480 |
+
elem_classes="slider-container"
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
with gr.Row():
|
| 484 |
+
seed_input = gr.Slider(
|
| 485 |
+
label="π± Seed",
|
| 486 |
+
minimum=0,
|
| 487 |
+
maximum=MAX_SEED,
|
| 488 |
+
step=1,
|
| 489 |
+
value=DEFAULT_SEED,
|
| 490 |
+
interactive=True
|
| 491 |
+
)
|
| 492 |
+
randomize_seed_checkbox = gr.Checkbox(
|
| 493 |
+
label="π² Random Seed",
|
| 494 |
+
value=True,
|
| 495 |
+
interactive=True
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
with gr.Group(elem_classes="audio-settings"):
|
| 499 |
+
gr.Markdown("### π΅ Audio Generation Settings")
|
| 500 |
+
|
| 501 |
+
enable_audio = gr.Checkbox(
|
| 502 |
+
label="π Enable Automatic Audio Generation",
|
| 503 |
+
value=True,
|
| 504 |
+
interactive=True
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
with gr.Column(visible=True) as audio_settings_group:
|
| 508 |
+
audio_custom_prompt = gr.Textbox(
|
| 509 |
+
label="Custom Audio Prompt (Optional)",
|
| 510 |
+
placeholder="Leave empty to auto-generate from video prompt, or specify custom audio description (e.g., 'car engine sound, traffic noise')",
|
| 511 |
+
value="",
|
| 512 |
+
)
|
| 513 |
+
audio_negative_prompt = gr.Textbox(
|
| 514 |
+
label="Audio Negative Prompt",
|
| 515 |
+
value=DEFAULT_AUDIO_NEGATIVE_PROMPT,
|
| 516 |
+
placeholder="Elements to avoid in audio",
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
with gr.Row():
|
| 520 |
+
audio_steps = gr.Slider(
|
| 521 |
+
minimum=10,
|
| 522 |
+
maximum=50,
|
| 523 |
+
step=5,
|
| 524 |
+
value=30,
|
| 525 |
+
label="ποΈ Audio Steps",
|
| 526 |
+
info="More steps = better quality"
|
| 527 |
+
)
|
| 528 |
+
audio_cfg_strength = gr.Slider(
|
| 529 |
+
minimum=1.0,
|
| 530 |
+
maximum=10.0,
|
| 531 |
+
step=0.5,
|
| 532 |
+
value=4.5,
|
| 533 |
+
label="ποΈ Audio Guidance",
|
| 534 |
+
info="Strength of prompt guidance"
|
| 535 |
+
)
|
| 536 |
+
|
| 537 |
+
# Toggle audio settings visibility
|
| 538 |
+
enable_audio.change(
|
| 539 |
+
fn=lambda x: gr.update(visible=x),
|
| 540 |
+
inputs=[enable_audio],
|
| 541 |
+
outputs=[audio_settings_group]
|
| 542 |
+
)
|
| 543 |
+
|
| 544 |
+
generate_button = gr.Button(
|
| 545 |
+
"π¬ Generate Video with Audio",
|
| 546 |
+
variant="primary",
|
| 547 |
+
elem_classes="generate-btn"
|
| 548 |
+
)
|
| 549 |
+
|
| 550 |
+
with gr.Column(scale=5):
|
| 551 |
+
video_output = gr.Video(
|
| 552 |
+
label="Generated Video with Audio",
|
| 553 |
+
autoplay=True,
|
| 554 |
+
interactive=False,
|
| 555 |
+
elem_classes="video-output",
|
| 556 |
+
height=600
|
| 557 |
+
)
|
| 558 |
+
|
| 559 |
+
gr.HTML("""
|
| 560 |
+
<div style="text-align: center; margin-top: 20px; color: #6b7280;">
|
| 561 |
+
<p>π‘ Tip: For better audio, use Custom Audio Prompt with sound descriptions!</p>
|
| 562 |
+
<p>π§ Examples: "car engine sound", "crowd cheering", "nature ambience"</p>
|
| 563 |
+
</div>
|
| 564 |
+
""")
|
| 565 |
+
|
| 566 |
+
# Examples section moved outside of columns
|
| 567 |
+
with gr.Row():
|
| 568 |
+
gr.Markdown("### π― Example Prompts")
|
| 569 |
+
|
| 570 |
+
gr.Examples(
|
| 571 |
+
examples=examples,
|
| 572 |
+
inputs=[prompt, nag_negative_prompt, nag_scale],
|
| 573 |
+
outputs=None, # Don't connect outputs to avoid index issues
|
| 574 |
+
cache_examples=False
|
| 575 |
+
)
|
| 576 |
+
|
| 577 |
+
# Connect UI elements
|
| 578 |
+
ui_inputs = [
|
| 579 |
+
prompt,
|
| 580 |
+
nag_negative_prompt, nag_scale,
|
| 581 |
+
height_input, width_input, duration_seconds_input,
|
| 582 |
+
steps_slider,
|
| 583 |
+
seed_input, randomize_seed_checkbox,
|
| 584 |
+
enable_audio, audio_custom_prompt, audio_negative_prompt,
|
| 585 |
+
audio_steps, audio_cfg_strength,
|
| 586 |
+
]
|
| 587 |
+
|
| 588 |
+
generate_button.click(
|
| 589 |
+
fn=generate_video_with_audio,
|
| 590 |
+
inputs=ui_inputs,
|
| 591 |
+
outputs=[video_output, seed_input],
|
| 592 |
+
)
|
| 593 |
|
| 594 |
if __name__ == "__main__":
|
| 595 |
+
demo.queue().launch( allowed_paths=["/","/tmp","/tmp/gradio"] )
|