How to Use Streamlit and Python to Build a Data Science App

Written by davisdavid | Published 2021/10/28
Tech Story Tags: data-science | python-web-app-development | blogging-fellowship | machine-learning | python | python-tutorials | sentiment-analysis | hackernoon-top-story

TLDRStreamlit is an open-source python library for creating and sharing web apps for data science and machine learning projects. The library can help you create and deploy your data science solution in a few minutes with a few lines of code. The data science web app will show a text field to add the movie's review and a simple button to submit the review and make predictions. It will show results whenever a user adds a movie’s review and clicks the "make prediction" button on the form section.via the TL;DR App

Web apps are still useful tools for data scientists to present their data science projects to users. Since we may not have web development skills, we can use open-source python libraries like Streamlit to easily develop web apps in a short time.

TABLE OF CONTENTS

  1. Introduction to Streamlit
  2. Installation and Set up 
  3. Develop the Web App 
  4. Test the Web App
  5. Conclusion

1. Introduction to Streamlit 

Streamlit is an open-source python library for creating and sharing web apps for data science and machine learning projects. The library can help you create and deploy your data science solution in a few minutes with a few lines of code.
Streamlit can seamlessly integrate with other popular python libraries used in Data science such as NumPy, Pandas, Matplotlib, Scikit-learn and many more.
Note: Streamlit uses React as a frontend framework to render the data on the screen.

2. Installation and Set up

Streamlit requires python >= 3.7 version in your machine.
To install streamlit, you need to run the command below in the terminal.
pip install streamlit 
You can also check the version installed on your machine with the following command.
streamlit --version
streamlit, version 1.1.0
After successfully installing streamlit, you can test the library by running the command below in the terminal.
streamlit hello
Streamlit's Hello app will appear in a new tab in your web browser.
This shows that everything is running ok, we can move on and create our first web app by using Streamlit.

3. Develop the Web App

In this part, we are going to deploy the trained NLP model that predicts the sentiment of a movie's review (positive or negative). You can access the source code and the dataset here.
The data science web app will show a text field to add the movie's review and a simple button to submit the review and make predictions.
Import Important Packages
The first step is to create a python file called app.py and then import required python packages for both streamlit and the trained NLP model.
# import packages
import streamlit as st
import os
import numpy as np
 
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
 
# text preprocessing modules
from string import punctuation
 
# text preprocessing modules
from nltk.tokenize import word_tokenize
 
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import re  # regular expression
import joblib
 
import warnings
 
warnings.filterwarnings("ignore")
# seeding
np.random.seed(123)
 
# load stop words
stop_words = stopwords.words("english")
Function to Clean the Review 
The reviews can have unnecessary words and characters that we don't need when making predictions.
We will clean the review by removing stopwords, numbers, and punctuation. Then we will convert each word into its base form by using the lemmatization process in the NLTK package.
The text_cleaning() function will handle all necessary steps to clean our review before making a prediction.
# function to clean the text
@st.cache
def text_cleaning(text, remove_stop_words=True, lemmatize_words=True):
    # Clean the text, with the option to remove stop_words and to lemmatize word
 
    # Clean the text
    text = re.sub(r"[^A-Za-z0-9]", " ", text)
    text = re.sub(r"\'s", " ", text)
    text = re.sub(r"http\S+", " link ", text)
    text = re.sub(r"\b\d+(?:\.\d+)?\s+", "", text)  # remove numbers
 
    # Remove punctuation from text
    text = "".join([c for c in text if c not in punctuation])
 
    # Optionally, remove stop words
    if remove_stop_words:
        text = text.split()
        text = [w for w in text if not w in stop_words]
        text = " ".join(text)
 
    # Optionally, shorten words to their stems
    if lemmatize_words:
        text = text.split()
        lemmatizer = WordNetLemmatizer()
        lemmatized_words = [lemmatizer.lemmatize(word) for word in text]
        text = " ".join(lemmatized_words)
 
    # Return a list of words
    return text
Function to Make Prediction 
The python function called make_prediction() will do the following tasks.
  1. Receive the review and clean it.
  2. Load the trained NLP model.
  3. Make a prediction.
  4. Estimate the probability of the prediction.
  5. Finally, it will return the predicted class and its probability.
# functon to make prediction
@st.cache
def make_prediction(review):
 
    # clearn the data
    clean_review = text_cleaning(review)
 
    # load the model and make prediction
    model = joblib.load("sentiment_model_pipeline.pkl")
 
    # make prection
    result = model.predict([clean_review])
 
    # check probabilities
    probas = model.predict_proba([clean_review])
    probability = "{:.2f}".format(float(probas[:, result]))
 
    return result, probability
Note: if the trained NLP model predicts 1, it means Positive and if it predicts 0, it means Negative.
Create App Title and Description 
You can create the title of your web app and its description by using the title() and write() method from streamlit.
# Set the app title
st.title("Sentiment Analyisis App")
st.write(
    "A simple machine laerning app to predict the sentiment of a movie's review"
)
To show the web app, you need to run the following command in your terminal.
streamlit run app.py 
Then you will see the web app automatically pop up in your web browser or you can use the local URL created http://localhost:8501.
Create a Form to Receive a Movie's Review
The next step is to create a simple form by using streamlit. The form will show a text field to add your review and below the text field, it will show a simple button to submit the review added and then make a prediction.
# Declare a form to receive a movie's review
form = st.form(key="my_form")
review = form.text_input(label="Enter the text of your movie review")
submit = form.form_submit_button(label="Make Prediction")
Now, you can see the form on the web app.
Make Predictions and show Results
Our last piece of code is to make predictions and show results whenever a user adds a movie’s review and clicks the "make prediction" button on the form section.
After clicking the button, the web app will run the make_prediction() function and show the result on the web app in the browser.
if submit:
    # make prediction from the input text
    result, probability = make_prediction(review)
 
    # Display results of the NLP task
    st.header("Results")
 
    if int(result) == 1:
        st.write("This is a positive review with a probabiliy of ", probability)
    else:
        st.write("This is a negative review with a probabiliy of ", probability)

4. Test the Web App

With a few lines of code, we have created a simple data science web app that can receive a movie review and predict if it is a positive review or a negative review.
To test the web app, fill the text field by adding a movie review of your choice. I added the following movie review about  Zack Snyder's Justice League movie released in 2021.
"I loved the movie from the beginning to the end. Just like Ray Fisher said, I was hoping that the movie doesn't end. The begging scene was mind blowing, liked that scene very much. Unlike 'the Justice League' the movie show every hero is best at their own thing, make us love every character. Thanks, Zack and the whole team."
Then click the make prediction button and view the result.
As you can see on the web app we have created, the trained NLP model predicts the review added is positive with a probability of 0.64.
I recommend you add another movie review on the data science web app we have created and test it again.

5. Conclusion

There are a lot of features and components from Streamlit that you can use to develop a data science web app the way you want. What you have learned here, are some of the common elements from streamlit.
To learn more you can visit their beautiful designed documentation pages here.
If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next article!.
You can also find me on Twitter @Davis_McDavid and you can read more articles like this here.
Want to keep up to date with all the latest in Data Science? Subscribe to our newsletter in the footer below!

Written by davisdavid | Data Scientist | AI Practitioner | Software Developer| Technical Writer
Published by HackerNoon on 2021/10/28