Upload README.md with huggingface_hub
Browse files
README.md
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
base_model: BenjaminOcampo/model-contrastive-hatebert__trained-in-ishate__seed-0
|
3 |
+
datasets:
|
4 |
+
- ISHate
|
5 |
+
language:
|
6 |
+
- en
|
7 |
+
library_name: transformers
|
8 |
+
license: bsl-1.0
|
9 |
+
metrics:
|
10 |
+
- f1
|
11 |
+
- accuracy
|
12 |
+
tags:
|
13 |
+
- hate-speech-detection
|
14 |
+
- implicit-hate-speech
|
15 |
+
---
|
16 |
+
|
17 |
+
This model card documents the demo paper "PEACE: Providing Explanations and
|
18 |
+
Analysis for Combating Hate Expressions" accepted at the 27th European
|
19 |
+
Conference on Artificial Intelligence: https://www.ecai2024.eu/calls/demos.
|
20 |
+
|
21 |
+
# The Model
|
22 |
+
This model is a hate speech detector fine-tuned specifically for detecting
|
23 |
+
implicit hate speech. It is based on the paper "PEACE: Providing Explanations
|
24 |
+
and Analysis for Combating Hate Expressions" by Greta Damo, Nicolás Benjamín
|
25 |
+
Ocampo, Elena Cabrio, and Serena Villata, presented at the 27th European
|
26 |
+
Conference on Artificial Intelligence.
|
27 |
+
|
28 |
+
# Training Parameters and Experimental Info
|
29 |
+
The model was trained using the ISHate dataset, focusing on implicit data.
|
30 |
+
Training parameters included:
|
31 |
+
- Batch size: 32
|
32 |
+
- Weight decay: 0.01
|
33 |
+
- Epochs: 4
|
34 |
+
- Learning rate: 2e-5
|
35 |
+
|
36 |
+
For detailed information on the training process, please refer to the [model's
|
37 |
+
paper](https://aclanthology.org/2023.findings-emnlp.441/).
|
38 |
+
|
39 |
+
# Usage
|
40 |
+
|
41 |
+
First you might need the transformers version 4.30.2.
|
42 |
+
|
43 |
+
```
|
44 |
+
pip install transformers==4.30.2
|
45 |
+
```
|
46 |
+
|
47 |
+
This model was created using pytorch vanilla. In order to load it you have to use the following Model Class.
|
48 |
+
|
49 |
+
```python
|
50 |
+
class ContrastiveModel(nn.Module):
|
51 |
+
def __init__(self, model):
|
52 |
+
super(ContrastiveModel, self).__init__()
|
53 |
+
self.model = model
|
54 |
+
self.embedding_dim = model.config.hidden_size
|
55 |
+
self.fc = nn.Linear(self.embedding_dim, self.embedding_dim)
|
56 |
+
self.classifier = nn.Linear(self.embedding_dim, 2) # Classification layer
|
57 |
+
|
58 |
+
def forward(self, input_ids, attention_mask):
|
59 |
+
outputs = self.model(input_ids, attention_mask)
|
60 |
+
embeddings = outputs.last_hidden_state[:, 0] # Use the CLS token embedding as the representation
|
61 |
+
embeddings = self.fc(embeddings)
|
62 |
+
logits = self.classifier(embeddings) # Apply classification layer
|
63 |
+
|
64 |
+
return embeddings, logits
|
65 |
+
```
|
66 |
+
|
67 |
+
Then, we instantiate the model as:
|
68 |
+
|
69 |
+
```python
|
70 |
+
from transformers import AutoModel, AutoTokenizer, AutoConfig
|
71 |
+
|
72 |
+
repo_name = "BenjaminOcampo/peace_cont_hatebert"
|
73 |
+
|
74 |
+
config = AutoConfig.from_pretrained(repo_name)
|
75 |
+
contrastive_model = ContrastiveModel(AutoModel.from_config(config))
|
76 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_name)
|
77 |
+
```
|
78 |
+
|
79 |
+
Finally, to load the weights of the model we do as follows:
|
80 |
+
|
81 |
+
```python
|
82 |
+
model_tmp_file = hf_hub_download(repo_id=repo_name, filename="model.pt", token=read_token)
|
83 |
+
|
84 |
+
state_dict = torch.load(model_tmp_file)
|
85 |
+
|
86 |
+
contrastive_model.load_state_dict(state_dict)
|
87 |
+
```
|
88 |
+
|
89 |
+
You can make predictions as any pytorch model:
|
90 |
+
|
91 |
+
```
|
92 |
+
import torch
|
93 |
+
|
94 |
+
text = "Are you sure that Islam is a peaceful religion?"
|
95 |
+
inputs = tokenizer(text, return_tensors="pt")
|
96 |
+
|
97 |
+
with torch.no_grad():
|
98 |
+
_, logits = contrastive_model(inputs["input_ids"], inputs["attention_mask"])
|
99 |
+
|
100 |
+
probabilities = torch.softmax(logits, dim=1)
|
101 |
+
_, predicted_labels = torch.max(probabilities, dim=1)
|
102 |
+
```
|
103 |
+
|
104 |
+
# Datasets
|
105 |
+
The model was trained on the [ISHate dataset](https://huggingface.co/datasets/BenjaminOcampo/ISHate), specifically
|
106 |
+
the training part of the dataset which focuses on implicit hate speech.
|
107 |
+
|
108 |
+
# Evaluation Results
|
109 |
+
The model's performance was evaluated using standard metrics, including F1 score
|
110 |
+
and accuracy. For comprehensive evaluation results, refer to the linked paper.
|
111 |
+
|
112 |
+
Authors:
|
113 |
+
- [Greta Damo](https://grexit-d.github.io/damo.greta.github.io/)
|
114 |
+
- [Nicolás Benjamín Ocampo](https://www.nicolasbenjaminocampo.com/)
|
115 |
+
- [Elena Cabrio](https://www-sop.inria.fr/members/Elena.Cabrio/)
|
116 |
+
- [Serena Villata](https://webusers.i3s.unice.fr/~villata/Home.html)
|