Update metrics.py
Browse files- metrics.py +78 -5
metrics.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
# metrics.py
|
| 2 |
from model_loader import metrics_models
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def compute_semantic_similarity(original, paraphrased):
|
| 5 |
"""
|
|
@@ -17,16 +19,87 @@ def compute_semantic_similarity(original, paraphrased):
|
|
| 17 |
|
| 18 |
def compute_empathy_score(paraphrased):
|
| 19 |
"""
|
| 20 |
-
Compute an empathy score for the paraphrased comment
|
|
|
|
| 21 |
Returns a score between 0 and 1.
|
| 22 |
"""
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
empathy_words = ["sorry", "understand", "care", "help", "support"]
|
| 26 |
words = paraphrased.lower().split()
|
| 27 |
empathy_count = sum(1 for word in words if word in empathy_words)
|
| 28 |
-
|
|
|
|
| 29 |
return round(score, 2)
|
| 30 |
except Exception as e:
|
| 31 |
print(f"Error computing empathy score: {str(e)}")
|
| 32 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# metrics.py
|
| 2 |
from model_loader import metrics_models
|
| 3 |
+
from classifier import classify_toxic_comment
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
def compute_semantic_similarity(original, paraphrased):
|
| 7 |
"""
|
|
|
|
| 19 |
|
| 20 |
def compute_empathy_score(paraphrased):
|
| 21 |
"""
|
| 22 |
+
Compute an empathy score for the paraphrased comment.
|
| 23 |
+
Enhanced to consider positive sentiment and supportive language.
|
| 24 |
Returns a score between 0 and 1.
|
| 25 |
"""
|
| 26 |
try:
|
| 27 |
+
empathy_words = ["sorry", "understand", "care", "help", "support", "appreciate", "encourage", "positive"]
|
|
|
|
| 28 |
words = paraphrased.lower().split()
|
| 29 |
empathy_count = sum(1 for word in words if word in empathy_words)
|
| 30 |
+
# Normalize by length, cap at 1.0
|
| 31 |
+
score = min(empathy_count / max(len(words), 1) * 2, 1.0) # Amplify for better sensitivity
|
| 32 |
return round(score, 2)
|
| 33 |
except Exception as e:
|
| 34 |
print(f"Error computing empathy score: {str(e)}")
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
def compute_bias_score(paraphrased):
|
| 38 |
+
"""
|
| 39 |
+
Compute a bias score for the paraphrased comment (placeholder).
|
| 40 |
+
Detects stereotypical or discriminatory language.
|
| 41 |
+
Returns a score between 0 and 1 (lower is less biased).
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
bias_indicators = ["race", "gender", "religion", "stereotype", "discriminate", "bias"]
|
| 45 |
+
words = paraphrased.lower().split()
|
| 46 |
+
bias_count = sum(1 for word in words if word in bias_indicators)
|
| 47 |
+
score = bias_count / max(len(words), 1)
|
| 48 |
+
return round(score, 2)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error computing bias score: {str(e)}")
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
def compute_hallucination_score(original, paraphrased):
|
| 54 |
+
"""
|
| 55 |
+
Compute a hallucination score by checking factual consistency.
|
| 56 |
+
High score indicates deviation from original meaning.
|
| 57 |
+
Returns a score between 0 and 1 (lower is better).
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
# Use semantic similarity as a proxy; low similarity suggests hallucination
|
| 61 |
+
similarity = compute_semantic_similarity(original, paraphrased)
|
| 62 |
+
if similarity is None:
|
| 63 |
+
return 0.5 # Default if similarity fails
|
| 64 |
+
# Inverse similarity scaled to penalize low similarity
|
| 65 |
+
score = max(0.0, (1.0 - similarity) * 0.5)
|
| 66 |
+
return round(score, 2)
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error computing hallucination score: {str(e)}")
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def compute_reward_scores(original, paraphrased):
|
| 72 |
+
"""
|
| 73 |
+
Compute all reward scores for a paraphrase.
|
| 74 |
+
Returns a dictionary with empathy, toxicity, bias, hallucination, and overall reward.
|
| 75 |
+
"""
|
| 76 |
+
try:
|
| 77 |
+
# Get toxicity from classifier
|
| 78 |
+
_, _, _, toxicity_score, bias_score, _, _, _, _, paraphrased_toxicity_score, paraphrased_bias_score, _, _ = classify_toxic_comment(paraphrased)
|
| 79 |
+
toxicity = paraphrased_toxicity_score if paraphrased_toxicity_score is not None else 0.5
|
| 80 |
+
|
| 81 |
+
# Compute other metrics
|
| 82 |
+
empathy = compute_empathy_score(paraphrased) or 0.5
|
| 83 |
+
bias = compute_bias_score(paraphrased) or 0.5
|
| 84 |
+
hallucination = compute_hallucination_score(original, paraphrased) or 0.5
|
| 85 |
+
|
| 86 |
+
# Overall reward: Weighted combination (adjust weights as needed)
|
| 87 |
+
reward = (0.4 * empathy) - (0.2 * toxicity) - (0.2 * bias) - (0.2 * hallucination)
|
| 88 |
+
reward = max(0.0, min(1.0, round(reward, 2)))
|
| 89 |
+
|
| 90 |
+
return {
|
| 91 |
+
"empathy": empathy,
|
| 92 |
+
"toxicity": toxicity,
|
| 93 |
+
"bias": bias,
|
| 94 |
+
"hallucination": hallucination,
|
| 95 |
+
"reward": reward
|
| 96 |
+
}
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"Error computing reward scores: {str(e)}")
|
| 99 |
+
return {
|
| 100 |
+
"empathy": 0.5,
|
| 101 |
+
"toxicity": 0.5,
|
| 102 |
+
"bias": 0.5,
|
| 103 |
+
"hallucination": 0.5,
|
| 104 |
+
"reward": 0.5
|
| 105 |
+
}
|