- Add test for Whisper.py

This commit is contained in:
Vadim committed 2023-06-20 00:59:52 +02:00
1 parent efab815fa0
commit b755f0bbbc
4 files changed
+74 -8

No files matched your search

@@ -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()
@@ -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()
@@ -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()
+10 -6
View File
@@ -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