Create Sentiment Analysis Web App Using Python And Huggingface

Hello Learners…

Welcome to my blog…

Table Of Content:

  • Introduction
  • What is sentiment analysis?
  • Sentiment analysis models from HuggingFace
  • Save the pre-trained model on my local system
  • Summary

Introduction:

In this post, I create a simple sentiment analysis web app using the python streamlit library and deep learning models from huggingface. this web app analysis your text and give a response with the label positive, negative, or neutral with the score of that label.

What is sentiment analysis?

Sentiment Analysis is an NLP(natural language processing) Technique that tells us the emotion(like positive, negative, or neutral) of our text.

Sentiment Analysis models from HuggingFace

HuggingFace is an open-source deep learning models community that provides state-of-the-art models through transformers API.

This is the URL for the Sentiment Analysis Models By HuggingFace:https://huggingface.co/models?other=sentiment-analysis

Save the pre-trained model on my local system

To use a pre-trained model it’s a good practice to download that model to the local system and then use it. you can use it directly without downloading it. I download it on my local system first then I use it.

Here I used this model: https://huggingface.co/finiteautomata/bertweet-base-sentiment-analysis

To save the pre-trained model on my local system I run the code below:

To run the given code you need to install pytorch and transformers using pip so we first install both

pip install torch
pip install transformers

After Installation, I create a save_pretrained.py file and paste the below code into that file

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")

model = AutoModelForSequenceClassification.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")

tokenizer.save_pretrained("bertweet-base-sentiment-analysis")
model.save_pretrained("bertweet-base-sentiment-analysis")

Now I run save_pretrained.py

python3 save_pretrained.py

It will create a bertweet-base-sentiment-analysis directory in the current directory and save all required files into this.

screenshot by the author

Now, I create a web application using the streamlit library.

For this first, I have to install streamlit using pip:

pip install streamlit

After installation now I create a streamlit_app.py file and paste the below code into that file.

import streamlit as st
from transformers import pipeline



st.title("Sentiment Analysis")

def main():
    try:
        text=st.text_input("Enter your text")

        if st.button("Click Here"):
            st.write("Your Text:",text)
            if text :
                
                classifier = pipeline("text-classification", model="bertweet-base-sentiment-analysis")
                    
                print(classifier(text))
                result=classifier(text) # it will return dict of list
                st.write(result[0])
            else:
                st.error("Please enter your text")
    except Exception as e:
        st.error({"errror":str(e)})

main()

Now run the streamlit_app.py file

streamlit run streamlit_app.py

You get an URL: http://localhost:8501

Open It in Browser if not opened automatically. you can see the below interface.

screenshot by author

Now enter the text, click the button, and see the results.

screenshot by the author

Here you can see the result. the label is the “POS”(positive) and the score of your result.

Here is the second example

screenshot by the author

Here you can see the result. the label is the “NEG”(negative) and the score of your result.

Summary

This is the basic Sentiment Analysis using Python’s streamlit library and transformers API by the huggingface community. you can use this model for your normal sentiment analysis task.it is a good way to start with NLP(Natural language Processing).

I hope you like the blog…

Happy Learning And Keep Learning…

Thank You…

References:

1 thought on “Create Sentiment Analysis Web App Using Python And Huggingface”

Leave a Comment