Skip to content

Instantly share code, notes, and snippets.

@l1m2p3
Last active January 7, 2018 20:08
Show Gist options
  • Save l1m2p3/56c8a77319e9c16e024672194b95b3a0 to your computer and use it in GitHub Desktop.
Save l1m2p3/56c8a77319e9c16e024672194b95b3a0 to your computer and use it in GitHub Desktop.
AWS Lambda handler file. Modify the code as you need
import boto3
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import json
# change this to use other models
from SimpleModel import SimpleModel
client = boto3.client('dynamodb')
table_name = 'wordvec'
#
# get word vectors for word in sentence and build a matrix with the vectors
#
def sentence_to_matrix(sentence):
words = sentence.split(' ')
# request cannot contain duplicate keys. remove duplicates
words_no_dup = list(set(words))
read_batch_size = 100
batches = [words_no_dup[i:i+read_batch_size] for i in range(0,len(words_no_dup),read_batch_size)]
wordvec_a = []
for batch in batches:
request = [{'word':{'S':word}} for word in batch]
response = client.batch_get_item(
RequestItems = {
table_name: {
'Keys': request
}
}
)
wordvec_a = wordvec_a + [(d['word']['S'], d['vector']['L']) for d in response['Responses'][table_name]]
lookup = dict(wordvec_a)
matrix = None
for word in words:
if word in lookup:
vec_raw = lookup[word]
vec = np.array([float(f['N']) for f in vec_raw])
else:
# random vector
vec = np.random.rand(300)
vec.resize(1, 1, vec.shape[0])
if matrix is None:
matrix = vec
else:
matrix = np.append(matrix, vec, axis=1)
matrix.resize(1, matrix.shape[0], matrix.shape[1], matrix.shape[2])
return matrix
def handler(event, context):
sentence = event['input']
input_matrix = sentence_to_matrix(sentence)
# load and run model
# you may need to modify this based on your model definition
model = torch.load('SimpleModel.pt')
model.eval()
torchIn = torch.from_numpy(input_matrix.astype(np.float32))
torchIn = Variable(torchIn)
output = model(torchIn)
# return result
result = {
'input': sentence,
'output': output.data.tolist()[0]
}
result = json.dumps(result)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment