How to Utilize Transformer Models for Detecting Trolling, Aggression, and Cyberbullying

Sep 12, 2024 | Educational

In today’s digital landscape, managing online toxicity is more critical than ever. This blog will guide you through the process of fine-tuning multilingual transformer models, originally designed for the TRAC 2020 competition, to detect behaviors such as trolling, aggression, and cyberbullying. Get ready to dive into a robust approach that leverages state-of-the-art AI technology!

Why Multilingual Joint Fine-tuning?

When dealing with diverse online communities, it’s vital to consider multiple languages. The models we discuss have been trained extensively to recognize harmful behaviors across different cultures and languages, making them powerful tools for online moderation.

Getting Started

To use the models effectively, follow these steps:

  • Download Required Files: Be sure to download the trained model files from Illinois Databank.
  • Set Up Environment: Ensure you have Python and the Transformers library installed.
  • Preparation: Create a folder to unzip the downloaded model files.

Code Walkthrough

Let’s break down the provided code into manageable pieces. Think of a chef preparing a unique dish by gathering ingredients (data) from different regions (models) and blending them to create a masterpiece (predictions).

python
from transformers import AutoModel, AutoTokenizer, AutoModelForSequenceClassification
import torch
from pathlib import Path
from scipy.special import softmax
import numpy as np
import pandas as pd

TASK_LABEL_IDS = {
    'Sub-task A': ['OAG', 'NAG', 'CAG'],
    'Sub-task B': ['GEN', 'NGEN'],
    'Sub-task C': ['OAG-GEN', 'OAG-NGEN', 'NAG-GEN', 'NAG-NGEN', 'CAG-GEN', 'CAG-NGEN']
}

model_version = 'databank'  # other option is hugging face library
if model_version == 'databank':
    # Specify the model path and load the model
    model_path = next(Path('databank_model').glob('*.output/*.model'))
    lang, task, _, base_model, _ = model_path.parts
    tokenizer = AutoTokenizer.from_pretrained(base_model)
    model = AutoModelForSequenceClassification.from_pretrained(model_path)
else:
    lang, task, base_model = 'ALL', 'Sub-task C', 'bert-base-multilingual-uncased'
    tokenizer = AutoTokenizer.from_pretrained(base_model)
    model = AutoModelForSequenceClassification.from_pretrained(base_model)

# Prepare the model for evaluation
model.eval()
sentence = "This is a good cat and this is a bad dog."
processed_sentence = f"{sentence}"
tokens = tokenizer.tokenize(processed_sentence)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokens)
tokens_tensor = torch.tensor([indexed_tokens])

# Making predictions
with torch.no_grad():
    logits = model(tokens_tensor, labels=None)
preds_probs = softmax(logits.detach().cpu().numpy(), axis=1)
preds = np.argmax(preds_probs, axis=1)
preds_labels = np.array(TASK_LABEL_IDS[task])[preds]
print(dict(zip(TASK_LABEL_IDS[task], preds_probs[0])), preds_labels)

Here’s a brief analysis of the code’s components:

  • The first part imports the essential libraries and defines task labels, categorizing the types of online abuse.
  • The conditional checks which model version to use, either from the databank or Hugging Face, loading the appropriate tokenizer and model.
  • The input sentence is processed, tokenized, and passed through the model to predict the probabilities of various abusive labels.
  • Finally, it prints the predictions alongside their confidence scores, helping you gauge how certain the model is regarding its findings.

Troubleshooting Tips

If you encounter issues, here are some troubleshooting steps you can take:

  • Ensure that you have correctly downloaded and unzipped the models from the Illinois Databank.
  • Check your Python environment to confirm that all required libraries (Transformers, NumPy, etc.) are installed.
  • Verify that you’re using the correct version of the model: the databank model or Hugging Face model.
  • If the model doesn’t seem to be predicting, ensure that the input sentence is formatted correctly; minor changes in tokenization can lead to different outputs.

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

Conclusion

Embracing the power of multilingual transformer models enables more robust detection of harmful online behaviors. By following this guide, you can tailor these models to fit your specific needs, contributing to a safer online community. 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