From 323fda90a99e2039d671a73c057966e9a636953d Mon Sep 17 00:00:00 2001 From: ThioJoe <12518330+ThioJoe@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:56:42 -0700 Subject: [PATCH] Further improve ElevenLabs error handling Fix error handling so it works with being async --- Scripts/TTS.py | 27 ++++++++++++++++++--------- Scripts/audio_builder.py | 6 +++++- main.py | 6 +++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Scripts/TTS.py b/Scripts/TTS.py index 3d674d6..6129efe 100644 --- a/Scripts/TTS.py +++ b/Scripts/TTS.py @@ -209,15 +209,24 @@ async def synthesize_text_elevenlabs_async_http(text, voiceID, modelID, apiKey=E break audio_bytes += chunk else: - print(f"\n\nERROR: ElevenLabs API returned code: {response.status} - Reason: {response.reason}") - if response.status == 401: - print(" > ElevenLabs did not accept the API key or you are unauthorized to use that voice.") - print(" > Did you set the correct ElevenLabs API key in the cloud_service_settings.ini file?") - elif response.status == 400: - print(" > Did you set the correct ElevenLabs API key in the cloud_service_settings.ini file?") - elif response.status == 429: - print(" > You may have exceeded the ElevenLabs API rate limit. Did you set the 'elevenlabs_max_concurrent' setting too high for your plan?") - exit() + try: + error_message = await response.text() + error_dict = json.loads(error_message) + print(f"\n\nERROR: ElevenLabs API returned code: {response.status} - {response.reason}") + print(f"Returned Error Status: {error_dict['detail']['status']}") + print(f"Returned Error Message: {error_dict['detail']['message']}") + print(f"Test: {error_dict['detail']['test']}") + except KeyError: + if response.status == 401: + print(" > ElevenLabs did not accept the API key or you are unauthorized to use that voice.") + print(" > Did you set the correct ElevenLabs API key in the cloud_service_settings.ini file?\n") + elif response.status == 400: + print(" > Did you set the correct ElevenLabs API key in the cloud_service_settings.ini file?\n") + elif response.status == 429: + print(" > You may have exceeded the ElevenLabs API rate limit. Did you set the 'elevenlabs_max_concurrent' setting too high for your plan?\n") + except Exception as ex: + print(f"ElevenLabs API error occurred.\n") + return None return audio_bytes diff --git a/Scripts/audio_builder.py b/Scripts/audio_builder.py index aff96d5..ab53a56 100644 --- a/Scripts/audio_builder.py +++ b/Scripts/audio_builder.py @@ -120,7 +120,11 @@ def build_audio(subsDict, langDict, totalAudioLength, twoPassVoiceSynth=False): subsDict[key]['TTS_FilePath_Trimmed'] = filePathTrimmed # Trim the clip and re-write file - rawClip = AudioSegment.from_file(value['TTS_FilePath'], format="mp3") + try: + rawClip = AudioSegment.from_file(value['TTS_FilePath'], format="mp3") + except KeyError: + print("\nERROR: An expected file was not found. This is likely because the TTS service failed to synthesize the audio. Refer to any error messages above.") + sys.exit() trimmedClip = trim_clip(rawClip) if config['debug_mode']: trimmedClip.export(filePathTrimmed, format="wav") diff --git a/main.py b/main.py index d88ac49..bdbe3b2 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ # License: GPLv3 # NOTE: By contributing to this project, you agree to the terms of the GPLv3 license, and agree to grant the project owner the right to also provide or sell this software, including your contribution, to anyone under any other license, with no compensation to you. -version = '0.17.1' +version = '0.17.2' print(f"------- 'Auto Synced Translated Dubs' script by ThioJoe - Release version {version} -------") # Import other files @@ -306,6 +306,10 @@ def process_language(langData, processedCount, totalLanguages): #======================================== Main Program ================================================ +# Set asyncio event loop policy to WindowsSelectorEventLoopPolicy if on Windows to avoid errors +if sys.platform == 'win32': + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + # Counter for number of languages processed processedCount = 0 totalLanguages = len(batchSettings)