From 5f193dc29e44e1206cfcaf19ddd0d7709439b708 Mon Sep 17 00:00:00 2001 From: jhj0517 <97279763+jhj0517@users.noreply.github.com> Date: Sat, 2 Nov 2024 15:52:58 +0900 Subject: [PATCH 1/4] Refactor return type --- modules/whisper/base_transcription_pipeline.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/whisper/base_transcription_pipeline.py b/modules/whisper/base_transcription_pipeline.py index 35605f5..2c475dd 100644 --- a/modules/whisper/base_transcription_pipeline.py +++ b/modules/whisper/base_transcription_pipeline.py @@ -179,7 +179,7 @@ def transcribe_file(self, add_timestamp: bool = True, progress=gr.Progress(), *pipeline_params, - ) -> list: + ) -> Tuple[str, str]: """ Write subtitle file from Files @@ -250,7 +250,7 @@ def transcribe_file(self, result_str = f"Done in {self.format_time(total_time)}! Subtitle is in the outputs folder.\n\n{total_result}" result_file_path = [info['path'] for info in files_info.values()] - return [result_str, result_file_path] + return result_str, result_file_path except Exception as e: print(f"Error transcribing file: {e}") @@ -264,7 +264,7 @@ def transcribe_mic(self, add_timestamp: bool = True, progress=gr.Progress(), *pipeline_params, - ) -> list: + ) -> Tuple[str, str]: """ Write subtitle file from microphone @@ -314,7 +314,7 @@ def transcribe_mic(self, ) result_str = f"Done in {self.format_time(time_for_task)}! Subtitle file is in the outputs folder.\n\n{subtitle}" - return [result_str, file_path] + return result_str, file_path except Exception as e: print(f"Error transcribing mic: {e}") raise @@ -327,7 +327,7 @@ def transcribe_youtube(self, add_timestamp: bool = True, progress=gr.Progress(), *pipeline_params, - ) -> list: + ) -> Tuple[str, str]: """ Write subtitle file from Youtube @@ -385,7 +385,7 @@ def transcribe_youtube(self, if os.path.exists(audio): os.remove(audio) - return [result_str, file_path] + return result_str, file_path except Exception as e: print(f"Error transcribing youtube: {e}") From 4ceaed1cd2fddb24f4a9107b39092e013d4eadd7 Mon Sep 17 00:00:00 2001 From: jhj0517 <97279763+jhj0517@users.noreply.github.com> Date: Sat, 2 Nov 2024 16:00:49 +0900 Subject: [PATCH 2/4] Refactor return type --- modules/whisper/base_transcription_pipeline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/whisper/base_transcription_pipeline.py b/modules/whisper/base_transcription_pipeline.py index 2c475dd..4882abd 100644 --- a/modules/whisper/base_transcription_pipeline.py +++ b/modules/whisper/base_transcription_pipeline.py @@ -179,7 +179,7 @@ def transcribe_file(self, add_timestamp: bool = True, progress=gr.Progress(), *pipeline_params, - ) -> Tuple[str, str]: + ) -> Tuple[str, List]: """ Write subtitle file from Files From 5fee9a3edb35828f06824a3850c2b5391446cac1 Mon Sep 17 00:00:00 2001 From: jhj0517 <97279763+jhj0517@users.noreply.github.com> Date: Sat, 2 Nov 2024 16:35:16 +0900 Subject: [PATCH 3/4] Caculate wer --- .github/workflows/ci.yml | 2 +- tests/test_config.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 076f0fc..571558c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: run: sudo apt-get update && sudo apt-get install -y git ffmpeg - name: Install dependencies - run: pip install -r requirements.txt pytest + run: pip install -r requirements.txt pytest jiwer - name: Run test run: python -m pytest -rs tests \ No newline at end of file diff --git a/tests/test_config.py b/tests/test_config.py index ba52892..f82e4f1 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,15 +1,16 @@ import functools +import jiwer +import os +import torch from modules.utils.paths import * from modules.utils.youtube_manager import * -import os -import torch - TEST_FILE_DOWNLOAD_URL = "https://github.com/jhj0517/whisper_flutter_new/raw/main/example/assets/jfk.wav" TEST_FILE_PATH = os.path.join(WEBUI_DIR, "tests", "jfk.wav") +TEST_ANSWER = "And so my fellow Americans ask not what your country can do for you ask what you can do for your country" TEST_YOUTUBE_URL = "https://www.youtube.com/watch?v=4WEQtgnBu0I&ab_channel=AndriaFitzer" -TEST_WHISPER_MODEL = "tiny.en" +TEST_WHISPER_MODEL = "tiny" TEST_UVR_MODEL = "UVR-MDX-NET-Inst_HQ_4" TEST_NLLB_MODEL = "facebook/nllb-200-distilled-600M" TEST_SUBTITLE_SRT_PATH = os.path.join(WEBUI_DIR, "tests", "test_srt.srt") @@ -34,3 +35,6 @@ def is_pytube_detected_bot(url: str = TEST_YOUTUBE_URL): print(f"Pytube has detected as a bot: {e}") return True + +def calculate_wer(answer, prediction): + return jiwer.wer(answer, prediction) From c7bfcf23169f59559838b37e4094b59b37df738e Mon Sep 17 00:00:00 2001 From: jhj0517 <97279763+jhj0517@users.noreply.github.com> Date: Sat, 2 Nov 2024 16:35:46 +0900 Subject: [PATCH 4/4] Calculate WER between gen result & answer --- tests/test_transcription.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/test_transcription.py b/tests/test_transcription.py index e9591d0..bc5267c 100644 --- a/tests/test_transcription.py +++ b/tests/test_transcription.py @@ -1,5 +1,6 @@ from modules.whisper.whisper_factory import WhisperFactory from modules.whisper.data_classes import * +from modules.utils.subtitle_manager import read_file from modules.utils.paths import WEBUI_DIR from test_config import * @@ -28,6 +29,10 @@ def test_transcribe( if not os.path.exists(audio_path): download_file(TEST_FILE_DOWNLOAD_URL, audio_path_dir) + answer = TEST_ANSWER + if diarization: + answer = "SPEAKER_00|"+TEST_ANSWER + whisper_inferencer = WhisperFactory.create_whisper_inference( whisper_type=whisper_type, ) @@ -54,7 +59,7 @@ def test_transcribe( ), ).to_list() - subtitle_str, file_path = whisper_inferencer.transcribe_file( + subtitle_str, file_paths = whisper_inferencer.transcribe_file( [audio_path], None, "SRT", @@ -62,12 +67,11 @@ def test_transcribe( gr.Progress(), *hparams, ) - - assert isinstance(subtitle_str, str) and subtitle_str - assert isinstance(file_path[0], str) and file_path + subtitle = read_file(file_paths[0]).split("\n") + assert calculate_wer(answer, subtitle[2].strip().replace(",", "").replace(".", "")) < 0.1 if not is_pytube_detected_bot(): - whisper_inferencer.transcribe_youtube( + subtitle_str, file_path = whisper_inferencer.transcribe_youtube( TEST_YOUTUBE_URL, "SRT", False, @@ -75,17 +79,17 @@ def test_transcribe( *hparams, ) assert isinstance(subtitle_str, str) and subtitle_str - assert isinstance(file_path[0], str) and file_path + assert os.path.exists(file_path) - whisper_inferencer.transcribe_mic( + subtitle_str, file_path = whisper_inferencer.transcribe_mic( audio_path, "SRT", False, gr.Progress(), *hparams, ) - assert isinstance(subtitle_str, str) and subtitle_str - assert isinstance(file_path[0], str) and file_path + subtitle = read_file(file_path).split("\n") + assert calculate_wer(answer, subtitle[2].strip().replace(",", "").replace(".", "")) < 0.1 def download_file(url, save_dir):