-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
85 lines (70 loc) · 2.44 KB
/
handler.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import re
import json
import random
import load_model
import utils
import polly
from dictionaries import DICTIONARIES
def handler(event, context):
print("Incoming event:")
print(event)
err_builder = utils.ErrorResponseBuilder(context.aws_request_id)
request = json.loads(event['body'])
if err := validate_input(request):
return err_builder.format_error_response(err[0], err[1])
model = request['model']
N = request['N']
M = request['M']
P, symbols = load_model.load_model(model, N)
new_words = list()
mp3s = list()
for i in range(M):
new_word = generate(P, symbols, N=N)
new_words.append(new_word)
mp3 = polly.getMP3(new_word)
if mp3 == -1:
mp3 = "Error producing mp3 file"
mp3s.append(mp3)
return {
"statusCode": 200,
"body": json.dumps({
"words": new_words,
"mp3_files": mp3s
}, ensure_ascii=False)
}
def validate_input(req):
if req['model'] is None or not isinstance(req['model'], str) or req['model'] == "":
return 400, "Model must be a string"
if req['model'] not in DICTIONARIES:
return 400, f"Model {req['model']} does not exist"
return None
def generate(P, symbols, N=2):
"""Takes in the probability distribution obtained from
build_model and creates a new word from it
"""
new_word = list()
# Put N "<start>" tags at start of new word to
# make indices work out properly
for i in range(N-1):
new_word.append("<start>")
# Pull new tokens from the probability distribution
# randomly until you get an "<end>" token
while("<end>" not in new_word):
# print(new_word)
rand = random.random()
counter = 0
# Get last (N-1) symbols in word
lastN1Symbols = " ".join(new_word[-(N-1):])
for i in range(len(symbols)):
# Skip this symbol if it doesn't exist or has probability 0
if lastN1Symbols not in P or \
symbols[i] not in P[lastN1Symbols] or\
P[lastN1Symbols][symbols[i]] == 0:
continue
# Increment the counter by the symbol's probability
# and use the one that makes the sum cross the random number
counter += P[lastN1Symbols][symbols[i]]
if counter > rand:
new_word.append(symbols[i])
break
return "".join(new_word[N-1:-1])