Omkar1806 commited on
Commit
cf0020c
·
verified ·
1 Parent(s): abcb1ff

Update env.py

Browse files
Files changed (1) hide show
  1. env.py +70 -47
env.py CHANGED
@@ -1,48 +1,71 @@
1
- import numpy as np
2
- import gymnasium as gym
3
- from gymnasium import spaces
4
-
5
- URGENCY_LABELS = ["General", "Billing", "Security Breach"]
6
- ROUTING_LABELS = ["AI Auto-Reply", "Tech Support", "Legal"]
7
- RESOLUTION_LABELS = ["Archive", "Draft Reply", "Escalate to Human"]
8
-
9
- class EmailTriageEnv(gym.Env):
10
- def __init__(self, task="all"):
11
- super().__init__()
12
- self.full_dataset = [
13
- {"difficulty": "easy", "description": "Spam promo", "correct_actions": (0, 0, 0)},
14
- {"difficulty": "easy", "description": "Routine support", "correct_actions": (0, 1, 1)},
15
- {"difficulty": "medium", "description": "Billing dispute", "correct_actions": (1, 2, 2)},
16
- {"difficulty": "medium", "description": "Refund request", "correct_actions": (1, 2, 2)},
17
- {"difficulty": "hard", "description": "IT password reset phish", "correct_actions": (2, 1, 2)},
18
- {"difficulty": "hard", "description": "Ransomware threat", "correct_actions": (2, 2, 2)}
 
 
19
  ]
20
-
21
- if task != "all":
22
- self._queue = [e for e in self.full_dataset if e.get("difficulty") == task]
23
- else:
24
- self._queue = self.full_dataset
25
-
26
- self.action_space = spaces.MultiDiscrete([3, 3, 3])
27
- self.observation_space = spaces.Box(low=0.0, high=1.0, shape=(10,), dtype=np.float32)
28
- self._step_idx = 0
29
-
30
- def reset(self, seed=None, options=None):
31
- self._step_idx = 0
32
- return np.zeros(10, dtype=np.float32), {}
33
-
34
- def step(self, action):
35
- if self._step_idx >= len(self._queue):
36
- return np.zeros(10), 0.0, True, False, {}
37
-
38
- email = self._queue[self._step_idx]
39
- correct = email["correct_actions"]
40
-
41
- reward = 1.0 if tuple(action) == tuple(correct) else 0.0
42
- # Penalty for missing security threats
43
- if correct[0] == 2 and action[0] != 2:
44
- reward = -2.0
45
-
46
- self._step_idx += 1
47
- done = self._step_idx >= len(self._queue)
48
- return np.zeros(10), float(reward), done, False, {"raw_reward": reward}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Dict, Any, Tuple
2
+ import random
3
+
4
+ URGENCY_LABELS = ["low", "medium", "high"]
5
+ ROUTING_LABELS = ["general", "support", "security"]
6
+ RESOLUTION_LABELS = ["ignore", "respond", "escalate"]
7
+
8
+
9
+ class EmailTriageEnv:
10
+ def __init__(self, task: str = "easy"):
11
+ self.task = task
12
+ self._queue: List[Dict] = []
13
+ self._index = 0
14
+ self._done = False
15
+
16
+ def _generate_emails(self) -> List[Dict]:
17
+ emails = [
18
+ {"description": "Password reset not working", "label": [2, 1, 2]},
19
+ {"description": "Billing refund request", "label": [1, 2, 2]},
20
+ {"description": "App is slow and buggy", "label": [0, 1, 1]},
21
  ]
22
+
23
+ if self.task in ["medium", "hard"]:
24
+ emails += [
25
+ {"description": "Possible phishing attempt detected", "label": [2, 2, 2]},
26
+ {"description": "Invoice mismatch and payment issue", "label": [1, 2, 2]},
27
+ ]
28
+
29
+ if self.task == "hard":
30
+ emails += [
31
+ {"description": "Ransomware attack suspected on system", "label": [2, 2, 2]},
32
+ {"description": "User reports data breach and performance issues", "label": [2, 2, 2]},
33
+ ]
34
+
35
+ random.shuffle(emails)
36
+ return emails
37
+
38
+ def reset(self) -> Dict[str, Any]:
39
+ self._queue = self._generate_emails()
40
+ self._index = 0
41
+ self._done = False
42
+ return self.state()
43
+
44
+ def state(self) -> Dict[str, Any]:
45
+ if self._done:
46
+ return {"done": True}
47
+
48
+ current = self._queue[self._index]
49
+ return {
50
+ "description": current["description"],
51
+ "step": self._index,
52
+ "remaining": len(self._queue) - self._index,
53
+ }
54
+
55
+ def step(self, action: List[int]) -> Tuple[Dict, float, bool, Dict, Dict]:
56
+ if self._done:
57
+ return self.state(), 0.0, True, {}, {}
58
+
59
+ correct = self._queue[self._index]["label"]
60
+
61
+ reward = sum(
62
+ 1.0 if a == b else 0.0
63
+ for a, b in zip(action, correct)
64
+ ) / 3.0
65
+
66
+ self._index += 1
67
+
68
+ if self._index >= len(self._queue):
69
+ self._done = True
70
+
71
+ return self.state(), reward, self._done, {}, {}