-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (131 loc) · 4.72 KB
/
app.py
File metadata and controls
133 lines (131 loc) · 4.72 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 17 14:47:25 2020
@author: TRUE
"""
import numpy as np
import flask
import pickle
# app
app = flask.Flask(__name__)
# load model
heart = pickle.load(open("logregheart.pkl","rb"))
# routes
@app.route("/")
def home():
return """
<body>
<h1>Heart Disease Prediction<h1>
</body>"""
@app.route("/predict", methods=["GET"])
def predict():
thal = flask.request.args["thal"]
cp = flask.request.args["cp"]
slope = flask.request.args["slope"]
exang = flask.request.args["exang"]
ca = flask.request.args["ca"]
fmap = {"normal": [1, 0, 0],
"fixed defect": [0, 1, 0],
"reversable defect": [0, 0, 1],
"typical angina": [1, 0, 0, 0],
"atypical angina": [0, 1, 0, 0],
"non anginal pain": [0, 0, 1, 0],
"asymptomatic": [0, 0, 0, 1],
"upsloping": [1, 0, 0],
"flat": [0, 1, 0],
"downsloping": [0, 0, 1]}
# X_new = fmap[thal] + fmap[cp] + fmap[slope]
X_new = np.array(fmap[thal] + fmap[cp] + fmap[slope] + [int(exang)] + [int(ca)]).reshape(1, -1)
yhat = heart.predict(X_new)
if yhat[0] == 1:
outcome = "heart disease"
else:
outcome = "normal"
prob = heart.predict_proba(X_new)
return "This patient is diagnosed as " + outcome + " with probability " + str(round(prob[0][1], 2))
@app.route("/page")
def page():
with open("page.html", 'r') as viz_file:
return viz_file.read()
@app.route("/result", methods=["GET", "POST"])
def result():
"""Gets prediction using the HTML form"""
if flask.request.method == "POST":
inputs = flask.request.form
thal = inputs["thal"]
cp = inputs["cp"]
slope = inputs["slope"]
exang = inputs["exang"]
ca = inputs["ca"]
fmap = {"normal": [1, 0, 0],
"fixed defect": [0, 1, 0],
"reversable defect": [0, 0, 1],
"typical angina": [1, 0, 0, 0],
"atypical angina": [0, 1, 0, 0],
"non anginal pain": [0, 0, 1, 0],
"asymptomatic": [0, 0, 0, 1],
"upsloping": [1, 0, 0],
"flat": [0, 1, 0],
"downsloping": [0, 0, 1]}
X_new = np.array(fmap[thal] + fmap[cp] + fmap[slope] + [int(exang)] + [int(ca)]).reshape(1, -1)
yhat = heart.predict(X_new)
if yhat[0] == 1:
outcome = "heart disease"
else:
outcome = "normal"
prob = heart.predict_proba(X_new)
# results = """
# <body>
# <h3> Heart Disease Diagnosis <h3>
# <p> Patient profile </p>
# <h5> Thalassemia: """ + thal + """</h5>
# <h5> Chest Pain: """ + cp + """</h5>
# <h5> Slope: """ + slope + """</h5>
# <h5> Exercise induced angina: """ + exang + """</h5>
# <h5> Number of major vessels (0-3) colored by flourosopy: """ + ca + """</h5>
# <p> This patient is diagnose as """ + outcome + """ with probability """ + str(round(prob[0][1], 2)) + """.
# </body>
# """
results = """
<body>
<h3> Heart Disease Diagnosis <h3>
<p><h4> Patient profile </h4></p>
<table>
<tr>
<td>Thalassemia: </td>
<td>""" + thal + """</td>
</tr>
<tr>
<td>Chest Pain: </td>
<td>""" + cp + """</td>
</tr>
<tr>
<td>Slope: </td>
<td>""" + slope + """</td>
</tr>
<tr>
<td>Exercise induced angina: </td>
<td>""" + exang + """</td>
</tr>
<tr>
<td>Number of major vessels</td>
<td>""" + ca + """</td>
</tr>
</table>
<p> This patient is diagnose as """ + outcome + """ with probability """ + str(round(prob[0][1], 2)) + """.
</body>"""
# <h3> Heart Disease Diagnosis <h3>
# <p> Patient profile </p>
# <h5> Thalassemia: """ + thal + """</h5>
# <h5> Chest Pain: """ + cp + """</h5>
# <h5> Slope: """ + slope + """</h5>
# <h5> Exercise induced angina: """ + exang + """</h5>
# <h5> Number of major vessels (0-3) colored by flourosopy: """ + ca + """</h5>
# <p> This patient is diagnose as """ + outcome + """ with probability """ + str(round(prob[0][1], 2)) + """.
# </body>
return results
if __name__ == '__main__':
"""Connect to Server"""
HOST = "127.0.0.1"
PORT = "4000"
app.run(HOST, PORT)