-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller.py
executable file
·58 lines (45 loc) · 2.02 KB
/
controller.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
from flask import Flask, render_template, session, redirect, url_for
from flask.ext.wtf import Form
from wtforms import IntegerField, StringField, SubmitField, SelectField, DecimalField
from wtforms.validators import Required
import pickle
from sklearn import datasets
# Initialize Flask App
app = Flask(__name__)
print("loading my model")
with open('model.pkl', 'rb') as handle:
machine_learning_model = pickle.load(handle)
print("model loaded")
# Initialize Form Class
class theForm(Form):
param1 = DecimalField(label='Sepal Length (cm):', places=2, validators=[Required()])
param2 = DecimalField(label='Sepal Width (cm):', places=2, validators=[Required()])
param3 = DecimalField(label='Petal Length (cm):', places=2, validators=[Required()])
param4 = DecimalField(label='Petal Width (cm):', places=2, validators=[Required()])
submit = SubmitField('Submit')
@app.route('/', methods=['GET', 'POST'])
def home():
print(session)
form = theForm(csrf_enabled=False)
if form.validate_on_submit(): # activates this if when i hit submit!
# Retrieve values from form
session['sepal_length'] = form.param1.data
session['sepal_width'] = form.param2.data
session['petal_length'] = form.param3.data
session['petal_width'] = form.param4.data
# Create array from values
flower_instance = [(session['sepal_length']), (session['sepal_width']), (session['petal_length']),
(session['petal_width'])]
# Return only the Predicted iris species
flowers = ['setosa', 'versicolor', 'virginica']
session['prediction'] = flowers[machine_learning_model.predict(flower_instance)[0]]
# Implement Post/Redirect/Get Pattern
return redirect(url_for('home'))
return render_template('home.html', form=form, **session)
# Handle Bad Requests
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
app.secret_key = 'super_secret_key_shhhhhh'
if __name__ == '__main__':
app.run(debug=True)