Update custom_interface.py
Browse files- custom_interface.py +58 -0
custom_interface.py
CHANGED
@@ -22,8 +22,66 @@ class ASR(Pretrained):
|
|
22 |
predictions = self.hparams.test_search(encoded_outputs, self.wav_lens)[0]
|
23 |
predicted_words = [self.hparams.tokenizer.decode_ids(prediction).split(" ") for prediction in predictions]
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
return predicted_words
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def classify_file(self, path):
|
29 |
# waveform = self.load_audio(path)
|
|
|
22 |
predictions = self.hparams.test_search(encoded_outputs, self.wav_lens)[0]
|
23 |
predicted_words = [self.hparams.tokenizer.decode_ids(prediction).split(" ") for prediction in predictions]
|
24 |
|
25 |
+
prediction = []
|
26 |
+
for sent in predicted_words:
|
27 |
+
sent = filter_repetitions(sent, 3)
|
28 |
+
sent = " ".join(sent)
|
29 |
+
prediction.append(sent)
|
30 |
+
predicted_words = prediction[0]
|
31 |
return predicted_words
|
32 |
|
33 |
+
def filter_repetitions(self, seq, max_repetition_length):
|
34 |
+
seq = list(seq)
|
35 |
+
output = []
|
36 |
+
max_n = len(seq) // 2
|
37 |
+
for n in range(max_n, 0, -1):
|
38 |
+
max_repetitions = max(max_repetition_length // n, 1)
|
39 |
+
# Don't need to iterate over impossible n values:
|
40 |
+
# len(seq) can change a lot during iteration
|
41 |
+
if (len(seq) <= n*2) or (len(seq) <= max_repetition_length):
|
42 |
+
continue
|
43 |
+
iterator = enumerate(seq)
|
44 |
+
# Fill first buffers:
|
45 |
+
buffers = [[next(iterator)[1]] for _ in range(n)]
|
46 |
+
for seq_index, token in iterator:
|
47 |
+
current_buffer = seq_index % n
|
48 |
+
if token != buffers[current_buffer][-1]:
|
49 |
+
# No repeat, we can flush some tokens
|
50 |
+
buf_len = sum(map(len, buffers))
|
51 |
+
flush_start = (current_buffer-buf_len) % n
|
52 |
+
# Keep n-1 tokens, but possibly mark some for removal
|
53 |
+
for flush_index in range(buf_len - buf_len%n):
|
54 |
+
if (buf_len - flush_index) > n-1:
|
55 |
+
to_flush = buffers[(flush_index + flush_start) % n].pop(0)
|
56 |
+
else:
|
57 |
+
to_flush = None
|
58 |
+
# Here, repetitions get removed:
|
59 |
+
if (flush_index // n < max_repetitions) and to_flush is not None:
|
60 |
+
output.append(to_flush)
|
61 |
+
elif (flush_index // n >= max_repetitions) and to_flush is None:
|
62 |
+
output.append(to_flush)
|
63 |
+
buffers[current_buffer].append(token)
|
64 |
+
# At the end, final flush
|
65 |
+
current_buffer += 1
|
66 |
+
buf_len = sum(map(len, buffers))
|
67 |
+
flush_start = (current_buffer-buf_len) % n
|
68 |
+
for flush_index in range(buf_len):
|
69 |
+
to_flush = buffers[(flush_index + flush_start) % n].pop(0)
|
70 |
+
# Here, repetitions just get removed:
|
71 |
+
if flush_index // n < max_repetitions:
|
72 |
+
output.append(to_flush)
|
73 |
+
seq = []
|
74 |
+
to_delete = 0
|
75 |
+
for token in output:
|
76 |
+
if token is None:
|
77 |
+
to_delete += 1
|
78 |
+
elif to_delete > 0:
|
79 |
+
to_delete -= 1
|
80 |
+
else:
|
81 |
+
seq.append(token)
|
82 |
+
output = []
|
83 |
+
return seq
|
84 |
+
|
85 |
|
86 |
def classify_file(self, path):
|
87 |
# waveform = self.load_audio(path)
|