1

I'm developing a Flask API to run a machine learning model and return the prediction. To do so, I am using Pickle to load the model into a global variable. What I would like to do is load the models into global variables when the Flask server starts up and create an endpoint to run the model when a user needs a prediction. This way I won't have to always load the models every time a call is made.

The code to load the model into a variable is:

loaded_model = pickle.load(open('model.sav', 'rb'))

and a very basic code example of the Flask API is:

app = Flask(__name__)

@app.route('/')
def home():
    return 'home'

@app.route('/getRecommendation', methods=['GET'])
def getRecommendation():
    return(loaded_model(paremeter))


if __name__ == '__main__':
    app.run()

Where should I put the code to load the models into variables so that it occurs when the flask server is started and are available to be called from the getRecommendation endpoint.

desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

0

A simple way is to create a load_model method that will load and return the model and then decorate it with functools.cache. It will take some to load on the first load and after that, it will be fast.

If you want to prevent that, you can also call load_model and discard the output inside your if __name__ block.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • Can you please help me with this. My loaded model always gives the same results in api, but works good when I regulary test it https://datascience.stackexchange.com/questions/109390/machine-learning-model-deployed-with-flask-api-always-gives-the-same-result – taga Mar 26 '22 at 16:53