AniMAntZeZo commited on
Commit
719afe3
·
verified ·
1 Parent(s): d39cb8e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +94 -1
README.md CHANGED
@@ -73,4 +73,97 @@ With the help of a well-known AI, a data set was compiled to verify the veracity
73
 
74
  It can be seen from the above that the model is able to distinguish between basic emotions, but has problems with specific ones (grief (горе), pride (гордость)), and also defines a neutral emotion more often than expected (most likely this happened due to inaccuracies in the dataset, since in ordinary speech you can "use" several emojis at once and most often people use the neutral one)
75
 
76
- The model will be updated as improvements are made.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  It can be seen from the above that the model is able to distinguish between basic emotions, but has problems with specific ones (grief (горе), pride (гордость)), and also defines a neutral emotion more often than expected (most likely this happened due to inaccuracies in the dataset, since in ordinary speech you can "use" several emojis at once and most often people use the neutral one)
75
 
76
+ The model will be updated as improvements are made.
77
+
78
+ # Usage example
79
+ ```
80
+ import torch
81
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
82
+
83
+ tokenizer = AutoTokenizer.from_pretrained('AniMAntZeZo/RuBert-tiny2-EmotionsDetected')
84
+ model = AutoModelForSequenceClassification.from_pretrained('AniMAntZeZo/RuBert-tiny2-EmotionsDetected')
85
+ model.to("cuda" if torch.cuda.is_available() else "cpu",
86
+
87
+
88
+ emotion_columns = [
89
+ "admiration", "amusement", "anger", "annoyance", "approval", "caring", "confusion", "curiosity", "desire",
90
+ "disappointment", "disapproval", "disgust", "embarrassment", "excitement", "fear", "gratitude", "grief", "joy",
91
+ "love", "nervousness", "optimism", "pride", "realization", "relief", "remorse", "sadness", "surprise", "neutral"
92
+ ]
93
+
94
+ def predict_emotions(
95
+ text,
96
+ model,
97
+ tokenizer,
98
+ emotion_columns,
99
+ device="cuda" if torch.cuda.is_available() else "cpu",
100
+ threshold=0.1
101
+ ):
102
+
103
+ emotion_translations = {
104
+ "admiration": "восхищение",
105
+ "amusement": "веселье",
106
+ "anger": "злость",
107
+ "annoyance": "раздражение",
108
+ "approval": "одобрение",
109
+ "caring": "забота",
110
+ "confusion": "непонимание",
111
+ "curiosity": "любопытство",
112
+ "desire": "желание",
113
+ "disappointment": "разочарование",
114
+ "disapproval": "неодобрение",
115
+ "disgust": "отвращение",
116
+ "embarrassment": "смущение",
117
+ "excitement": "возбуждение",
118
+ "fear": "страх",
119
+ "gratitude": "признательность",
120
+ "grief": "горе",
121
+ "joy": "радость",
122
+ "love": "любовь",
123
+ "nervousness": "нервозность",
124
+ "optimism": "оптимизм",
125
+ "pride": "гордость",
126
+ "realization": "осознание",
127
+ "relief": "облегчение",
128
+ "remorse": "раскаяние",
129
+ "sadness": "грусть",
130
+ "surprise": "удивление",
131
+ "neutral": "нейтральность",
132
+ }
133
+
134
+ model.to(device)
135
+ model.eval()
136
+ inputs = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=128).to(device)
137
+ with torch.no_grad():
138
+ logits = model(**inputs).logits
139
+ probabilities = torch.sigmoid(logits).squeeze().cpu().numpy()
140
+
141
+ predictions = {
142
+ f"{emotion} ({emotion_translations[emotion]})": prob
143
+ for emotion, prob in zip(emotion_columns, probabilities) if prob > threshold
144
+ }
145
+
146
+ sorted_predictions = dict(sorted(predictions.items(), key=lambda item: item[1], reverse=True))
147
+
148
+ return sorted_predictions
149
+ ```
150
+ [INPUT]
151
+ ```
152
+ example_text = "Как же я рад!"
153
+ predictions = predict_emotions(example_text, model, tokenizer, emotion_columns)
154
+ print("Emotions:", predictions)
155
+ ```
156
+ [OUTPUT]
157
+ ```
158
+ Emotions: {'joy (радость)': 0.6736836, 'excitement (возбуждение)': 0.25723574}
159
+ ```
160
+ [INPUT]
161
+ ```
162
+ example_text = "Я обиделся!"
163
+ predictions = predict_emotions(example_text, model, tokenizer, emotion_columns)
164
+ print("Emotions:", predictions)
165
+ ```
166
+ [OUTPUT]
167
+ ```
168
+ Emotions: {'sadness (грусть)': 0.3111033, 'disappointment (разочарование)': 0.2943853, 'annoyance (раздражение)': 0.19748639, 'anger (злость)': 0.16338393}
169
+ ```