Develop a NLP Model in Python & Deploy It with Flask, Step by Step

Our ML systems workflow is like this: Train offline -> Make model available as a service -> Predict online.A classifier is trained offline with spam and non-spam messages.The trained model is deployed as a service to serve users.Figure 1When we develop a machine learning model, we need to think about how to deploy it, that is, how to make this model available to other users.Kaggle and Data science bootcamps are great for learning how to build and optimize models, but they don’t teach engineers how to take them to the next step, where there’s a major difference between building a model, and actually getting it ready for people to use in their products and services.In this article, we will focus on both: building a machine learning model for spam SMS message classification, then create an API for the model, using Flask, the Python micro framework for building web applications.This API allows us to utilize the predictive capabilities through HTTP requests..Let’s get started!ML Model BuildingThe data is a collection of SMS messages tagged as spam or ham that can be found here..First, we will use this dataset to build a prediction model that will accurately classify which texts are spam.Naive Bayes classifiers are a popular statistical technique of e-mail filtering..They typically use bag of words features to identify spam e-mail..Therefore, We’ll build a simple message classifier using Naive Bayes theorem.NB_spam.pyFigure 2Not only Naive Bayes classifier is easy to implement but also provides very good result.After training the model, it is desirable to have a way to persist the model for future use without having to retrain..To achieve this, we add the following lines to save our model as a .pkl file for the later use.from sklearn.externals import joblibjoblib.dump(clf, 'NB_spam_model.pkl')And we can load and use saved model later like so:NB_spam_model = open('NB_spam_model.pkl','rb')clf = joblib.load(NB_spam_model)The above process called “persist model in a standard format”, that is, models are persisted in a certain format specific to the language in development.And the model will be served in a micro-service that expose endpoints to receive requests from client..This is the next step.Turning the Spam Message Classifier into a Web ApplicationHaving prepared the code for classifying SMS messages in the previous section, we will develop a web application that consists of a simple web page with a form field that lets us enter a message..After submitting the message to the web application, it will render it on a new page which gives us a result of spam or not spam.First, we create a folder for this project called SMS-Message-Spam-Detector , this is the directory tree inside the folder..We will explain each file.spam.csvapp.pytemplates/ home.html result.htmlstatic/ style.cssSMS Message Spam Detector folderThe sub-directory templates is the directory in which Flask will look for static HTML files for rendering in the web browser, in our case, we have two html files: home.html and result.html .app.pyThe app.py file contains the main code that will be executed by the Python interpreter to run the Flask web application, it included the ML code for classifying SMS messages:app.pyWe ran our application as a single module; thus we initialized a new Flask instance with the argument __name__ to let Flask know that it can find the HTML template folder (templates) in the same directory where it is located.Next, we used the route decorator (@app.route('/')) to specify the URL that should trigger the execution of the home function.Our home function simply rendered the home.html HTML file, which is located in the templates folder.Inside the predict function, we access the spam data set, pre-process the text, and make predictions, then store the model..We access the new message entered by the user and use our model to make a prediction for its label.we used the POST method to transport the form data to the server in the message body..Finally, by setting the debug=True argument inside the app.run method, we further activated Flask's debugger.Lastly, we used the run function to only run the application on the server when this script is directly executed by the Python interpreter, which we ensured using the if statement with __name__ == '__main__'.home.htmlThe following are the contents of the home.html file that will render a text form where a user can enter a message:home.htmlstyle.cssIn the header section of home.html, we loaded styles.cssfile..CSS is to determine how the look and feel of HTML documents.. More details

Leave a Reply