Spaces:
Running
Running
Delete demo.py
Browse files
demo.py
DELETED
@@ -1,135 +0,0 @@
|
|
1 |
-
import logging
|
2 |
-
from argparse import ArgumentParser
|
3 |
-
from pathlib import Path
|
4 |
-
|
5 |
-
import torch
|
6 |
-
import torchaudio
|
7 |
-
|
8 |
-
from mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate,
|
9 |
-
load_video, make_video, setup_eval_logging)
|
10 |
-
from mmaudio.model.flow_matching import FlowMatching
|
11 |
-
from mmaudio.model.networks import MMAudio, get_my_mmaudio
|
12 |
-
from mmaudio.model.utils.features_utils import FeaturesUtils
|
13 |
-
|
14 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
15 |
-
torch.backends.cudnn.allow_tf32 = True
|
16 |
-
|
17 |
-
log = logging.getLogger()
|
18 |
-
|
19 |
-
|
20 |
-
@torch.inference_mode()
|
21 |
-
def main():
|
22 |
-
setup_eval_logging()
|
23 |
-
|
24 |
-
parser = ArgumentParser()
|
25 |
-
parser.add_argument('--variant',
|
26 |
-
type=str,
|
27 |
-
default='large_44k_v2',
|
28 |
-
help='small_16k, small_44k, medium_44k, large_44k, large_44k_v2')
|
29 |
-
parser.add_argument('--video', type=Path, help='Path to the video file')
|
30 |
-
parser.add_argument('--prompt', type=str, help='Input prompt', default='')
|
31 |
-
parser.add_argument('--negative_prompt', type=str, help='Negative prompt', default='')
|
32 |
-
parser.add_argument('--duration', type=float, default=8.0)
|
33 |
-
parser.add_argument('--cfg_strength', type=float, default=4.5)
|
34 |
-
parser.add_argument('--num_steps', type=int, default=25)
|
35 |
-
|
36 |
-
parser.add_argument('--mask_away_clip', action='store_true')
|
37 |
-
|
38 |
-
parser.add_argument('--output', type=Path, help='Output directory', default='./output')
|
39 |
-
parser.add_argument('--seed', type=int, help='Random seed', default=42)
|
40 |
-
parser.add_argument('--skip_video_composite', action='store_true')
|
41 |
-
parser.add_argument('--full_precision', action='store_true')
|
42 |
-
|
43 |
-
args = parser.parse_args()
|
44 |
-
|
45 |
-
if args.variant not in all_model_cfg:
|
46 |
-
raise ValueError(f'Unknown model variant: {args.variant}')
|
47 |
-
model: ModelConfig = all_model_cfg[args.variant]
|
48 |
-
model.download_if_needed()
|
49 |
-
seq_cfg = model.seq_cfg
|
50 |
-
|
51 |
-
if args.video:
|
52 |
-
video_path: Path = Path(args.video).expanduser()
|
53 |
-
else:
|
54 |
-
video_path = None
|
55 |
-
prompt: str = args.prompt
|
56 |
-
negative_prompt: str = args.negative_prompt
|
57 |
-
output_dir: str = args.output.expanduser()
|
58 |
-
seed: int = args.seed
|
59 |
-
num_steps: int = args.num_steps
|
60 |
-
duration: float = args.duration
|
61 |
-
cfg_strength: float = args.cfg_strength
|
62 |
-
skip_video_composite: bool = args.skip_video_composite
|
63 |
-
mask_away_clip: bool = args.mask_away_clip
|
64 |
-
|
65 |
-
device = 'cuda'
|
66 |
-
dtype = torch.float32 if args.full_precision else torch.bfloat16
|
67 |
-
|
68 |
-
output_dir.mkdir(parents=True, exist_ok=True)
|
69 |
-
|
70 |
-
# load a pretrained model
|
71 |
-
net: MMAudio = get_my_mmaudio(model.model_name).to(device, dtype).eval()
|
72 |
-
net.load_weights(torch.load(model.model_path, map_location=device, weights_only=True))
|
73 |
-
log.info(f'Loaded weights from {model.model_path}')
|
74 |
-
|
75 |
-
# misc setup
|
76 |
-
rng = torch.Generator(device=device)
|
77 |
-
rng.manual_seed(seed)
|
78 |
-
fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=num_steps)
|
79 |
-
|
80 |
-
feature_utils = FeaturesUtils(tod_vae_ckpt=model.vae_path,
|
81 |
-
synchformer_ckpt=model.synchformer_ckpt,
|
82 |
-
enable_conditions=True,
|
83 |
-
mode=model.mode,
|
84 |
-
bigvgan_vocoder_ckpt=model.bigvgan_16k_path)
|
85 |
-
feature_utils = feature_utils.to(device, dtype).eval()
|
86 |
-
|
87 |
-
if video_path is not None:
|
88 |
-
log.info(f'Using video {video_path}')
|
89 |
-
clip_frames, sync_frames, duration = load_video(video_path, duration)
|
90 |
-
if mask_away_clip:
|
91 |
-
clip_frames = None
|
92 |
-
else:
|
93 |
-
clip_frames = clip_frames.unsqueeze(0)
|
94 |
-
sync_frames = sync_frames.unsqueeze(0)
|
95 |
-
else:
|
96 |
-
log.info('No video provided -- text-to-audio mode')
|
97 |
-
clip_frames = sync_frames = None
|
98 |
-
|
99 |
-
seq_cfg.duration = duration
|
100 |
-
net.update_seq_lengths(seq_cfg.latent_seq_len, seq_cfg.clip_seq_len, seq_cfg.sync_seq_len)
|
101 |
-
|
102 |
-
log.info(f'Prompt: {prompt}')
|
103 |
-
log.info(f'Negative prompt: {negative_prompt}')
|
104 |
-
|
105 |
-
audios = generate(clip_frames,
|
106 |
-
sync_frames, [prompt],
|
107 |
-
negative_text=[negative_prompt],
|
108 |
-
feature_utils=feature_utils,
|
109 |
-
net=net,
|
110 |
-
fm=fm,
|
111 |
-
rng=rng,
|
112 |
-
cfg_strength=cfg_strength)
|
113 |
-
audio = audios.float().cpu()[0]
|
114 |
-
if video_path is not None:
|
115 |
-
save_path = output_dir / f'{video_path.stem}.flac'
|
116 |
-
else:
|
117 |
-
safe_filename = prompt.replace(' ', '_').replace('/', '_').replace('.', '')
|
118 |
-
save_path = output_dir / f'{safe_filename}.flac'
|
119 |
-
torchaudio.save(save_path, audio, seq_cfg.sampling_rate)
|
120 |
-
|
121 |
-
log.info(f'Audio saved to {save_path}')
|
122 |
-
if video_path is not None and not skip_video_composite:
|
123 |
-
video_save_path = output_dir / f'{video_path.stem}.mp4'
|
124 |
-
make_video(video_path,
|
125 |
-
video_save_path,
|
126 |
-
audio,
|
127 |
-
sampling_rate=seq_cfg.sampling_rate,
|
128 |
-
duration_sec=seq_cfg.duration)
|
129 |
-
log.info(f'Video saved to {output_dir / video_save_path}')
|
130 |
-
|
131 |
-
log.info('Memory usage: %.2f GB', torch.cuda.max_memory_allocated() / (2**30))
|
132 |
-
|
133 |
-
|
134 |
-
if __name__ == '__main__':
|
135 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|