Omkar1806 commited on
Commit
8a137ab
·
verified ·
1 Parent(s): ff7bec7

Delete environment.py

Browse files
Files changed (1) hide show
  1. environment.py +0 -501
environment.py DELETED
@@ -1,501 +0,0 @@
1
- """
2
- EmailTriageEnv — Intelligent Email Gatekeeper RL Environment
3
- ============================================================
4
- Observation : flat Box vector encoding keywords, sentiment, and context.
5
- Action : MultiDiscrete([3, 3, 3])
6
- [0] Urgency — 0=General, 1=Billing, 2=Security Breach
7
- [1] Routing — 0=AI Auto-Reply, 1=Tech Support, 2=Legal
8
- [2] Resolution— 0=Archive, 1=Draft Reply, 2=Escalate to Human
9
- Reward : +0.5 fully correct | -0.4 wrong priority on crisis email
10
- Cumulative ideal score over one episode ≈ 1.0 (normalised).
11
- Episode : one email per step; done after all emails in the batch.
12
- """
13
-
14
- import numpy as np
15
- import gymnasium as gym
16
- from gymnasium import spaces
17
-
18
- # ---------------------------------------------------------------------------
19
- # Vocabulary & encoding helpers
20
- # ---------------------------------------------------------------------------
21
-
22
- # Fixed keyword vocabulary — extend freely.
23
- KEYWORD_VOCAB = [
24
- "invoice", "payment", "overdue", "refund", # billing
25
- "hacked", "breach", "unauthorized", "password", # security
26
- "crash", "error", "bug", "slow", # tech
27
- "lawsuit", "legal", "attorney", "sue", # legal
28
- "spam", "offer", "win", "free", # spam
29
- "urgent", "critical", "angry", "threat", # sentiment signals
30
- ]
31
-
32
- # Sentiment classes → index
33
- SENTIMENT_MAP = {"positive": 0, "neutral": 1, "negative": 2}
34
-
35
- # Context classes → index
36
- CONTEXT_MAP = {"spam": 0, "billing": 1, "tech": 2, "security": 3, "legal": 4}
37
-
38
- # Observation vector length: keyword flags + one-hot sentiment + one-hot context
39
- OBS_DIM = len(KEYWORD_VOCAB) + len(SENTIMENT_MAP) + len(CONTEXT_MAP)
40
-
41
- # ---------------------------------------------------------------------------
42
- # Mock email dataset
43
- # ---------------------------------------------------------------------------
44
- # Each entry:
45
- # keywords : list[str] — subset of KEYWORD_VOCAB
46
- # sentiment : str — key in SENTIMENT_MAP
47
- # context : str — key in CONTEXT_MAP
48
- # difficulty : str — "easy" | "medium" | "hard"
49
- # correct_actions: tuple — (urgency, routing, resolution)
50
- # description : str — human-readable label (for debugging)
51
-
52
- EMAIL_DATASET = [
53
- # ── Easy: Spam vs Real ──────────────────────────────────────────────────
54
- {
55
- "description": "Spam promo",
56
- "keywords": ["spam", "offer", "win", "free"],
57
- "sentiment": "positive",
58
- "context": "spam",
59
- "difficulty": "easy",
60
- "correct_actions": (0, 0, 0), # General | AI Auto-Reply | Archive
61
- },
62
- {
63
- "description": "Spam lottery",
64
- "keywords": ["free", "win", "offer"],
65
- "sentiment": "positive",
66
- "context": "spam",
67
- "difficulty": "easy",
68
- "correct_actions": (0, 0, 0),
69
- },
70
- {
71
- "description": "Routine support request",
72
- "keywords": ["slow", "error"],
73
- "sentiment": "neutral",
74
- "context": "tech",
75
- "difficulty": "easy",
76
- "correct_actions": (0, 1, 1), # General | Tech Support | Draft Reply
77
- },
78
- {
79
- "description": "General billing inquiry",
80
- "keywords": ["invoice", "payment"],
81
- "sentiment": "neutral",
82
- "context": "billing",
83
- "difficulty": "easy",
84
- "correct_actions": (1, 0, 1), # Billing | AI Auto-Reply | Draft Reply
85
- },
86
- # ── Medium: Billing / Tech context ──────────────────────────────────────
87
- {
88
- "description": "Overdue invoice complaint",
89
- "keywords": ["invoice", "overdue", "payment", "angry"],
90
- "sentiment": "negative",
91
- "context": "billing",
92
- "difficulty": "medium",
93
- "correct_actions": (1, 0, 1), # Billing | AI Auto-Reply | Draft Reply
94
- },
95
- {
96
- "description": "Refund dispute",
97
- "keywords": ["refund", "payment", "angry"],
98
- "sentiment": "negative",
99
- "context": "billing",
100
- "difficulty": "medium",
101
- "correct_actions": (1, 2, 2), # Billing | Legal | Escalate
102
- },
103
- {
104
- "description": "App crash report",
105
- "keywords": ["crash", "bug", "error"],
106
- "sentiment": "negative",
107
- "context": "tech",
108
- "difficulty": "medium",
109
- "correct_actions": (0, 1, 1), # General | Tech Support | Draft Reply
110
- },
111
- {
112
- "description": "Persistent login bug",
113
- "keywords": ["bug", "password", "error"],
114
- "sentiment": "negative",
115
- "context": "tech",
116
- "difficulty": "medium",
117
- "correct_actions": (0, 1, 1),
118
- },
119
- # ── Medium: Passive-Aggressive Legal Threats ────────────────────────────
120
- {
121
- "id": "TC-M-01",
122
- "description": "Polite refund ultimatum with implied legal action",
123
- "keywords": ["refund", "legal", "angry", "threat"],
124
- "sentiment": "negative",
125
- "context": "legal",
126
- "difficulty": "medium",
127
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
128
- },
129
- {
130
- "id": "TC-M-02",
131
- "description": "Overdue invoice with attorney CC warning",
132
- "keywords": ["invoice", "overdue", "attorney", "legal", "payment", "threat"],
133
- "sentiment": "negative",
134
- "context": "legal",
135
- "difficulty": "medium",
136
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
137
- },
138
- {
139
- "id": "TC-M-03",
140
- "description": "Disappointed customer implying regulatory complaint",
141
- "keywords": ["angry", "threat", "legal"],
142
- "sentiment": "negative",
143
- "context": "legal",
144
- "difficulty": "medium",
145
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
146
- },
147
- {
148
- "id": "TC-M-04",
149
- "description": "SLA breach complaint with legal remedy language",
150
- "keywords": ["breach", "legal", "threat", "angry"],
151
- "sentiment": "negative",
152
- "context": "legal",
153
- "difficulty": "medium",
154
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
155
- },
156
- {
157
- "id": "TC-M-05",
158
- "description": "Billing dispute with small claims court mention",
159
- "keywords": ["refund", "payment", "angry", "lawsuit", "urgent"],
160
- "sentiment": "negative",
161
- "context": "legal",
162
- "difficulty": "medium",
163
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
164
- },
165
- {
166
- "id": "TC-M-06",
167
- "description": "GDPR deletion request with legal consequence warning",
168
- "keywords": ["legal", "breach", "threat", "angry"],
169
- "sentiment": "negative",
170
- "context": "legal",
171
- "difficulty": "medium",
172
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
173
- },
174
- {
175
- "id": "TC-M-07",
176
- "description": "Subscription cancellation with chargeback and CFPB threat",
177
- "keywords": ["payment", "refund", "angry", "threat", "legal"],
178
- "sentiment": "negative",
179
- "context": "legal",
180
- "difficulty": "medium",
181
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
182
- },
183
- {
184
- "id": "TC-M-08",
185
- "description": "Vendor threatening IP infringement claim",
186
- "keywords": ["unauthorized", "legal", "attorney", "threat"],
187
- "sentiment": "negative",
188
- "context": "legal",
189
- "difficulty": "medium",
190
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
191
- },
192
- # ── Hard: Subtle Phishing Attempts ──────────────────────────────────────
193
- {
194
- "id": "TC-H-01",
195
- "description": "IT password reset disguised as routine security audit",
196
- "keywords": ["password", "unauthorized", "critical", "urgent", "threat"],
197
- "sentiment": "negative",
198
- "context": "security",
199
- "difficulty": "hard",
200
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
201
- },
202
- {
203
- "id": "TC-H-02",
204
- "description": "Fake invoice payment portal redirect — credential harvest",
205
- "keywords": ["invoice", "payment", "password", "unauthorized", "urgent"],
206
- "sentiment": "neutral",
207
- "context": "security",
208
- "difficulty": "hard",
209
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
210
- },
211
- {
212
- "id": "TC-H-03",
213
- "description": "HR benefits enrollment with credential capture",
214
- "keywords": ["password", "urgent", "critical"],
215
- "sentiment": "neutral",
216
- "context": "security",
217
- "difficulty": "hard",
218
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
219
- },
220
- {
221
- "id": "TC-H-04",
222
- "description": "Fake account suspension notice with login link",
223
- "keywords": ["unauthorized", "password", "breach", "urgent", "threat"],
224
- "sentiment": "negative",
225
- "context": "security",
226
- "difficulty": "hard",
227
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
228
- },
229
- {
230
- "id": "TC-H-05",
231
- "description": "Vendor onboarding BEC — admin credentials via reply",
232
- "keywords": ["password", "unauthorized", "urgent"],
233
- "sentiment": "neutral",
234
- "context": "security",
235
- "difficulty": "hard",
236
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
237
- },
238
- {
239
- "id": "TC-H-06",
240
- "description": "Fake new sign-in alert — was this you? phish",
241
- "keywords": ["unauthorized", "password", "hacked", "breach", "urgent"],
242
- "sentiment": "negative",
243
- "context": "security",
244
- "difficulty": "hard",
245
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
246
- },
247
- {
248
- "id": "TC-H-07",
249
- "description": "Payroll migration phish — salary interruption fear",
250
- "keywords": ["payment", "password", "urgent", "threat"],
251
- "sentiment": "negative",
252
- "context": "security",
253
- "difficulty": "hard",
254
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
255
- },
256
- {
257
- "id": "TC-H-08",
258
- "description": "Software license renewal — admin credential request",
259
- "keywords": ["password", "critical", "urgent", "error"],
260
- "sentiment": "neutral",
261
- "context": "security",
262
- "difficulty": "hard",
263
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
264
- },
265
- # ── Hard: Phishing + Legal Threat Overlay ───────────────────────────────
266
- {
267
- "id": "TC-H-09",
268
- "description": "Fake GDPR breach notice — credential harvest via legal fear",
269
- "keywords": ["breach", "hacked", "password", "legal", "threat", "urgent", "unauthorized"],
270
- "sentiment": "negative",
271
- "context": "security",
272
- "difficulty": "hard",
273
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
274
- # NOTE: legal language is the deception layer; credential harvest = Tech Support
275
- },
276
- {
277
- "id": "TC-H-10",
278
- "description": "Ransomware disguised as software compliance audit",
279
- "keywords": ["hacked", "breach", "unauthorized", "lawsuit", "legal", "threat", "critical", "urgent"],
280
- "sentiment": "negative",
281
- "context": "security",
282
- "difficulty": "hard",
283
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
284
- },
285
- {
286
- "id": "TC-H-11",
287
- "description": "Extortion — threatening to publish stolen data",
288
- "keywords": ["hacked", "breach", "unauthorized", "attorney", "threat", "critical", "urgent"],
289
- "sentiment": "negative",
290
- "context": "security",
291
- "difficulty": "hard",
292
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
293
- },
294
- {
295
- "id": "TC-H-12",
296
- "description": "Fake law firm letter claiming evidence of data misuse",
297
- "keywords": ["unauthorized", "breach", "attorney", "lawsuit", "legal", "threat"],
298
- "sentiment": "negative",
299
- "context": "legal",
300
- "difficulty": "hard",
301
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
302
- },
303
- # ── Hard: Crisis / Security threats ─────────────────────────────────────
304
- {
305
- "description": "Account hacked — urgent",
306
- "keywords": ["hacked", "unauthorized", "password", "urgent", "angry"],
307
- "sentiment": "negative",
308
- "context": "security",
309
- "difficulty": "hard",
310
- "correct_actions": (2, 1, 2), # Security | Tech Support | Escalate
311
- },
312
- {
313
- "description": "Data breach notification",
314
- "keywords": ["breach", "unauthorized", "critical", "threat"],
315
- "sentiment": "negative",
316
- "context": "security",
317
- "difficulty": "hard",
318
- "correct_actions": (2, 1, 2),
319
- },
320
- {
321
- "description": "Legal threat — lawsuit",
322
- "keywords": ["lawsuit", "legal", "attorney", "threat", "angry"],
323
- "sentiment": "negative",
324
- "context": "legal",
325
- "difficulty": "hard",
326
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
327
- },
328
- {
329
- "description": "Ransomware / extortion threat",
330
- "keywords": ["hacked", "threat", "critical", "urgent", "breach"],
331
- "sentiment": "negative",
332
- "context": "security",
333
- "difficulty": "hard",
334
- "correct_actions": (2, 2, 2), # Security | Legal | Escalate
335
- },
336
- ]
337
-
338
- # ---------------------------------------------------------------------------
339
- # Reward weights — adjust freely
340
- # ---------------------------------------------------------------------------
341
- REWARD_CORRECT_FULL = 1.0 # teeno actions bilkul sahi
342
- REWARD_PARTIAL_ONE_WRONG = 0.2 # urgency sahi + sirf ek dimension galat
343
- REWARD_PARTIAL_BOTH_WRONG= 0.1 # urgency sahi lekin routing aur resolution dono galat
344
- PENALTY_MISSED_SECURITY = -2.0 # security breach email miss kiya
345
-
346
-
347
- # ---------------------------------------------------------------------------
348
- # Environment
349
- # ---------------------------------------------------------------------------
350
-
351
- class EmailTriageEnv(gym.Env):
352
- """
353
- Single-email-per-step triage environment.
354
-
355
- Parameters
356
- ----------
357
- batch : list[dict] | None
358
- Custom email batch. Defaults to the full EMAIL_DATASET.
359
- shuffle : bool
360
- Shuffle the batch on each reset (default True).
361
- """
362
-
363
- metadata = {"render_modes": ["human"]}
364
-
365
- def __init__(self, batch: list | None = None, shuffle: bool = True):
366
- super().__init__()
367
-
368
- self.email_batch = batch if batch is not None else EMAIL_DATASET
369
- self.shuffle = shuffle
370
-
371
- # Action space: [urgency(3), routing(3), resolution(3)]
372
- self.action_space = spaces.MultiDiscrete([3, 3, 3])
373
-
374
- # Observation space: binary keyword flags + one-hot sentiment + one-hot context
375
- self.observation_space = spaces.Box(
376
- low=0.0, high=1.0, shape=(OBS_DIM,), dtype=np.float32
377
- )
378
-
379
- # Internal state (populated by reset)
380
- self._queue: list[dict] = []
381
- self._current_email: dict = {}
382
- self._step_idx: int = 0
383
-
384
- # Normalisation constant: max possible reward per episode
385
- self._max_episode_reward = len(self.email_batch) * REWARD_CORRECT_FULL
386
-
387
- # ------------------------------------------------------------------
388
- # Helpers
389
- # ------------------------------------------------------------------
390
-
391
- def _encode(self, email: dict) -> np.ndarray:
392
- """Convert an email dict into a flat float32 observation vector."""
393
- # Keyword flags (binary)
394
- kw_flags = np.array(
395
- [1.0 if kw in email["keywords"] else 0.0 for kw in KEYWORD_VOCAB],
396
- dtype=np.float32,
397
- )
398
- # One-hot sentiment
399
- sentiment_vec = np.zeros(len(SENTIMENT_MAP), dtype=np.float32)
400
- sentiment_vec[SENTIMENT_MAP[email["sentiment"]]] = 1.0
401
-
402
- # One-hot context
403
- context_vec = np.zeros(len(CONTEXT_MAP), dtype=np.float32)
404
- context_vec[CONTEXT_MAP[email["context"]]] = 1.0
405
-
406
- return np.concatenate([kw_flags, sentiment_vec, context_vec])
407
-
408
- def _compute_reward(self, action: np.ndarray, email: dict) -> float:
409
- """
410
- Strict reward rules (priority order mein check hote hain):
411
- -2.0 — security breach email ko urgency=2 nahi diya (sabse bada penalty)
412
- +1.0 — teeno actions bilkul sahi (exact match)
413
- +0.2 — sirf urgency sahi, routing AUR resolution dono sahi (2 out of 3)
414
- +0.1 — sirf urgency sahi, baaki ek galat (1 dimension wrong)
415
- 0.0 — urgency hi galat hai (non-security email)
416
- """
417
- urgency = int(action[0])
418
- routing = int(action[1])
419
- resolution = int(action[2])
420
- correct = email["correct_actions"]
421
-
422
- # Priority 1: Security breach miss — sabse bada crime
423
- if correct[0] == 2 and urgency != 2:
424
- return PENALTY_MISSED_SECURITY
425
-
426
- # Priority 2: Perfect match
427
- if (urgency, routing, resolution) == correct:
428
- return REWARD_CORRECT_FULL
429
-
430
- # Priority 3: Urgency sahi hai — partial credit
431
- if urgency == correct[0]:
432
- # Dono routing aur resolution galat hain
433
- routing_ok = (routing == correct[1])
434
- resolution_ok = (resolution == correct[2])
435
- if routing_ok and not resolution_ok:
436
- return REWARD_PARTIAL_ONE_WRONG # sirf resolution galat
437
- if resolution_ok and not routing_ok:
438
- return REWARD_PARTIAL_ONE_WRONG # sirf routing galat
439
- return REWARD_PARTIAL_BOTH_WRONG # dono galat
440
-
441
- return 0.0
442
-
443
- # ------------------------------------------------------------------
444
- # gymnasium API
445
- # ------------------------------------------------------------------
446
-
447
- def reset(self, *, seed: int | None = None, options: dict | None = None):
448
- super().reset(seed=seed)
449
-
450
- self._queue = list(self.email_batch)
451
- if self.shuffle:
452
- self.np_random.shuffle(self._queue) # uses gymnasium's seeded RNG
453
-
454
- self._step_idx = 0
455
- self._current_email = self._queue[self._step_idx]
456
-
457
- obs = self._encode(self._current_email)
458
- info = {"description": self._current_email["description"],
459
- "difficulty": self._current_email["difficulty"]}
460
- return obs, info
461
-
462
- def step(self, action: np.ndarray):
463
- """
464
- Process one email triage decision.
465
-
466
- Returns
467
- -------
468
- obs, reward, terminated, truncated, info
469
- """
470
- # ── Bug Fix: correct_actions PEHLE save karo, PHIR pointer badlao ──
471
- # Pehle current email ka ground truth capture karo reward ke saath
472
- scored_email = self._current_email
473
- reward = self._compute_reward(action, scored_email)
474
- normalised_reward = reward / self._max_episode_reward
475
-
476
- # Ab pointer aage badhao
477
- self._step_idx += 1
478
- terminated = self._step_idx >= len(self._queue)
479
-
480
- if not terminated:
481
- self._current_email = self._queue[self._step_idx]
482
- obs = self._encode(self._current_email)
483
- else:
484
- obs = self._encode(scored_email) # terminal step pe last obs return karo
485
-
486
- # info mein SCORED email ka data — agli email ka nahi
487
- info = {
488
- "raw_reward": reward,
489
- "correct_actions": scored_email["correct_actions"],
490
- "difficulty": scored_email["difficulty"],
491
- "description": scored_email.get("description", ""),
492
- }
493
- return obs, normalised_reward, terminated, False, info
494
-
495
- def render(self, mode: str = "human"):
496
- """Print current email details to stdout."""
497
- e = self._current_email
498
- print(f"[Step {self._step_idx}] {e['description']} "
499
- f"| difficulty={e['difficulty']} "
500
- f"| sentiment={e['sentiment']} "
501
- f"| context={e['context']}")