Djacon commited on
Commit
1161779
·
1 Parent(s): 2831cbc

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +22 -20
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
-
3
  license: mit
4
- language: ["ru"]
 
5
  tags:
6
  - russian
7
  - classification
@@ -10,15 +10,16 @@ tags:
10
  - emotion-recognition
11
  - multiclass
12
  widget:
13
- - text: "Как дела?"
14
- - text: "Дурак твой дед"
15
- - text: "Только попробуй!!!"
16
- - text: "Не хочу в школу("
17
- - text: "Сейчас ровно час дня"
18
- - text: "А ты уверен, что эти полоски снизу не врут? Точно уверен? Вот прям 100 процентов?"
 
 
19
  datasets:
20
- - Djacon/ru_goemotions
21
-
22
  ---
23
 
24
  # First - you should prepare few functions to talk to model
@@ -27,7 +28,7 @@ datasets:
27
  import torch
28
  from transformers import BertForSequenceClassification, AutoTokenizer
29
 
30
- LABELS = ['радость', 'интерес', 'удивление', 'печаль', 'гнев', 'отвращение', 'страх', 'вина', 'нейтрально']
31
  tokenizer = AutoTokenizer.from_pretrained('Djacon/rubert-tiny2-russian-emotion-detection')
32
  model = BertForSequenceClassification.from_pretrained('Djacon/rubert-tiny2-russian-emotion-detection')
33
 
@@ -36,27 +37,28 @@ model = BertForSequenceClassification.from_pretrained('Djacon/rubert-tiny2-russi
36
  def predict_emotion(text: str) -> str:
37
  inputs = tokenizer(text, truncation=True, return_tensors='pt')
38
  inputs = inputs.to(model.device)
39
-
40
  outputs = model(**inputs)
41
-
42
- pred = torch.nn.functional.softmax(outputs.logits, dim=1)
43
  pred = pred.argmax(dim=1)
 
 
44
 
45
- return LABELS[pred[0]]
46
 
47
  # Probabilistic prediction of emotion in a text
48
  @torch.no_grad()
49
- def predict_emotions(text: str) -> list:
50
  inputs = tokenizer(text, truncation=True, return_tensors='pt')
51
  inputs = inputs.to(model.device)
52
 
53
  outputs = model(**inputs)
54
 
55
- pred = torch.nn.functional.softmax(outputs.logits, dim=1)
56
 
57
  emotions_list = {}
58
  for i in range(len(pred[0].tolist())):
59
- emotions_list[LABELS[i]] = round(pred[0].tolist()[i], 4)
60
  return emotions_list
61
  ```
62
 
@@ -68,8 +70,8 @@ not_simple_prediction = predict_emotions("Какой же сегодня пре
68
 
69
  print(simple_prediction)
70
  print(not_simple_prediction)
71
- # happiness
72
- # {'neutral': 0.0004941817605867982, 'happiness': 0.9979524612426758, 'sadness': 0.0002536600804887712, 'enthusiasm': 0.0005498139653354883, 'fear': 0.00025326196919195354, 'anger': 0.0003583927755244076, 'disgust': 0.00013807788491249084}
73
  ```
74
 
75
  # Citations
 
1
  ---
 
2
  license: mit
3
+ language:
4
+ - ru
5
  tags:
6
  - russian
7
  - classification
 
10
  - emotion-recognition
11
  - multiclass
12
  widget:
13
+ - text: Как дела?
14
+ - text: Дурак твой дед
15
+ - text: Только попробуй!!!
16
+ - text: Не хочу в школу(
17
+ - text: Сейчас ровно час дня
18
+ - text: >-
19
+ А ты уверен, что эти полоски снизу не врут? Точно уверен? Вот прям 100
20
+ процентов?
21
  datasets:
22
+ - Djacon/ru_go_emotions
 
23
  ---
24
 
25
  # First - you should prepare few functions to talk to model
 
28
  import torch
29
  from transformers import BertForSequenceClassification, AutoTokenizer
30
 
31
+ LABELS_RU = ['нейтрально', 'радость', 'грусть', 'гнев', 'интерес', 'удивление', 'отвращение', 'страх', 'вина', 'стыд']
32
  tokenizer = AutoTokenizer.from_pretrained('Djacon/rubert-tiny2-russian-emotion-detection')
33
  model = BertForSequenceClassification.from_pretrained('Djacon/rubert-tiny2-russian-emotion-detection')
34
 
 
37
  def predict_emotion(text: str) -> str:
38
  inputs = tokenizer(text, truncation=True, return_tensors='pt')
39
  inputs = inputs.to(model.device)
40
+
41
  outputs = model(**inputs)
42
+
43
+ pred = torch.nn.functional.sigmoid(outputs.logits)
44
  pred = pred.argmax(dim=1)
45
+
46
+ return LABELS_RU[pred[0]]
47
 
 
48
 
49
  # Probabilistic prediction of emotion in a text
50
  @torch.no_grad()
51
+ def predict_emotions(text: str) -> dict:
52
  inputs = tokenizer(text, truncation=True, return_tensors='pt')
53
  inputs = inputs.to(model.device)
54
 
55
  outputs = model(**inputs)
56
 
57
+ pred = torch.nn.functional.sigmoid(outputs.logits)
58
 
59
  emotions_list = {}
60
  for i in range(len(pred[0].tolist())):
61
+ emotions_list[LABELS_RU[i]] = round(pred[0].tolist()[i], 4)
62
  return emotions_list
63
  ```
64
 
 
70
 
71
  print(simple_prediction)
72
  print(not_simple_prediction)
73
+ # радость
74
+ # {'нейтрально': 0.1985, 'радость': 0.7419, 'грусть': 0.0261, 'гнев': 0.0295, 'интерес': 0.1983, 'удивление': 0.4305, 'отвращение': 0.0082, 'страх': 0.008, 'вина': 0.0046, 'стыд': 0.0097}
75
  ```
76
 
77
  # Citations