Views
No views yet
1wget https://raw.githubusercontent.com/voidful/hubert-cluster-code/main/km_feat_100_layer_20
2wget https://cdn-media.huggingface.co/speech_samples/sample1.flac1import joblib
2import torch
3from transformers import Wav2Vec2FeatureExtractor, HubertModel
4import soundfile as sf
5
6
7class HubertCode(object):
8 def __init__(self, hubert_model, km_path, km_layer):
9 self.processor = Wav2Vec2FeatureExtractor.from_pretrained(hubert_model)
10 self.model = HubertModel.from_pretrained(hubert_model)
11 self.km_model = joblib.load(km_path)
12 self.km_layer = km_layer
13 self.C_np = self.km_model.cluster_centers_.transpose()
14 self.Cnorm_np = (self.C_np ** 2).sum(0, keepdims=True)
15
16 self.C = torch.from_numpy(self.C_np)
17 self.Cnorm = torch.from_numpy(self.Cnorm_np)
18 if torch.cuda.is_available():
19 self.C = self.C.cuda()
20 self.Cnorm = self.Cnorm.cuda()
21 self.model = self.model.cuda()
22
23 def __call__(self, filepath, sampling_rate=None):
24 speech, sr = sf.read(filepath)
25 input_values = self.processor(speech, return_tensors="pt", sampling_rate=sr).input_values
26 if torch.cuda.is_available():
27 input_values = input_values.cuda()
28 hidden_states = self.model(input_values, output_hidden_states=True).hidden_states
29 x = hidden_states[self.km_layer].squeeze()
30 dist = (
31 x.pow(2).sum(1, keepdim=True)
32 - 2 * torch.matmul(x, self.C)
33 + self.Cnorm
34 )
35 return dist.argmin(dim=1).cpu().numpy()1hc = HubertCode("facebook/hubert-large-ll60k", './km_feat_100_layer_20', 20)
2voice_ids = hc('./sample1.flac')1from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2tokenizer = AutoTokenizer.from_pretrained("voidful/asr_hubert_cluster_bart_base")
3model = AutoModelForSeq2SeqLM.from_pretrained("voidful/asr_hubert_cluster_bart_base")1gen_output = model.generate(input_ids=tokenizer("".join([f":vtok{i}:" for i in voice_ids]),return_tensors='pt').input_ids,max_length=1024)
2print(tokenizer.decode(gen_output[0], skip_special_tokens=True))going along slushy country roads and speaking to damp audience in drifty school rooms day after day for a fortnight he'll have to put in an appearance at some place of worship on sunday morning and he can come to ask immediately afterwards