Created
July 4, 2020 03:25
-
-
Save Theo-/e8820b91280fe2b093a522146c037587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import pandas as pd | |
import os | |
from sklearn.neighbors import NearestNeighbors | |
import sys | |
from joblib import load | |
# load the model | |
clf = load('my_model.joblib') | |
# define a predict function as an endpoint | |
def make_prediction(dictOfInputs): | |
x=pd.DataFrame.from_dict(dictOfInputs, orient='index').transpose() | |
isKneighbor = isinstance(clf, NearestNeighbors) | |
if isKneighbor: | |
return clf.kneighbors(x)[0].tolist() | |
return clf.predict(x)[0].tolist() | |
@app.route("/", methods=["GET","POST"]) | |
def predict(): | |
data = {"success": False} | |
body = flask.request.json | |
params = None | |
if (body == None): | |
body = flask.request.args | |
if "inputs" not in body.keys(): | |
data["message"] = "The inputs parameter is missing. Try and use {inputs: [array of inputs to the model]}" | |
else: | |
params = body["inputs"] | |
# if parameters are found, return a prediction | |
if (params != None): | |
dictOfInputs = { i : params[i] for i in range(0, len(params) ) } | |
try: | |
data["outputs"] = make_prediction(dictOfInputs) | |
data["success"] = True | |
except Exception as e: | |
data["message"] = "There was an error while running your model: " + str(e) | |
# return a response in json format | |
return flask.jsonify(data) | |
# start the flask app, allow remote connections | |
app.run(host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment