Build a machine learning web app in Python

Original Source Here

We are going to build a simple sentiment analysis application. This app will get a sentence as user input, and return with a prediction of whether this sentence is positive, negative, or neutral.

Here’s how the end product will look:

Image by author

Step 1: Setup

You can use any Python IDE to build and deploy this app. I suggest using Atom, Visual Studio Code, or Eclipse. You won’t be able to use a Jupyter Notebook to build the web application. Jupyter Notebooks are built primarily for data analysis, and it isn’t feasible to run a web server with Jupyter.

Once you have a programming IDE set up, you will need to have the following libraries installed: Pandas, Nltk, and Flask.

Step 2: Building the app

Create a folder for this entire sentiment analysis project. Under your main folder, you will need to have the following files:

Image by author

Let’s go through these files one by one:

First, create the file called This is the file that will contain all your machine learning code.

Once that’s done, copy and paste the following lines of code into your file:

import pandas as pd
import nltk
from flask import request
from flask import jsonify
from flask import Flask, render_template
app = Flask(__name__)
def my_form():
return render_template('index.html')
@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']'vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
score = ((sid.polarity_scores(str(text)))['compound'])
label = 'This sentence is positive'
elif(score == 0):
label = 'This sentence is neutral'
label = 'This sentence is negative'
return(render_template('index.html', variable=label))if __name__ == "__main__":'8088',threaded=False)

I chose to run this application on port 8088, but any empty port will work fine.

Before you run the app, create a folder called templates under your parent directory. Then, create one file within this folder called index.html:

Image by author

In the index.html file, type the following lines of code:

<!DOCTYPE html>
<link rel='stylesheet' href='bootstrap.min.css'></style>
<link rel='stylesheet' href='font-awesome.min.css'></style>
<div class="container">
<div class = "centered">
<header class="text-center text-white">
<title> sentiment analysis app </title>
<h1 class = 'header'>
Sentiment Analysis App
<p class = 'desc'>
Type a sentence, click on the submit button and wait for your prediction.
<form method="POST" onSubmit = 'return validate();' class='form'>
<input name="text">
<input type="submit" onclick = 'this.form.submit();'>
<p id='demo'> </p>
<style> .container {
position: relative;
text-align: center;
color: white;
.header {
color: black
p { color: #111; font-family: 'Helvetica Neue', sans-serif; font-size: 14px; line-height: 24px; margin: 0 0 24px; text-justify: inter-word; } h1 { color: #111; font-family: 'Helvetica Neue', sans-serif; font-size: 36px; font-weight: bold; letter-spacing: -1px; line-height: 1; text-align: center; } #demo {
color: #111; font-family: 'Helvetica Neue', sans-serif; font-size: 22px; line-height: 24px; margin: 0 0 24px; text-justify: inter-word;
input[type=submit] {
background-color: #f06237; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 0.95em;
input[type=submit]:hover {
background-color: #b0540e;


The above lines of code will create the front-end of our application: the input text-box, description, and submit button.

Upon clicking the submit button, the output from the Flask app will be displayed on the screen.

In this case, the output will be positive, negative, or neutral, depending on the user’s input.


We now have everything we need to run the Flask app locally.

Run the app on Python, and go to the location at which your application resides. In my case, for instance, it will be

You should see the app displayed on your screen:

Image by author

You can play around with your app and enter random sentences as input, to check if the algorithm makes accurate predictions.


Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: