Sentiment Analysis with Hugging Face Transformers in Python

Sentiment analysis is a critical tool in natural language processing (NLP) that helps extract meaning from textual data. In this tutorial, we'll show you how to perform sentiment analysis using the Hugging Face Transformers library in Python.

Table of Contents

Introduction to Hugging Face Transformers

Hugging Face Transformers is a popular open-source library that provides state-of-the-art NLP models for various tasks, including sentiment analysis. It offers pre-trained models and an easy-to-use API, making it simple to implement NLP pipelines in your Python applications.

Installation and Setup

First, let's install the required packages. You can install the Hugging Face Transformers library using pip:

pip install transformers

Now, let's import the necessary modules:

from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline

Loading Pre-trained Models

Hugging Face offers a wide range of pre-trained models for various tasks. For sentiment analysis, we'll use the distilbert-base-uncased-finetuned-sst-2-english model. You can load the pre-trained model and its corresponding tokenizer using the AutoModelForSequenceClassification and AutoTokenizer classes, respectively:

model_name = "distilbert-base-uncased-finetuned-sst-2-english"

model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

Tokenization

Tokenization is the process of converting raw text into a format that the model can understand. The tokenizer object we created earlier can be used to tokenize our input text:

text = "I love using Hugging Face Transformers for NLP tasks!"

tokens = tokenizer(text, return_tensors="pt", truncation=True, padding=True)

The return_tensors argument specifies the format of the output tensors ("pt" for PyTorch), while truncation and padding ensure that the input is trimmed or padded to fit the model's requirements.

Model Inference

Now that we have tokenized our input, we can use the model to perform sentiment analysis:

output = model(**tokens)

The output object contains the model's prediction logits. To convert these logits into probabilities, we can apply the softmax function:

import torch

probabilities = torch.nn.functional.softmax(output.logits, dim=-1)

Finally, we can extract the predicted sentiment label by finding the index of the maximum probability:

sentiment = torch.argmax(probabilities)

Putting It All Together

Let's create a function to perform sentiment analysis using the Hugging Face Transformers library:

def sentiment_analysis(text):
    tokens = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    output = model(**tokens)
    probabilities = torch.nn.functional.softmax(output.logits, dim=-1)
    sentiment = torch.argmax(probabilities)
    
    return "Positive" if sentiment.item() == 1 else "Negative"

text = "I love using Hugging Face Transformers for NLP tasks!"
print(sentiment_analysis(text))  # Output: Positive

You can now use the sentiment_analysis function to easily perform sentiment analysis on any given text using Hugging Face Transformers in Python.

An AI coworker, not just a copilot

View VelocityAI