Skip to content

Commit

Permalink
Further improve ElevenLabs error handling
Browse files Browse the repository at this point in the history
Fix error handling so it works with being async
  • Loading branch information
ThioJoe committed Jan 12, 2024
1 parent 56cf830 commit 323fda9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
27 changes: 18 additions & 9 deletions Scripts/TTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion Scripts/audio_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 323fda9

Please sign in to comment.