Views
No views yet
excitement: 0.5836, joy: 0.5290annoyance: 0.6147, anger: 0.4669sadness: 0.5321, remorse: 0.9107optimized_thresholds.json):| Emotion | Accuracy | Precision | Recall | F1 Score | MCC | Support | Threshold |
|---|---|---|---|---|---|---|---|
| admiration | 0.9410 | 0.6649 | 0.7361 | 0.6987 | 0.6672 | 504 | 0.4500 |
| amusement | 0.9801 | 0.7635 | 0.8561 | 0.8071 | 0.7981 | 264 | 0.4500 |
| anger | 0.9694 | 0.6176 | 0.4242 | 0.5030 | 0.4970 | 198 | 0.4500 |
| annoyance | 0.9121 | 0.3297 | 0.4750 | 0.3892 | 0.3502 | 320 | 0.3500 |
| approval | 0.8843 | 0.2966 | 0.5755 | 0.3915 | 0.3572 | 351 | 0.3500 |
| caring | 0.9759 | 0.5196 | 0.3926 | 0.4473 | 0.4396 | 135 | 0.4500 |
| confusion | 0.9711 | 0.4861 | 0.4575 | 0.4714 | 0.4567 | 153 | 0.4500 |
| curiosity | 0.9368 | 0.4442 | 0.8275 | 0.5781 | 0.5783 | 284 | 0.4000 |
| desire | 0.9865 | 0.5714 | 0.4819 | 0.5229 | 0.5180 | 83 | 0.4000 |
| disappointment | 0.9565 | 0.2906 | 0.3907 | 0.3333 | 0.3150 | 151 | 0.3500 |
| disapproval | 0.9235 | 0.3405 | 0.5918 | 0.4323 | 0.4118 | 267 | 0.3500 |
| disgust | 0.9810 | 0.6250 | 0.4065 | 0.4926 | 0.4950 | 123 | 0.5500 |
| embarrassment | 0.9947 | 0.7000 | 0.3784 | 0.4912 | 0.5123 | 37 | 0.5000 |
| excitement | 0.9790 | 0.4486 | 0.4660 | 0.4571 | 0.4465 | 103 | 0.4000 |
| fear | 0.9836 | 0.4599 | 0.8077 | 0.5860 | 0.6023 | 78 | 0.3000 |
| gratitude | 0.9888 | 0.9450 | 0.8778 | 0.9102 | 0.9049 | 352 | 0.5500 |
| grief | 0.9985 | 0.3333 | 0.3333 | 0.3333 | 0.3326 | 6 | 0.3000 |
| joy | 0.9768 | 0.6061 | 0.6211 | 0.6135 | 0.6016 | 161 | 0.4500 |
| love | 0.9825 | 0.7826 | 0.8319 | 0.8065 | 0.7978 | 238 | 0.5000 |
| nervousness | 0.9952 | 0.4348 | 0.4348 | 0.4348 | 0.4324 | 23 | 0.4000 |
| optimism | 0.9689 | 0.5436 | 0.5699 | 0.5564 | 0.5405 | 186 | 0.4000 |
| pride | 0.9980 | 0.8571 | 0.3750 | 0.5217 | 0.5662 | 16 | 0.4000 |
| realization | 0.9737 | 0.5217 | 0.1655 | 0.2513 | 0.2838 | 145 | 0.4500 |
| relief | 0.9982 | 0.5385 | 0.6364 | 0.5833 | 0.5845 | 11 | 0.3000 |
| remorse | 0.9912 | 0.5426 | 0.9107 | 0.6800 | 0.6992 | 56 | 0.3500 |
| sadness | 0.9757 | 0.5845 | 0.5321 | 0.5570 | 0.5452 | 156 | 0.4500 |
| surprise | 0.9724 | 0.4772 | 0.6667 | 0.5562 | 0.5504 | 141 | 0.3500 |
| neutral | 0.7485 | 0.5821 | 0.8372 | 0.6867 | 0.5102 | 1787 | 0.4000 |


