-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
46 lines (40 loc) · 1.35 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
from flask import Flask,jsonify,request,Response
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import train_test_split
import json
import os
app = Flask(__name__)
port = int(os.getenv("PORT", 3000))
iris = load_iris()
x = iris.data
y = iris.target
x_train,x_test,y_train,y_test= train_test_split(x,y)
rfc = RandomForestClassifier(n_estimators=100,n_jobs=2)
rfc.fit(x_train,y_train)
@app.route('/predict_api', methods=['POST'])
def predict():
# Error checking
req_body = request.get_json(force=True)
# Convert JSON to numpy array
sepal_length = req_body['sl']
sepal_width = req_body['sw']
petal_length = req_body['pl']
petal_width = req_body['pw']
iris_class = rfc.predict([[sepal_length, sepal_width, petal_length, petal_width]])
print(iris_class)
if (iris_class[0] == 0):
result = "Iris Setosa"
elif (iris_class[0] == 1):
result = "Iris Versicolor"
elif (iris_class[0] == 2):
result = "Iris Virginica"
msg = {
"message": "Your flower is %s" % (result)
}
resp = Response(response=json.dumps(msg),
status=200, \
mimetype="application/json")
return resp
if __name__ == '__main__':
app.run(host='0.0.0.0',port=port)