-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
38 lines (31 loc) · 1.29 KB
/
api.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
import flask
import json
import numpy as np
from sklearn.externals import joblib
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
@app.route("/")
@app.route("/index")
def index():
return flask.render_template('index.html')
@app.route("/predict", methods=['POST'])
def make_predictions():
if request.method =='POST':
sl = request.form.get('sepal-length')
sw = request.form.get('sepal-width')
pl = request.form.get('petal-length')
pw = request.form.get('petal-width')
X = np.array([sl,sw,pl,pw]).reshape(1,-1)
prediction = model.predict(X)
resultText = json.dumps(prediction.tolist(),sort_keys = False)
resultTextObject = json.loads(resultText)
# This json.dumps will convert 'prediction' to JSON
return render_template('predictPage.html',response=resultTextObject[0])
# 'response' is a Jinja2 variable embedded in predictPage.html
@app.route('/api')
def hello():
response = {'MESSAGE':'Welcome to the new API route'}
return jsonify(response)
# if __name__ == '__main__':
model = joblib.load('model.pkl')
# app.run(host='0.0.0.0', port=8000, debug=True)