Spaces:
Running
Running
File size: 10,944 Bytes
e2f3a00 baf2993 e2f3a00 baf2993 e2f3a00 baf2993 e2f3a00 baf2993 e2f3a00 baf2993 e2f3a00 |
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
import json
import os
import platform
import unittest
from aip_trainer import app_logger
from aip_trainer.lambdas import lambdaSpeechToScore
from tests import EVENTS_FOLDER
text_dict = {"de": "Ich bin Alex, wer bist du?", "en": "Hi there, how are you?"}
expected_output = {
"de": {
"real_transcript": text_dict["de"],
"ipa_transcript": "\u026a\u00e7 bi\u02d0n a\u02d0l\u025bksv\u025b\u02d0 b\u025bst\u025b\u02d0 du\u02d0",
"pronunciation_accuracy": 63.0,
"real_transcripts": text_dict["de"],
"matched_transcripts": "ich bin alexwe - beste du",
"real_transcripts_ipa": "\u026a\u00e7 bi\u02d0n a\u02d0l\u025bks, v\u0250 b\u026ast du\u02d0?",
"matched_transcripts_ipa": "\u026a\u00e7 bi\u02d0n a\u02d0l\u025bksv\u0259 - b\u0259st\u0259 du\u02d0",
"pair_accuracy_category": "0 0 2 2 2 0",
"start_time": "0.0 0.3075 0.62525 2.1346875 1.5785625 2.1346875",
"end_time": "0.328 0.6458125 1.44025 2.4730625 2.15525 2.4730625",
"is_letter_correct_all_words": "111 111 11111 000 1011 111 ",
},
"en": {
"real_transcript": text_dict["en"],
"ipa_transcript": "ha\u026a ha\u028a \u0259r ju",
"pronunciation_accuracy": 69.0,
"real_transcripts": text_dict["en"],
"matched_transcripts": "hi - how are you",
"real_transcripts_ipa": "ha\u026a \u00f0\u025br, ha\u028a \u0259r ju?",
"matched_transcripts_ipa": "ha\u026a ha\u028a \u0259r ju",
"pair_accuracy_category": "0 2 0 0 0",
"start_time": "0.2245625 1.3228125 0.852125 1.04825 1.3228125",
"end_time": "0.559875 1.658125 1.14825 1.344375 1.658125",
"is_letter_correct_all_words": "11 000001 111 111 1111 ",
},
}
def assert_raises_get_speech_to_score_dict(self, real_text, file_bytes_or_audiotmpfile, language, exc, error_message):
from aip_trainer.lambdas import lambdaSpeechToScore
with self.assertRaises(exc):
try:
lambdaSpeechToScore.get_speech_to_score_dict(
real_text, file_bytes_or_audiotmpfile, language, remove_random_file=False
)
except exc as e:
self.assertEqual(str(e), error_message)
raise e
def check_value_by_field(value, match):
import re
assert len(value.strip()) > 0
for word in value.lstrip().rstrip().split(" "):
word_check = re.findall(match, word.strip())
assert len(word_check) == 1
assert word_check[0] == word.strip()
def check_output_by_field(output, key, match, expected_output):
check_value_by_field(output[key], match)
output[key] = expected_output[key]
return output
def check_output(self, output, expected_output):
self.maxDiff = None
try:
assert len(output["matched_transcripts"].strip()) > 0
assert len(output["matched_transcripts_ipa"].strip()) > 0
assert len(output["ipa_transcript"].strip()) > 0
assert len(output["real_transcripts_ipa"].strip()) > 0
output = check_output_by_field(
output, "is_letter_correct_all_words", "[01]+", expected_output
)
output = check_output_by_field(output, "end_time", "\d+\.\d+", expected_output)
output = check_output_by_field(
output, "start_time", "\d+\.\d+", expected_output
)
pronunciation_accuracy = output["pronunciation_accuracy"]
assert isinstance(pronunciation_accuracy, float)
assert pronunciation_accuracy <= 100
output["matched_transcripts"] = expected_output["matched_transcripts"]
output["matched_transcripts_ipa"] = expected_output["matched_transcripts_ipa"]
output["pronunciation_accuracy"] = expected_output["pronunciation_accuracy"]
output["pair_accuracy_category"] = expected_output["pair_accuracy_category"]
output["ipa_transcript"] = expected_output["ipa_transcript"]
output["real_transcript"] = expected_output["real_transcript"]
output["real_transcripts_ipa"] = expected_output["real_transcripts_ipa"]
self.assertDictEqual(expected_output, output)
except Exception as e:
app_logger.error(f"e:{e}.")
raise e
class TestGetAccuracyFromRecordedAudio(unittest.TestCase):
def setUp(self):
if platform.system() == "Windows" or platform.system() == "Win32":
os.environ["PYTHONUTF8"] = "1"
def tearDown(self):
if (
platform.system() == "Windows" or platform.system() == "Win32"
) and "PYTHONUTF8" in os.environ:
del os.environ["PYTHONUTF8"]
def test_GetAccuracyFromRecordedAudio(self):
with open(EVENTS_FOLDER / "GetAccuracyFromRecordedAudio.json", "r") as src:
inputs_outputs = json.load(src)
inputs = inputs_outputs["inputs"]
outputs = inputs_outputs["outputs"]
for event_name, event_content in inputs.items():
current_expected_output = outputs[event_name]
output = lambdaSpeechToScore.lambda_handler(event_content, [])
output = json.loads(output)
app_logger.info(
f"output type:{type(output)}, expected_output type:{type(current_expected_output)}."
)
check_output(self, output, current_expected_output)
def test_get_speech_to_score_en_ok(self):
from aip_trainer.lambdas import lambdaSpeechToScore
language = "en"
path = EVENTS_FOLDER / f"test_{language}.wav"
output = lambdaSpeechToScore.get_speech_to_score_dict(
real_text=text_dict[language],
file_bytes_or_audiotmpfile=str(path),
language=language,
remove_random_file=False,
)
check_output(self, output, expected_output[language])
def test_get_speech_to_score_en_ok_remove_input_file(self):
import shutil
from aip_trainer.lambdas import lambdaSpeechToScore
language = "en"
path = EVENTS_FOLDER / f"test_{language}.wav"
path2 = EVENTS_FOLDER / f"test2_{language}.wav"
shutil.copy(path, path2)
assert path2.exists() and path2.is_file()
output = lambdaSpeechToScore.get_speech_to_score_dict(
real_text=text_dict[language],
file_bytes_or_audiotmpfile=str(path2),
language=language,
remove_random_file=True,
)
assert not path2.exists()
check_output(self, output, expected_output[language])
def test_get_speech_to_score_de_ok(self):
from aip_trainer.lambdas import lambdaSpeechToScore
language = "de"
path = EVENTS_FOLDER / f"test_{language}.wav"
output = lambdaSpeechToScore.get_speech_to_score_dict(
real_text=text_dict[language],
file_bytes_or_audiotmpfile=str(path),
language=language,
remove_random_file=False,
)
check_output(self, output, expected_output[language])
def test_get_speech_to_score_de_ok_remove_input_file(self):
import shutil
from aip_trainer.lambdas import lambdaSpeechToScore
language = "de"
path = EVENTS_FOLDER / f"test_{language}.wav"
path2 = EVENTS_FOLDER / f"test2_{language}.wav"
shutil.copy(path, path2)
assert path2.exists() and path2.is_file()
output = lambdaSpeechToScore.get_speech_to_score_dict(
real_text=text_dict[language],
file_bytes_or_audiotmpfile=str(path2),
language=language,
remove_random_file=True,
)
assert not path2.exists()
check_output(self, output, expected_output[language])
def test_get_speech_to_score_tuple_de_ok(self):
from aip_trainer.lambdas import lambdaSpeechToScore
language = "de"
path = EVENTS_FOLDER / f"test_{language}.wav"
(
real_transcripts,
is_letter_correct_all_words,
pronunciation_accuracy,
ipa_transcript,
real_transcripts_ipa,
dumped,
) = lambdaSpeechToScore.get_speech_to_score_tuple(
real_text=text_dict[language],
file_bytes_or_audiotmpfile=str(path),
language=language,
remove_random_file=False,
)
assert real_transcripts == text_dict[language]
check_value_by_field(is_letter_correct_all_words, "[01]+")
assert isinstance(pronunciation_accuracy, float)
assert pronunciation_accuracy <= 100
assert len(ipa_transcript.strip()) > 0
assert len(real_transcripts_ipa.strip()) > 0
check_output(self, json.loads(dumped), expected_output[language])
def test_get_speech_to_score_tuple_en_ok(self):
from aip_trainer.lambdas import lambdaSpeechToScore
language = "en"
path = EVENTS_FOLDER / f"test_{language}.wav"
(
real_transcripts,
is_letter_correct_all_words,
pronunciation_accuracy,
ipa_transcript,
real_transcripts_ipa,
dumped,
) = lambdaSpeechToScore.get_speech_to_score_tuple(
real_text=text_dict[language],
file_bytes_or_audiotmpfile=str(path),
language=language,
remove_random_file=False,
)
assert real_transcripts == text_dict[language]
check_value_by_field(is_letter_correct_all_words, "[01]+")
assert isinstance(pronunciation_accuracy, float)
assert pronunciation_accuracy <= 100
assert len(ipa_transcript.strip()) > 0
assert len(real_transcripts_ipa.strip()) > 0
check_output(self, json.loads(dumped), expected_output[language])
def test_get_speech_to_score_dict__de_empty_input_text(self):
language = "de"
path = EVENTS_FOLDER / f"test_{language}.wav"
assert_raises_get_speech_to_score_dict(self, "", str(path), language, ValueError, "cannot read an empty/None text: ''...")
def test_get_speech_to_score_dict__en_empty_input_text(self):
language = "en"
path = EVENTS_FOLDER / f"test_{language}.wav"
assert_raises_get_speech_to_score_dict(self, "", str(path), language, ValueError, "cannot read an empty/None text: ''...")
def test_get_speech_to_score_dict__de_empty_input_file(self):
language = "de"
assert_raises_get_speech_to_score_dict(self, "text fake", "", language, ValueError, "cannot read an empty/None file: ''...")
def test_get_speech_to_score_dict__en_empty_input_file(self):
language = "en"
assert_raises_get_speech_to_score_dict(self, "text fake", "", language, ValueError, "cannot read an empty/None file: ''...")
def test_get_speech_to_score_dict__empty_language(self):
assert_raises_get_speech_to_score_dict(self, "text fake", "fake_file", "", NotImplementedError, "Not tested/supported with '' language...")
if __name__ == "__main__":
unittest.main()
|