Create custom_interface.py
Browse files- custom_interface.py +98 -0
custom_interface.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from speechbrain.inference.interfaces import Pretrained
|
3 |
+
import librosa
|
4 |
+
|
5 |
+
|
6 |
+
class ASR(Pretrained):
|
7 |
+
def __init__(self, *args, **kwargs):
|
8 |
+
super().__init__(*args, **kwargs)
|
9 |
+
|
10 |
+
def encode_batch(self, device, wavs, wav_lens=None, normalize=False):
|
11 |
+
wavs = wavs.to(device)
|
12 |
+
wav_lens = wav_lens.to(device)
|
13 |
+
|
14 |
+
# Forward pass
|
15 |
+
encoded_outputs = self.mods.encoder_w2v2(wavs.detach())
|
16 |
+
# append
|
17 |
+
tokens_bos = torch.zeros((wavs.size(0), 1), dtype=torch.long).to(device)
|
18 |
+
embedded_tokens = self.mods.embedding(tokens_bos)
|
19 |
+
decoder_outputs, _ = self.mods.decoder(embedded_tokens, encoded_outputs, wav_lens)
|
20 |
+
|
21 |
+
# Output layer for seq2seq log-probabilities
|
22 |
+
predictions = self.hparams.test_search(encoded_outputs, wav_lens)[0]
|
23 |
+
# predicted_words = [self.hparams.tokenizer.decode_ids(prediction).split(" ") for prediction in predictions]
|
24 |
+
predicted_words = []
|
25 |
+
for prediction in predictions:
|
26 |
+
prediction = [token for token in prediction if token != 0]
|
27 |
+
predicted_words.append(self.hparams.tokenizer.decode_ids(prediction).split(" "))
|
28 |
+
prediction = []
|
29 |
+
for sent in predicted_words:
|
30 |
+
sent = self.filter_repetitions(sent, 3)
|
31 |
+
prediction.append(sent)
|
32 |
+
predicted_words = prediction
|
33 |
+
return predicted_words
|
34 |
+
|
35 |
+
def filter_repetitions(self, seq, max_repetition_length):
|
36 |
+
seq = list(seq)
|
37 |
+
output = []
|
38 |
+
max_n = len(seq) // 2
|
39 |
+
for n in range(max_n, 0, -1):
|
40 |
+
max_repetitions = max(max_repetition_length // n, 1)
|
41 |
+
# Don't need to iterate over impossible n values:
|
42 |
+
# len(seq) can change a lot during iteration
|
43 |
+
if (len(seq) <= n*2) or (len(seq) <= max_repetition_length):
|
44 |
+
continue
|
45 |
+
iterator = enumerate(seq)
|
46 |
+
# Fill first buffers:
|
47 |
+
buffers = [[next(iterator)[1]] for _ in range(n)]
|
48 |
+
for seq_index, token in iterator:
|
49 |
+
current_buffer = seq_index % n
|
50 |
+
if token != buffers[current_buffer][-1]:
|
51 |
+
# No repeat, we can flush some tokens
|
52 |
+
buf_len = sum(map(len, buffers))
|
53 |
+
flush_start = (current_buffer-buf_len) % n
|
54 |
+
# Keep n-1 tokens, but possibly mark some for removal
|
55 |
+
for flush_index in range(buf_len - buf_len%n):
|
56 |
+
if (buf_len - flush_index) > n-1:
|
57 |
+
to_flush = buffers[(flush_index + flush_start) % n].pop(0)
|
58 |
+
else:
|
59 |
+
to_flush = None
|
60 |
+
# Here, repetitions get removed:
|
61 |
+
if (flush_index // n < max_repetitions) and to_flush is not None:
|
62 |
+
output.append(to_flush)
|
63 |
+
elif (flush_index // n >= max_repetitions) and to_flush is None:
|
64 |
+
output.append(to_flush)
|
65 |
+
buffers[current_buffer].append(token)
|
66 |
+
# At the end, final flush
|
67 |
+
current_buffer += 1
|
68 |
+
buf_len = sum(map(len, buffers))
|
69 |
+
flush_start = (current_buffer-buf_len) % n
|
70 |
+
for flush_index in range(buf_len):
|
71 |
+
to_flush = buffers[(flush_index + flush_start) % n].pop(0)
|
72 |
+
# Here, repetitions just get removed:
|
73 |
+
if flush_index // n < max_repetitions:
|
74 |
+
output.append(to_flush)
|
75 |
+
seq = []
|
76 |
+
to_delete = 0
|
77 |
+
for token in output:
|
78 |
+
if token is None:
|
79 |
+
to_delete += 1
|
80 |
+
elif to_delete > 0:
|
81 |
+
to_delete -= 1
|
82 |
+
else:
|
83 |
+
seq.append(token)
|
84 |
+
output = []
|
85 |
+
return seq
|
86 |
+
|
87 |
+
|
88 |
+
def classify_file(self, path, device):
|
89 |
+
waveform, sr = librosa.load(path, sr=16000)
|
90 |
+
|
91 |
+
waveform = torch.tensor(waveform).to(device)
|
92 |
+
waveform = waveform.to(device)
|
93 |
+
# Fake a batch:
|
94 |
+
batch = waveform.unsqueeze(0)
|
95 |
+
rel_length = torch.tensor([1.0]).to(device)
|
96 |
+
outputs = self.encode_batch(device, batch, rel_length)
|
97 |
+
outputs = " ".join(outputs[0])
|
98 |
+
return outputs
|