Spaces:
Running
Running
zach
commited on
Commit
·
3885d80
1
Parent(s):
4772f0e
Improve Anthropic error handling, add UnretryableAnthropicError
Browse files- src/app.py +1 -1
- src/integrations/anthropic_api.py +18 -9
- src/integrations/elevenlabs_api.py +1 -0
src/app.py
CHANGED
@@ -64,7 +64,7 @@ def generate_text(
|
|
64 |
except AnthropicError as ae:
|
65 |
logger.error(f"AnthropicError while generating text: {str(ae)}")
|
66 |
raise gr.Error(
|
67 |
-
"There was an issue communicating with the Anthropic API. Please try again later."
|
68 |
)
|
69 |
except Exception as e:
|
70 |
logger.error(f"Unexpected error while generating text: {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} Please try again later."
|
68 |
)
|
69 |
except Exception as e:
|
70 |
logger.error(f"Unexpected error while generating text: {e}")
|
src/integrations/anthropic_api.py
CHANGED
@@ -24,7 +24,7 @@ import logging
|
|
24 |
from typing import List, Optional, Union
|
25 |
|
26 |
# Third-Party Library Imports
|
27 |
-
from anthropic import Anthropic
|
28 |
from anthropic.types import Message, ModelParam, TextBlock
|
29 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
30 |
|
@@ -124,6 +124,14 @@ class AnthropicError(Exception):
|
|
124 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
125 |
super().__init__(message)
|
126 |
self.original_exception = original_exception
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
|
129 |
# Initialize the Anthropic client
|
@@ -190,13 +198,14 @@ def generate_text_with_claude(character_description: str) -> str:
|
|
190 |
return str(blocks or "No content generated.")
|
191 |
|
192 |
except Exception as e:
|
193 |
-
logger.exception(f"Error generating text with the Anthropic API: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
raise AnthropicError(
|
195 |
-
message=(
|
196 |
-
f"Error generating text with Anthropic: {e}. "
|
197 |
-
f'HTTP Status: {getattr(response, "status", "N/A")}. '
|
198 |
-
f"Prompt (truncated): {truncate_text(prompt)}. "
|
199 |
-
f"Model: {anthropic_config.model}, Max tokens: {anthropic_config.max_tokens}"
|
200 |
-
),
|
201 |
original_exception=e,
|
202 |
-
)
|
|
|
24 |
from typing import List, Optional, Union
|
25 |
|
26 |
# Third-Party Library Imports
|
27 |
+
from anthropic import APIError, Anthropic
|
28 |
from anthropic.types import Message, ModelParam, TextBlock
|
29 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
30 |
|
|
|
124 |
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
125 |
super().__init__(message)
|
126 |
self.original_exception = original_exception
|
127 |
+
self.message = message
|
128 |
+
|
129 |
+
|
130 |
+
class UnretryableAnthropicError(AnthropicError):
|
131 |
+
"""Custom exception for errors related to the Anthropic TTS API that should not be retried."""
|
132 |
+
|
133 |
+
def __init__(self, message: str, original_exception: Optional[Exception] = None):
|
134 |
+
super().__init__(message, original_exception)
|
135 |
|
136 |
|
137 |
# Initialize the Anthropic client
|
|
|
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"Failed to generate text with Anthropic: \"{e.body['error']['message']}\"",
|
206 |
+
original_exception=e,
|
207 |
+
) from e
|
208 |
raise AnthropicError(
|
209 |
+
message=("Failed to generate text with Anthropic: {e}. "),
|
|
|
|
|
|
|
|
|
|
|
210 |
original_exception=e,
|
211 |
+
) from e
|
src/integrations/elevenlabs_api.py
CHANGED
@@ -129,6 +129,7 @@ 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 |
if isinstance(e, ApiError):
|
133 |
if e.status_code >= 400 and e.status_code < 500:
|
134 |
raise UnretryableElevenLabsError(
|
|
|
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(
|