Skip to content

Instantly share code, notes, and snippets.

@socratesk
Created February 27, 2018 20:40
Show Gist options
  • Save socratesk/b881a75e8ed1d7ff8c42465f0ae53b3a to your computer and use it in GitHub Desktop.
Save socratesk/b881a75e8ed1d7ff8c42465f0ae53b3a to your computer and use it in GitHub Desktop.
# 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 values from browser
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)
# 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 Server...")
# Call function that loads Model
load_model()
# Run Server
app.run()
@promisejeremiah
Copy link

Thanks Socratesk.
I was trying to create a flask API app for an Iris dataset model and I had this error that has left me almost depressed and stucked for some days now.
Please kindly look into the codes and help me see what I'm doing wrong.
IMG_20200923_210422
IMG_20200923_210414
IMG_20200923_210405

If need be I can push the codes to repo.
Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment