# Building a Sentiment Analysis App with Gradio and Hugging Face Transformers

In this tutorial, we will create a sentiment analysis app using Gradio and Hugging Face's Transformers library. This app will classify the sentiment of a given text as positive, neutral, or negative.

# Prerequisites

Before we start, make sure you have Python installed on your system. You also need to install the following libraries:

  • transformers
  • numpy
  • scipy
  • gradio

You can install these libraries using pip:

pip install transformers numpy scipy gradio

# Step 1: Importing the Required Libraries

First, we need to import the required libraries and modules:

from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
import numpy as np
from scipy.special import softmax
import gradio as gr

# Step 2: Loading the Pre-trained Model and Tokenizer

We will use a pre-trained model from the Hugging Face model hub. In this example, we are using the cardiffnlp/twitter-roberta-base-sentiment-latest model.

model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"

tokenizer = AutoTokenizer.from_pretrained(model_path)
config = AutoConfig.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)

# Step 3: Defining the Sentiment Analysis Function

Next, we define a function that takes a text input, processes it using the tokenizer and the model, and returns the sentiment scores.

def sentiment_analysis(text):
    encoded_input = tokenizer(text, return_tensors='pt')
    output = model(**encoded_input)
    scores_ = output[0][0].detach().numpy()
    scores_ = softmax(scores_)
    labels = ['Negative', 'Neutral', 'Positive']
    scores = {l: float(s) for (l, s) in zip(labels, scores_)}
    return scores

# Step 4: Creating the Gradio Interface

Now, we create a Gradio interface for our sentiment analysis function. This interface includes a text input, an output label, and some example inputs.

demo = gr.Interface(
    theme=gr.themes.Base(),
    fn=sentiment_analysis,
    inputs=gr.Textbox(placeholder="Write your text here..."),
    outputs="label",
    examples=[
        ["I'm thrilled about the job offer!"],
        ["The weather today is absolutely beautiful."],
        ["I had a fantastic time at the concert last night."],
        ["I'm so frustrated with this software glitch."],
        ["The customer service was terrible at the store."],
        ["I'm really disappointed with the quality of this product."]
    ],
    title='Sentiment Analysis App',
    description='This app classifies a positive, neutral, or negative sentiment.'
)

# Step 5: Launching the App

Finally, we launch the Gradio app. We specify the server name and port to make the app accessible.

demo.launch(server_name="0.0.0.0", server_port=9200)

# Step 6: Using cnvrg to launch the App

  1. Click Apps in your project.
  2. Select Generic App.
  3. For Command, type the command required to run your app. python gradio_app.py
  4. For Image, select a relevant Docker image or custom image that suits your application.
  5. For Compute, select the appropriate compute resources to run your application.
  6. Click Publish. cnvrg will deploy your application based on the provided configuration.

Here is the complete code for the sentiment analysis app:

from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
import numpy as np
from scipy.special import softmax
import gradio as gr

model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"

tokenizer = AutoTokenizer.from_pretrained(model_path)
config = AutoConfig.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)

def sentiment_analysis(text):
    encoded_input = tokenizer(text, return_tensors='pt')
    output = model(**encoded_input)
    scores_ = output[0][0].detach().numpy()
    scores_ = softmax(scores_)
    labels = ['Negative', 'Neutral', 'Positive']
    scores = {l: float(s) for (l, s) in zip(labels, scores_)}
    return scores

demo = gr.Interface(
    theme=gr.themes.Base(),
    fn=sentiment_analysis,
    inputs=gr.Textbox(placeholder="Write your text here..."),
    outputs="label",
    examples=[        ["I'm thrilled about the job offer!"],
        ["The weather today is absolutely beautiful."],
        ["I had a fantastic time at the concert last night."],
        ["I'm so frustrated with this software glitch."],
        ["The customer service was terrible at the store."],
        ["I'm really disappointed with the quality of this product."]
    ],
    title='Sentiment Analysis App',
    description='This app classifies a positive, neutral, or negative sentiment.'
)

demo.launch(server_name="0.0.0.0", server_port=9200)
Last Updated: 6/18/2024, 7:36:59 AM