-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
163 lines (103 loc) · 4.38 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import flask
import os
import pickle
import pandas as pd
import skimage
app = flask.Flask(__name__, template_folder='templates')
#path_to_vectorizer = 'models/vectorizer.pkl'
path_to_vectorizer = "kn_vectorizer.pkl"
#path_to_text_classifier = 'models/text-classifier.pkl'
path_to_text_classifier = "kn_models.pkl"
path_to_image_classifier = 'models/image-classifier.pkl'
with open(path_to_vectorizer, 'rb') as f:
vectorizer = pickle.load(f)
with open(path_to_text_classifier, 'rb') as f:
model = pickle.load(f)
with open(path_to_image_classifier, 'rb') as f:
image_classifier = pickle.load(f)
##### pipeline from jupyter notebook ######
translator = str.maketrans('', '', string.punctuation)
def remove_stopwords(a):
return " ".join([word for word in word_tokenize(a) if word not in stopwords])
def remove_sp_char(a):
return a.translate(translator)
def text_pipeline2(a):
a = remove_sp_char(a.lower())
a = remove_stopwords(a)
return a
###################################################
@app.route('/', methods=['GET', 'POST'])
def main():
if flask.request.method == 'GET':
# Just render the initial form, to get input
return(flask.render_template('main.html'))
if flask.request.method == 'POST':
# Get the input from the user.
user_input_text = flask.request.form['user_input_text']
parsed_text = text_pipeline2(user_input_text)
# Turn the text into numbers using our vectorizer
X = vectorizer.transform([parsed_text])
# Make a prediction
predictions = model.predict(X)
# Get the first and only value of the prediction.
prediction = predictions[0]
# Get the predicted probabs
predicted_probas = model.predict_proba(X)
# Get the value of the first, and only, predicted proba.
predicted_proba = predicted_probas[0]
# The first element in the predicted probabs is % democrat
precent_democrat = predicted_proba[0]
# The second elemnt in predicted probas is % republican
precent_republican = predicted_proba[1]
return flask.render_template('main.html',
input_text=user_input_text,
parsed_text=parsed_text,
result=prediction,
precent_democrat=precent_democrat,
precent_republican=precent_republican)
@app.route('/input_values/', methods=['GET', 'POST'])
def input_values():
if flask.request.method == 'GET':
# Just render the initial form, to get input
return(flask.render_template('input_values.html'))
if flask.request.method == 'POST':
# Get the input from the user.
var_one = flask.request.form['input_variable_one']
var_two = flask.request.form['another-input-variable']
var_three = flask.request.form['third-input-variable']
list_of_inputs = [var_one, var_two, var_three]
return(flask.render_template('input_values.html',
returned_var_one=var_one,
returned_var_two=var_two,
returned_var_three=var_three,
returned_list=list_of_inputs))
return(flask.render_template('input_values.html'))
@app.route('/images/')
def images():
return flask.render_template('images.html')
@app.route('/bootstrap/')
def bootstrap():
return flask.render_template('bootstrap.html')
@app.route('/classify_image/', methods=['GET', 'POST'])
def classify_image():
if flask.request.method == 'GET':
# Just render the initial form, to get input
return(flask.render_template('classify_image.html'))
if flask.request.method == 'POST':
# Get file object from user input.
file = flask.request.files['file']
if file:
# Read the image using skimage
img = skimage.io.imread(file)
# Resize the image to match the input the model will accept
img = skimage.transform.resize(img, (28, 28))
# Flatten the pixels from 28x28 to 784x0
img = img.flatten()
# Get prediction of image from classifier
predictions = image_classifier.predict([img])
# Get the value of the prediction
prediction = predictions[0]
return flask.render_template('classify_image.html', prediction=str(prediction))
return(flask.render_template('classify_image.html'))
if __name__ == '__main__':
app.run(debug=True)