-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
115 lines (93 loc) · 2.86 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Backend script to give predictions and train triage detection of patient data
part of CodeVsCovid19 Hackathon
Author: Claudio Fanconi
Email: claudio.fanconi@outlook.com
"""
# Import libraries
from flask import Flask, request, jsonify
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
import pickle
import os
app = Flask(__name__)
# Broadcast
HOSTNAME = "0.0.0.0"
# HTTP Port
PORT = 5000
FILENAME = "finalized_model.sav"
print("Loading Classifier from Disk...")
# load the model from disk
model = pickle.load(open(FILENAME, "rb"))
print("Done.\n")
# root
@app.route("/")
def index():
"""
this is a root dir of my server
return:
str where the user is greeted
"""
return "hello there %s" % request.remote_addr
# POST
@app.route("/predict", methods=["POST"])
def predict():
"""
Predicts the Triage level of patient.
return:
json response containing the prediciton
"""
json = request.get_json()
patientID = str(json["patientID"])
preconditions = str(json["preconditions"])
if preconditions == "Arthritis":
json["preconditions_Arthritis"] = 1
json["preconditions_Asthma"] = 0
json["preconditions_Cancer"] = 0
json["preconditions_Hypertension"] = 0
json["preconditions_None"] = 0
elif preconditions == "Asthma":
json["preconditions_Arthritis"] = 0
json["preconditions_Asthma"] = 1
json["preconditions_Cancer"] = 0
json["preconditions_Hypertension"] = 0
json["preconditions_None"] = 0
elif preconditions == "Cancer":
json["preconditions_Arthritis"] = 0
json["preconditions_Asthma"] = 0
json["preconditions_Cancer"] = 1
json["preconditions_Hypertension"] = 0
json["preconditions_None"] = 0
elif preconditions == "Hypertension":
json["preconditions_Arthritis"] = 0
json["preconditions_Asthma"] = 0
json["preconditions_Cancer"] = 0
json["preconditions_Hypertension"] = 1
json["preconditions_None"] = 0
else:
json["preconditions_Arthritis"] = 0
json["preconditions_Asthma"] = 0
json["preconditions_Cancer"] = 0
json["preconditions_Hypertension"] = 0
json["preconditions_None"] = 1
del json["preconditions"]
X = pd.DataFrame(json, index=[0])
X = X.drop(["patientID"], axis=1)
# Predict Model
try:
y_pred = model.predict(X)
y_proba = model.predict_proba(X)
except:
response = {"STATUS": "FAILED", "ERROR": "Something went wrong..."}
return jsonify(response)
# Build response:
response = {
"STATUS": "OK",
"patientID": patientID,
"triage_level": int(y_pred[0]),
"PROBABILITY": y_proba.max(),
}
return jsonify(response)
if __name__ == "__main__":
app.run(host=HOSTNAME, port=PORT)