-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathmain.py
More file actions
40 lines (32 loc) · 1.09 KB
/
main.py
File metadata and controls
40 lines (32 loc) · 1.09 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
import os
import boto3
import pickle
import json
def lambda_handler(event, context):
print(f"Received event: {json.dumps(event)}")
# Retrieve JSON from body
payload = json.loads(event["body"])
sample = payload.get("sample")
# Specify the S3 bucket and object key
bucket_name = os.environ["MODEL_BUCKET_NAME"]
object_key = os.environ["MODEL_OBJECT_KEY"]
# Create an S3 client
s3 = boto3.client("s3")
# Download the file from S3
response = s3.get_object(Bucket=bucket_name, Key=object_key)
model_data = response["Body"].read()
# Load the model from the downloaded data
model = pickle.loads(model_data)
# Run inference.
print(f"Running inference on sample: {sample}")
index_prediction = int(model.predict(sample)[0])
print(f"Prediction index: {index_prediction}")
prediction = model.classes_names[index_prediction]
print(f"Prediction: {prediction}")
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({"prediction": prediction})
}