From e623ea254a9ea0c661ca4baa9137485c68a91886 Mon Sep 17 00:00:00 2001 From: TheZet Date: Sun, 22 Dec 2024 09:04:41 -0500 Subject: [PATCH] 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