Skip to content

Commit

Permalink
Merge pull request #38 from McGill-MMA-EnterpriseAnalytics/aladelca-p…
Browse files Browse the repository at this point in the history
…atch-5

predict
  • Loading branch information
aladelca authored Apr 26, 2024
2 parents e416943 + 01d68c0 commit 535f90c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 09_model_pipeline/predict/src/main_predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json
import pandas as pd
from utils import *
from predict import Predict as pr
def lambda_handler(event, context):
bucket_name = 'enterprise-data-science'
path = 'training_files/trained_model.pickle'
#client = boto3.client('s3')

data = json.loads(json.dumps(event))
'''
with open(event) as f:
data = json.load(f)
print(data)
'''
#data = json.loads(json.dumps(event))
try:
data = data['result']
except:
data = data
df = pd.DataFrame(data['transactions'])

predictions = pr.main_predict(df, bucket_name, path)


#results = {'results': predictions}
results_dicts = [json.loads(item) for item in predictions]
response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": results_dicts
}
return response
14 changes: 14 additions & 0 deletions 09_model_pipeline/predict/src/predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from utils import *
import numpy
class Predict:
def main_predict(data, bucket_name, path):
model = load_models_from_s3(bucket_name, path )

probas = model.predict_proba(data)
preds = np.where(probas[:,1] >= 0.4, 1, 0)
data['prob_0'] = probas[:,0]
data['prob_1'] = probas[:,1]
data['predictions'] = preds
record = data.to_dict(orient='records')[0]
json_result = data.to_json(orient='records', lines=True).splitlines()
return json_result
15 changes: 15 additions & 0 deletions 09_model_pipeline/predict/src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import boto3
from io import BytesIO
import joblib
def load_models_from_s3(bucket_name, object_name):

s3 = boto3.client('s3')
model_file = BytesIO()
s3.download_fileobj(bucket_name, object_name, model_file)

model_file.seek(0)


modelo = joblib.load(model_file)

return modelo

0 comments on commit 535f90c

Please sign in to comment.