Download README.md from AU-STANDBY/constructive_classifier_EN_DA_non_context_enriched: direct link, hf CLI and curl.
- Browser
- Download file 6.8 kB
-
https://huggingface.co/AU-STANDBY/constructive_classifier_EN_DA_non_context_enriched/resolve/main/README.md
- Command line
-
hf download hf://AU-STANDBY/constructive_classifier_EN_DA_non_context_enriched/README.md
-
curl -L -o README.md https://huggingface.co/AU-STANDBY/constructive_classifier_EN_DA_non_context_enriched/resolve/main/README.md
datasets:
- AU-STANDBY/annotated_constructive_dataset_DA_EN
language:
- en
- da
base_model:
- intfloat/multilingual-e5-large
pipeline_tag: text-classification
Multilingual Multi-task Classifier for Constructive Speech, Hateful Speech, and Agreement
Model description
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:
Hateful speech
Constructive speech
Agreement
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.
The model supports both English and Danish.
Model variants
Two model variants are available:
Non-context-enriched: Classifies a response independently, without additional conversational context.
Context-enriched: Classifies a response using both the reference text and the response, allowing the model to take the conversational context into account.
For the context-enriched model, the input should be formatted as:
[reference text] [SEP] [response text]
where [SEP] is the tokenizer's separator token.
The context-enriched model can be found here
Base model
The classifier is built on top of intfloat/multilingual-e5-large.
Labels and model outputs
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.
| Task | Class 0 | Class 1 | Class 2 |
|---|---|---|---|
| Hateful speech | Not Hateful | Hateful | Unclassifiable Hateful |
| Constructive speech | Not Constructive | Constructive | Unclassifiable Constructive |
| Agreement | Agree | Disagree | Unclear |
Each task produces:
Label: The predicted class (the class with the highest probability).
Score: The probability assigned to the predicted class.
Probabilities: The probabilities for all three classes within that task.
Model performance
| Constructive | Hate | Agree | Avg. F1 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Y | N | U | F1 | Y | N | U | F1 | A | D | U | F1 | |
| .77 | .92 | .77 | .88 | .65 | .91 | .68 | .86 | .70 | .80 | .56 | .71 | .816 |
The table presents the model's performance across three classification tasks: constructive speech, hate speech, and agreement. For constructive and hate speech, Y = Yes, N = No, and U = Unclassifiable. For agreement, A = Agree, D = Disagree, and U = Unclear. F1 scores are reported for each task, alongside the average F1 score across all three tasks.
Data preprocessing
For optimal consistency with the training data, it is recommended to apply the same text preprocessing before classification.
import re
def preprocess_text(text):
#e.g. @PeterNice123 -> @user
text = re.sub(r'@\w+', '@user', text)
# https -> <link>
text = re.sub(r'http\S+', '<link>', text)
return text
df['text'] = df['text'].apply(preprocess_text)
Usage
The following code demonstrates how to load and use the model on example texts.
import pandas as pd
import numpy as np
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_id = "AU-STANDBY/constructive_classifier_EN_DA_non_context_enriched"
tokenizer = AutoTokenizer.from_pretrained(model_id, token=True)
model = AutoModelForSequenceClassification.from_pretrained(
model_id,
token=True
)
model.eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
label_classes = {
"hateful": [
"Not Hateful",
"Hateful",
"Unclassifiable Hateful"
],
"constructive": [
"Not Constructive",
"Constructive",
"Unclassifiable Constructive"
],
"agree": [
"Agree",
"Disagree",
"Unclear"
]
}
def predict_texts(texts, batch_size=16):
model.eval()
results = []
for i in range(0, len(texts), batch_size):
batch_texts = texts[i:i + batch_size]
# Tokenize
inputs = tokenizer(
batch_texts,
padding="max_length",
truncation=True,
max_length=512,
return_tensors="pt"
)
inputs = {k: v.to(device) for k, v in inputs.items()}
# Get predictions
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
# Split logits into the three classification tasks
logits_groups = {
"hateful": logits[:, :3],
"constructive": logits[:, 3:6],
"agree": logits[:, 6:9]
}
# Calculate softmax separately for each task
probabilities = {
task: torch.softmax(group_logits, dim=1).cpu().numpy()
for task, group_logits in logits_groups.items()
}
# Process predictions for each text
for j, text in enumerate(batch_texts):
row = {"text": text}
for task, probs in probabilities.items():
# Get highest probability and corresponding label
pred_idx = np.argmax(probs[j])
pred_score = probs[j][pred_idx]
row[f"{task}_label"] = label_classes[task][pred_idx]
row[f"{task}_score"] = pred_score
# Also retain probabilities for all three labels
for k, label in enumerate(label_classes[task]):
row[f"{task}_prob_{k}"] = probs[j][k]
results.append(row)
return pd.DataFrame(results)
texts = [
"I appreciate your perspective, and I think you make a good point.",
"You are completely wrong and your argument makes no sense.",
"I strongly disagree with what you are saying.",
"Thank you for sharing your opinion. I understand where you are coming from."
]
predictions_df = predict_texts(texts)
Citation
If you use this model, please cite:
[LINK]
Contact
For questions please contact:
- Ida Dencker / ida3mail@gmail.com
- Simon Karg / simonkarg@ps.au.dk