Update evaluator_module.py (#17)
Browse files- Update evaluator_module.py (bbadeb4cf08318a2bc50d34a03f55f39aadde90e)
Co-authored-by: Manay Porwal <manayporwal07@users.noreply.huggingface.co>
- evaluator_module.py +51 -40
evaluator_module.py
CHANGED
|
@@ -13,7 +13,6 @@ import concurrent.futures
|
|
| 13 |
import random
|
| 14 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 15 |
import evaluate
|
| 16 |
-
from unieval.metric.evaluator import get_evaluator
|
| 17 |
import warnings
|
| 18 |
warnings.filterwarnings("ignore")
|
| 19 |
|
|
@@ -50,52 +49,60 @@ class AetherScoreEvaluator:
|
|
| 50 |
self.sentence_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 51 |
|
| 52 |
# Scoring weights # Domain Specific weights can be added for better results
|
| 53 |
-
self.weights = {
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# In-memory cache
|
| 57 |
self.cache = {}
|
| 58 |
|
| 59 |
# LLM Judge
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# " \"hallucination_score\": <float between 0 and 1>,\n"
|
| 70 |
-
# " \"assumption_control\": <float between 0 and 1>,\n"
|
| 71 |
-
# " \"explanation\": \"<one or two sentences>\"\n"
|
| 72 |
-
# "}"
|
| 73 |
-
# )
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# except Exception as e:
|
| 79 |
-
# return {
|
| 80 |
-
# "hallucination_score": (0.1, f"HF model failed: {e}"),
|
| 81 |
-
# "assumption_control": (0.1, f"HF model failed: {e}")
|
| 82 |
-
# }
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
|
| 101 |
|
|
@@ -193,11 +200,15 @@ class AetherScoreEvaluator:
|
|
| 193 |
|
| 194 |
scores, reasons = {}, {}
|
| 195 |
|
| 196 |
-
#
|
| 197 |
halluc_results = self.hallucination_score(prompt, response)
|
| 198 |
scores['hallucination_score'], reasons['hallucination_score'] = halluc_results['hallucination_score']
|
| 199 |
scores['assumption_control'], reasons['assumption_control'] = halluc_results['assumption_control']
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
# Evaluating Instruction Following, Coherence and Accuracy
|
| 202 |
scores['instruction_following'], reasons['instruction_following'] = self._evaluate_instruction_following(prompt, response)
|
| 203 |
scores['coherence'], reasons['coherence'] = self._evaluate_coherence(response)
|
|
|
|
| 13 |
import random
|
| 14 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 15 |
import evaluate
|
|
|
|
| 16 |
import warnings
|
| 17 |
warnings.filterwarnings("ignore")
|
| 18 |
|
|
|
|
| 49 |
self.sentence_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 50 |
|
| 51 |
# Scoring weights # Domain Specific weights can be added for better results
|
| 52 |
+
self.weights = {
|
| 53 |
+
'instruction_following': 0.20,
|
| 54 |
+
'hallucination_score': 0.15,
|
| 55 |
+
'assumption_control': 0.15,
|
| 56 |
+
'coherence': 0.15,
|
| 57 |
+
'accuracy': 0.15,
|
| 58 |
+
'llm_judge_score': 0.20
|
| 59 |
+
}
|
| 60 |
# In-memory cache
|
| 61 |
self.cache = {}
|
| 62 |
|
| 63 |
# LLM Judge
|
| 64 |
+
def _evaluate_with_llm_judge(self, prompt: str, response: str) -> Dict:
|
| 65 |
+
print("Using HF model LLM Judge...")
|
| 66 |
+
|
| 67 |
+
query = (
|
| 68 |
+
f"Prompt: {prompt}\n"
|
| 69 |
+
f"Response: {response}\n\n"
|
| 70 |
+
"Return only a JSON object:\n"
|
| 71 |
+
"{\n"
|
| 72 |
+
' "score": <float between 0 and 1>,\n'
|
| 73 |
+
' "explanation": "<short reasoning>"\n'
|
| 74 |
+
"}\n"
|
| 75 |
+
)
|
| 76 |
|
| 77 |
+
try:
|
| 78 |
+
result = self.judge_model(query, max_new_tokens=128, truncation=True)
|
| 79 |
+
output = result[0]['generated_text']
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return {"llm_judge_score": (0.0, f"HF model failed: {e}")}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Defaults
|
| 84 |
+
score = 0.5
|
| 85 |
+
explanation = output.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
try:
|
| 88 |
+
parsed = json.loads(output.strip())
|
| 89 |
+
score = float(parsed.get("score", score))
|
| 90 |
+
explanation = parsed.get("explanation", explanation)
|
| 91 |
+
except Exception:
|
| 92 |
+
pass # fallback to defaults
|
| 93 |
|
| 94 |
+
# Hardcoded interpretation
|
| 95 |
+
if score < 0.3:
|
| 96 |
+
auto_expl = "Poor alignment with the prompt."
|
| 97 |
+
elif score < 0.7:
|
| 98 |
+
auto_expl = "Partially aligned with the prompt."
|
| 99 |
+
else:
|
| 100 |
+
auto_expl = "Well aligned with the prompt."
|
| 101 |
|
| 102 |
+
explanation = f"{explanation} | {auto_expl}"
|
| 103 |
+
|
| 104 |
+
return {"llm_judge_score": (score, explanation)}
|
| 105 |
+
|
| 106 |
|
| 107 |
|
| 108 |
|
|
|
|
| 200 |
|
| 201 |
scores, reasons = {}, {}
|
| 202 |
|
| 203 |
+
# hallucination and assumption controle
|
| 204 |
halluc_results = self.hallucination_score(prompt, response)
|
| 205 |
scores['hallucination_score'], reasons['hallucination_score'] = halluc_results['hallucination_score']
|
| 206 |
scores['assumption_control'], reasons['assumption_control'] = halluc_results['assumption_control']
|
| 207 |
+
|
| 208 |
+
# llm judge score
|
| 209 |
+
llm_judge_results = self._evaluate_with_llm_judge(prompt, response)
|
| 210 |
+
scores['llm_judge_score'], reasons['llm_judge_score'] = llm_judge_results['llm_judge_score']
|
| 211 |
+
|
| 212 |
# Evaluating Instruction Following, Coherence and Accuracy
|
| 213 |
scores['instruction_following'], reasons['instruction_following'] = self._evaluate_instruction_following(prompt, response)
|
| 214 |
scores['coherence'], reasons['coherence'] = self._evaluate_coherence(response)
|