-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.py
More file actions
executable file
·68 lines (55 loc) · 1.78 KB
/
handler.py
File metadata and controls
executable file
·68 lines (55 loc) · 1.78 KB
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
try:
import unzip_requirements
except ImportError:
pass
import io
import json
import base64
import boto3
import torch
print('torch version:',torch.__version__)
from requests_toolbelt.multipart import decoder
from german_to_english import translate_sentence
MODEL_PATH = 'model.pt'
METADATA_PATH = 'model_metadata.pkl'
def fetch_inputs(event):
print('Fetching Content-Type')
if 'Content-Type' in event['headers']:
content_type_header = event['headers']['Content-Type']
else:
content_type_header = event['headers']['content-type']
print('Loading body...')
body = base64.b64decode(event['body'])
print('Body loaded')
# Obtain the final input that will be used by the model
input_text = decoder.MultipartDecoder(body, content_type_header).parts[0]
print('Input obtained')
return input_text.content.decode('utf-8')
def translate(event, context):
"""Style the content image."""
try:
input_text = fetch_inputs(event)
print(input_text)
# Output Sentiment
output = translate_sentence(input_text, MODEL_PATH, METADATA_PATH)
print(output)
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': True
},
'body': json.dumps({'data': output})
}
except Exception as e:
print(repr(e))
return {
'statusCode': 500,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': True
},
'body': json.dumps({'error': repr(e)})
}