Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in python , How to load just one time a ML model in a rest service like django or flask?

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?

like image 718
Fermin Pitol Avatar asked Dec 10 '22 05:12

Fermin Pitol


2 Answers

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
like image 118
Kaushal Kumar Avatar answered Dec 12 '22 17:12

Kaushal Kumar


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

like image 24
Fermin Pitol Avatar answered Dec 12 '22 18:12

Fermin Pitol