Created
October 22, 2018 22:33
-
-
Save ranman/6b02d82c5260228688a3a11f553bf062 to your computer and use it in GitHub Desktop.
example of translate and polly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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