Supastrikas-004 manayporwal07 commited on
Commit
16d4be5
·
verified ·
1 Parent(s): 5535d43

Update evaluator_module.py (#39)

Browse files

- Update evaluator_module.py (9d0d6c25aabcf7e27201675fcaff554759bcb474)


Co-authored-by: Manay Porwal <manayporwal07@users.noreply.huggingface.co>

Files changed (1) hide show
  1. evaluator_module.py +66 -31
evaluator_module.py CHANGED
@@ -40,44 +40,79 @@ class AetherScoreEvaluator:
40
  self.cache = {}
41
 
42
  # LLM Judge
43
- def _evaluate_with_llm_judge(self, prompt: str, response: str) -> Dict:
44
 
45
- print("Using HF model LLM Judge...")
46
 
47
- query = (
48
- f"Prompt: {prompt}\n"
49
- f"Response: {response}\n\n"
50
- "Return ONLY a valid JSON object in this exact format:\n"
51
- "{\n"
52
- " \"hallucination_score\": <float between 0 and 1>,\n"
53
- " \"assumption_control\": <float between 0 and 1>,\n"
54
- " \"explanation\": \"<one or two sentences>\"\n"
55
- "}"
56
- )
57
 
58
- try:
59
- result = self.judge_model(query, max_new_tokens=128, truncation=True)
60
- output = result[0]['generated_text']
61
- except Exception as e:
62
- return {
63
- "hallucination_score": (0.1, f"HF model failed: {e}"),
64
- "assumption_control": (0.1, f"HF model failed: {e}")
65
- }
66
 
67
- # Default values
68
 
69
- halluc_score = random.uniform(0.3, 0.7)
70
- assumption_score = random.uniform(0.3, 0.7)
71
- explanation = output
72
 
73
- try:
74
- parsed = json.loads(output.strip())
75
- halluc_score = float(parsed.get("hallucination_score", halluc_score))
76
- assumption_score = float(parsed.get("assumption_control", assumption_score))
77
- explanation = parsed.get("explanation", explanation)
78
- except Exception:
79
- pass # fallback to defaults
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  return {
82
  "hallucination_score": (halluc_score, explanation),
83
  "assumption_control": (assumption_score, explanation)
 
40
  self.cache = {}
41
 
42
  # LLM Judge
43
+ # def _evaluate_with_llm_judge(self, prompt: str, response: str) -> Dict:
44
 
45
+ # print("Using HF model LLM Judge...")
46
 
47
+ # query = (
48
+ # f"Prompt: {prompt}\n"
49
+ # f"Response: {response}\n\n"
50
+ # "Return ONLY a valid JSON object in this exact format:\n"
51
+ # "{\n"
52
+ # " \"hallucination_score\": <float between 0 and 1>,\n"
53
+ # " \"assumption_control\": <float between 0 and 1>,\n"
54
+ # " \"explanation\": \"<one or two sentences>\"\n"
55
+ # "}"
56
+ # )
57
 
58
+ # try:
59
+ # result = self.judge_model(query, max_new_tokens=128, truncation=True)
60
+ # output = result[0]['generated_text']
61
+ # except Exception as e:
62
+ # return {
63
+ # "hallucination_score": (0.1, f"HF model failed: {e}"),
64
+ # "assumption_control": (0.1, f"HF model failed: {e}")
65
+ # }
66
 
67
+ # # Default values
68
 
69
+ # halluc_score = random.uniform(0.3, 0.7)
70
+ # assumption_score = random.uniform(0.3, 0.7)
71
+ # explanation = output
72
 
73
+ # try:
74
+ # parsed = json.loads(output.strip())
75
+ # halluc_score = float(parsed.get("hallucination_score", halluc_score))
76
+ # assumption_score = float(parsed.get("assumption_control", assumption_score))
77
+ # explanation = parsed.get("explanation", explanation)
78
+ # except Exception:
79
+ # pass # fallback to defaults
80
 
81
+ # return {
82
+ # "hallucination_score": (halluc_score, explanation),
83
+ # "assumption_control": (assumption_score, explanation)
84
+ # }
85
+
86
+ import random
87
+ import json
88
+
89
+ def _evaluate_with_llm_judge(self, prompt: str, response: str) -> Dict:
90
+
91
+ print("Using rule-based evaluation instead of HF LLM...")
92
+
93
+ prompt_words = set(prompt.lower().split())
94
+ response_words = response.lower().split()
95
+
96
+ # Hallucination score: fraction of words in response that are not in prompt
97
+ if response_words:
98
+ halluc_score = len([w for w in response_words if w not in prompt_words]) / len(response_words)
99
+ else:
100
+ halluc_score = 0.1
101
+
102
+ # Assumption control: fraction of sentences starting with uncertain words
103
+ uncertain_starts = ("i assume", "maybe", "probably", "likely", "could be")
104
+ sentences = response.lower().split(".")
105
+ if sentences:
106
+ assumption_score = sum(1 for s in sentences if s.strip().startswith(uncertain_starts)) / len(sentences)
107
+ else:
108
+ assumption_score = 0.1
109
+
110
+ # Ensure scores are between 0 and 1
111
+ # halluc_score = max(0, min(1, halluc_score))
112
+ # assumption_score = max(0, min(1, assumption_score))
113
+
114
+ explanation = "Rule-based evaluation applied."
115
+
116
  return {
117
  "hallucination_score": (halluc_score, explanation),
118
  "assumption_control": (assumption_score, explanation)