Last active
March 31, 2020 08:49
-
-
Save francoismarceau29/02114070195301463ae72b75713aad5b to your computer and use it in GitHub Desktop.
Deployment of SciKit model on AWS Lambda using S3 and Boto3 (WIP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.externals import joblib | |
from boto.s3.key import Key | |
from boto.s3.connection import S3Connection | |
from flask import Flask | |
from flask import request | |
from flask import json | |
BUCKET_NAME = 'your-s3-bucket-name' | |
MODEL_FILE_NAME = 'your-model-name.pkl' | |
MODEL_LOCAL_PATH = '/tmp/' + MODEL_FILE_NAME | |
app = Flask(__name__) | |
@app.route('/', methods=['POST']) | |
def index(): | |
payload = json.loads(request.get_data().decode('utf-8')) | |
prediction = predict(payload['payload']) | |
data = {} | |
data['data'] = prediction[-1] | |
return json.dumps(data) | |
def load_model(): | |
conn = S3Connection() | |
bucket = conn.create_bucket(BUCKET_NAME) | |
key_obj = Key(bucket) | |
key_obj.key = MODEL_FILE_NAME | |
contents = key_obj.get_contents_to_filename(MODEL_LOCAL_PATH) | |
return joblib.load(MODEL_LOCAL_PATH) | |
def predict(data): | |
# Process your data, create a dataframe/vector and make your predictions | |
final_formatted_data = [] | |
return load_model().predict(final_formatted_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment