I have a ML model saved in a pkl (pickel file), I have no problem loading this model and using it for prediction, even I have a rest service that expose it, the only problem is that I load the model in every request, something like this:
https://www.analyticsvidhya.com/blog/2017/09/machine-learning-models-as-apis-using-flask/
I really want that my model just load one time like a global variable and every request useing this variable without the necessity of load the model every request
is it possible?
You can assign the model variable in settings.py
. Whenever the server will start/restart django will store the model variable globally. It can be accessed like
from django.conf import settings
print settings.my_ml_model_variable
Based on the comment of Kaushal, I solved my problem using django rest framework as the following:
first I saved my model as :
> joblib.dump(<your scikit model here> , <"yourfilename.pkl">, compress
> = 1)
Once I had my model saved with the pkl extension I needed to create a variable in the settings.py file(this file is created automatically by django)
YOURMODEL = joblib.load(<"yourfilename.pkl">)
The django process call this file when you start your server, so it is called just one time
Now we just need to call our model in whatever place we want, usually in a views.py file since we are using django and/or django-rest-framework
myModel = getattr(settings, 'YOURMODEL', 'the_default_value')
res = myModel.predict_proba(s).tolist()
A simple example of the rest service:
from django.conf import settings
class myClass(APIView):
permission_classes = (permissions.AllowAny,)
'''Httpverb post method'''
def post(self, request,format=None):
myModel = getattr(settings, '../mymodel.pkl', 'the_default_value')
data = preparePostData(request.data)
res = myModel.predict_proba(data).tolist()
message = prepareMessage(res)
return Response(message, status=status.HTTP_200_OK)
Here preparePostData and prepareMessage are just function that I developed for prepare the object to my model and my response
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With