gsar78 commited on
Commit
7e197e2
·
verified ·
1 Parent(s): 2c7c2ae

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +211 -0
README.md ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hellenic Sentiment AI - Version 2.0
2
+
3
+ ![HellenicSentimentAI Logo](https://huggingface.co/gsar78/HellenicSentimentAI/resolve/main/HellenicSentimentAI_logo.png?download=true)
4
+
5
+ ## Model Description
6
+
7
+ This is the second version of Hellenic Sentiment AI.
8
+
9
+ Like the first one, it is an open-weights model and designed for **emotion** and **sentiment** classification of text in Greek language.
10
+
11
+ For Emotions, it is a based on a custom multi-label classification head which extends the version 1.1 model.
12
+
13
+ The Emotion labels available for classification are the following:
14
+ ```Python
15
+ emotion_labels = [
16
+ 'joy', 'trust', 'excitement', 'gratitude', 'hope', 'love', 'pride',
17
+ 'anger', 'disgust', 'fear', 'sadness', 'anxiety', 'frustration', 'guilt',
18
+ 'disappointment', 'surprise', 'anticipation', 'neutral'
19
+ ]
20
+ ```
21
+
22
+ The Sentiment polarity labels remain the same as in Version 1.1 of the model.
23
+
24
+ For reference, these are:
25
+ ```Python
26
+ sentiment_labels = ['negative', 'neutral', 'positive']
27
+ ```
28
+
29
+
30
+ ## Model Details
31
+
32
+ - **Model Name:** Hellenic Sentiment AI
33
+ - **Model Version:** 2.0
34
+ - **Language:** Emotion classification: only Greek (Version 2.0), Sentiment polarity: Multilingual (El, En, Fr, It, Es, De, Ar) (Version 1.1)
35
+ - **Framework:** Transformers from HuggingFace
36
+ - **Max Sequence Length:** 512
37
+ - **Base Architecture:** RoBERTa
38
+ - **Training Data:** The model (version 2.0) was trained on a custom, curated (Greek language only) dataset of reviews with their respective emotions, comprising human-handpicked reviews from products, places, restaurants, etc., with a specific emphasis on Greek language texts, and labeling of the emotions was performed manually by a human.
39
+
40
+
41
+ ## Production readiness
42
+
43
+ This model is a production-grade sentiment analysis solution, carefully designed and trained to deliver high-performance results in downstream applications. With its robust architecture and rigorous testing, it is ready to be deployed in real-world scenarios, providing accurate and reliable sentiment analysis capabilities for a wide range of use cases.
44
+
45
+ ## Ongoing Improvement
46
+
47
+ To ensure the model remains at the forefront of sentiment analysis capabilities, it is regularly updated and fine-tuned using new data and techniques.
48
+
49
+ This commitment to ongoing improvement enables the model to adapt to emerging trends, nuances, and complexities in language, ensuring that it continues to provide exceptional performance and accuracy in production environments.
50
+
51
+
52
+ ## Usage:
53
+
54
+ Simplified code to run the Emotion classification.
55
+
56
+ ```python
57
+ import torch
58
+ from transformers import AutoTokenizer, AutoConfig
59
+
60
+ # Load the tokenizer and model from the local directory
61
+ model_dir = "gsar78/HellenicSentimentAI_v2"
62
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
63
+ config = AutoConfig.from_pretrained(model_dir)
64
+ model = CustomModel.from_pretrained(model_dir, config=config, num_emotion_labels=18)
65
+
66
+
67
+
68
+ # Function to predict sentiment and emotion
69
+ def predict(texts):
70
+ # Tokenize the input texts
71
+ inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt", max_length=512)
72
+
73
+ # Move inputs to the same device as the model
74
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
75
+ inputs = {k: v.to(device) for k, v in inputs.items()}
76
+
77
+ # Ensure the model is on the correct device
78
+ model.to(device)
79
+ model.eval() # Set the model to evaluation mode
80
+
81
+ # Clear any gradients
82
+ model.zero_grad()
83
+
84
+ # Get model predictions
85
+ with torch.no_grad():
86
+ outputs = model(**inputs)
87
+
88
+ # Extract logits
89
+ emotion_logits = outputs["emotion_logits"]
90
+ sentiment_logits = outputs["sentiment_logits"]
91
+
92
+ # Convert logits to probabilities
93
+ emotion_probs = torch.sigmoid(emotion_logits)
94
+ sentiment_probs = torch.softmax(sentiment_logits, dim=1)
95
+
96
+ # Convert tensors to lists for easier handling
97
+ emotion_probs_list = (emotion_probs * 100).tolist()[0] # Get the first (and only) sample and convert to %
98
+ sentiment_probs_list = (sentiment_probs * 100).tolist()[0] # Get the first (and only) sample and convert to %
99
+
100
+ # Define the sentiment and emotion labels
101
+ sentiment_labels = ['negative', 'neutral', 'positive']
102
+ emotion_labels = [
103
+ 'joy', 'trust', 'excitement', 'gratitude', 'hope', 'love', 'pride',
104
+ 'anger', 'disgust', 'fear', 'sadness', 'anxiety', 'frustration', 'guilt',
105
+ 'disappointment', 'surprise', 'anticipation', 'neutral'
106
+ ]
107
+
108
+ # Threshold for displaying probabilities
109
+ threshold = 0.0
110
+
111
+ # Map emotion probabilities to their corresponding labels
112
+ emotion_results = {label: prob for label, prob in zip(emotion_labels, emotion_probs_list) if prob > 0.30}
113
+
114
+ # Map sentiment probabilities to their corresponding labels
115
+ sentiment_results = {label: prob for label, prob in zip(sentiment_labels, sentiment_probs_list) if prob > threshold}
116
+
117
+ return emotion_results, sentiment_results
118
+
119
+ # Example usage
120
+ sample_texts = ["Απολαύσαμε μια υ��έροχη βραδιά σε αυτό το εστιατόριο. "
121
+ "Το μενού ήταν πολύ καλά σχεδιασμένο και κάθε πιάτο ήταν μια γευστική έκπληξη. "
122
+ "Η εξυπηρέτηση ήταν άψογη και η ατμόσφαιρα ευχάριστη. Σίγουρα θα επιστρέψουμε για άλλη μια φορά."]
123
+ print("Text: ", sample_texts[0])
124
+ emotion_results, sentiment_results = predict(sample_texts)
125
+
126
+ print("\nSentiment probabilities (%):")
127
+ for label, prob in sentiment_results.items():
128
+ print(f" {label}: {prob:.2f}%")
129
+ # Print the results
130
+ print("\nEmotion probabilities (%):")
131
+ for label, prob in emotion_results.items():
132
+ print(f" {label}: {prob:.2f}%")
133
+
134
+
135
+
136
+ # Change the text and predict again
137
+ # Print the results
138
+ print("\n======")
139
+ print("\nNew prediction:")
140
+ sample_texts = ["Η τελευταία μας εμπειρία στο εστιατόριο αυτό δεν ήταν ιδιαίτερα θετική. "
141
+ "Αν και ο χώρος είχε μια ενδιαφέρουσα ατμόσφαιρα, το φαγητό ήταν μέτριο και η εξυπηρέτηση ήταν αργή. "
142
+ "Οι τιμές ήταν επίσης απογοητευτικές για την ποιότητα που προσφέρθηκε."]
143
+ print("Text: ", sample_texts[0])
144
+ emotion_results, sentiment_results = predict(sample_texts)
145
+
146
+ print("\nSentiment probabilities (%):")
147
+ for label, prob in sentiment_results.items():
148
+ print(f" {label}: {prob:.2f}%")
149
+ print("\nEmotion probabilities (%):")
150
+ for label, prob in emotion_results.items():
151
+ print(f" {label}: {prob:.2f}%")
152
+ ```
153
+
154
+ Expected output:
155
+
156
+ ```context
157
+ Text: Απολαύσαμε μια υπέροχη βραδιά σε αυτό το εστιατόριο. Το μενού ήταν πολύ καλά σχεδιασμένο και κάθε πιάτο ήταν μια γευστική έκπληξη. Η εξυπηρέτηση ήταν άψογη και η ατμόσφαιρα ευχάριστη. Σίγουρα θα επιστρέψουμε για άλλη μια φορά.
158
+
159
+ Sentiment probabilities (%):
160
+ negative: 17.36%
161
+ neutral: 11.31%
162
+ positive: 71.33%
163
+
164
+ Emotion probabilities (%):
165
+ joy: 99.92%
166
+ trust: 93.40%
167
+ excitement: 73.43%
168
+ gratitude: 97.52%
169
+ hope: 0.33%
170
+ love: 12.20%
171
+ pride: 1.09%
172
+ anticipation: 0.31%
173
+
174
+ ======
175
+
176
+ New prediction:
177
+ Text: Η τελευταία μας εμπειρία στο εστιατόριο αυτό δεν ήταν ιδιαίτερα θετική. Αν και ο χώρος είχε μια ενδιαφέρουσα ατμόσφαιρα, το φαγητό ήταν μέτριο και η εξυπηρέτηση ήταν αργή. Οι τιμές ήταν επίσης απογοητευτικές για την ποιότητα που προσφέρθηκε.
178
+
179
+ Sentiment probabilities (%):
180
+ negative: 58.39%
181
+ neutral: 16.34%
182
+ positive: 25.27%
183
+
184
+ Emotion probabilities (%):
185
+ frustration: 68.61%
186
+ disappointment: 99.84%
187
+ neutral: 0.75%
188
+ ```
189
+
190
+ For simplicity and as an alternative, you can run this Google Colab:
191
+ [Google Colab](https://colab.research.google.com/drive/1Hr7NCCA3VprpFL8WLpO3lKHQaUlYkF62?usp=sharing)
192
+
193
+
194
+ ## Evaluation
195
+
196
+ Due to time constraints, there is no official benchmark yet.
197
+
198
+ However, the evaluation on a test dataset is the following:
199
+
200
+ Evaluation results:
201
+ {
202
+ 'eval_loss': 0.0322,
203
+ 'eval_accuracy': 0.7857,
204
+ 'eval_hamming_loss': 0.0141,
205
+ 'eval_precision': 0.9785,
206
+ 'eval_recall': 0.9133,
207
+ 'eval_f1': 0.9448
208
+ }
209
+
210
+
211
+