Open In App

Create a Simple Sentiment Analysis WebApp using Streamlit

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how we can create a simple Sentiment Analysis webApp using with the help of Streamlit Module in Python.

Required Modules:

For this, we will need Three Modules.

  • Streamlit
  • TextBlob
  • Streamlit_extras (optional).

The module Streamlit_extras is optional because it will be used to add some extra features which will not affect the result of the Analysed Sentiment.

pip install streamlit
pip install -U textblob
pip install streamlit_extras

Stepwise Implementation

Firstly as we are creating a simple Sentiment Analysis app we will not fetch data using any other third-party API from websites like Twitter or Facebook. This will make this complex. We will simply put the text as user input will analyze their Sentiment and show the result.

Step – 1: First, let’s import all our required modules.

Python3




import streamlit as st
from textblob import TextBlob


Based on the animations or special attributes we will use in our web app we will import them from the Streamlit_Extras Module later.

Step – 2: Now we will give a suitable title to our web app and create a text_area in which user can put their text to analyze the sentiment.

Python3




st.title("A Simple Sentiment Analysis WebApp.")
  
text = st.text_area("Please Enter your text")



 

Step – 3: Then we will create a button by passing a text to it so that after entering the text user can click that button to get the Output.

Python3




if st.button("Analyze the Sentiment"):
  blob = TextBlob(message)
  result = blob.sentiment
  st.write(result)



 

Now as from the output we can see that is giving the result in a JSON format in the simplest way possible. Now we will make this webapp little better by adding some extra features and making the output look more convincing and concise.

We will now use the Streamlit_Extras module to add some extra features –

Firstly we will split the polarity and subjectivity and store them in some other variable. Then using the polarity value we will add some raining emojis, if the polarity is >= 0 then a smile emoji will rain to show positive sentiment, and if the polarity is negative a crying or tensed emoji will rain pointing it out that the sentiment is negative.

Also, we will use the warning and successful method of streamlit to print the result, unlike last time when we used simple to write.

Python3




if st.button("Analyze the Sentiment"):
  blob = TextBlob(message)
  result = blob.sentiment
  polarity = result.polarity
  subjectivity = result.subjectivity
  if polarity < 0:
    st.warning("The entered text has negative sentiments associated with it"+str(polarity))
    rain(
    emoji="????",
    font_size=20# the size of emoji
    falling_speed=3# speed of raining
    animation_length="infinite"# for how much time the animation will happen
)
  if polarity >= 0:
    st.success("The entered text has positive sentiments associated with it."+str(polarity))
    rain(
    emoji="????",
    font_size=20# the size of emoji
    falling_speed=3# speed of raining
    animation_length="infinite"# for how much time the animation will happen
    )
  st.success(result)



 

Below is the complete implementation

Python3




import streamlit as st
from textblob import TextBlob
  
  
from streamlit_extras.let_it_rain import rain
  
st.title("A Simple Sentiment Analysis WebApp.")
  
message = st.text_area("Please Enter your text")
  
if st.button("Analyze the Sentiment"):
  blob = TextBlob(message)
  result = blob.sentiment
  polarity = result.polarity
  subjectivity = result.subjectivity
  if polarity < 0:
    st.warning("The entered text has negative sentiments associated with it"+str(polarity))
    rain(
    emoji="????",
    font_size=20# the size of emoji
    falling_speed=3# speed of raining
    animation_length="infinite"# for how much time the animation will happen
)
  if polarity >= 0:
    st.success("The entered text has positive sentiments associated with it."+str(polarity))
    rain(
    emoji="????",
    font_size=20# the size of emoji
    falling_speed=3# speed of raining
    animation_length="infinite"# for how much time the animation will happen
    )
  st.success(result)


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads