Skip to content

Instantly share code, notes, and snippets.

@ranman
Created October 22, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranman/6b02d82c5260228688a3a11f553bf062 to your computer and use it in GitHub Desktop.
Save ranman/6b02d82c5260228688a3a11f553bf062 to your computer and use it in GitHub Desktop.
example of translate and polly
import boto3
import random
translate = boto3.client("translate")
polly = boto3.client("polly")
def get_valid_voices_for_language(language_code="en"):
return [
voice['Id'] for voice in polly.describe_voices()['Voices']
if voice['LanguageCode'].startswith(language_code)
]
def translate_and_voice(text):
# Grab a random voice in the target language
voice = random.choice(get_valid_voices_for_language(language_code="en"))
translated = translate.translate_text(
SourceLanguageCode="auto",
TargetLanguageCode="en",
Text=text
)
# return the audiostream
return polly.synthesize_speech(
OutputFormat="mp3",
Text=translated['TranslatedText'],
VoiceId=voice
)['AudioStream']
def lambda_handler(event, context):
audio = translate_and_voice(event['text'])
# return raw bytes
return resp['AudioStream'].read()
# you could also upload that to s3 or do whatever you want with the raw audio stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment