-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
48 lines (35 loc) · 1.26 KB
/
app.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
from flask import Flask, request, jsonify
import joblib
import numpy as np
from xgboost import XGBClassifier
app = Flask(__name__)
model = joblib.load('model.h5')
def return_prediction(model, sample_json):
input_data = [
int(sample_json['Age']),
int(sample_json['gender'].lower() in ['m', 'male']),
int(sample_json['gender'].lower() not in ['m', 'male', 'f', 'female']),
int(sample_json['treatment'].lower() in ['y', 'yes']),
int(sample_json['family_history'].lower() in ['y', 'yes']),
int(sample_json['obs_consequence'].lower() in ['y', 'yes']),
int(sample_json['leave_Very_difficult'].lower() in ['y', 'yes']),
int(sample_json['mental_vs_physical'].lower() in ['n', 'no']),
]
prediction = model.predict(np.array(input_data).reshape((1,-1)))
prediction_map = {
0:'Never',
1:'Rarely',
2:'Sometimes',
3:'Often'
}
return prediction_map[prediction[0]]
@app.route("/")
def index():
return "<h1>Flask app is running</h1>"
@app.route('/api/predict', methods=['POST'])
def flower_prediction():
content = request.json
results = return_prediction(model, content)
return jsonify(results)
if __name__=='__main__':
app.run()