-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
57 lines (52 loc) · 2.08 KB
/
api.py
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
54
55
56
57
# ./python_code/api.py
import os
import pickle
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS
import numpy as np
import pandas as pd
import pdb
app = Flask(__name__)
CORS(app)
api = Api(app)
# Require a parser to parse our POST request.
# TODO: can we automatically parse the args from the cmd, instead of having to update this everytime?
parser = reqparse.RequestParser()
parser.add_argument("LOAN_AMOUNT")
parser.add_argument("LENDER_TERM")
parser.add_argument("NUM_BORROWERS")
parser.add_argument("PERCENT_FEMALE")
parser.add_argument("PLANNED_DURATION")
parser.add_argument("CURRENCY_POLICY")
parser.add_argument("REPAYMENT_INTERVAL")
parser.add_argument("DISTRIBUTION_MODEL")
# Unpickle our model so we can use it!
if os.path.isfile("./low_categorical_feat_model.pkl"):
model = pickle.load(open("./low_categorical_feat_model.pkl", "rb"))
else:
raise FileNotFoundError
class Predict(Resource):
def post(self):
args = parser.parse_args()
# Sklearn is VERY PICKY on how you put your values in...
X = [
args["LOAN_AMOUNT"],
args["LENDER_TERM"],
args["NUM_BORROWERS"],
args["PERCENT_FEMALE"],
args["PLANNED_DURATION"],
args["CURRENCY_POLICY"],
args["REPAYMENT_INTERVAL"],
args["DISTRIBUTION_MODEL"]
]
col_names = ["LOAN_AMOUNT", "LENDER_TERM", "NUM_BORROWERS", "PERCENT_FEMALE",
"PLANNED_DURATION", "CURRENCY_POLICY", "REPAYMENT_INTERVAL", "DISTRIBUTION_MODEL"]
df_test = pd.DataFrame(data = [X], columns = col_names)
df_test[['LOAN_AMOUNT', 'LENDER_TERM', 'NUM_BORROWERS', 'PERCENT_FEMALE', 'PLANNED_DURATION']] = df_test[['LOAN_AMOUNT', 'LENDER_TERM', 'NUM_BORROWERS', 'PERCENT_FEMALE', 'PLANNED_DURATION']].astype(float)
df_test[['CURRENCY_POLICY', 'REPAYMENT_INTERVAL', 'DISTRIBUTION_MODEL']] = df_test[['CURRENCY_POLICY', 'REPAYMENT_INTERVAL', 'DISTRIBUTION_MODEL']].astype('category')
_y = model.predict(df_test)[0]
return {"class": _y}
api.add_resource(Predict, "/predict")
if __name__ == "__main__":
app.run(debug=True)