Spaces:
Running
Running
File size: 6,385 Bytes
9d61c9b |
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 |
import unittest
import torch
from training.preprocess.wav2vec_aligner import Wav2VecAligner
class TestWav2VecAligner(unittest.TestCase):
def setUp(self):
self.model = Wav2VecAligner()
self.text = "I HAD THAT CURIOSITY BESIDE ME AT THIS MOMENT"
self.wav_path = "./mocks/audio_example.wav"
def test_load_audio(self):
_, sample_rate = self.model.load_audio(self.wav_path)
self.assertEqual(sample_rate, 16_000)
with self.assertRaises(FileNotFoundError):
self.model.load_audio("./nonexistent/path.wav")
def test_encode(self):
tokens = self.model.encode(self.text)
torch.testing.assert_close(
tokens,
torch.tensor(
[
[
10,
4,
11,
7,
14,
4,
6,
11,
7,
6,
4,
19,
16,
13,
10,
8,
12,
10,
6,
22,
4,
24,
5,
12,
10,
14,
5,
4,
17,
5,
4,
7,
6,
4,
6,
11,
10,
12,
4,
17,
8,
17,
5,
9,
6,
],
],
),
)
def test_decode(self):
transcript = self.model.decode(
[
[
10,
4,
11,
7,
14,
4,
6,
11,
7,
6,
4,
19,
16,
13,
10,
8,
12,
10,
6,
22,
4,
24,
5,
12,
10,
14,
5,
4,
17,
5,
4,
7,
6,
4,
6,
11,
10,
12,
4,
17,
8,
17,
5,
9,
6,
],
],
)
self.assertEqual(transcript, self.text)
def test_align_single_sample(self):
audio_input, _ = self.model.load_audio(self.wav_path)
emissions, tokens, transcript = self.model.align_single_sample(
audio_input, self.text,
)
self.assertEqual(emissions.shape, torch.Size([169, 32]))
self.assertEqual(
len(tokens),
47,
)
self.assertEqual(transcript, "|I|HAD|THAT|CURIOSITY|BESIDE|ME|AT|THIS|MOMENT|")
def test_get_trellis(self):
audio_input, _ = self.model.load_audio(self.wav_path)
emissions, tokens, _ = self.model.align_single_sample(audio_input, self.text)
trellis = self.model.get_trellis(emissions, tokens)
self.assertEqual(emissions.shape, torch.Size([169, 32]))
self.assertEqual(len(tokens), 47)
# Add assertions here based on the expected behavior of get_trellis
self.assertIsInstance(trellis, torch.Tensor)
self.assertEqual(trellis.shape, torch.Size([169, 47]))
def test_backtrack(self):
audio_input, _ = self.model.load_audio(self.wav_path)
emissions, tokens, _ = self.model.align_single_sample(audio_input, self.text)
trellis = self.model.get_trellis(emissions, tokens)
path = self.model.backtrack(trellis, emissions, tokens)
# Add assertions here based on the expected behavior of backtrack
self.assertIsInstance(path, list)
self.assertEqual(len(path), 169)
def test_merge_repeats(self):
audio_input, _ = self.model.load_audio(self.wav_path)
emissions, tokens, transcript = self.model.align_single_sample(
audio_input, self.text,
)
trellis = self.model.get_trellis(emissions, tokens)
path = self.model.backtrack(trellis, emissions, tokens)
merged_path = self.model.merge_repeats(path, transcript)
# Add assertions here based on the expected behavior of merge_repeats
self.assertIsInstance(merged_path, list)
self.assertEqual(len(merged_path), 47)
def test_merge_words(self):
audio_input, _ = self.model.load_audio(self.wav_path)
emissions, tokens, transcript = self.model.align_single_sample(
audio_input, self.text,
)
trellis = self.model.get_trellis(emissions, tokens)
path = self.model.backtrack(trellis, emissions, tokens)
merged_path = self.model.merge_repeats(path, transcript)
merged_words = self.model.merge_words(merged_path)
# Add assertions here based on the expected behavior of merge_words
self.assertIsInstance(merged_words, list)
self.assertEqual(len(merged_words), 9)
def test_forward(self):
result = self.model(self.wav_path, self.text)
# self.assertEqual(result, expected_result)
self.assertEqual(len(result), 9)
def test_save_segments(self):
# self.model.save_segments(self.wav_path, self.text, "./mocks/wav2vec_aligner/audio")
self.assertEqual(True, True)
if __name__ == "__main__":
unittest.main()
|