CoolFace
Modelpublic

fyaronskiy/ModernBERT-large-english-go-emotions

sourceHugging Facemitupdated 1y agoView on Hugging Face
8likes417downloads
Model Card

Summary

This is ModernBERT-large model finetuned on go_emotions dataset for multilabel classification. Model can be used to extract all emotions from english text or detect certain emotions. Thresholds are selected on validation set by maximizing f1 macro over all labels. You can use Flash Attention 2 to speed up inference.

Also ONNX version of model and INT8 quantized model is available. information about them is posted in the last sections.

The quality of the model varies greatly across all classes (look at the table with metrics below). There are classes like admiration, amusement, optimism, fear, remorse and others where the model shows high recognition quality, and classes that pose difficulties for the model - disappointment, realization that do have much fewer examples in the training data.

Usage

Using model is easy with Huggingface Transformers.

ModernBERT architecture is supported in the transformers version 4.48.0 and later, so you need to install it:

bash
pip install "transformers>=4.48.0"

Here is how you can extract emotions contained in text:

python
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained('fyaronskiy/ModernBERT-large-go-emotions')
model = AutoModelForSequenceClassification.from_pretrained('fyaronskiy/ModernBERT-large-go-emotions')
best_thresholds = [0.5510204081632653, 0.26530612244897955, 0.14285714285714285, 0.12244897959183673, 0.44897959183673464, 0.22448979591836732, 0.2040816326530612, 0.4081632653061224, 0.5306122448979591, 0.22448979591836732, 0.2857142857142857, 0.3061224489795918, 0.2040816326530612, 0.14285714285714285, 0.1020408163265306, 0.4693877551020408, 0.24489795918367346, 0.3061224489795918, 0.2040816326530612, 0.36734693877551017, 0.2857142857142857, 0.04081632653061224, 0.3061224489795918, 0.16326530612244897, 0.26530612244897955, 0.32653061224489793, 0.12244897959183673, 0.2040816326530612]
LABELS = ['admiration', 'amusement', 'anger', 'annoyance', 'approval', 'caring', 'confusion', 'curiosity', 'desire', 'disappointment', 'disapproval', 'disgust', 'embarrassment', 'excitement', 'fear', 'gratitude', 'grief', 'joy', 'love', 'nervousness', 'optimism', 'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise', 'neutral']
ID2LABEL = dict(enumerate(LABELS))
python
def detect_emotions(text):
  inputs = tokenizer(text, truncation=True, add_special_tokens=True, max_length=128, return_tensors='pt')
  with torch.no_grad():
      logits = model(**inputs).logits
  probas = torch.sigmoid(logits).squeeze(dim=0)  
  class_binary_labels = (probas > torch.tensor(best_thresholds)).int()
  return [ID2LABEL[label_id] for label_id, value in enumerate(class_binary_labels) if value == 1]
print(detect_emotions('You have excellent service and the best coffee in the city, I love your coffee shop!'))
#['admiration', 'love']

This is the way to get all emotions and their scores:

python
def predict(text):
    inputs = tokenizer(text, truncation=True, add_special_tokens=True, max_length=128, return_tensors='pt')
    with torch.no_grad():
        logits = model(**inputs).logits
    probas = torch.sigmoid(logits).squeeze(dim=0).tolist()
    probas = [round(proba, 3) for proba in probas]    
    
    labels2probas = dict(zip(LABELS, probas))
    probas_dict_sorted = dict(sorted(labels2probas.items(), key=lambda x: x[1], reverse=True))
    return probas_dict_sorted
print(predict('You have excellent service and the best coffee in the city, I love your coffee shop!'))
#{'admiration': 0.982, 'love': 0.689, 'approval': 0.014, 'gratitude': 0.003, 'joy': 0.003, 'amusement': 0.001, 'curiosity': 0.001, 'excitement': 0.001, 'realization': 0.001, 'surprise': 0.001, 'anger': 0.0, 'annoyance': 0.0, 'caring': 0.0, 'confusion': 0.0, 'desire': 0.0, 'disappointment': 0.0, 'disapproval': 0.0, 'disgust': 0.0, 'embarrassment': 0.0, 'fear': 0.0, 'grief': 0.0, 'nervousness': 0.0, 'optimism': 0.0, 'pride': 0.0, 'relief': 0.0, 'remorse': 0.0, 'sadness': 0.0, 'neutral': 0.0}

Eval results on test split of go-emotions

precisionrecallf1-scoresupportthreshold
admiration0.680.720.75040.55
amusement0.760.910.832640.27
anger0.440.530.481980.14
annoyance0.270.460.343200.12
approval0.410.380.43510.45
caring0.370.460.411350.22
confusion0.360.510.421530.2
curiosity0.450.770.572840.41
desire0.660.460.54830.53
disappointment0.410.260.321510.22
disapproval0.390.540.452670.29
disgust0.520.410.461230.31
embarrassment0.520.410.45370.2
excitement0.290.590.391030.14
fear0.550.780.65780.1
gratitude0.960.880.923520.47
grief0.290.670.460.24
joy0.570.660.611610.31
love0.740.870.82380.2
nervousness0.370.430.4230.37
optimism0.60.580.591860.29
pride0.280.440.34160.04
realization0.360.190.241450.31
relief0.620.450.53110.16
remorse0.510.840.63560.27
sadness0.540.560.551560.33
surprise0.470.630.541410.12
neutral0.580.820.6817870.2
micro avg0.540.670.66329
macro avg0.50.580.526329
weighted avg0.550.670.66329
samples avg0.590.690.616329

