File size: 7,358 Bytes
f36f417
 
 
 
50274d7
 
 
e8c7c0f
190e59b
 
f36f417
e6e1d10
 
 
 
 
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
 
f36f417
e6e1d10
f36f417
e6e1d10
 
 
 
 
f36f417
e6e1d10
f36f417
e6e1d10
 
 
 
 
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
 
 
f36f417
e6e1d10
f36f417
e6e1d10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
 
 
f36f417
e6e1d10
 
 
f36f417
e6e1d10
 
f36f417
e6e1d10
f36f417
e6e1d10
 
 
 
 
 
 
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
 
 
 
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
f36f417
e6e1d10
 
 
 
f36f417
e6e1d10
f36f417
e6e1d10
 
 
 
 
 
 
 
 
 
 
 
 
 
f36f417
e6e1d10
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
license: cc
language:
- pt
tags:
- Hate Speech
- kNOwHATE
widget:
- text: "as pessoas tem que perceber que ser 'panasca' não é deixar de ser homem, é deixar de ser humano 😂😂"
pipeline_tag: text-classification
---
---
<img align="left" width="140" height="140" src="https://ilga-portugal.pt/files/uploads/2023/06/logo_HATE_cores_page-0001-1024x539.jpg">
<p style="text-align: center;">&nbsp;&nbsp;&nbsp;&nbsp;This is the model card for HateBERTimbau. 
  You may be interested in some of the other models from the <a href="https://huggingface.co/knowhate">kNOwHATE project</a>.
</p>

---

# HateBERTimbau

**HateBERTimbau** is a foundation, large language model for European **Portuguese** from **Portugal** for Hate Speech content.

It is an **encoder** of the BERT family, based on the neural architecture Transformer and 
developed over the [BERTimbau](https://huggingface.co/neuralmind/bert-large-portuguese-cased) model, retrained on a dataset of 229,103 tweets specifically focused on potential hate speech.

## Model Description

- **Developed by:** [kNOwHATE: kNOwing online HATE speech: knowledge + awareness = TacklingHate](https://knowhate.eu)
- **Funded by:** [European Union](https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/opportunities/topic-details/cerv-2021-equal)
- **Model type:** Transformer-based model retrained for Hate Speech in Portuguese social media text
- **Language:** Portuguese
- **Retrained from model:** [neuralmind/bert-large-portuguese-cased](https://huggingface.co/neuralmind/bert-large-portuguese-cased)

Several models were developed by fine-tuning Base HateBERTimbau for Hate Speech detection present in the table bellow:

| HateBERTimbau's Family of Models                                                                             | 
|---------------------------------------------------------------------------------------------------------|
| [**HateBERTimbau YouTube**](https://huggingface.co/knowhate/HateBERTimbau-youtube)        |
| [**HateBERTimbau Twitter**](https://huggingface.co/knowhate/HateBERTimbau-twitter)        |
| [**HateBERTimbau YouTube+Twitter**](https://huggingface.co/knowhate/HateBERTimbau-yt-tt)|

# Uses

You can use this model directly with a pipeline for masked language modeling:

```python
from transformers import pipeline
unmasker = pipeline('fill-mask', model='knowhate/HateBERTimbau')

unmasker("Os [MASK] são todos uns animais, deviam voltar para a sua terra.")

[{'score': 0.6771652698516846,
  'token': 12714,
  'token_str': 'africanos',
  'sequence': 'Os africanos são todos uns animais, deviam voltar para a sua terra.'},
 {'score': 0.08679857850074768,
  'token': 15389,
  'token_str': 'homossexuais',
  'sequence': 'Os homossexuais são todos uns animais, deviam voltar para a sua terra.'},
 {'score': 0.03806231543421745,
  'token': 4966,
  'token_str': 'portugueses',
  'sequence': 'Os portugueses são todos uns animais, deviam voltar para a sua terra.'},
 {'score': 0.035253893584012985,
  'token': 16773,
  'token_str': 'Portugueses',
  'sequence': 'Os Portugueses são todos uns animais, deviam voltar para a sua terra.'},
 {'score': 0.023521048948168755,
  'token': 8618,
  'token_str': 'brancos',
  'sequence': 'Os brancos são todos uns animais, deviam voltar para a sua terra.'}]

```

Or this model can be used by fine-tuning it for a specific task/dataset:

```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
from datasets import load_dataset

tokenizer = AutoTokenizer.from_pretrained("knowhate/HateBERTimbau")
model = AutoModelForSequenceClassification.from_pretrained("knowhate/HateBERTimbau")
dataset = load_dataset("knowhate/youtube-train")

def tokenize_function(examples):
    return tokenizer(examples["sentence1"], examples["sentence2"], padding="max_length", truncation=True)

tokenized_datasets = dataset.map(tokenize_function, batched=True)

training_args = TrainingArguments(output_dir="hatebertimbau", evaluation_strategy="epoch")
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_datasets["train"],
    eval_dataset=tokenized_datasets["validation"],
)

trainer.train()

```

# Training

## Data

229,103 tweets associated with offensive content were used to retrain the base model.

## Training Hyperparameters

- Batch Size: 4 samples
- Epochs: 100
- Learning Rate: 5e-5 with Adam optimizer
- Maximum Sequence Length: 512 sentence pieces

# Testing

## Data

We used two different datasets for testing, one for YouTube comments [here](https://huggingface.co/datasets/knowhate/youtube-test) and another for Tweets [here](https://huggingface.co/datasets/knowhate/twitter-test).

## Hate Speech Classification Results (with no fine-tuning)

| Dataset         | Precision  | Recall    | F1-score     |
|:----------------|:-----------|:----------|:-------------|
| **YouTube**     | 0.928      | 0.108     | **0.193**    | 
| **Twitter**     | 0.686      | 0.211     | **0.323**    |

# BibTeX Citation

``` latex
@mastersthesis{Matos-Automatic-Hate-Speech-Detection-in-Portuguese-Social-Media-Text,
title = {{Automatic Hate Speech Detection in Portuguese Social Media Text}},
author = {Matos, Bernardo Cunha},
month = nov,
year = {2022},
abstract = {{Online Hate Speech (HS) has been growing dramatically on social media and its uncontrolled spread has motivated researchers to develop a diversity of methods for its automated detection. However, the detection of online HS in Portuguese still merits further research. To fill this gap, we explored different models that proved to be successful in the literature to address this task. In particular, we have explored models that use the BERT architecture. Beyond testing single-task models we also explored multitask models that use the information on other related categories to learn HS. To better capture the semantics of this type of texts, we developed HateBERTimbau, a retrained version of BERTimbau more directed to social media language including potential HS targeting African descent, Roma, and LGBTQI+ communities. The performed experiments were based on CO-HATE and FIGHT, corpora of social media messages posted by the Portuguese online community that were labelled regarding the presence of HS among other categories.
The results achieved show the importance of considering the annotator's agreement on the data used to develop HS detection models. Comparing different subsets of data used for the training of the models it was shown that, in general, a higher agreement on the data leads to better results.
HATEBERTimbau consistently outperformed BERTimbau on both datasets confirming that further pre-training of BERTimbau was a successful strategy to obtain a language model more suitable for online HS detection in Portuguese. 
The implementation of target-specific models, and multitask learning have shown potential in obtaining better results.}},
language = {eng},
copyright = {embargoed-access},
}
```

# Acknowledgements

This work was funded in part by the European Union under Grant CERV-2021-EQUAL (101049306).
However the views and opinions expressed are those of the author(s) only and do not necessarily reflect those of the European Union or Knowhate Project.
Neither the European Union nor the Knowhate Project can be held responsible.