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.