webis/tldr-17
Viewer • Updated • 1.33M • 1.28k • 56
How to use NiklasKoch/modernbert-discussion-classifier with PEFT:
from peft import PeftModel
from transformers import AutoModelForSequenceClassification
base_model = AutoModelForSequenceClassification.from_pretrained("answerdotai/ModernBERT-base")
model = PeftModel.from_pretrained(base_model, "NiklasKoch/modernbert-discussion-classifier")How to use NiklasKoch/modernbert-discussion-classifier with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="NiklasKoch/modernbert-discussion-classifier") # Load model directly
from transformers import AutoModel
model = AutoModel.from_pretrained("NiklasKoch/modernbert-discussion-classifier", dtype="auto")A lightweight, high-throughput ModernBERT-based model for classifying constructive vs non-constructive conversations in online forums like Reddit. Optimized for processing vast amounts of Reddit discussion data efficiently.
This model is a QLoRA (Quantized LoRA) fine-tuned version of answerdotai/ModernBERT-base specifically designed as a lightweight solution for large-scale Reddit discussion analysis.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from peft import PeftModel
import torch
# Load base model and tokenizer
base_model_name = "answerdotai/ModernBERT-base"
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
model = AutoModelForSequenceClassification.from_pretrained(
base_model_name,
num_labels=2
)
# Load the fine-tuned adapters
model = PeftModel.from_pretrained(model, "NiklasKoch/modernbert-discussion-classifier")
model.eval()
# Classify text (optimized for batch processing)
def classify_text(text):
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=4096
)
# Move inputs to same device as model (important for GPU usage)
inputs = {k: v.to(next(model.parameters()).device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
# 0 = non-constructive, 1 = constructive
predicted_class = torch.argmax(predictions, dim=-1).item()
confidence = predictions[0][predicted_class].item()
return {
'class': 'constructive' if predicted_class == 1 else 'non-constructive',
'confidence': confidence,
'scores': {
'non-constructive': predictions[0][0].item(),
'constructive': predictions[0][1].item()
}
}
# Example usage - Reddit discussion
text = "[author0] LEGO: What do you think you're doing?!? [author1] I don't get it did he reveal bionicle reboot or smthn? [author2] Not really, he did announce something but was super vague, seems like a sort of passion project we wants to do with the community, he even said it might not even be bionicle. [author1] So is that image fan made or is it one of his passion projects [author2] Those pictures are real and on his insta, he did a stream talking about it I'm sure you can find somewhere, search up Fabre bionicle stream 2020 or something. [author1] OK thanks"
result = classify_text(text)
print(result)
r: 16lora_alpha: 32lora_dropout: 0.1Wqkv, Wo, Wi, denseYNACC:
Accuracy: 0.63
Precision: 0.63
F1-Score: 0.65
IAC:
Accuracy: 0.79
Precision: 0.85
F1-Score: 0.87
Reddit:
Accuracy: 0.57
Precision: 0.74
F1-Score: 0.67
Niklas Koch, Georg August University of Göttingen
Base model
answerdotai/ModernBERT-base