
# First - you should prepare few functions to talk to model

```python
import torch
from transformers import BertForSequenceClassification, AutoTokenizer

LABELS = ['neutral', 'happiness', 'sadness', 'enthusiasm', 'fear', 'anger', 'disgust']
tokenizer = AutoTokenizer.from_pretrained('Aniemore/rubert-tiny2-russian-emotion-detection')
model = BertForSequenceClassification.from_pretrained('Aniemore/rubert-tiny2-russian-emotion-detection')

@torch.no_grad()
def predict_emotion(text: str) -> str:
    """
        We take the input text, tokenize it, pass it through the model, and then return the predicted label
        :param text: The text to be classified
        :type text: str
        :return: The predicted emotion
    """
    inputs = tokenizer(text, max_length=512, padding=True, truncation=True, return_tensors='pt')
    outputs = model(**inputs)
    predicted = torch.nn.functional.softmax(outputs.logits, dim=1)
    predicted = torch.argmax(predicted, dim=1).numpy()
        
    return LABELS[predicted[0]]

@torch.no_grad()    
def predict_emotions(text: str) -> list:
    """
        It takes a string of text, tokenizes it, feeds it to the model, and returns a dictionary of emotions and their
        probabilities
        :param text: The text you want to classify
        :type text: str
        :return: A dictionary of emotions and their probabilities.
    """
    inputs = tokenizer(text, max_length=512, padding=True, truncation=True, return_tensors='pt')
    outputs = model(**inputs)
    predicted = torch.nn.functional.softmax(outputs.logits, dim=1)
    emotions_list = {}
    for i in range(len(predicted.numpy()[0].tolist())):
        emotions_list[LABELS[i]] = predicted.numpy()[0].tolist()[i]
    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}
```

# Or, just simply use [our package (GitHub)](https://github.com/aniemore/Aniemore), that can do whatever you want (or maybe not)
🤗

# Citations
```
@misc{Aniemore,
  author = {Артем Аментес, Илья Лубенец, Никита Давидчук},
  title = {Открытая библиотека искусственного интеллекта для анализа и выявления эмоциональных оттенков речи человека},
  year = {2022},
  publisher = {Hugging Face},
  journal = {Hugging Face Hub},
  howpublished = {\url{https://huggingface.com/aniemore/Aniemore}},
  email = {hello@socialcode.ru}
}
```
{"language": ["ru"], "license": "mit", "tags": ["russian", "classification", "emotion", "emotion-detection", "emotion-recognition", "multiclass"], "datasets": ["Aniemore/cedr-m7"], "widget": [{"text": "\u041a\u0430\u043a \u0434\u0435\u043b\u0430?"}, {"text": "\u0414\u0443\u0440\u0430\u043a \u0442\u0432\u043e\u0439 \u0434\u0435\u0434"}, {"text": "\u0422\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439!!!"}, {"text": "\u041d\u0435 \u0445\u043e\u0447\u0443 \u0432 \u0448\u043a\u043e\u043b\u0443("}, {"text": "\u0421\u0435\u0439\u0447\u0430\u0441 \u0440\u043e\u0432\u043d\u043e \u0447\u0430\u0441 \u0434\u043d\u044f"}, {"text": "\u0410 \u0442\u044b \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u044d\u0442\u0438 \u043f\u043e\u043b\u043e\u0441\u043a\u0438 \u0441\u043d\u0438\u0437\u0443 \u043d\u0435 \u0432\u0440\u0443\u0442? \u0422\u043e\u0447\u043d\u043e \u0443\u0432\u0435\u0440\u0435\u043d? \u0412\u043e\u0442 \u043f\u0440\u044f\u043c 100 \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u043e\u0432?"}], "model-index": [{"name": "RuBERT tiny2 For Russian Text Emotion Detection by Ilya Lubenets", "results": [{"task": {"type": "multilabel-text-classification", "name": "Multilabel Text Classification"}, "dataset": {"name": "CEDR M7", "type": "Aniemore/cedr-m7", "args": "ru"}, "metrics": [{"type": "accuracy", "value": "85%", "name": "multilabel accuracy"}]}, {"task": {"type": "text-classification", "name": "Text Classification"}, "dataset": {"name": "CEDR M7", "type": "Aniemore/cedr-m7", "args": "ru"}, "metrics": [{"type": "accuracy", "value": "76%", "name": "accuracy"}]}]}]}
transformers pytorch safetensors bert text-classification russian classification emotion emotion-detection emotion-recognition multiclass ru dataset:Aniemore/cedr-m7 doi:10.57967/hf/1275 license:mit model-index autotrain_compatible endpoints_compatible region:us
TAGS
#transformers #pytorch #safetensors #bert #text-classification #russian #classification #emotion #emotion-detection #emotion-recognition #multiclass #ru #dataset-Aniemore/cedr-m7 #doi-10.57967/hf/1275 #license-mit #model-index #autotrain_compatible #endpoints_compatible #region-us 


# First - you should prepare few functions to talk to model



# And then - just gently ask a model to predict your emotion



# Or, just simply use our package (GitHub), that can do whatever you want (or maybe not)


s

# First - you should prepare few functions to talk to model # And then - just gently ask a model to predict your emotion # Or, just simply use our package (GitHub), that can do whatever you want (or maybe not)


s
TAGS
#transformers #pytorch #safetensors #bert #text-classification #russian #classification #emotion #emotion-detection #emotion-recognition #multiclass #ru #dataset-Aniemore/cedr-m7 #doi-10.57967/hf/1275 #license-mit #model-index #autotrain_compatible #endpoints_compatible #region-us 
 # First - you should prepare few functions to talk to model # And then - just gently ask a model to predict your emotion # Or, just simply use our package (GitHub), that can do whatever you want (or maybe not)


s
passage: TAGS
#transformers #pytorch #safetensors #bert #text-classification #russian #classification #emotion #emotion-detection #emotion-recognition #multiclass #ru #dataset-Aniemore/cedr-m7 #doi-10.57967/hf/1275 #license-mit #model-index #autotrain_compatible #endpoints_compatible #region-us 
# First - you should prepare few functions to talk to model# And then - just gently ask a model to predict your emotion# Or, just simply use our package (GitHub), that can do whatever you want (or maybe not)


s