harshal15122003 commited on
Commit
0c7c564
·
verified ·
1 Parent(s): bdc6062

Update graders.py

Browse files
Files changed (1) hide show
  1. graders.py +28 -55
graders.py CHANGED
@@ -1,9 +1,25 @@
1
  """
2
  graders.py — Email Sorting OpenEnv
3
- 3 grader tasks: easy, medium, hard. Scores 0.0–1.0.
4
  """
5
 
6
- from env import EMAILS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # ============================================
9
  # TASK 1 — EASY: Obvious spam detection
@@ -18,18 +34,9 @@ EASY_EMAILS = [
18
  ]
19
 
20
  def grade_easy_sorting(agent_fn=None):
21
- """
22
- Easy task: classify obvious spam vs important emails.
23
- agent_fn(email) -> 'spam' | 'important' | 'promotion'
24
- Returns score strictly in (0.0, 1.0)
25
- """
26
  if agent_fn is None:
27
  agent_fn = baseline_agent
28
- correct = 0
29
- for email in EASY_EMAILS:
30
- prediction = agent_fn(email)
31
- if prediction == email["label"]:
32
- correct += 1
33
  raw = correct / len(EASY_EMAILS)
34
  score = round(min(0.99, max(0.01, raw)), 2)
35
  return {"task": "easy_sorting", "correct": correct, "total": len(EASY_EMAILS), "score": score}
@@ -49,17 +56,9 @@ MEDIUM_EMAILS = [
49
  ]
50
 
51
  def grade_medium_sorting(agent_fn=None):
52
- """
53
- Medium task: classify emails across all 3 categories.
54
- Returns score strictly in (0.0, 1.0)
55
- """
56
  if agent_fn is None:
57
  agent_fn = baseline_agent
58
- correct = 0
59
- for email in MEDIUM_EMAILS:
60
- prediction = agent_fn(email)
61
- if prediction == email["label"]:
62
- correct += 1
63
  raw = correct / len(MEDIUM_EMAILS)
64
  score = round(min(0.99, max(0.01, raw)), 2)
65
  return {"task": "medium_sorting", "correct": correct, "total": len(MEDIUM_EMAILS), "score": score}
@@ -81,39 +80,14 @@ HARD_EMAILS = [
81
  ]
82
 
83
  def grade_hard_sorting(agent_fn=None):
84
- """
85
- Hard task: subtle emails that are easy to misclassify.
86
- Returns score strictly in (0.0, 1.0)
87
- """
88
  if agent_fn is None:
89
  agent_fn = baseline_agent
90
- correct = 0
91
- for email in HARD_EMAILS:
92
- prediction = agent_fn(email)
93
- if prediction == email["label"]:
94
- correct += 1
95
  raw = correct / len(HARD_EMAILS)
96
  score = round(min(0.99, max(0.01, raw)), 2)
97
  return {"task": "hard_sorting", "correct": correct, "total": len(HARD_EMAILS), "score": score}
98
 
99
 
100
- # ============================================
101
- # SIMPLE RULE-BASED AGENT (for baseline)
102
- # ============================================
103
-
104
- def baseline_agent(email: dict) -> str:
105
- text = (email.get("subject", "") + " " + email.get("body", "")).lower()
106
- spam_kw = ["won", "free", "prize", "claim", "urgent", "congratulations",
107
- "selected", "suspended", "verify", "overdue", "pre-approved"]
108
- promo_kw = ["off", "sale", "deal", "discount", "offer", "save",
109
- "shop", "upgrade", "trial", "rewards", "loyalty"]
110
- spam_score = sum(1 for k in spam_kw if k in text)
111
- promo_score = sum(1 for k in promo_kw if k in text)
112
- if spam_score >= 2: return "spam"
113
- if promo_score >= 1: return "promotion"
114
- return "important"
115
-
116
-
117
  # ============================================
118
  # RUN ALL GRADERS
119
  # ============================================
@@ -122,20 +96,19 @@ def run_all_graders(agent_fn=None):
122
  if agent_fn is None:
123
  agent_fn = baseline_agent
124
 
125
- results = []
126
- results.append(grade_easy_sorting(agent_fn))
127
- results.append(grade_medium_sorting(agent_fn))
128
- results.append(grade_hard_sorting(agent_fn))
 
129
 
130
  avg_score = round(sum(r["score"] for r in results) / len(results), 4)
131
- all_passed = all(r["score"] >= 0.0 for r in results)
132
-
133
  print(f"[GRADER] easy={results[0]['score']} medium={results[1]['score']} hard={results[2]['score']} avg={avg_score}", flush=True)
