Contents :
- What is model and why do we need to deploy it
- Creating a mini machine learning project
- Deploying our model on the server
- Conclusion
What is model and why do we need to deploy it?
When as data scientist or the machine learning engineer wants to use the data present in the database and do something meaningful. so, for example image classification or sentiment analysis and etc they are required to build a model which is essentially a file.
but what is the next step that needs to be consider after we have build the model what’s next that’s where the Model deployment comes into the picture it is one of the most essential part of any machine learning life cycle deployment of the model into production to help and build our application for the users which is the end goal.
A data scientist or ml engineer can use several techniques to build his model such as using Sci kit learn module, Tensorflow framework or the mlr3 package for the R language to create the module. Similarly like that there are several ways you can deploy your module using flask API, Django and etc however for this blog we will focus on making the model Model deployment using the flask.
Building the model
Dataset:
For today’s topic we will take a small dataset of airline sentiment analysis it consist of nearly 12000 rows and two columns, i’ve preprocessed it with the help of NLTK library and sklearn for converting the sentiment from the string to text
For converting the sentience to the integer i’ve used the BAG of word model which is a daily simple technique then to handle the imbalance dataset i’ve used the SMOTE technique for handling the classes.
Code for Model deployment:
The basic libraries required for the text preprocessing:
Libraries required for model building:
For handling the imbalance dataset:
Reading the data
Top 5 rows of the data
Data Preprocessing
Converting the text and also handling the imbalance inside the dataset:
Training the model
Saving the model and vocabulary with the help of the pickle library
Now that we have created and built the model, we can now work on deploying the it using the flask as the rest API after deploying we are expected to get the result in the json format so that we can use that rest api to deploy it and keep the code different from the rest of the developers and we cn also update and change the model without affecting our main WEB App. From there the client can send the request to the API, and upon receiving the request then the server will receive the request and then make the prediction and send the result to the client side in the json format.
Let’s start making the Model deployment
In the above piece of code what we have done is that we have loaded the model into the program and then initialize the flask app then in the function api_call we have received the request through get_json and then the little preprocessing to the sentence after that we used model.predct to predict the sentence sentiment and then return it in json format with the help of jsonify function.
In the above piece of code what we have done is that we have created the local host through flask in the previous code and then by creating the server we are send the request through here and after sending the sentence the sentence is preprocessed and then sent to the model for prediction and then a result is sent back which we have print using print(r.json()) function inside the command prompt.
Conclusion:
From this blog we are able to understand how to create our sentiment analysis classifier and then deploy on the local host using python flask module as the rest API. so After reading i would suggest you to learn more about how to deploy model on the website using the flask app or even on the cloud to get the deeper understanding of things.
written by : Mukut Khandelwal
reviewed by: Vikas Bhardwaj
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs