-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_code.py
More file actions
53 lines (46 loc) · 1.48 KB
/
lambda_code.py
File metadata and controls
53 lines (46 loc) · 1.48 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
import os
import io
import boto3
import json
import csv
# grab environment variables
ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
runtime= boto3.client('runtime.sagemaker')
#test should be json of format {'text':'sentiment'}
def lambda_handler(event, context):
# TODO implement
if 'body' in event:
input_text = json.loads(event['body']).get("text")
else:
input_text = event.get("text")
# Construct payload for SageMaker model
payload = {
"inputs": input_text
}
payload_str = json.dumps(payload)
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
ContentType='application/json',
Body=payload_str)
response_body = response['Body'].read().decode()
result = json.loads(response_body)
# Extract the 'label' value
label_value = result[0]["label"] # Adjust "label" to match the actual key name in the response
# Prepare the response dictionary with just the label value
preds = {"Prediction": label_value}
response_dict = {
"statusCode": 200,
"body": json.dumps(preds)
}
return response_dict
#add this to lambda permissions
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "sagemaker:InvokeEndpoint",
"Resource": "*"
}
]
}