-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
45 lines (38 loc) · 1.36 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
import boto3
import traceback
import os
import base64
polly_client = boto3.client('polly')
def lambda_handler(event, context):
print('event: ', event)
text = event['text']
voiceId = event['voiceId']
langCode = event['langCode']
speed = event['speed']
ssml_text = f'<speak><prosody rate="{speed}%">{text}</prosody></speak>'
try:
response = polly_client.synthesize_speech(
Text=ssml_text,
TextType='ssml', # 'ssml'|'text'
Engine='neural', # 'standard'|'neural'
LanguageCode=langCode,
OutputFormat='ogg_vorbis', # 'json'|'mp3'|'ogg_vorbis'|'pcm',
VoiceId=voiceId
# SampleRate=16000, # "8000", "16000", "22050", and "24000".
# SpeechMarkTypes= # 'sentence'|'ssml'|'viseme'|'word'
)
encoded_content = base64.b64encode(response['AudioStream'].read()).decode()
# print('encoded_content: ', encoded_content)
except Exception:
err_msg = traceback.format_exc()
print('error message: ', err_msg)
raise Exception ("Not able to create a speech using polly")
return {
'statusCode': 200,
"isBase64Encoded": True,
"headers": {
"Content-Type": "audio/ogg"
},
'body': encoded_content
}