How to deploy NLP model using Flask on localhost
Explore the blog for learn
In this tutorial, we are going to learn how to deploy the Natural Language Processing model on the localhost using the Python’s Framework flask and Machine Learning model will be deploy same as the NLP model. And in the next post, We will learn how to deploy a flask application on the heroku platform.
So for the learning purpose, we are going to deploy a Movies Reviews Sentiment Analysis in NLP trained model on the localhost using Flask. If you want to learn how to train the Movies Reviews Sentiment Analysis model, you can visit the previous post of how to train Movies Reviews Sentiment Analysis in natural language processing, which is already uploaded on my blog. If you don't read that so you can read that post from the below link:
Movies Reviews Sentiment Analysis in Natural Language Processing: Movies Reviews Sentiment Analysis
Make sure that your hierarchy of the flask application should be like the image below. The hierarchy of the flask application contains the two folders template and static, one python file, one requirements.txt file, one profile, one vectorizer file and one machine learning trained model.
Template folder : It contains your webpages (html files).
Static folder : It contains your CSS, JavaScript, images files
Python file : This file contains the code of a working application and embedded the webpage in this file.
Machine learning model : This is your trained Movies Reviews Sentiment Analysis model.
Vectorizer file : This is a Vectorizer file. This file is used to convert the categorical data into numeric characters. And you need to save it like a machine learning model.
requirements.txt : This file contains all the required modules which are used to run and implement the flask application.
Procfile file : Procfile file (without extension) contains the one line code for running the application on heroku. We will learn in the next post how to deploy a flask application with a machine learning model on the heroku platform.
.ipnyb file : It's a optional file. You can keep in the hierarchy.
1 . templates / home.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Movies Reviews Classifier</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='spam-favicon.ico') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
<script src="https://kit.fontawesome.com/5f3f547070.js" crossorigin="anonymous"></script>
</head>
<body>
<!-- Website Title -->
<div class="container">
<h2 class='container-heading'><span>Movies Reviews Classifier</span> </h2>
<div class='description'>
<p>A Machine Learning Web App which is implemented using Flask</p>
</div>
</div>
<!-- Text Area -->
<div class="ml-container">
<form action="{{ url_for('predict') }}" method="POST">
<textarea class='message-box' name="message" rows="15" cols="75" placeholder="Enter Your Message Here..."></textarea><br/>
<input type="submit" class="my-cta-button" value="Predict">
</form>
</div>
<!-- Footer -->
<div class='footer'>
<div class="contact">
<a target="_blank" href="https://github.com/JatinSadhwani02/Movies_Review_Classification_NLP"><i class="fab fa-github fa-lg contact-icon"></i></a>
<a target="_blank" href="https://www.linkedin.com/in/jatin-sadhwani-404531152/"><i class="fab fa-linkedin fa-lg contact-icon"></i></a>
</div>
<p class='footer-description'>Made with ❤️ by Jatin Sadhwani.</p>
</div>
</body>
</html>
1. templates / result.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Movies Reviews Classifier</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='spam-favicon.ico') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
<script src="https://kit.fontawesome.com/5f3f547070.js" crossorigin="anonymous"></script>
</head>
<body>
<!-- Website Title -->
<div class="container">
<h2 class='container-heading'><span>Movies Reviews Classifier</span></h2>
<div class='description'>
<p>A Machine Learning Web App which is implemented using Flask</p>
</div>
</div>
<!-- Result -->
<div class="results">
{% if prediction==1 %}
<h1>Prediction: <span class='danger'>Positive Review.</span></h1>
<img class="gif" src="https://media.giphy.com/media/l1J9vkau0r5BYk07e/giphy.gif" alt="happy Image" , height="400" , width="400">
{% elif prediction==0 %}
<h1>Prediction: <span class='safe'>Negative Review</span></h1>
<img class="gif" src="https://media.giphy.com/media/Pn1h5Un3LZD9uq3u07/giphy.gif" alt="sad image" , height="400" , width="400">
{% endif %}
</div>
<!-- Footer -->
<div class='footer'>
<div class="contact">
<a target="_blank" href="https://github.com/JatinSadhwani02/Movies_Review_Classification_NLP"><i class="fab fa-github fa-lg contact-icon"></i></a>
<a target="_blank" href="https://www.linkedin.com/in/jatin-sadhwani-404531152/"><i class="fab fa-linkedin fa-lg contact-icon"></i></a>
</div>
<p class='footer-description'>Made with ❤️ by Jatin Sadhwani.</p>
</div>
</body>
</html>
2. static / style.css
html{
height: 100%;
margin: 0;
}
body{
font-family: Arial, Helvetica,sans-serif;
text-align: center;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
/* Website Title */
.container{
padding: 30px;
position: relative;
background: linear-gradient(45deg, #ffffff, #ffffff, #f9f9f9, #eeeeee, #e0e4e1, #d7e1ec);
background-size: 500% 500%;
animation: change-gradient 10s ease-in-out infinite;
}
@keyframes change-gradient {
0%{
background-position: 0 50%;
}
50%{
background-position: 100% 50%;
}
100%{
background-position: 0 50%;
}
}
.container-heading{
margin: 0;
}
.container span{
color: #ff0000;
}
.description p{
font-style: italic;
font-size: 14px;
margin: 3px 0 0;
}
/* Text Area */
.ml-container{
margin: 30px 0;
flex: 1 0 auto;
}
.message-box{
margin-bottom: 20px;
}
/* Predict Button */
.my-cta-button{
background: #f9f9f9;
border: 2px solid #000000;
border-radius: 1000px;
box-shadow: 3px 3px #8c8c8c;
padding: 10px 36px;
color: #000000;
display: inline-block;
font: italic bold 20px/1 "Calibri", sans-serif;
text-align: center;
}
.my-cta-button:hover{
color: #ff0000;
border: 2px solid #ff0000;
}
.my-cta-button:active{
box-shadow: 0 0;
}
/* Footer */
.footer{
font-size: 14px;
padding: 20px;
flex-shrink: 0;
position: relative;
background: linear-gradient(45deg, #ffffff, #ffffff, #f9f9f9, #eeeeee, #e0e4e1, #d7e1ec);
background-size: 500% 500%;
animation: change-gradient 10s ease-in-out infinite;
}
.contact-icon{
color: #000000;
padding: 7px;
}
.contact-icon:hover{
color: #8c8c8c;
}
.footer-description{
margin: 0;
font-size: 12px;
}
/* Result */
.results{
padding: 30px 0 0;
flex: 1 0 auto;
}
.danger{
color: #ff0000;
}
.safe{
color: green;
}
.gif{
width: 30%;
}
2. static / spam-favicon.ico
Download this icon and keep in the static folder.
3. app.py
c# Importing essential libraries
from flask import Flask, render_template, request
import pickle
# Load the Naive Bayes model and TfidfVectorizer object from disk
filename = 'Movies_Review_Classification.pkl'
classifier = pickle.load(open(filename, 'rb'))
cv = pickle.load(open('count-Vectorizer.pkl','rb'))
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/predict',methods=['POST'])
def predict():
if request.method == 'POST':
message = request.form['message']
data = [message]
vect = cv.transform(data).toarray()
my_prediction = classifier.predict(vect)
return render_template('result.html', prediction=my_prediction)
if __name__ == '__main__':
app.run(debug=True)
4. requirements.txt
Flask==1.1.1
gunicorn==19.9.0
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.15.5
numpy>=1.9.2
scipy>=0.15.1
scikit-learn>=0.18
matplotlib>=1.4.3
pandas>=0.19
web: gunicorn app:app
6. Machine Learning Model
7. Vectorizer Model
Download Vectorizer model and machine learning model from the given link : ML and Vectorizer Model
Make a hierarchy of this material like the image which is shown above. And that, you can use the project.
How to download complete project
You can download the complete project from the below link and run that project on the localhost.
Download Movies Reviews Sentiment Analysis : Movies Reviews Sentiment Analysis in NLP
Just click go to the GitHub project repository using the above link and just simply click on the Fork button. After click on the fork button, project will be added in your GitHub account.
How to run the project
- After fork into the GitHub account.
- Download the project into your local system.
- Extract the folder.
- Go into the project folder.
- Open command prompt and go to the project folder using the cd command.
- Write in the command prompt (python app.py).
- You will get a link like (http://127.0.0.1:5000/)
- Copy the link and paste into the browser.
- your project will run.

