From b755f0bbbccb517c73ec25e28668fdf3f43a1d6c Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 20 Jun 2023 00:59:52 +0200 Subject: [PATCH] - Add test for Whisper.py --- .../Speech_Recognition/test_Whisper.py | 56 +++++++++++++++++++ .../Speech_Recognition/test_hyphenation.py | 5 +- .../UltraStar/test_ultrastar_writer.py | 5 ++ src/modules/Speech_Recognition/Whisper.py | 16 ++++-- 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 pytest/modules/Speech_Recognition/test_Whisper.py diff --git a/pytest/modules/Speech_Recognition/test_Whisper.py b/pytest/modules/Speech_Recognition/test_Whisper.py new file mode 100644 index 0000000..8f78701 --- /dev/null +++ b/pytest/modules/Speech_Recognition/test_Whisper.py @@ -0,0 +1,56 @@ +"""Tests for whisper.py""" + +import unittest +from src.modules.Speech_Recognition.TranscribedData import TranscribedData +from src.modules.Speech_Recognition.Whisper import convert_to_transcribed_data + + +class ConvertToTranscribedDataTest(unittest.TestCase): + def test_convert_to_transcribed_data(self): + # Arrange + result_aligned = { + "segments": [ + { + "words": [ + {"word": "UltraSinger", "start": 1.23, "end": 2.34, "confidence": 0.95}, + {"word": "is", "start": 2.34, "end": 3.45, "confidence": 0.9}, + {"word": "cool!", "start": 3.45, "end": 4.56, "confidence": 0.85}, + ] + }, + { + "words": [ + {"word": "And", "start": 4.56, "end": 5.67, "confidence": 0.95}, + {"word": "will", "start": 5.67, "end": 6.78, "confidence": 0.9}, + {"word": "be", "start": 6.78, "end": 7.89, "confidence": 0.85}, + {"word": "better!", "start": 7.89, "end": 9.01, "confidence": 0.8}, + ] + }, + ] + } + + # Words should have space at the end + expected_output = [ + TranscribedData( + {"word": "UltraSinger ", "start": 1.23, "end": 2.34, "is_hyphen": None, "confidence": 0.95}), + TranscribedData({"word": "is ", "start": 2.34, "end": 3.45, "is_hyphen": None, "confidence": 0.9}), + TranscribedData({"word": "cool! ", "start": 3.45, "end": 4.56, "is_hyphen": None, "confidence": 0.85}), + TranscribedData({"word": "And ", "start": 4.56, "end": 5.67, "is_hyphen": None, "confidence": 0.95}), + TranscribedData({"word": "will ", "start": 5.67, "end": 6.78, "is_hyphen": None, "confidence": 0.9}), + TranscribedData({"word": "be ", "start": 6.78, "end": 7.89, "is_hyphen": None, "confidence": 0.85}), + TranscribedData({"word": "better! ", "start": 7.89, "end": 9.01, "is_hyphen": None, "confidence": 0.8}), + ] + + # Act + transcribed_data = convert_to_transcribed_data(result_aligned) + + # Assert + self.assertEqual(len(transcribed_data), len(expected_output)) + for i in range(len(transcribed_data)): + self.assertEqual(transcribed_data[i].word, expected_output[i].word) + self.assertEqual(transcribed_data[i].end, expected_output[i].end) + self.assertEqual(transcribed_data[i].start, expected_output[i].start) + self.assertEqual(transcribed_data[i].is_hyphen, expected_output[i].is_hyphen) + + +if __name__ == "__main__": + unittest.main() diff --git a/pytest/modules/Speech_Recognition/test_hyphenation.py b/pytest/modules/Speech_Recognition/test_hyphenation.py index a177529..cfd5dd8 100644 --- a/pytest/modules/Speech_Recognition/test_hyphenation.py +++ b/pytest/modules/Speech_Recognition/test_hyphenation.py @@ -1,5 +1,6 @@ """Tests for hyphenation.py""" +import unittest from src.modules.Speech_Recognition.hyphenation import hyphenation from hyphen import Hyphenator @@ -12,5 +13,5 @@ def test_hypenation(): assert hyphenation("Abend, ", Hyphenator("de_AT")) == None assert hyphenation("Abend.", Hyphenator("de_AT")) == None - - +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/pytest/modules/UltraStar/test_ultrastar_writer.py b/pytest/modules/UltraStar/test_ultrastar_writer.py index bad111e..136e6e6 100644 --- a/pytest/modules/UltraStar/test_ultrastar_writer.py +++ b/pytest/modules/UltraStar/test_ultrastar_writer.py @@ -1,3 +1,5 @@ +"""Tests for the ultrastar_writer.py module.""" + import unittest from unittest.mock import patch, mock_open from src.modules.Ultrastar.ultrastar_writer import create_ultrastar_txt_from_automation @@ -131,3 +133,6 @@ class TestCreateUltrastarTxt(unittest.TestCase): default_ultrastar_class, bpm ) return mock_file + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/src/modules/Speech_Recognition/Whisper.py b/src/modules/Speech_Recognition/Whisper.py index a827f3e..0232c8f 100644 --- a/src/modules/Speech_Recognition/Whisper.py +++ b/src/modules/Speech_Recognition/Whisper.py @@ -11,7 +11,8 @@ from src.modules.console_colors import ( from src.modules.Speech_Recognition.TranscribedData import TranscribedData -def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_name: str = None) -> (list[TranscribedData], str): +def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_name: str = None) -> ( +list[TranscribedData], str): """Transcribe with whisper""" print( @@ -25,7 +26,6 @@ def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_nam "float16" if device == "cuda" else "int8" ) # change to "int8" if low on GPU mem (may reduce accuracy) - # transcribe with original whisper loaded_whisper_model = whisperx.load_model( model, device=device, compute_type=compute_type ) @@ -57,8 +57,13 @@ def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_nam return_char_alignments=False, ) - transcribed_data = [] + transcribed_data = convert_to_transcribed_data(result_aligned) + return transcribed_data, language + + +def convert_to_transcribed_data(result_aligned): + transcribed_data = [] for segment in result_aligned["segments"]: for obj in segment["words"]: if len(obj) < 4: @@ -67,7 +72,6 @@ def transcribe_with_whisper(audio_path: str, model: str, device="cpu", model_nam ) continue vtd = TranscribedData(obj) # create custom Word object - vtd.word = vtd.word + " " + vtd.word = vtd.word + " " # add space to end of word transcribed_data.append(vtd) # and add it to list - - return transcribed_data, language + return transcribed_data