-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
63 lines (46 loc) · 1.54 KB
/
server.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
# import Flask class from the flask module
from flask import Flask, request
import numpy as np
import pickle
# Create Flask object to run
app = Flask(__name__)
@app.route('/')
def home():
return "Hi, Welcome to Flask!!"
@app.route('/predict')
def predict():
# Get data sent via the GET request
sepLen = request.args['sepal_length']
sepWid = request.args['sepal_width']
petLen = request.args['petal_length']
petWid = request.args['petal_width']
testData = np.array([sepLen, sepWid, petLen, petWid]).reshape(1,4)
class_prediced = int(svmIrisModel.predict(testData)[0])
output = "Predicted Iris Class: " + str(class_prediced)
return (output)
@app.route('/predict_iris', methods=['POST'])
def predict_iris():
if request.method == 'POST':
# Get data sent via the GET request
data = request.form
sepLen = data['sepal_length']
sepWid = data['sepal_width']
petLen = data['petal_length']
petWid = data['petal_width']
testData = np.array([sepLen, sepWid, petLen, petWid]).reshape(1,4)
class_prediced = int(svmIrisModel.predict(testData)[0])
output = "Predicted Iris Class: " + str(class_prediced)
return output
# Load the pre-trained and persisted SVM model
# Note: The model will be loaded only once at the start of the server
def load_model():
global svmIrisModel
svmIrisFile = open('models/SVMModel.pckl', 'rb')
svmIrisModel = pickle.load(svmIrisFile)
svmIrisFile.close()
if __name__ == "__main__":
print("**Starting Flask Server...")
# Call function that loads Model
load_model()
# Run Server
app.run()