Inference Unlimited

Building Your Own Sentiment Analysis Tool Using LLM

Introduction

Sentiment analysis is the process of determining the emotional tone of text, which can be applied in various fields such as marketing, customer service, and public opinion research. In this article, we will show you how to build your own sentiment analysis tool using large language models (LLMs).

Model Selection

The first step is to choose an appropriate language model. We can choose between:

For our example, we will use Hugging Face Transformers with the DistilBERT model, which is a lighter version of BERT and is well-suited for sentiment analysis tasks.

Installing Required Libraries

To get started, let's install the necessary libraries:

pip install transformers torch pandas

Loading the Model and Tokenizer

Next, load the model and tokenizer:

from transformers import pipeline

# Loading the ready-to-use sentiment analysis tool
sentiment_pipeline = pipeline("sentiment-analysis")

Preparing Data

Prepare a dataset for testing. We can use a simple example:

texts = [
    "I love this product, it's fantastic!",
    "I do not recommend it, very disappointed.",
    "Average product, nothing exceptional.",
    "The performance is satisfying, but the price is too high."
]

Sentiment Analysis

Now we can perform sentiment analysis on our texts:

results = sentiment_pipeline(texts)

for text, result in zip(texts, results):
    print(f"Text: {text}")
    print(f"Sentiment: {result['label']} (Confidence: {result['score']:.2f})")
    print("---")

Customizing the Model

If we want to customize the model for our specific data, we can use the Hugging Face Transformers library to train the model on our dataset.

from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import Trainer, TrainingArguments
import pandas as pd
from sklearn.model_selection import train_test_split

# Example dataset
data = pd.DataFrame({
    "text": ["I love this product", "I do not recommend", "Average product"],
    "label": [1, 0, 0]  # 1 - positive, 0 - negative
})

# Splitting data into training and test sets
train_texts, test_texts, train_labels, test_labels = train_test_split(
    data["text"], data["label"], test_size=0.2
)

# Loading tokenizer and model
model_name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)

# Tokenizing data
train_encodings = tokenizer(list(train_texts), truncation=True, padding=True)
test_encodings = tokenizer(list(test_texts), truncation=True, padding=True)

# Class to handle data
class SentimentDataset(torch.utils.data.Dataset):
    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels

    def __getitem__(self, idx):
        item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
        item["labels"] = torch.tensor(self.labels[idx])
        return item

    def __len__(self):
        return len(self.labels)

train_dataset = SentimentDataset(train_encodings, train_labels)
test_dataset = SentimentDataset(test_encodings, test_labels)

# Training settings
training_args = TrainingArguments(
    output_dir="./results",
    num_train_epochs=3,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=64,
    warmup_steps=500,
    weight_decay=0.01,
    logging_dir="./logs",
    logging_steps=10,
)

# Training the model
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=test_dataset
)

trainer.train()

Deploying the Model

After training the model, we can save it and use it for sentiment analysis:

model.save_pretrained("./custom_sentiment_model")
tokenizer.save_pretrained("./custom_sentiment_model")

# Loading the customized model
custom_model = AutoModelForSequenceClassification.from_pretrained("./custom_sentiment_model")
custom_tokenizer = AutoTokenizer.from_pretrained("./custom_sentiment_model")

# Example analysis
custom_pipeline = pipeline("sentiment-analysis", model=custom_model, tokenizer=custom_tokenizer)
print(custom_pipeline("This product is great!"))

Summary

In this article, we demonstrated how to build your own sentiment analysis tool using large language models. Step by step, we discussed model selection, data preparation, sentiment analysis, and model customization to meet our needs. With this tool, we can effectively analyze the emotional tone of texts in various fields.

Język: EN | Wyświetlenia: 14

← Powrót do listy artykułów