-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
95 lines (71 loc) · 2.83 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
# Dependencies used to build the web app
from flask import Flask, render_template, jsonify, request
from werkzeug.exceptions import HTTPException
import traceback
import io
# Used to load and run the model
# Packages: scikit-learn, joblib
from sklearn.externals import joblib
import numpy as np
app = Flask(__name__)
model = None
def load_model():
"""
This method loads our model, so that we can use it to perform predictions.
"""
global model
if not model:
# print("--->>>> loading model...")
# TODO: Change the filename to match your model's filename
model = joblib.load("churn_classifier.pkl")
return model
# Homepage: The form
@app.route('/')
def form():
# Get the values if specified in the URL (so we can edit them)
values = request.values
return render_template('form.html', form_values=values)
@app.route('/process_form', methods=["POST"])
def process_form():
# Get the values that were submitted in the form, and
# convert them to correct numeric format (integer or floats)
values = {
'contract-month-month': float(request.form['contract-month-month']),
'tenure': float(request.form['tenure']),
'monthly-charges': float(request.form['monthly-charges']),
'internet-fo': float(request.form['internet-fo']),
'payment-electonic': float(request.form['payment-electonic']),
}
# These are the values that we will display on the results page
input_values = {
"Contract Month to Month (0,1)": values['contract-month-month'],
"Tenure (months)": values['tenure'],
"Monthly Charges ($)": values['monthly-charges'],
"Internet Fiber Optics (0,1)": values['internet-fo'],
"Electronic Payment": values['payment-electonic'],
}
# Load the model & model params
model = load_model()
model_params = [[
values['contract-month-month'],
values['tenure'],
values['monthly-charges'],
values['internet-fo'],
values['payment-electonic'],
]]
# Use our model to perform predictions
# model.predict returns an array containing the prediction
# e.g. => [[0]]
prediction = model.predict(model_params)[0]
print(prediction)
# model.predict_proba returns an array containing the probabilities of each class
# e.g. => [[0.65566831, 0.34433169]]
probabilities = model.predict_proba(model_params)[0]
print(probabilities)
return render_template('results.html', prediction=prediction, probabilities=probabilities, input_values=input_values, form_values=values)
# Start the server
if __name__ == "__main__":
print("* Starting Flask server..."
"please wait until server has fully started")
# debug=True options allows us to view our changes without restarting the server.
app.run(host='0.0.0.0', debug=True)