From 302b1bcd172547458572c2c94584c0a5d7f1646a Mon Sep 17 00:00:00 2001 From: TheZet Date: Thu, 19 Dec 2024 19:21:49 -0500 Subject: [PATCH 1/2] MonkeyPatch FasterWhisperPipeline to detect low confidence --- src/Settings.py | 2 +- src/modules/Speech_Recognition/Whisper.py | 57 +++++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/Settings.py b/src/Settings.py index 2c9e68f..ff53ccc 100644 --- a/src/Settings.py +++ b/src/Settings.py @@ -11,7 +11,7 @@ from modules.Ultrastar.ultrastar_txt import FormatVersion @dataclass class Settings: APP_VERSION = "0.0.12" - + CONFIDENCE_THRESHOLD = 0.6 create_midi = True create_plot = False create_audio_chunks = False diff --git a/src/modules/Speech_Recognition/Whisper.py b/src/modules/Speech_Recognition/Whisper.py index 7778fef..11806cd 100644 --- a/src/modules/Speech_Recognition/Whisper.py +++ b/src/modules/Speech_Recognition/Whisper.py @@ -1,7 +1,6 @@ """Whisper Speech Recognition Module""" - -import sys - +import inspect +import textwrap import torch import whisperx from enum import Enum @@ -52,6 +51,24 @@ def number_to_words(line,language='en'): out_tokens.append(token) return ''.join(out_tokens) +def replace_code_lines(source, start_token, end_token, + replacement, escape_tokens=True): + """Replace the source code between `start_token` and `end_token` + in `source` with `replacement`. The `start_token` portion is included + in the replaced code. If `escape_tokens` is True (default), + escape the tokens to avoid them being treated as a regular expression.""" + + if escape_tokens: + start_token = re.escape(start_token) + end_token = re.escape(end_token) + + def replace_with_indent(match): + indent = match.group(1) + return textwrap.indent(replacement, indent) + + return re.sub(r"^(\s+)({}[\s\S]+?)(?=^\1{})".format(start_token, end_token), + replace_with_indent, source, flags=re.MULTILINE) + def transcribe_with_whisper( audio_path: str, model: WhisperModel, @@ -63,6 +80,40 @@ def transcribe_with_whisper( keep_numbers: bool = False, ) -> TranscriptionResult: """Transcribe with whisper""" + # Info: Monkey Patch FasterWhisperPipeline.detect_language to include error handling for low confidence Test with https://www.youtube.com/watch?v=tcV7VN3l3bY + src = textwrap.dedent(inspect.getsource(whisperx.asr.FasterWhisperPipeline.detect_language)) + # Replace the relevant part of the method + start_token = "if audio.shape[0] < N_SAMPLES:" + end_token = "return language" + replacement = """\ + #Added imports + from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, red_highlighted + from Settings import Settings + #End Import addition + if audio.shape[0] < N_SAMPLES: + print("Warning: audio is shorter than 30s, language detection may be inaccurate.") + model_n_mels = self.model.feat_kwargs.get("feature_size") + segment = log_mel_spectrogram(audio[: N_SAMPLES], + n_mels=model_n_mels if model_n_mels is not None else 80, + padding=0 if audio.shape[0] >= N_SAMPLES else N_SAMPLES - audio.shape[0]) + encoder_output = self.model.encode(segment) + results = self.model.model.detect_language(encoder_output) + language_token, language_probability = results[0][0] + language = language_token[2:-2] + print(f"Detected language: {language} ({language_probability:.2f}) in first 30s of audio...") + #Added handling for low detection probability (test with https://www.youtube.com/watch?v=tcV7VN3l3bY) + if language_probability < Settings.CONFIDENCE_THRESHOLD: + print(f"{ULTRASINGER_HEAD} {red_highlighted('Warning:')} Language detection probability for detected language {language} is below {Settings.CONFIDENCE_THRESHOLD}, results may be inaccurate.") + language_response = input(f"{ULTRASINGER_HEAD} Do you want to continue with {language} (default) or override with another language (y)? (y/n): ").strip().lower() == 'y' + if language_response: + language = input(f"{ULTRASINGER_HEAD} Please enter the language code for the language you want to use (e.g. 'en', 'de', 'es', etc.): ").strip().lower() + #End addition + """ + new_src = replace_code_lines(src, start_token, end_token, replacement) + # Compile it and execute it in the target module's namespace + exec(compile(new_src, "", "exec"), whisperx.asr.__dict__) + whisperx.asr.FasterWhisperPipeline.detect_language = whisperx.asr.detect_language + #End Monkey Patch # Info: Regardless of the audio sampling rate used in the original audio file, whisper resample the audio signal to 16kHz (via ffmpeg). So the standard input from (44.1 or 48 kHz) should work. From e623ea254a9ea0c661ca4baa9137485c68a91886 Mon Sep 17 00:00:00 2001 From: TheZet Date: Sun, 22 Dec 2024 09:04:41 -0500 Subject: [PATCH 2/2] Addressed PR Comments --- requirements.in | 3 ++- requirements.txt | 6 +++++- src/Settings.py | 3 ++- src/modules/Speech_Recognition/Whisper.py | 25 ++++++++++++++++++++--- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/requirements.in b/requirements.in index d89ff24..618ba4d 100644 --- a/requirements.in +++ b/requirements.in @@ -30,4 +30,5 @@ music21 unidecode dataclasses dataclasses_json -num2words \ No newline at end of file +num2words +inputimeout \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2107d5d..3397548 100644 --- a/requirements.txt +++ b/requirements.txt @@ -71,7 +71,9 @@ contourpy==1.2.1 crepe==0.0.15 # via -r requirements.in ctranslate2==4.4.0 - # via faster-whisper + # via + # faster-whisper + # whisperx cycler==0.12.1 # via matplotlib dataclasses==0.6 @@ -166,6 +168,8 @@ imageio==2.34.2 # via crepe iniconfig==2.0.0 # via pytest +inputimeout==1.0.4 + # via -r requirements.in isort==5.13.2 # via # -r requirements.in diff --git a/src/Settings.py b/src/Settings.py index ff53ccc..9a68acf 100644 --- a/src/Settings.py +++ b/src/Settings.py @@ -10,8 +10,9 @@ from modules.Ultrastar.ultrastar_txt import FormatVersion @dataclass_json @dataclass class Settings: - APP_VERSION = "0.0.12" + APP_VERSION = "0.0.13-dev1" CONFIDENCE_THRESHOLD = 0.6 + CONFIDENCE_PROMPT_TIMEOUT = 4 create_midi = True create_plot = False create_audio_chunks = False diff --git a/src/modules/Speech_Recognition/Whisper.py b/src/modules/Speech_Recognition/Whisper.py index 11806cd..7896134 100644 --- a/src/modules/Speech_Recognition/Whisper.py +++ b/src/modules/Speech_Recognition/Whisper.py @@ -80,7 +80,7 @@ def transcribe_with_whisper( keep_numbers: bool = False, ) -> TranscriptionResult: """Transcribe with whisper""" - # Info: Monkey Patch FasterWhisperPipeline.detect_language to include error handling for low confidence Test with https://www.youtube.com/watch?v=tcV7VN3l3bY + # Info: Monkey Patch FasterWhisperPipeline.detect_language to include error handling for low confidence src = textwrap.dedent(inspect.getsource(whisperx.asr.FasterWhisperPipeline.detect_language)) # Replace the relevant part of the method start_token = "if audio.shape[0] < N_SAMPLES:" @@ -89,6 +89,7 @@ def transcribe_with_whisper( #Added imports from modules.console_colors import ULTRASINGER_HEAD, blue_highlighted, red_highlighted from Settings import Settings + from inputimeout import inputimeout, TimeoutOccurred #End Import addition if audio.shape[0] < N_SAMPLES: print("Warning: audio is shorter than 30s, language detection may be inaccurate.") @@ -101,10 +102,28 @@ def transcribe_with_whisper( language_token, language_probability = results[0][0] language = language_token[2:-2] print(f"Detected language: {language} ({language_probability:.2f}) in first 30s of audio...") - #Added handling for low detection probability (test with https://www.youtube.com/watch?v=tcV7VN3l3bY) + #Added handling for low detection probability if language_probability < Settings.CONFIDENCE_THRESHOLD: print(f"{ULTRASINGER_HEAD} {red_highlighted('Warning:')} Language detection probability for detected language {language} is below {Settings.CONFIDENCE_THRESHOLD}, results may be inaccurate.") - language_response = input(f"{ULTRASINGER_HEAD} Do you want to continue with {language} (default) or override with another language (y)? (y/n): ").strip().lower() == 'y' + print(f"{ULTRASINGER_HEAD} Override the language below or re-run with parameter {blue_highlighted('--language xx')} to specify the song language...") + try: + response = inputimeout( + prompt=f"{ULTRASINGER_HEAD} Do you want to continue with {language} (default) or override with another language (y)? (y/n): ", + timeout=Settings.CONFIDENCE_PROMPT_TIMEOUT + ).strip().lower() + except TimeoutOccurred: + import locale + print(f"{ULTRASINGER_HEAD} No user input received in {Settings.CONFIDENCE_PROMPT_TIMEOUT} seconds. Attempting automatic override with system locale.") + print(f"{ULTRASINGER_HEAD} Trying to get language from default locale...") + current_locale = locale.getlocale() + if current_locale[0]: + language_code = current_locale[0][:2].strip().lower() + print(f"{ULTRASINGER_HEAD} Found language code: {language_code} in locale. Setting language to {blue_highlighted(language_code)}...") + language = language_code + else: + print(f"{ULTRASINGER_HEAD} No locale is set.") + response = 'n' + language_response = response == 'y' if language_response: language = input(f"{ULTRASINGER_HEAD} Please enter the language code for the language you want to use (e.g. 'en', 'de', 'es', etc.): ").strip().lower() #End addition