|
--- |
|
|
|
license: mit |
|
language: ["ru"] |
|
tags: |
|
- russian |
|
- classification |
|
- emotion |
|
- emotion-detection |
|
- emotion-recognition |
|
- multiclass |
|
widget: |
|
- text: "Как дела?" |
|
- text: "Дурак твой дед" |
|
- text: "Только попробуй!!!" |
|
- text: "Не хочу в школу(" |
|
- text: "Сейчас ровно час дня" |
|
- text: "А ты уверен, что эти полоски снизу не врут? Точно уверен? Вот прям 100 процентов?" |
|
datasets: |
|
- Djacon/ru_goemotions |
|
|
|
--- |
|
|
|
# First - you should prepare few functions to talk to model |
|
|
|
```python |
|
import torch |
|
from transformers import BertForSequenceClassification, AutoTokenizer |
|
|
|
LABELS = ['радость', 'интерес', 'удивление', 'печаль', 'гнев', 'отвращение', 'страх', 'вина', 'нейтрально'] |
|
tokenizer = AutoTokenizer.from_pretrained('Djacon/rubert-tiny2-russian-emotion-detection') |
|
model = BertForSequenceClassification.from_pretrained('Djacon/rubert-tiny2-russian-emotion-detection') |
|
|
|
# Predicting emotion in text |
|
@torch.no_grad() |
|
def predict_emotion(text: str) -> str: |
|
inputs = tokenizer(text, truncation=True, return_tensors='pt') |
|
inputs = inputs.to(model.device) |
|
|
|
outputs = model(**inputs) |
|
|
|
pred = torch.nn.functional.softmax(outputs.logits, dim=1) |
|
pred = pred.argmax(dim=1) |
|
|
|
return LABELS[pred[0]] |
|
|
|
# Probabilistic prediction of emotion in a text |
|
@torch.no_grad() |
|
def predict_emotions(text: str) -> list: |
|
inputs = tokenizer(text, truncation=True, return_tensors='pt') |
|
inputs = inputs.to(model.device) |
|
|
|
outputs = model(**inputs) |
|
|
|
pred = torch.nn.functional.softmax(outputs.logits, dim=1) |
|
|
|
emotions_list = {} |
|
for i in range(len(pred[0].tolist())): |
|
emotions_list[LABELS[i]] = round(pred[0].tolist()[i], 4) |
|
return emotions_list |
|
``` |
|
|
|
# And then - just gently ask a model to predict your emotion |
|
|
|
```python |
|
simple_prediction = predict_emotion("Какой же сегодня прекрасный день, братья") |
|
not_simple_prediction = predict_emotions("Какой же сегодня прекрасный день, братья") |
|
|
|
print(simple_prediction) |
|
print(not_simple_prediction) |
|
# happiness |
|
# {'neutral': 0.0004941817605867982, 'happiness': 0.9979524612426758, 'sadness': 0.0002536600804887712, 'enthusiasm': 0.0005498139653354883, 'fear': 0.00025326196919195354, 'anger': 0.0003583927755244076, 'disgust': 0.00013807788491249084} |
|
``` |
|
|
|
# Citations |
|
``` |
|
@misc{Djacon, |
|
author = {Djacon}, |
|
year = {2023}, |
|
publisher = {Hugging Face}, |
|
journal = {Hugging Face Hub}, |
|
} |
|
``` |