How to Use ByT5 Small for Portuguese Product Reviews

Sep 12, 2024 | Educational

In this article, we will delve into how to utilize the ByT5 Small model specifically finetuned for sentiment analysis on product reviews in Portuguese. With clear, step-by-step instructions, you’ll be able to set this model up and start analyzing reviews in no time!

Model Description

The ByT5 Small model is a specialized version developed by Google, designed for analyzing sentiments from product reviews in Portuguese. This model was finetuned with a focus on effectiveness in evaluating customer feedback.

For those technically inclined, the academic paper detailing this work is available at https://arxiv.org/abs/2105.13626.

Training Data

The training data consists of product reviews sourced from Americanas.com. If you are interested in accessing this dataset, you can find it at https://github.com/HeyLucasLeao/finetuning-byt5-model.

Training Procedure

The model was finetuned using the Trainer Class available on the Hugging Face library. Key evaluation metrics included accuracy, precision, recall, and F1 score. Here are the training specifics:

  • Learning Rate: 1e-4
  • Epochs: 1

Colab Notebooks for Finetuning and Metrics

For those looking to finetune the model or check metrics, you can use the following Colab links:

Performance Scores

Here is an overview of the model’s performance on different datasets:

  • Training Set:
    • Accuracy: 0.8974
    • F1 Score: 0.9272
    • Precision: 0.9580
    • Recall: 0.8983
  • Test Set:
    • Accuracy: 0.8958
    • F1 Score: 0.9261
    • Precision: 0.9559
    • Recall: 0.8981
  • Validation Set:
    • Accuracy: 0.8925
    • F1 Score: 0.9239
    • Precision: 0.9525
    • Recall: 0.8969

How to Use the Model

Now, let’s get our hands a bit dirty with some code. Imagine you are a chef preparing a delightful dish. You need the right ingredients, the right tools, and the right steps to bring it all together. Here’s how you can classify product reviews using Python:

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

if torch.cuda.is_available():
    device = torch.device('cuda')
else:
    device = torch.device('cpu')

print(device)

tokenizer = AutoTokenizer.from_pretrained("HeyLucasLeao/byt5-small-pt-product-reviews")
model = AutoModelForSeq2SeqLM.from_pretrained("HeyLucasLeao/byt5-small-pt-product-reviews")
model.to(device)

def classificar_review(review):
    inputs = tokenizer([review], padding='max_length', truncation=True, max_length=512, return_tensors='pt')
    input_ids = inputs.input_ids.to(device)
    attention_mask = inputs.attention_mask.to(device)
    
    output = model.generate(input_ids, attention_mask=attention_mask)
    pred = np.argmax(output.cpu(), axis=1)
    dici = {0: "Review Negativo", 1: "Review Positivo"}
    return dici[pred.item()]

classificar_review(review)

In this code recipe:

  • First, we check if we have a robust kitchen (CUDA device) available; otherwise, we’ll use the basic tools (CPU).
  • Next, we gather our ingredients (tokenizer and model) from their respective pantry shelves.
  • Finally, we define the method classificar_review that will prepare our dish—a polite classification of the review as positive or negative.

Troubleshooting Ideas

If you encounter issues while using the model, consider the following troubleshooting tips:

  • Ensure you have the necessary libraries (like Transformers) installed in your Python environment.
  • Double-check that you’re correctly referencing the model path and version.
  • Monitor GPU memory usage if you are utilizing a CUDA device to avoid out-of-memory errors.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox