Deploying the ML model to the flask with joblib

Sundaram Dubey
2 min readMay 26, 2020

--

In this blog, we will know how to deploy a Machine Learning model on a Flask App. In this blog, I am going to deploy a Simple Spam Classification Model to flask App.

If you have read before about deployment of the ML model you have heard of ‘Pickle’. This is the most famous library for model deployment and you will get so many blogs on this over the internet. But the question is, Is it most suitable in every case. The answer is no.

When we made a model with the sckit-learn classifiers, then Pickle faces some issues. These issues are also described in docs(https://scikit-learn.org/stable/modules/model_persistence.html). But the Solution is the JOBLIB library. In this blog, We will create a flask app and deploy the model in the flask App with the help of joblib.

from here you can download pre-trained model used in this blog. Models are in output file on kaggle- https://www.kaggle.com/thedeathcure/sms-spam-filtering. Model files are present in both format — model.joblib(joblib) and model.pkl (pickle)

Create a Simple flask app —

start your flask app

here I am using my pre-trained model so all libraries that are going to use in prediction with your model you have to import that.

dumping and loading a model

In your templates, the mainpage.html would be like -

mainpage template

Some Common Errors during Deployment

Errors are inevitable when you try to go out of the box or try something new in development.

moduleNotFoundError: No module named ‘sklearn.svm._classes’

The reason for this error is the version difference of sckit-learn. This occurs due to when you use a pre-trained model or you trained a model on Kaggle or colab and try to delay it on your local. Then you have to install the same version of sckit-learn on your local too.

pip install -U sckit-learn==0.22.1

mine model used this version. I tried this and error resolved. So keep the versions in mind.

Below is a complete app.py for my application. You can use my model with this app. And run using

python3 -m flask run

complete app.py

Run the app and On the port localhost:5000, you see something like this -

Thanks for making it till the end 😃 !

--

--

Sundaram Dubey
Sundaram Dubey

Written by Sundaram Dubey

SDE @Swiggy | Ex-Paytm | HBTU CSE’21 | GSoC ’21 @DIAL | Google CodeIn mentor’20

No responses yet