Spaces:
Sleeping
Sleeping
Update metrics.py
Browse files- metrics.py +6 -11
metrics.py
CHANGED
@@ -6,7 +6,7 @@ from transformers import pipeline
|
|
6 |
# Load Sentence-BERT model for semantic similarity
|
7 |
sentence_bert_model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
|
9 |
-
# Load a pre-trained emotion classifier
|
10 |
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=None)
|
11 |
|
12 |
def compute_semantic_similarity(original_comment, paraphrased_comment):
|
@@ -14,11 +14,8 @@ def compute_semantic_similarity(original_comment, paraphrased_comment):
|
|
14 |
Compute the semantic similarity between the original and paraphrased comments using Sentence-BERT.
|
15 |
Returns a score between 0 and 1 (higher is better).
|
16 |
"""
|
17 |
-
# Encode the comments into embeddings
|
18 |
original_embedding = sentence_bert_model.encode(original_comment, convert_to_tensor=True)
|
19 |
paraphrased_embedding = sentence_bert_model.encode(paraphrased_comment, convert_to_tensor=True)
|
20 |
-
|
21 |
-
# Compute cosine similarity
|
22 |
similarity_score = util.cos_sim(original_embedding, paraphrased_embedding)[0][0].item()
|
23 |
return round(similarity_score, 2)
|
24 |
|
@@ -29,11 +26,14 @@ def compute_emotion_shift(original_comment, paraphrased_comment):
|
|
29 |
"""
|
30 |
# Classify emotions in the original comment
|
31 |
original_emotions = emotion_classifier(original_comment)
|
32 |
-
|
|
|
|
|
33 |
|
34 |
# Classify emotions in the paraphrased comment
|
35 |
paraphrased_emotions = emotion_classifier(paraphrased_comment)
|
36 |
-
|
|
|
37 |
|
38 |
# Define negative and positive emotions
|
39 |
negative_emotions = ['anger', 'sadness', 'fear']
|
@@ -52,13 +52,8 @@ def compute_empathy_score(paraphrased_comment):
|
|
52 |
Compute a proxy empathy score based on politeness keywords.
|
53 |
Returns a score between 0 and 1 (higher indicates more empathy).
|
54 |
"""
|
55 |
-
# Define a list of politeness/empathy-related keywords
|
56 |
empathy_keywords = ['please', 'thank you', 'appreciate', 'understand', 'sorry', 'consider', 'kindly', 'help', 'support']
|
57 |
-
|
58 |
-
# Count the number of empathy keywords in the paraphrased comment
|
59 |
comment_lower = paraphrased_comment.lower()
|
60 |
keyword_count = sum(1 for keyword in empathy_keywords if keyword in comment_lower)
|
61 |
-
|
62 |
-
# Normalize the score (arbitrary scaling; max 3 keywords for a score of 1)
|
63 |
empathy_score = min(keyword_count / 3, 1.0)
|
64 |
return round(empathy_score, 2)
|
|
|
6 |
# Load Sentence-BERT model for semantic similarity
|
7 |
sentence_bert_model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
|
9 |
+
# Load a pre-trained emotion classifier
|
10 |
emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=None)
|
11 |
|
12 |
def compute_semantic_similarity(original_comment, paraphrased_comment):
|
|
|
14 |
Compute the semantic similarity between the original and paraphrased comments using Sentence-BERT.
|
15 |
Returns a score between 0 and 1 (higher is better).
|
16 |
"""
|
|
|
17 |
original_embedding = sentence_bert_model.encode(original_comment, convert_to_tensor=True)
|
18 |
paraphrased_embedding = sentence_bert_model.encode(paraphrased_comment, convert_to_tensor=True)
|
|
|
|
|
19 |
similarity_score = util.cos_sim(original_embedding, paraphrased_embedding)[0][0].item()
|
20 |
return round(similarity_score, 2)
|
21 |
|
|
|
26 |
"""
|
27 |
# Classify emotions in the original comment
|
28 |
original_emotions = emotion_classifier(original_comment)
|
29 |
+
# Since pipeline returns a list of lists, take the first (and only) inner list
|
30 |
+
original_emotions = original_emotions[0] if isinstance(original_emotions, list) and original_emotions else []
|
31 |
+
original_dominant_emotion = max(original_emotions, key=lambda x: x['score'])['label'] if original_emotions else "unknown"
|
32 |
|
33 |
# Classify emotions in the paraphrased comment
|
34 |
paraphrased_emotions = emotion_classifier(paraphrased_comment)
|
35 |
+
paraphrased_emotions = paraphrased_emotions[0] if isinstance(paraphrased_emotions, list) and paraphrased_emotions else []
|
36 |
+
paraphrased_dominant_emotion = max(paraphrased_emotions, key=lambda x: x['score'])['label'] if paraphrased_emotions else "unknown"
|
37 |
|
38 |
# Define negative and positive emotions
|
39 |
negative_emotions = ['anger', 'sadness', 'fear']
|
|
|
52 |
Compute a proxy empathy score based on politeness keywords.
|
53 |
Returns a score between 0 and 1 (higher indicates more empathy).
|
54 |
"""
|
|
|
55 |
empathy_keywords = ['please', 'thank you', 'appreciate', 'understand', 'sorry', 'consider', 'kindly', 'help', 'support']
|
|
|
|
|
56 |
comment_lower = paraphrased_comment.lower()
|
57 |
keyword_count = sum(1 for keyword in empathy_keywords if keyword in comment_lower)
|
|
|
|
|
58 |
empathy_score = min(keyword_count / 3, 1.0)
|
59 |
return round(empathy_score, 2)
|