Spaces:
Running
Running
twitchard
commited on
improve 11labs error handling
Browse files- src/app.py +1 -1
- src/integrations/elevenlabs_api.py +15 -1
src/app.py
CHANGED
@@ -156,7 +156,7 @@ def text_to_speech(
|
|
156 |
except ElevenLabsError as ee:
|
157 |
logger.error(f"ElevenLabsError while synthesizing speech from text: {str(ee)}")
|
158 |
raise gr.Error(
|
159 |
-
"There was an issue communicating with the Elevenlabs API. Please try again later."
|
160 |
)
|
161 |
except HumeError as he:
|
162 |
logger.error(f"HumeError while synthesizing speech from text: {str(he)}")
|
|
|
156 |
except ElevenLabsError as ee:
|
157 |
logger.error(f"ElevenLabsError while synthesizing speech from text: {str(ee)}")
|
158 |
raise gr.Error(
|
159 |
+
f"There was an issue communicating with the Elevenlabs API. {ee.message} Please try again later."
|
160 |
)
|
161 |
except HumeError as he:
|
162 |
logger.error(f"HumeError while synthesizing speech from text: {str(he)}")
|
src/integrations/elevenlabs_api.py
CHANGED
@@ -27,6 +27,7 @@ from typing import Optional
|
|
27 |
|
28 |
# Third-Party Library Imports
|
29 |
from elevenlabs import ElevenLabs, TextToVoiceCreatePreviewsRequestOutputFormat
|
|
|
30 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
31 |
|
32 |
# Local Application Imports
|
@@ -63,6 +64,14 @@ class ElevenLabsError(Exception):
|
|
63 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
64 |
super().__init__(message)
|
65 |
self.original_exception = original_exception
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
|
68 |
# Initialize the ElevenLabs client
|
@@ -120,7 +129,12 @@ def text_to_speech_with_elevenlabs(character_description: str, text: str) -> byt
|
|
120 |
return None, save_base64_audio_to_file(base64_audio, filename)
|
121 |
|
122 |
except Exception as e:
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
124 |
raise ElevenLabsError(
|
125 |
message=f"Failed to synthesize speech with ElevenLabs: {e}",
|
126 |
original_exception=e,
|
|
|
27 |
|
28 |
# Third-Party Library Imports
|
29 |
from elevenlabs import ElevenLabs, TextToVoiceCreatePreviewsRequestOutputFormat
|
30 |
+
from elevenlabs.core import ApiError
|
31 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
32 |
|
33 |
# Local Application Imports
|
|
|
64 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
65 |
super().__init__(message)
|
66 |
self.original_exception = original_exception
|
67 |
+
self.message = message
|
68 |
+
|
69 |
+
|
70 |
+
class UnretryableElevenLabsError(ElevenLabsError):
|
71 |
+
"""Custom exception for errors related to the ElevenLabs TTS API that should not be retried."""
|
72 |
+
|
73 |
+
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
74 |
+
super().__init__(message, original_exception)
|
75 |
|
76 |
|
77 |
# Initialize the ElevenLabs client
|
|
|
129 |
return None, save_base64_audio_to_file(base64_audio, filename)
|
130 |
|
131 |
except Exception as e:
|
132 |
+
if isinstance(e, ApiError):
|
133 |
+
if e.status_code >= 400 and e.status_code < 500:
|
134 |
+
raise UnretryableElevenLabsError(
|
135 |
+
message=f"Failed to synthesize speech with ElevenLabs: \"{e.body['detail']['message']}\"",
|
136 |
+
original_exception=e,
|
137 |
+
) from e
|
138 |
raise ElevenLabsError(
|
139 |
message=f"Failed to synthesize speech with ElevenLabs: {e}",
|
140 |
original_exception=e,
|