134
 
135
  return {
136
  "tasks": results,
137
  "average_score": avg_score,
138
- "all_passed": all_passed
139
  }
140
 
141
 
@@ -143,5 +116,5 @@ if __name__ == "__main__":
143
  print("Running all graders with baseline agent...\n")
144
  results = run_all_graders()
145
  for r in results["tasks"]:
146
- print(f"Task: {r['task']:8s} | Score: {r['score']} | {r['correct']}/{r['total']} correct")
147
  print(f"\nAverage Score: {results['average_score']}")
 
1
  """
2
  graders.py — Email Sorting OpenEnv
3
+ 3 grader tasks: easy, medium, hard. Scores strictly in (0, 1).
4
  """
5
 
6
+
7
+ # ============================================
8
+ # BASELINE AGENT (default for all graders)
9
+ # ============================================
10
+
11
+ def baseline_agent(email: dict) -> str:
12
+ text = (email.get("subject", "") + " " + email.get("body", "")).lower()
13
+ spam_kw = ["won", "free", "prize", "claim", "urgent", "congratulations",
14
+ "selected", "suspended", "verify", "overdue", "pre-approved"]
15
+ promo_kw = ["off", "sale", "deal", "discount", "offer", "save",
16
+ "shop", "upgrade", "trial", "rewards", "loyalty"]
17
+ spam_score = sum(1 for k in spam_kw if k in text)
18
+ promo_score = sum(1 for k in promo_kw if k in text)
19
+ if spam_score >= 2: return "spam"
20
+ if promo_score >= 1: return "promotion"
21
+ return "important"
22
+
23
 
24
  # ============================================
25
  # TASK 1 — EASY: Obvious spam detection
 
34
  ]
35
 
36
  def grade_easy_sorting(agent_fn=None):
 
 
 
 
 
37
  if agent_fn is None:
38
  agent_fn = baseline_agent
39
+ correct = sum(1 for e in EASY_EMAILS if agent_fn(e) == e["label"])
 
 
 
 
40
  raw = correct / len(EASY_EMAILS)
41
  score = round(min(0.99, max(0.01, raw)), 2)
42
  return {"task": "easy_sorting", "correct": correct, "total": len(EASY_EMAILS), "score": score}
 
56
  ]
57
 
58
  def grade_medium_sorting(agent_fn=None):
 
 
 
 
59
  if agent_fn is None:
60
  agent_fn = baseline_agent
61
+ correct = sum(1 for e in MEDIUM_EMAILS if agent_fn(e) == e["label"])
 
 
 
 
62
  raw = correct / len(MEDIUM_EMAILS)
63
  score = round(min(0.99, max(0.01, raw)), 2)
64
  return {"task": "medium_sorting", "correct": correct, "total": len(MEDIUM_EMAILS), "score": score}
 
80
  ]
81
 
82
  def grade_hard_sorting(agent_fn=None):
 
 
 
 
83
  if agent_fn is None:
84
  agent_fn = baseline_agent
85
+ correct = sum(1 for e in HARD_EMAILS if agent_fn(e) == e["label"])
 
 
 
 
86
  raw = correct / len(HARD_EMAILS)
87
  score = round(min(0.99, max(0.01, raw)), 2)
88
  return {"task": "hard_sorting", "correct": correct, "total": len(HARD_EMAILS), "score": score}
89
 
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  # ============================================
92
  # RUN ALL GRADERS
93
  # ============================================
 
96
  if agent_fn is None:
97
  agent_fn = baseline_agent
98
 
99
+ results = [
100
+ grade_easy_sorting(agent_fn),
101
+ grade_medium_sorting(agent_fn),
102
+ grade_hard_sorting(agent_fn),
103
+ ]
104
 
105
  avg_score = round(sum(r["score"] for r in results) / len(results), 4)
 
 
106
  print(f"[GRADER] easy={results[0]['score']} medium={results[1]['score']} hard={results[2]['score']} avg={avg_score}", flush=True)
107
 
108
  return {
109
  "tasks": results,
110
  "average_score": avg_score,
111
+ "all_passed": all(0 < r["score"] < 1 for r in results)
112
  }
113
 
114
 
 
116
  print("Running all graders with baseline agent...\n")
117
  results = run_all_graders()
118
  for r in results["tasks"]:
119
+ print(f"Task: {r['task']:15s} | Score: {r['score']} | {r['correct']}/{r['total']} correct")
120
  print(f"\nAverage Score: {results['average_score']}")