In this article I will explain how to deploy your machine learning model to a simple web application using Flask. Before you start, you would need to build and train your own model. I go through a simple walk through on how to train a sentiment classification model using Logistic Regression here
Learning how to deploy your ML model is a great skill to have especially if you want to pursue a career in the field. Learning how to deploy your model can increase your chances in landing that data science, or ML engineer job as it is a common skill to have. Once you are done reading you will have:
Knowledge on libraries and packages to use.
skills for deploying your ML model.
confidence to launch your own model!
Unfortunately, people are afraid to actually try because of the unknown. Not knowing where to start, how to approach the problem or even afraid of how much work it takes.
Not knowing where to start is the number 1 reason why people don't try. Other reasons are as follows:
Knowledge of certain languages required for deploying model.
What libraries to use.
How to use your model for predicting raw data.
Learning Flask.
However, in this walkthrough I will show you how to simply deploy your model using Flask. This is to give you, the reader a sense of how simple it actually is. The hardest step, is to sit and start. The following steps are as follows:
Saving your model and loading it.
Create your predict.py
Develop an app.py file to launch your model.
Here's how, step by step:
Knowing how to save and load your model is every important because you will either need it for fine tuning purpose or even model deployment.
To start, we first train our model as shown in the walkthrough from here. There are two models we want to save for this particular case. We want the TF-IDF model for feature extraction of the raw data, and we want the Logistic Regression model (LR_model) for predicting the users input.
To do that we just use the following:
# Model path
tfidf_path = '../models/td_idf.pickle'
LR_model_path = '../models/log_reg_model.pickle'
# Save model using pickle
pickle.dump(tf_idf, open(tfidf_path, "wb"))
pickle.dump(log_reg_model, open(LR_model_path, "wb"))Where tf_idf is your feature extraction model and log_reg_model is your trained model.
To load the model:
tf_idf = pickle.load(open("../models/td_idf.pickle", "rb"))
model = pickle.load(open("../models/log_reg_model.pickle", "rb"))If you were anything like me, and you thought that all you had to do was load your model and everything magically works then I got news for you. That's all wrong. To actually deploy your model you need to load all the required models and preprocessing algorithms.
The following steps for developing your own predict function:
Pre process input text.
Load both the TF-IDF and logistic regression model.
Extract features from your pre processed input text.
pass text through model and return prediction.
The following function can be seen below:
import pickle
# Load pre_process script and import text_preprocessor
from pre_process import text_preprocessor
def classify(text, tf_idf, model):
label_decoder = {0:'negative', 1:'neautral', 2:'positive'}
# tf_idf = pickle.load(open("../models/td_idf.pickle", "rb"))
# model = pickle.load(open("../models/log_reg_model.pickle", "rb"))
p_text = text_preprocessor(text)
p_text = [p_text]
features = tf_idf.transform(p_text)
pred = model.predict(features)
return label_decoder.get(pred[0])
if __name__ == "__main__":
tf_idf = pickle.load(open("../models/td_idf.pickle", "rb"))
model = pickle.load(open("../models/log_reg_model.pickle", "rb"))
text = input("Type tweet: ")
pred = classify(text, tf_idf, model)
print('Your tweet is classified as {}'.format(pred))This is the part that can get intimidating for many. However, it's a lot simpler than it looks. We just need to import the necessary scripts that were developed in the process of preprocessing, feature engineering and training the model.
To get started, ensure you have Flask install. Then to familiarize yourself with Flask the simplest web application that you can build is the following:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello World!'
if __name__=="__main__":
app.run(host="0.0.0.0")The above flask application does the following:
Imports the Flask class from the flask package. Flask is used to create instances of web applications.
Initializes the app as an instance of Flask.
Sets a function that responds to the index route, ‘/’, and displays 'Hello, World!`
Runs the app if the current script being executed is app.py.
Now that we have a simple understanding of how flask work we can proceed with developing our web application. This can be more tricky because we now have to create an index.html script for our web application, and the following code can be seen below:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>My Machine Learning Model</title>
<!-- Quick Start: CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="login">
<h2>Tweet Sentiment Classifier</h2>
<p>Introduce your tweet:</p>
<!-- Inputs for our ML model -->
<form action="{{ url_for('predict')}}"method="post">
<input type="text" name="tweet" placeholder="Tweets" required="required" />
<button type="submit" class="btn btn-primary btn-block btn-large">Classify Tweet!</button>
</form>
<br>
<br>
<b> {{ prediction_text }} </b>
</div>
</body>
</html>Now before we proceed to the actual Flask application, let me explain a little on what is going on in the index.html. If you aren't familiar with HTML then I suggest you read up on an introduction to HTML here.
To summarize, two of the most important sections are the <form>...</form> and the {{ prediction_text }}. What the form does is that it acts as a GET method where the user can input a text and submit "classify" button where it then passes the text through the data preprocessing pipeline and into the trained model and then outputs the prediction as shown in the "prediction_text".
Now onto the Flask Application! This part is the bow to the gift box. In order to develop the web application we use the following code below:
from flask import Flask, request, render_template
import pickle
import sys
sys.path.append("./src")
sys.path.append("./models")
from src.predict import classify
app = Flask(__name__)
tf_idf = pickle.load(open("./models/td_idf.pickle", "rb"))
model = pickle.load(open("./models/log_reg_model.pickle", "rb"))
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict',methods=['POST'])
def predict():
"""Grabs the input values and uses them to make prediction"""
tweet = request.form['tweet']
prediction = classify(tweet, tf_idf, model)
return render_template('index.html', prediction_text=f'Your tweet "{tweet}" is classified as {prediction} sentiment.')
if __name__=="__main__":
app.run(host="0.0.0.0")Now, I know this may seem a bit intimidating but worry not. Let me begin with the @app.route(). This maps the specific route ('/') to access a particular function or page, and in the first route it is our index.html file. Now when we focus on the second route, the @app.route('/predict',methods=['POST']). This section acts as our machine learning pipeline where the users input text goes through the data processing, feature extraction and into the trained model so that the POST method then outputs the prediction into the {{ prediction_text }} section of our web application. Within the main section we then deploy our application using app.run(host="0.0.0.0") and what the 0.0.0.0 means is that the application will be visible in the local network and not local machine. Now all you have to do is copy and past the link displayed on your CLI once you run your application!
In this article we have done a walk through on how to deploy your trained model using Flask. With this walkthrough you have experienced first hand what it's like to set up your model for production. Now think of a problem, brainstorm ML solutions and start building!
You can access the project I used as an example through my GitHub link.