inference.py from the repository. The script handles all preprocessing, model loading, and inference for you.inference.py and make predictions:1# pip install transformers torch huggingface_hub emoji -q
2
3from huggingface_hub import hf_hub_download
4import importlib.util
5
6# download inference script
7path = hf_hub_download(repo_id="logasanjeev/bert-emotion-classifier", filename="inference.py")
8
9# load module
10spec = importlib.util.spec_from_file_location("inference", path)
11inference = importlib.util.module_from_spec(spec)
12spec.loader.exec_module(inference)
13
14# run prediction
15text = "I’m thrilled to win this award! 😄"
16result, processed = inference.predict_emotions(text)
17
18print("Input:", text)
19print("Processed:", processed)
20print("Predicted Emotions:", result)Input: I’m thrilled to win this award! 😄
Processed: i’m thrilled to win this award ! grinning_face_with_smiling_eyes
Predicted Emotions:
excitement: 0.5836
joy: 0.5290inference.py manually:pip install transformers torch huggingface_hub emojiinference.py from the repository.1from inference import predict_emotions
2
3result, processed = predict_emotions("I’m thrilled to win this award! 😄")
4print(f"Input: I’m thrilled to win this award! 😄")
5print(f"Processed: {processed}")
6print("Predicted Emotions:")
7print(result)python inference.py "I’m thrilled to win this award! 😄"onnx_inference.py. This script leverages ONNX Runtime for inference, which is typically more lightweight than PyTorch.onnx_inference.py and make predictions:1# pip install transformers torch huggingface_hub emoji -q
2
3from huggingface_hub import hf_hub_download
4import importlib.util
5
6# download inference script
7path = hf_hub_download(repo_id="logasanjeev/bert-emotion-classifier", filename="inference.py")
8
9# load module
10spec = importlib.util.spec_from_file_location("inference", path)
11inference = importlib.util.module_from_spec(spec)
12spec.loader.exec_module(inference)
13
14# run prediction
15text = "I’m thrilled to win this award! 😄"
16result, processed = inference.predict_emotions(text)
17
18print("Input:", text)
19print("Processed:", processed)
20print("Predicted Emotions:", result)Input: I’m thrilled to win this award! 😄
Processed: i’m thrilled to win this award ! grinning_face_with_smiling_eyes
Predicted Emotions:
excitement: 0.5836
joy: 0.5290onnx_inference.py manually:pip install transformers onnxruntime huggingface_hub emoji numpyonnx_inference.py from the repository.1from onnx_inference import predict_emotions
2
3result, processed = predict_emotions("I’m thrilled to win this award! 😄")
4print(f"Input: I’m thrilled to win this award! 😄")
5print(f"Processed: {processed}")
6print("Predicted Emotions:")
7print(result)python onnx_inference.py "I’m thrilled to win this award! 😄"u/username) with [USER].r/subreddit) with [SUBREDDIT].[URL].emoji.demojize (e.g., 😊 → smiling_face_with_smiling_eyes).1from transformers import BertForSequenceClassification, BertTokenizer
2import torch
3import json
4import requests
5import re
6import emoji
7
8def preprocess_text(text):
9 text = re.sub(r'u/\w+', '[USER]', text)
10 text = re.sub(r'r/\w+', '[SUBREDDIT]', text)
11 text = re.sub(r'http[s]?://\S+', '[URL]', text)
12 text = emoji.demojize(text, delimiters=(" ", " "))
13 text = text.lower()
14 return text
15
16repo_id = "logasanjeev/bert-emotion-classifier"
17model = BertForSequenceClassification.from_pretrained(repo_id)
18tokenizer = BertTokenizer.from_pretrained(repo_id)
19
20thresholds_url = f"https://huggingface.co/{repo_id}/raw/main/optimized_thresholds.json"
21thresholds_data = json.loads(requests.get(thresholds_url).text)
22emotion_labels = thresholds_data["emotion_labels"]
23thresholds = thresholds_data["thresholds"]
24
25text = "I’m just chilling today."
26processed_text = preprocess_text(text)
27encodings = tokenizer(processed_text, padding='max_length', truncation=True, max_length=128, return_tensors='pt')
28with torch.no_grad():
29 logits = torch.sigmoid(model(**encodings).logits).numpy()[0]
30predictions = [(emotion_labels[i], round(logit, 4)) for i, (logit, thresh) in enumerate(zip(logits, thresholds)) if logit >= thresh]
31predictions = sorted(predictions, key=lambda x: x[1], reverse=True)
32print(predictions)
33# Output: [('neutral', 0.8147)]onnx_inference.py as shown above. Alternatively, you can use the manual approach below:1import onnxruntime as ort
2import numpy as np
3
4onnx_url = f"https://huggingface.co/{repo_id}/raw/main/model.onnx"
5with open("model.onnx", "wb") as f:
6 f.write(requests.get(onnx_url).content)
7
8text = "I’m thrilled to win this award! 😄"
9processed_text = preprocess_text(text)
10encodings = tokenizer(processed_text, padding='max_length', truncation=True, max_length=128, return_tensors='np')
11session = ort.InferenceSession("model.onnx")
12inputs = {
13 'input_ids': encodings['input_ids'].astype(np.int64),
14 'attention_mask': encodings['attention_mask'].astype(np.int64)
15}
16logits = session.run(None, inputs)[0][0]
17logits = 1 / (1 + np.exp(-logits)) # Sigmoid
18predictions = [(emotion_labels[i], round(logit, 4)) for i, (logit, thresh) in enumerate(zip(logits, thresholds)) if logit >= thresh]
19predictions = sorted(predictions, key=lambda x: x[1], reverse=True)
20print(predictions)
21# Output: [('excitement', 0.5836), ('joy', 0.5290)]grief, support=6) have lower F1 scores due to limited data.onnxruntime and compatible hardware (opset 14).