-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_lambda_function.py
More file actions
37 lines (31 loc) · 1.4 KB
/
Copy pathflask_lambda_function.py
File metadata and controls
37 lines (31 loc) · 1.4 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
import functions
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
# Bellow code should be uncommented when running in AWS:Lambda
# above should be commented as well as flask import + app definition
# def lambda_handler(event, context):
"""
Lambda handler: When a request hits the API gateway linked to this lambda_function this is the function that gets
called.
The request data is passed as the event variable which is a dictionary object, in this case it the json of
the POST request from which we extract the body details
"""
# Parses the details from the POST request: extracts model and input data
# Based on model it imports the trained model from local
# Outputs the predicted user based on input data
try:
prediction = functions.predict_user(request.get_json())
# Below code should be uncommented when running from AWS, above should be commented.
# prediction = functions.predict_user(event)
return jsonify({'statuscode': 200,
'status': 'success',
'predicted user': str(prediction)
})
except Exception as e:
return jsonify({'statuscode': 400,
'status': 'error',
'message': str(e)})
if __name__ == '__main__':
app.run(debug=True)