toiletsandpaper
commited on
Commit
•
3a5a5c0
1
Parent(s):
1611116
README.md
CHANGED
@@ -17,4 +17,62 @@ widget:
|
|
17 |
- text: "Сейчас ровно час дня"
|
18 |
- text: "А ты уверен, что эти полоски снизу не врут? Точно уверен? Вот прям 100 процентов?"
|
19 |
|
20 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
- text: "Сейчас ровно час дня"
|
18 |
- text: "А ты уверен, что эти полоски снизу не врут? Точно уверен? Вот прям 100 процентов?"
|
19 |
|
20 |
+
---
|
21 |
+
|
22 |
+
# First - you should prepare few functions to talk to model
|
23 |
+
|
24 |
+
```python
|
25 |
+
import torch
|
26 |
+
from transformers import BertForSequenceClassification, AutoTokenizer
|
27 |
+
|
28 |
+
LABELS = ['neutral', 'happiness', 'sadness', 'enthusiasm', 'fear', 'anger', 'disgust']
|
29 |
+
tokenizer = AutoTokenizer.from_pretrained('Aniemore/rubert-tiny2-russian-emotion-detection')
|
30 |
+
model = BertForSequenceClassification.from_pretrained('Aniemore/rubert-tiny2-russian-emotion-detection')
|
31 |
+
|
32 |
+
@torch.no_grad()
|
33 |
+
def predict_emotion(text: str) -> str:
|
34 |
+
"""
|
35 |
+
We take the input text, tokenize it, pass it through the model, and then return the predicted label
|
36 |
+
:param text: The text to be classified
|
37 |
+
:type text: str
|
38 |
+
:return: The predicted emotion
|
39 |
+
"""
|
40 |
+
inputs = tokenizer(text, max_length=512, padding=True, truncation=True, return_tensors='pt')
|
41 |
+
outputs = model(**inputs)
|
42 |
+
predicted = torch.nn.functional.softmax(outputs.logits, dim=1)
|
43 |
+
predicted = torch.argmax(predicted, dim=1).numpy()
|
44 |
+
|
45 |
+
return LABELS[predicted[0]]
|
46 |
+
|
47 |
+
@torch.no_grad()
|
48 |
+
def predict_emotions(text: str) -> list:
|
49 |
+
"""
|
50 |
+
It takes a string of text, tokenizes it, feeds it to the model, and returns a dictionary of emotions and their
|
51 |
+
probabilities
|
52 |
+
:param text: The text you want to classify
|
53 |
+
:type text: str
|
54 |
+
:return: A dictionary of emotions and their probabilities.
|
55 |
+
"""
|
56 |
+
inputs = tokenizer(text, max_length=512, padding=True, truncation=True, return_tensors='pt')
|
57 |
+
outputs = model(**inputs)
|
58 |
+
predicted = torch.nn.functional.softmax(outputs.logits, dim=1)
|
59 |
+
emotions_list = {}
|
60 |
+
for i in range(len(predicted.numpy()[0].tolist())):
|
61 |
+
emotions_list[LABELS[i]] = predicted.numpy()[0].tolist()[i]
|
62 |
+
return emotions_list
|
63 |
+
```
|
64 |
+
|
65 |
+
# And then - just gently ask a model to predict your emotion
|
66 |
+
|
67 |
+
```python
|
68 |
+
simple_prediction = predict_emotion("Какой же сегодня прекрасный день, братья")
|
69 |
+
not_simple_prediction = predict_emotions("Какой же сегодня прекрасный день, братья")
|
70 |
+
|
71 |
+
print(simple_prediction)
|
72 |
+
print(not_simple_prediction)
|
73 |
+
# happiness
|
74 |
+
# {'neutral': 0.0004941817605867982, 'happiness': 0.9979524612426758, 'sadness': 0.0002536600804887712, 'enthusiasm': 0.0005498139653354883, 'fear': 0.00025326196919195354, 'anger': 0.0003583927755244076, 'disgust': 0.00013807788491249084}
|
75 |
+
```
|
76 |
+
|
77 |
+
# Or, just simply use [our package (GitHub)](https://github.com/aniemore/Aniemore), that can do whatever you want (or maybe not)
|
78 |
+
🤗
|