ONNX and quantized versions of model

Full precision ONNX model (onnx/model.onnx) - 1.6x faster than Transformer model, quality is the same.

INT8 quantized model (onnx/model_quantized.onnx) - 2.5x faster than Transformer model, quality is almost the same.

In table below results of tests of inference of 5427 samples of testset. I tested inference with batchsize 1 on Intel Xeon CPU with 2 vCPUs (Google Colab).

ModelSizef1 macroaccelerationTime of inference
Original model1.58 GB0.521x49 min 41 sec
onnx.model1.58 GB0.521.6x30 min 42 sec
model_quantized.onnx0.55 GB0.512.5x19 min 57 sec

How to use ONNX versions

Loading full precision model

python
import torch
from transformers import AutoTokenizer
from optimum.onnxruntime import ORTModelForSequenceClassification
model_id = "fyaronskiy/ModernBERT-large-english-go-emotions"
file_name = "onnx/model.onnx"
model = ORTModelForSequenceClassification.from_pretrained(model_id, file_name=file_name)
tokenizer = AutoTokenizer.from_pretrained(model_id)

INT8 quantized model

python
model_id = "fyaronskiy/ModernBERT-large-english-go-emotions"
subfolder = "onnx"
file_name = "model_quantized.onnx"
model = ORTModelForSequenceClassification.from_pretrained(model_id, file_name=file_name, subfolder = subfolder)
tokenizer = AutoTokenizer.from_pretrained(model_id)

After loading, using ONNX models for inference is the same as for usual Transformer model:

python
# best thresholds for full precision ONNX model:
best_thresholds = [0.5510204081632653, 0.26530612244897955, 0.14285714285714285, 0.12244897959183673, 0.44897959183673464, 0.22448979591836732, 0.2040816326530612, 0.4081632653061224, 0.5306122448979591, 0.22448979591836732, 0.2857142857142857, 0.3061224489795918, 0.2040816326530612, 0.14285714285714285, 0.1020408163265306, 0.4693877551020408, 0.24489795918367346, 0.3061224489795918, 0.2040816326530612, 0.36734693877551017, 0.2857142857142857, 0.04081632653061224, 0.3061224489795918, 0.16326530612244897, 0.26530612244897955, 0.32653061224489793, 0.12244897959183673, 0.2040816326530612]
# best thresholds for INT8 quantized model:
# best_thresholds = [0.5510204081632653, 0.24489795918367346, 0.18367346938775508, 0.08163265306122448, 0.2857142857142857, 0.32653061224489793, 0.3877551020408163, 0.3877551020408163, 0.44897959183673464, 0.1020408163265306, 0.22448979591836732, 0.12244897959183673, 0.061224489795918366, 0.4693877551020408, 0.16326530612244897, 0.44897959183673464, 0.24489795918367346, 0.26530612244897955, 0.2040816326530612, 0.2040816326530612, 0.2857142857142857, 0.04081632653061224, 0.32653061224489793, 0.14285714285714285, 0.16326530612244897, 0.36734693877551017, 0.12244897959183673, 0.3061224489795918]
LABELS = ['admiration', 'amusement', 'anger', 'annoyance', 'approval', 'caring', 'confusion', 'curiosity', 'desire', 'disappointment', 'disapproval', 'disgust', 'embarrassment', 'excitement', 'fear', 'gratitude', 'grief', 'joy', 'love', 'nervousness', 'optimism', 'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise', 'neutral']
ID2LABEL = dict(enumerate(LABELS))

def detect_emotions(text):
  inputs = tokenizer(text, truncation=True, add_special_tokens=True, max_length=128, return_tensors='pt')
  with torch.no_grad():
      logits = model(**inputs).logits
  probas = torch.sigmoid(logits).squeeze(dim=0)  
  class_binary_labels = (probas > torch.tensor(best_thresholds)).int()
  return [ID2LABEL[label_id] for label_id, value in enumerate(class_binary_labels) if value == 1]
print(detect_emotions('You have excellent service and the best coffee in the city, I love your coffee shop!'))
#['admiration', 'love']

I've done quantization with:

python
from onnxruntime.quantization import quantize_dynamic, QuantType

quantize_dynamic(
    "models/fyaronskiy_ModernBERT-large-english-go-emotions/onnx/model.onnx",
    "models/fyaronskiy_ModernBERT-large-english-go-emotions/onnx/model_quantized.onnx",
    weight_type=QuantType.QUInt8,
    op_types_to_quantize=['MatMul', 'Gemm'],
    per_channel=False,
)