-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
112 lines (79 loc) · 2.45 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
# Import dependencies
import numpy as np
from flask import (
Flask,
render_template,
jsonify)
from joblib import load
from sklearn.datasets import load_breast_cancer
# Set up Flask
app = Flask(__name__)
# home route
@app.route("/")
def index():
"""Renders homepage"""
return render_template("index.html")
# vizualisations/tableau route
@app.route("/story")
def story():
"""Renders visualizations page"""
return render_template("story.html")
@app.route("/cases")
def cases():
"""Renders case studies page"""
return render_template("cases.html")
@app.route("/calculator")
def calc():
"""Renders demo page"""
return render_template("calculator.html")
@app.route("/cta")
def cta():
"""Renders call to action page"""
return render_template("cta.html")
@app.route("/features/<patientID>")
def features(patientID):
"""Returns list of features for given patient ID"""
# Create list of feature names
feature_names = [
"Radius (worst)",
"Texture (worst)",
"Perimeter (worst)",
"Area (worst)",
"Smoothness (worst)",
"Compactness (worst)",
"Concavity (worst)",
"Concave points (worst)",
"Symmetry (worst)",
"Fractal dimension (worst)"]
row = int(patientID) - 19000
# Load dataset from sklearn and set X to feature array
X = load_breast_cancer().data
feature_values = X[row]
# Select only features to be displayed
feature_values = feature_values[20:]
# Create dictionary of keys feature names and values
features_dict = dict(zip(feature_names, feature_values))
return jsonify(features_dict)
@app.route("/analyze/<patientID>")
def analyze(patientID):
"""Analyzes data for given patient using machine learning model
and returns diagnosis"""
# Translate patient ID to row
row = (int(patientID) - 19000)
# Load features, model, and scaler
X = load_breast_cancer().data
model = load("cancer_model.joblib")
scaler = load("scaler.out")
# Get features for selected row and scale
row = np.array([row])
feature_values = X[row]
feature_values = scaler.transform(feature_values)
# Predict diagnosis
prediction = model.predict(feature_values)
if prediction == 0:
diagnosis = "Benign"
else:
diagnosis = "Malignant"
return jsonify(diagnosis)
if __name__ == "__main__":
app.run(debug=False, port=8000, host="localhost", threaded=True)