Spaces:
Running
Running
zach
commited on
Commit
·
2f050a8
1
Parent(s):
3885d80
Improve Hume errors, fix error message text in UI
Browse files- src/app.py +4 -4
- src/integrations/anthropic_api.py +3 -4
- src/integrations/elevenlabs_api.py +2 -3
- src/integrations/hume_api.py +17 -2
src/app.py
CHANGED
@@ -64,11 +64,11 @@ def generate_text(
|
|
64 |
except AnthropicError as ae:
|
65 |
logger.error(f"AnthropicError while generating text: {str(ae)}")
|
66 |
raise gr.Error(
|
67 |
-
f
|
68 |
)
|
69 |
except Exception as e:
|
70 |
logger.error(f"Unexpected error while generating text: {e}")
|
71 |
-
raise gr.Error("Failed to generate text. Please try again.")
|
72 |
|
73 |
|
74 |
def text_to_speech(
|
@@ -156,12 +156,12 @@ 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 |
-
f
|
160 |
)
|
161 |
except HumeError as he:
|
162 |
logger.error(f"HumeError while synthesizing speech from text: {str(he)}")
|
163 |
raise gr.Error(
|
164 |
-
|
165 |
)
|
166 |
except Exception as e:
|
167 |
logger.error(f"Unexpected error during TTS generation: {e}")
|
|
|
64 |
except AnthropicError as ae:
|
65 |
logger.error(f"AnthropicError while generating text: {str(ae)}")
|
66 |
raise gr.Error(
|
67 |
+
f'There was an issue communicating with the Anthropic API: "{ae.message}"'
|
68 |
)
|
69 |
except Exception as e:
|
70 |
logger.error(f"Unexpected error while generating text: {e}")
|
71 |
+
raise gr.Error("Failed to generate text. Please try again later.")
|
72 |
|
73 |
|
74 |
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 |
+
f'There was an issue communicating with the Elevenlabs API: "{ee.message}"'
|
160 |
)
|
161 |
except HumeError as he:
|
162 |
logger.error(f"HumeError while synthesizing speech from text: {str(he)}")
|
163 |
raise gr.Error(
|
164 |
+
f'There was an issue communicating with the Hume API: "{he.message}"'
|
165 |
)
|
166 |
except Exception as e:
|
167 |
logger.error(f"Unexpected error during TTS generation: {e}")
|
src/integrations/anthropic_api.py
CHANGED
@@ -128,7 +128,7 @@ class AnthropicError(Exception):
|
|
128 |
|
129 |
|
130 |
class UnretryableAnthropicError(AnthropicError):
|
131 |
-
"""Custom exception for errors related to the Anthropic
|
132 |
|
133 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
134 |
super().__init__(message, original_exception)
|
@@ -198,14 +198,13 @@ def generate_text_with_claude(character_description: str) -> str:
|
|
198 |
return str(blocks or "No content generated.")
|
199 |
|
200 |
except Exception as e:
|
201 |
-
logger.exception(f"Error generating text with the Anthropic API: {str(e)}")
|
202 |
if isinstance(e, APIError):
|
203 |
if e.status_code >= 400 and e.status_code < 500:
|
204 |
raise UnretryableAnthropicError(
|
205 |
-
message=f"
|
206 |
original_exception=e,
|
207 |
) from e
|
208 |
raise AnthropicError(
|
209 |
-
message=("
|
210 |
original_exception=e,
|
211 |
) from e
|
|
|
128 |
|
129 |
|
130 |
class UnretryableAnthropicError(AnthropicError):
|
131 |
+
"""Custom exception for errors related to the Anthropic API that should not be retried."""
|
132 |
|
133 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
134 |
super().__init__(message, original_exception)
|
|
|
198 |
return str(blocks or "No content generated.")
|
199 |
|
200 |
except Exception as e:
|
|
|
201 |
if isinstance(e, APIError):
|
202 |
if e.status_code >= 400 and e.status_code < 500:
|
203 |
raise UnretryableAnthropicError(
|
204 |
+
message=f"\"{e.body['error']['message']}\"",
|
205 |
original_exception=e,
|
206 |
) from e
|
207 |
raise AnthropicError(
|
208 |
+
message=(f"{e.message}"),
|
209 |
original_exception=e,
|
210 |
) from e
|
src/integrations/elevenlabs_api.py
CHANGED
@@ -129,14 +129,13 @@ def text_to_speech_with_elevenlabs(character_description: str, text: str) -> byt
|
|
129 |
return None, save_base64_audio_to_file(base64_audio, filename)
|
130 |
|
131 |
except Exception as e:
|
132 |
-
logger.exception(f"Error generating text with the ElevenLabs API: {str(e)}")
|
133 |
if isinstance(e, ApiError):
|
134 |
if e.status_code >= 400 and e.status_code < 500:
|
135 |
raise UnretryableElevenLabsError(
|
136 |
-
message=f"
|
137 |
original_exception=e,
|
138 |
) from e
|
139 |
raise ElevenLabsError(
|
140 |
-
message=f"
|
141 |
original_exception=e,
|
142 |
) from e
|
|
|
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"{e.body['detail']['message']}",
|
136 |
original_exception=e,
|
137 |
) from e
|
138 |
raise ElevenLabsError(
|
139 |
+
message=f"{e}",
|
140 |
original_exception=e,
|
141 |
) from e
|
src/integrations/hume_api.py
CHANGED
@@ -27,6 +27,7 @@ from typing import Literal, Optional
|
|
27 |
|
28 |
# Third-Party Library Imports
|
29 |
import requests
|
|
|
30 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
31 |
|
32 |
# Local Application Imports
|
@@ -70,6 +71,15 @@ class HumeConfig:
|
|
70 |
class HumeError(Exception):
|
71 |
"""Custom exception for errors related to the Hume TTS API."""
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
74 |
super().__init__(message)
|
75 |
self.original_exception = original_exception
|
@@ -136,8 +146,13 @@ def text_to_speech_with_hume(character_description: str, text: str) -> bytes:
|
|
136 |
return generation_id, save_base64_audio_to_file(base64_audio, filename)
|
137 |
|
138 |
except Exception as e:
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
140 |
raise HumeError(
|
141 |
-
message=f"
|
142 |
original_exception=e,
|
143 |
) from e
|
|
|
27 |
|
28 |
# Third-Party Library Imports
|
29 |
import requests
|
30 |
+
from requests.exceptions import HTTPError
|
31 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
32 |
|
33 |
# Local Application Imports
|
|
|
71 |
class HumeError(Exception):
|
72 |
"""Custom exception for errors related to the Hume TTS API."""
|
73 |
|
74 |
+
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
75 |
+
super().__init__(message)
|
76 |
+
self.original_exception = original_exception
|
77 |
+
self.message = message
|
78 |
+
|
79 |
+
|
80 |
+
class UnretryableHumeError(HumeError):
|
81 |
+
"""Custom exception for errors related to the Hume TTS API that should not be retried."""
|
82 |
+
|
83 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
84 |
super().__init__(message)
|
85 |
self.original_exception = original_exception
|
|
|
146 |
return generation_id, save_base64_audio_to_file(base64_audio, filename)
|
147 |
|
148 |
except Exception as e:
|
149 |
+
if isinstance(e, HTTPError):
|
150 |
+
if e.response.status_code >= 400 and e.response.status_code < 500:
|
151 |
+
raise UnretryableHumeError(
|
152 |
+
message=f'"{e.response.text}"',
|
153 |
+
original_exception=e,
|
154 |
+
) from e
|
155 |
raise HumeError(
|
156 |
+
message=f"{e}",
|
157 |
original_exception=e,
|
158 |
) from e
|