Skip to content

Commit

Permalink
fix(error): added more specific errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Mar 24, 2024
1 parent f8bb89c commit 0d9d6ab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
9 changes: 5 additions & 4 deletions ayushma/utils/language_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def translate_text(target, text):
return result["translatedText"]
except Exception as e:
print(e)
raise APIException("Translation failed")
raise APIException(f"Translation failed: {e}")


language_code_voice_map = {
Expand Down Expand Up @@ -55,7 +55,8 @@ def text_to_speech(text, language_code, service):
synthesis_input = texttospeech.SynthesisInput(text=text)

voice = texttospeech.VoiceSelectionParams(
language_code=language_code, name=language_code_voice_map[language_code]
language_code=language_code,
name=language_code_voice_map[language_code],
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
Expand All @@ -77,7 +78,7 @@ def text_to_speech(text, language_code, service):
)
return response.read()
else:
raise APIException("Service not supported")
raise APIException("Text-to-speech: service not supported.")
except Exception as e:
print(e)
return None
raise APIException(f"Text-to-speech failed: {e}")
84 changes: 46 additions & 38 deletions ayushma/utils/speech_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ def __init__(self, api_key, language_code):
self.language_code = language_code

def recognize(self, audio):
client = OpenAI(api_key=self.api_key)
transcription = client.audio.transcriptions.create(
model="whisper-1",
# https://github.com/openai/openai-python/tree/main#file-uploads
file=(audio.name, audio.read()),
language=self.language_code.replace("-IN", ""),
# api_version="2020-11-07",
)
return transcription.text
try:
client = OpenAI(api_key=self.api_key)
transcription = client.audio.transcriptions.create(
model="whisper-1",
# https://github.com/openai/openai-python/tree/main#file-uploads
file=(audio.name, audio.read()),
language=self.language_code.replace("-IN", ""),
# api_version="2020-11-07",
)
return transcription.text
except Exception as e:
raise ValueError(f"Failed to recognize speech with whisper engine: {e}")


class GoogleEngine:
Expand All @@ -31,41 +34,47 @@ def __init__(self, api_key, language_code):
self.language_code = language_code

def recognize(self, audio):
client = speech.SpeechClient()
audio_content = audio.file.read()
audio_data = speech.RecognitionAudio(content=audio_content)
try:
client = speech.SpeechClient()
audio_content = audio.file.read()
audio_data = speech.RecognitionAudio(content=audio_content)

config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED,
language_code=self.language_code,
)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED,
language_code=self.language_code,
)

response = client.recognize(config=config, audio=audio_data)
if not response.results:
return ""
return response.results[0].alternatives[0].transcript
response = client.recognize(config=config, audio=audio_data)
if not response.results:
return ""
return response.results[0].alternatives[0].transcript
except Exception as e:
raise ValueError(f"Failed to recognize speech with google engine: {e}")


class SelfHostedEngine:
def __init__(self, api_key, language_code):
self.language_code = language_code

def recognize(self, audio):
response = requests.post(
settings.SELF_HOSTED_ENDPOINT,
files={"audio": audio},
data={
# change this model to get faster results see: https://github.com/coronasafe/care-whisper
"model": "small",
"language": self.language_code.replace("-IN", ""),
},
)

if not response.ok:
print("Failed to recognize speech with self hosted engine")
return ""
response = response.json()
return response["data"]["transcription"].strip()
try:
response = requests.post(
settings.SELF_HOSTED_ENDPOINT,
files={"audio": audio},
data={
# change this model to get faster results see: https://github.com/coronasafe/care-whisper
"model": "small",
"language": self.language_code.replace("-IN", ""),
},
)

if not response.ok:
print("Failed to recognize speech with self hosted engine")
return ""
response = response.json()
return response["data"]["transcription"].strip()
except Exception as e:
raise ValueError(f"Failed to recognize speech with self hosted engine: {e}")


engines = {
Expand All @@ -88,8 +97,7 @@ def speech_to_text(engine_id, audio, language_code):
engine = engine_class(api_key, language_code)
recognized_text = engine.recognize(audio)
if not recognized_text:
raise ValueError("Failed to detect any speech in provided audio")
raise ValueError("Speech-to-text: Failed to recognize speech")
return recognized_text
except Exception as e:
print(f"Failed to recognize speech with {engine_name} engine: {e}")
raise e
raise ValueError(f"Speech-to-text failed with {engine_name} engine: {e}")

0 comments on commit 0d9d6ab

Please sign in to comment.