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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
base_model:
|
| 8 |
- intfloat/multilingual-e5-large
|
| 9 |
pipeline_tag: text-classification
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Multilingual Multi-task Classifier for Constructive Speech, Hateful Speech, and Agreement
|
| 13 |
+
|
| 14 |
+
## Model description
|
| 15 |
+
|
| 16 |
+
This model is a multilingual, multi-task classifier developed as part of the STANDBY research project. It is designed to classify text along three dimensions:
|
| 17 |
+
|
| 18 |
+
1) Hateful speech
|
| 19 |
+
|
| 20 |
+
2) Constructive speech
|
| 21 |
+
|
| 22 |
+
3) Agreement
|
| 23 |
+
|
| 24 |
+
The classifier is designed to capture these dimensions independently rather than treating them as mutually exclusive categories. Consequently, a text can for example simultaneously be classified as hateful and constructive.
|
| 25 |
+
|
| 26 |
+
The model supports both English and Danish.
|
| 27 |
+
|
| 28 |
+
## Model variants
|
| 29 |
+
|
| 30 |
+
Two model variants are available:
|
| 31 |
+
|
| 32 |
+
1) Non-context-enriched: Classifies a response independently, without additional conversational context.
|
| 33 |
+
|
| 34 |
+
2) Context-enriched: Classifies a response using both the reference text and the response, allowing the model to take the conversational context into account.
|
| 35 |
+
|
| 36 |
+
For the context-enriched model, the input should be formatted as:
|
| 37 |
+
|
| 38 |
+
[reference text] [SEP] [response text]
|
| 39 |
+
|
| 40 |
+
where [SEP] is the tokenizer's separator token.
|
| 41 |
+
|
| 42 |
+
The non-context-enriched model can be found [here](https://huggingface.co/AU-STANDBY/constructive_classifier_EN_DA_non_context_enriched)
|
| 43 |
+
|
| 44 |
+
## Base model
|
| 45 |
+
|
| 46 |
+
The classifier is built on top of intfloat/multilingual-e5-large.
|
| 47 |
+
|
| 48 |
+
## Labels and model outputs
|
| 49 |
+
|
| 50 |
+
The model produces nine output logits, divided into three separate classification tasks. A softmax function is applied independently to each task, resulting in three probability distributions.
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
| Task | Class 0 | Class 1 | Class 2 |
|
| 54 |
+
|---|---|---|---|
|
| 55 |
+
| Hateful speech | Not Hateful | Hateful | Unclassifiable Hateful |
|
| 56 |
+
| Constructive speech | Not Constructive | Constructive | Unclassifiable Constructive |
|
| 57 |
+
| Agreement | Agree | Disagree | Unclear |
|
| 58 |
+
|
| 59 |
+
Each task produces:
|
| 60 |
+
|
| 61 |
+
1) Label: The predicted class (the class with the highest probability).
|
| 62 |
+
|
| 63 |
+
2) Score: The probability assigned to the predicted class.
|
| 64 |
+
|
| 65 |
+
3) Probabilities: The probabilities for all three classes within that task.
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
## Installation and requirements
|
| 69 |
+
|
| 70 |
+
The models were tested using the following package versions:
|
| 71 |
+
|
| 72 |
+
| Package | Version |
|
| 73 |
+
|---|---|
|
| 74 |
+
| Python | 3.12.11 |
|
| 75 |
+
| PyTorch | 2.2.2 |
|
| 76 |
+
| Transformers | 4.49.0 |
|
| 77 |
+
| NumPy | 1.26.4 |
|
| 78 |
+
| Pandas | 2.2.3 |
|
| 79 |
+
|
| 80 |
+
## Data preprocessing
|
| 81 |
+
|
| 82 |
+
For optimal consistency with the training data, it is recommended to apply the same text preprocessing before classification.
|
| 83 |
+
|
| 84 |
+
```
|
| 85 |
+
import re
|
| 86 |
+
def preprocess_text(text):
|
| 87 |
+
#e.g. @PeterNice123 -> @user
|
| 88 |
+
text = re.sub(r'@\w+', '@user', text)
|
| 89 |
+
# https -> <link>
|
| 90 |
+
text = re.sub(r'http\S+', '<link>', text)
|
| 91 |
+
return text
|
| 92 |
+
|
| 93 |
+
df['text'] = df['text'].apply(preprocess_text)
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
## Usage
|
| 98 |
+
|
| 99 |
+
The following code demonstrates how to load and use the model on example texts.
|
| 100 |
+
|
| 101 |
+
```
|
| 102 |
+
import pandas as pd
|
| 103 |
+
import numpy as np
|
| 104 |
+
import torch
|
| 105 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 106 |
+
|
| 107 |
+
model_id = "AU-STANDBY/constructive_classifier_EN_DA_context_enriched"
|
| 108 |
+
|
| 109 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=True)
|
| 110 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 111 |
+
model_id,
|
| 112 |
+
token=True
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
model.eval()
|
| 116 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 117 |
+
model.to(device)
|
| 118 |
+
|
| 119 |
+
label_classes = {
|
| 120 |
+
"hateful": [
|
| 121 |
+
"Not Hateful",
|
| 122 |
+
"Hateful",
|
| 123 |
+
"Unclassifiable Hateful"
|
| 124 |
+
],
|
| 125 |
+
"constructive": [
|
| 126 |
+
"Not Constructive",
|
| 127 |
+
"Constructive",
|
| 128 |
+
"Unclassifiable Constructive"
|
| 129 |
+
],
|
| 130 |
+
"agree": [
|
| 131 |
+
"Agree",
|
| 132 |
+
"Disagree",
|
| 133 |
+
"Unclear"
|
| 134 |
+
]
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def predict_texts(rep_texts, ref_texts, tokenizer, model):
|
| 139 |
+
|
| 140 |
+
# Tokenize reference and response as a pair,
|
| 141 |
+
# following the exact structure used during training
|
| 142 |
+
encoded_data = tokenizer(
|
| 143 |
+
ref_texts,
|
| 144 |
+
rep_texts,
|
| 145 |
+
padding="max_length",
|
| 146 |
+
truncation=True,
|
| 147 |
+
max_length=512,
|
| 148 |
+
return_attention_mask=True,
|
| 149 |
+
return_tensors="pt"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
input_ids = encoded_data["input_ids"].to(device)
|
| 153 |
+
attention_mask = encoded_data["attention_mask"].to(device)
|
| 154 |
+
|
| 155 |
+
with torch.inference_mode():
|
| 156 |
+
outputs = model(
|
| 157 |
+
input_ids=input_ids,
|
| 158 |
+
attention_mask=attention_mask
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
logits = outputs.logits
|
| 162 |
+
|
| 163 |
+
# Split the 9 logits into the three classification tasks
|
| 164 |
+
logits_groups = {
|
| 165 |
+
"hateful": logits[:, :3],
|
| 166 |
+
"constructive": logits[:, 3:6],
|
| 167 |
+
"agree": logits[:, 6:9]
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
# Calculate probabilities separately for each task
|
| 171 |
+
probabilities = {
|
| 172 |
+
task: torch.softmax(group_logits, dim=1).cpu().numpy()
|
| 173 |
+
for task, group_logits in logits_groups.items()
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
results = []
|
| 177 |
+
|
| 178 |
+
for i, (rep_text, ref_text) in enumerate(zip(rep_texts, ref_texts)):
|
| 179 |
+
|
| 180 |
+
row = {
|
| 181 |
+
"text": rep_text,
|
| 182 |
+
"ref_text": ref_text
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
for task, probs in probabilities.items():
|
| 186 |
+
|
| 187 |
+
# Predicted class and its probability
|
| 188 |
+
pred_idx = np.argmax(probs[i])
|
| 189 |
+
pred_score = probs[i][pred_idx]
|
| 190 |
+
|
| 191 |
+
row[f"{task}_label"] = label_classes[task][pred_idx]
|
| 192 |
+
row[f"{task}_score"] = float(pred_score)
|
| 193 |
+
|
| 194 |
+
# Probability for each of the three labels
|
| 195 |
+
for k, label in enumerate(label_classes[task]):
|
| 196 |
+
row[f"{task}_prob_{k}"] = float(probs[i][k])
|
| 197 |
+
|
| 198 |
+
results.append(row)
|
| 199 |
+
|
| 200 |
+
return results
|
| 201 |
+
|
| 202 |
+
rep_texts = [
|
| 203 |
+
"I appreciate your perspective, and I think you make a good point.",
|
| 204 |
+
"You are completely wrong and your argument makes no sense.",
|
| 205 |
+
"I strongly disagree with what you are saying.",
|
| 206 |
+
"Thank you for sharing your opinion. I understand where you are coming from."
|
| 207 |
+
]
|
| 208 |
+
|
| 209 |
+
ref_texts = [
|
| 210 |
+
"I think we should consider both sides of the issue before making a decision.",
|
| 211 |
+
"Everyone who disagrees with me is an idiot.",
|
| 212 |
+
"This policy is clearly the best solution to the problem.",
|
| 213 |
+
"I don't think your argument is supported by the evidence."
|
| 214 |
+
]
|
| 215 |
+
|
| 216 |
+
predictions_df = pd.DataFrame(
|
| 217 |
+
predict_texts(rep_texts, ref_texts, tokenizer, model)
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
## Citation
|
| 223 |
+
|
| 224 |
+
If you use this model, please cite:
|
| 225 |
+
|
| 226 |
+
[INSERT PREPRINT TITLE AND LINK]
|
| 227 |
+
|
| 228 |
+
## Contact
|
| 229 |
+
|
| 230 |
+
For questions please contact:
|
| 231 |
+
|
| 232 |
+
- Ida Dencker / ida3mail@gmail.com
|
| 233 |
+
- Simon Karg / simonkarg@ps.au.dk
|