-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
69 lines (59 loc) · 2.3 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import json
from base64 import b64encode
import boto3
from botocore import exceptions
polly = boto3.client("polly")
def lambda_handler(event, context):
"""Lambda function handler to convert text to speech.
It takes the following parameters: inputText, voiceId, and textType.
:param event: The event passed to the lambda function.
:param context: The context passed from the lambda function.
:return: The response from the Polly API.
"""
# Get the parameters from the event
input_text = event["inputText"]
voice_id = event["voiceId"]
selected_interpreter = event["textType"]
types_of_interpretation = ["ssml", "text"]
output_text = None
try:
"""Check if the selected interpreter is valid and is in the list of types
and decide what type of text to use. Otherwise raise a ValueError
"""
if selected_interpreter in types_of_interpretation:
if selected_interpreter == "ssml":
output_text = (
f"<speak><phoneme alphabet='ipa' ph='{input_text}' /></speak>"
)
elif selected_interpreter == "text":
output_text = input_text
else:
raise ValueError("Selected interpreter is not valid")
except ValueError as ve:
return {"statusCode": 400, "error": str(ve)}
try:
# Early return if the input text or voice id is empty
if not input_text:
raise ValueError("Missing inputText")
if not voice_id:
raise ValueError("Missing voiceId")
# Synthesize speech from input text
speech = polly.synthesize_speech(
OutputFormat="ogg_vorbis",
TextType=selected_interpreter,
Text=output_text,
VoiceId=voice_id,
)
# Handle exceptions
except exceptions.ClientError as ce:
return {"statusCode": 400, "error": str(ce)}
except ValueError as ve:
return {"statusCode": 400, "error": str(ve)}
# Return the content type and the base64-encoded audio stream as a string in the body of the response
response = {
"statusCode": 200,
"contentType": speech["ContentType"],
"body": "base64," + str(b64encode(speech["AudioStream"].read()), "utf-8"),
}
# Return the response
return response