forked from tanujjain/deploy-ml-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
35 lines (25 loc) · 894 Bytes
/
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
# Serve model as a flask application
import pickle
import numpy as np
from flask import Flask, request
model = None
app = Flask(__name__)
def load_model():
global model
# model variable refers to the global variable
with open('iris_trained_model.pkl', 'rb') as f:
model = pickle.load(f)
@app.route('/')
def home_endpoint():
return 'Hello World!'
@app.route('/predict', methods=['POST'])
def get_prediction():
# Works only for a single sample
if request.method == 'POST':
data = request.get_json() # Get data posted as a json
data = np.array(data)[np.newaxis, :] # converts shape from (4,) to (1, 4)
prediction = model.predict(data) # runs globally loaded model on the data
return str(prediction[0])
if __name__ == '__main__':
load_model() # load model at the beginning once only
app.run(host='0.0.0.0', port=80)