-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTS_initializeTask.py
64 lines (57 loc) · 1.97 KB
/
TTS_initializeTask.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import os
from boto3 import client
# from base64 import b64decode
# from urllib import parse
# base64Decoder function is to be called if messages are sent encoded in base 64.
# This happens when the message is sent by HTML forms, but not necessarily
# when sent with a Javascript fetch request
#
#def base64Decoder(message):
# message = b64decode(message.encode('utf-8'))
# message = message.decode('utf-8')
# return message
# httpTextParser is called when the message payload is encoded in http escape characters
# This happens when the message is sent by HTML forms, but not necessarily
# when sent with a Javascript fetch request
#
#def httpTextParser(body: str)->str: #Unescape http escape chars
# parsed = body[5:]
# parsed = parse.unquote(body)
# parsed = parsed.replace('+',' ') #Turn '+' into whitespace
# return parsed
def lambda_handler(event, context):
try:
body=event['body']
except:
return {
"statusCode": 400,
"body": "ERROR: TTS payload not specified"
}
print(body)
# message = httpTextParser(base64Decoder(body))
message = str(body)
if len(message.split()) > 50 or len(message) > 300:
return {
"statusCode": 413,
"body": "Your message is too long. Try again"
}
polly_client = client('polly')
response = polly_client.start_speech_synthesis_task(
Engine='standard',
LanguageCode='en-US',
OutputFormat='mp3',
OutputS3BucketName= os.environ['BucketName'],
OutputS3KeyPrefix= os.environ['KeyPrefix'],
Text=message,
VoiceId='Amy'
)
response = response['SynthesisTask']
taskId = response['TaskId']
bucketKey = str(os.environ['KeyPrefix']+"."+taskId+".mp3")
return json.dumps({
"isBase64Encoded": False,
"statusCode": 202,
"headers": {"x-tts-bucketkey": bucketKey},
"body": {"taskId": taskId}
})