Spaces:
Running
on
Zero
Running
on
Zero
File size: 11,844 Bytes
b4bbb92 2ba71a4 b4bbb92 2ba71a4 b4bbb92 2ba71a4 b4bbb92 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 159 160 161 162 163 164 165 166 167 168 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 |
#!/usr/bin/env python3
from typing import Any, Callable
import json
import os
import fire
import torch
import torchaudio
import soundfile as sf
import numpy as np
from modeling_uniflow_audio import UniFlowAudioModel
from constants import TIME_ALIGNED_TASKS, NON_TIME_ALIGNED_TASKS
class InferenceCLI:
def __init__(self):
self.model_name = None
self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
self.g2p = None
self.speaker_model = None
self.svs_processor = None
self.singer_mapping = None
self.video_preprocessor = None
self.video_size = (256, 256)
self.video_fps = 10
def init_model(self, model_name):
self.model_name = model_name
self.model = UniFlowAudioModel(f"wsntxxn/{model_name}")
self.model.to(self.device)
self.sample_rate = self.model.config["sample_rate"]
def init_speaker_model(self, ):
import wespeaker
if self.speaker_model is None:
self.speaker_model = wespeaker.load_model("english")
self.speaker_model.set_device(self.device)
def init_svs_processor(self, ):
from utils.diffsinger_utilities import SVSInputConverter, TokenTextEncoder
if self.svs_processor is None:
phoneme_list = json.load(open(self.model.svs_phone_set_path, "r"))
self.svs_processor = {
"converter":
SVSInputConverter(
self.model.svs_singer_mapping, self.model.svs_pinyin2ph
),
"tokenizer":
TokenTextEncoder(
None, vocab_list=phoneme_list, replace_oov=','
)
}
def init_video_preprocessor(self, ):
if self.video_preprocessor is None:
from transformers import CLIPImageProcessor, CLIPVisionModel
import torchvision
self.video_preprocessor = {
"transform":
torchvision.transforms.Resize(self.video_size),
"processor":
CLIPImageProcessor.
from_pretrained("openai/clip-vit-large-patch14"),
"encoder":
CLIPVisionModel.
from_pretrained("openai/clip-vit-large-patch14")
}
self.video_preprocessor["encoder"].to(self.device)
self.video_preprocessor["encoder"].eval()
def on_inference_start(self, model_name):
if self.model_name is None or model_name != self.model_name:
self.init_model(model_name)
@staticmethod
def add_prehook(func: Callable, ):
def wrapper(self, *args, **kwargs):
model_name = kwargs["model_name"]
self.on_inference_start(model_name)
return func(self, *args, **kwargs)
return wrapper
@add_prehook
def t2a(
self,
caption: str,
model_name: str = "UniFlow-Audio-large",
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 5.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
self._run_inference(
content=caption,
task="text_to_audio",
instruction=instruction,
instruction_idx=instruction_idx,
model_name=model_name,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path
)
@add_prehook
def t2m(
self,
caption: str,
model_name: str,
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 5.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
self._run_inference(
content=caption,
task="text_to_music",
model_name=model_name,
instruction=instruction,
instruction_idx=instruction_idx,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path,
)
@add_prehook
def tts(
self,
transcript: str,
ref_speaker_speech: str,
model_name: str = "UniFlow-Audio-large",
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 5.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
from g2p_en import G2p
import nltk
self.init_speaker_model()
if not self.g2p:
if not os.path.exists(
os.path.expanduser(
"~/nltk_data/taggers/averaged_perceptron_tagger_eng"
)
):
nltk.download("averaged_perceptron_tagger_eng")
self.g2p = G2p()
phonemes = self.g2p(transcript)
phonemes = [ph for ph in phonemes if ph != " "]
phone_indices = [
self.model.tts_phone2id.get(
p, self.model.tts_phone2id.get("spn", 0)
) for p in phonemes
]
xvector = self.speaker_model.extract_embedding(ref_speaker_speech)
content = {
"phoneme": np.array(phone_indices, dtype=np.int64),
"spk": np.array(xvector, dtype=np.float32),
}
self._run_inference(
content=content,
task="text_to_speech",
model_name=model_name,
instruction=instruction,
instruction_idx=instruction_idx,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path,
)
@add_prehook
def _audio_input_inference(
self,
input_audio: str,
task: str,
model_name: str,
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 5.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
waveform, orig_sr = torchaudio.load(input_audio)
waveform = waveform.mean(0)
waveform = torchaudio.functional.resample(
waveform, orig_freq=orig_sr, new_freq=self.sample_rate
)
self._run_inference(
content=waveform,
task=task,
instruction=instruction,
instruction_idx=instruction_idx,
model_name=model_name,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path
)
def se(
self,
noisy_speech: str,
model_name: str,
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 1.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
self._audio_input_inference(
input_audio=noisy_speech,
task="speech_enhancement",
instruction=instruction,
instruction_idx=instruction_idx,
model_name=model_name,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path
)
def sr(
self,
low_sr_audio: str,
model_name: str,
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 1.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
self._audio_input_inference(
input_audio=low_sr_audio,
task="audio_super_resolution",
instruction=instruction,
instruction_idx=instruction_idx,
model_name=model_name,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path
)
@add_prehook
def v2a(
self,
video: str,
model_name: str,
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 5.0,
num_steps: int = 25,
output_path: str = "./output.mp4",
):
from utils.video import read_video_frames, merge_audio_video
self.init_video_preprocessor()
video_path = video
video = read_video_frames(
video,
duration=None,
fps=self.video_fps,
video_size=self.video_size,
resize_transform=self.video_preprocessor["transform"]
)
pixel_values = self.video_preprocessor["processor"](
images=video, return_tensors="pt"
).pixel_values.to(self.device)
with torch.no_grad():
output = self.video_preprocessor["encoder"](pixel_values)
video_feature = output.pooler_output
waveform = self._run_inference(
content=video_feature,
task="video_to_audio",
model_name=model_name,
instruction=instruction,
instruction_idx=instruction_idx,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path,
)
merge_audio_video(
waveform, video_path, output_path, audio_fps=self.sample_rate
)
@add_prehook
def svs(
self,
singer: str,
music_score: str,
model_name: str,
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 5.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
self.init_svs_processor()
text, note, note_dur = music_score.split('<sep>')
if singer not in self.model.svs_singer_mapping:
print(f"Unsupported singer {singer}, available singers: ")
print(list(self.model.svs_singer_mapping.keys()))
raise KeyError
midi = self.svs_processor["converter"].preprocess_input({
"spk_name": singer,
"text": text,
"notes": note,
"notes_duration": note_dur,
})
midi["phoneme"] = self.svs_processor["tokenizer"].encode(
midi["phoneme"]
)
self._run_inference(
content=midi,
task="singing_voice_synthesis",
model_name=model_name,
instruction=instruction,
instruction_idx=instruction_idx,
guidance_scale=guidance_scale,
num_steps=num_steps,
output_path=output_path,
)
def _run_inference(
self,
content: Any,
task: str,
model_name: str,
instruction: str | None = None,
instruction_idx: int | None = None,
guidance_scale: float = 5.0,
num_steps: int = 25,
output_path: str = "./output.wav",
):
if self.model_name is None or model_name != self.model_name:
self.init_model(model_name)
if task in TIME_ALIGNED_TASKS:
is_time_aligned = True
else:
is_time_aligned = False
if instruction:
instruction = [instruction]
if instruction_idx:
instruction_idx = [instruction_idx]
waveform = self.model.sample(
content=[content],
task=[task],
is_time_aligned=[is_time_aligned],
instruction=instruction,
instruction_idx=instruction_idx,
num_steps=num_steps,
guidance_scale=guidance_scale,
disable_progress=False
)
waveform = waveform[0, 0].cpu().numpy()
if not output_path.endswith(".mp4"):
sf.write(output_path, waveform, self.sample_rate)
return waveform
if __name__ == "__main__":
fire.Fire(InferenceCLI)
|