Spaces:
Build error
Build error
File size: 1,253 Bytes
93f4bab |
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 |
import librosa
import torch
import torch.nn as nn
def load_cn_model(ch_hubert_path):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
from fairseq import checkpoint_utils
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
[ch_hubert_path],
suffix="",
)
model = models[0]
model = model.to(device)
model.eval()
return model
def get_cn_hubert_units(con_model, audio_path, dev):
audio, sampling_rate = librosa.load(audio_path)
if len(audio.shape) > 1:
audio = librosa.to_mono(audio.transpose(1, 0))
if sampling_rate != 16000:
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
feats = torch.from_numpy(audio).float()
if feats.dim() == 2: # double channels
feats = feats.mean(-1)
assert feats.dim() == 1, feats.dim()
feats = feats.view(1, -1)
padding_mask = torch.BoolTensor(feats.shape).fill_(False)
inputs = {
"source": feats.to(dev),
"padding_mask": padding_mask.to(dev),
"output_layer": 9, # layer 9
}
with torch.no_grad():
logits = con_model.extract_features(**inputs)
feats = con_model.final_proj(logits[0])
return feats
|