Update env.py
Browse files
env.py
CHANGED
|
@@ -2,9 +2,12 @@ import numpy as np
|
|
| 2 |
import gymnasium as gym
|
| 3 |
from gymnasium import spaces
|
| 4 |
|
| 5 |
-
# ------
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
KEYWORD_VOCAB = [
|
| 9 |
"invoice", "payment", "overdue", "refund", # billing
|
| 10 |
"hacked", "breach", "unauthorized", "password", # security
|
|
@@ -18,111 +21,76 @@ SENTIMENT_MAP = {"positive": 0, "neutral": 1, "negative": 2}
|
|
| 18 |
CONTEXT_MAP = {"spam": 0, "billing": 1, "tech": 2, "security": 3, "legal": 4}
|
| 19 |
OBS_DIM = len(KEYWORD_VOCAB) + len(SENTIMENT_MAP) + len(CONTEXT_MAP)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
{"id": "TC-H-01", "description": "IT password reset disguised", "keywords": ["password", "unauthorized", "critical", "urgent"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
|
| 23 |
-
{"id": "TC-H-02", "description": "Fake invoice portal redirect", "keywords": ["invoice", "payment", "password", "unauthorized"], "sentiment": "neutral", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
|
| 24 |
-
{"id": "TC-H-03", "description": "HR benefits credential capture", "keywords": ["password", "urgent", "critical"], "sentiment": "neutral", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
|
| 25 |
-
{"id": "TC-H-04", "description": "Account suspension notice", "keywords": ["unauthorized", "password", "breach", "urgent"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
|
| 26 |
-
{"id": "TC-H-09", "description": "Fake GDPR breach notice", "keywords": ["breach", "password", "legal", "threat"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
|
| 27 |
-
{"id": "TC-H-10", "description": "Ransomware / Compliance audit", "keywords": ["hacked", "breach", "lawsuit", "legal"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 2, 2)},
|
| 28 |
-
{"id": "TC-H-11", "description": "Extortion threat", "keywords": ["hacked", "breach", "attorney", "threat"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 2, 2)},
|
| 29 |
-
{"id": "TC-H-12", "description": "Law firm data misuse letter", "keywords": ["unauthorized", "breach", "attorney", "lawsuit"], "sentiment": "negative", "context": "legal", "difficulty": "hard", "correct_actions": (2, 2, 2)},
|
| 30 |
-
{"description": "Account hacked - urgent", "keywords": ["hacked", "unauthorized", "password"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
|
| 31 |
-
{"description": "Data breach notification", "keywords": ["breach", "unauthorized", "critical"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 1, 2)},
|
| 32 |
-
{"description": "Legal threat - lawsuit", "keywords": ["lawsuit", "legal", "attorney", "threat"], "sentiment": "negative", "context": "legal", "difficulty": "hard", "correct_actions": (2, 2, 2)},
|
| 33 |
-
{"description": "Ransomware threat", "keywords": ["hacked", "threat", "critical", "breach"], "sentiment": "negative", "context": "security", "difficulty": "hard", "correct_actions": (2, 2, 2)},
|
| 34 |
-
]
|
| 35 |
-
|
| 36 |
-
# ---------------------------------------------------------------------------
|
| 37 |
-
# Environment
|
| 38 |
-
# ---------------------------------------------------------------------------
|
| 39 |
class EmailTriageEnv(gym.Env):
|
| 40 |
-
def __init__(self, task="
|
| 41 |
super().__init__()
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
self.action_space = spaces.MultiDiscrete([3, 3, 3])
|
| 44 |
self.observation_space = spaces.Box(low=0.0, high=1.0, shape=(OBS_DIM,), dtype=np.float32)
|
| 45 |
self._step_idx = 0
|
| 46 |
|
| 47 |
def _encode(self, email):
|
| 48 |
-
kw_flags = np.array([1.0 if kw in email
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def reset(self, seed=None, options=None):
|
| 54 |
super().reset(seed=seed)
|
| 55 |
self._step_idx = 0
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def step(self, action):
|
| 59 |
email = self.email_batch[self._step_idx]
|
| 60 |
correct = email["correct_actions"]
|
|
|
|
|
|
|
| 61 |
reward = 0.0
|
| 62 |
-
|
| 63 |
-
# Critical: Penalty for missing Security Urgency
|
| 64 |
if correct[0] == 2 and action[0] != 2:
|
| 65 |
-
reward = -2.0
|
| 66 |
elif tuple(action) == correct:
|
| 67 |
reward = 1.0
|
| 68 |
elif action[0] == correct[0]:
|
| 69 |
reward = 0.2
|
| 70 |
|
| 71 |
self._step_idx += 1
|
| 72 |
-
|
| 73 |
-
obs = self._encode(email) if not done else np.zeros(OBS_DIM)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def predict(self, obs):
|
| 82 |
-
is_security_kw = any(obs[4:8]) # hacked, breach, unauthorized, password
|
| 83 |
-
is_legal_kw = any(obs[12:16]) # lawsuit, legal, attorney, sue
|
| 84 |
-
context_security = obs[28] == 1.0
|
| 85 |
-
context_legal = obs[29] == 1.0
|
| 86 |
-
|
| 87 |
-
# Default: General
|
| 88 |
-
urgency, routing, resolution = 0, 0, 0
|
| 89 |
-
|
| 90 |
-
# Rule 1: High Urgency for Security/Legal
|
| 91 |
-
if context_security or context_legal or is_security_kw:
|
| 92 |
-
urgency = 2 # Security Breach Level
|
| 93 |
-
resolution = 2 # Escalate to Human
|
| 94 |
-
|
| 95 |
-
# Rule 2: Routing Logic
|
| 96 |
-
# Agar Legal context hai ya specific keywords hain, toh Legal routing
|
| 97 |
-
if context_legal or is_legal_kw:
|
| 98 |
-
routing = 2 # Legal
|
| 99 |
-
else:
|
| 100 |
-
routing = 1 # Tech Support (Security/Phishing cases)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
agent = EmailGatekeeperAgent()
|
| 110 |
-
obs, info = env.reset()
|
| 111 |
-
|
| 112 |
-
total_reward = 0
|
| 113 |
-
print(f"{'Email Description':<40} | {'Agent Action':<12} | {'Reward'}")
|
| 114 |
-
print("-" * 70)
|
| 115 |
-
|
| 116 |
-
done = False
|
| 117 |
-
count = 0
|
| 118 |
-
while not done:
|
| 119 |
-
action = agent.predict(obs)
|
| 120 |
-
obs, reward, done, _, info = env.step(action)
|
| 121 |
-
total_reward += reward
|
| 122 |
-
count += 1
|
| 123 |
-
print(f"{info['description'][:38]:<40} | {str(action):<12} | {reward:>6}")
|
| 124 |
-
|
| 125 |
-
final_score = max(0, total_reward / count)
|
| 126 |
-
print("-" * 70)
|
| 127 |
-
print(f"TOTAL SCORE: {final_score:.3f}")
|
| 128 |
-
|
|
|
|
| 2 |
import gymnasium as gym
|
| 3 |
from gymnasium import spaces
|
| 4 |
|
| 5 |
+
# --- UI LABELS (Ye labels app.py mang raha hai) ---
|
| 6 |
+
URGENCY_LABELS = ["General", "Billing", "Security Breach"]
|
| 7 |
+
ROUTING_LABELS = ["AI Auto-Reply", "Tech Support", "Legal"]
|
| 8 |
+
RESOLUTION_LABELS = ["Archive", "Draft Reply", "Escalate to Human"]
|
| 9 |
+
|
| 10 |
+
# --- Vocabulary & Encoding Configuration ---
|
| 11 |
KEYWORD_VOCAB = [
|
| 12 |
"invoice", "payment", "overdue", "refund", # billing
|
| 13 |
"hacked", "breach", "unauthorized", "password", # security
|
|
|
|
| 21 |
CONTEXT_MAP = {"spam": 0, "billing": 1, "tech": 2, "security": 3, "legal": 4}
|
| 22 |
OBS_DIM = len(KEYWORD_VOCAB) + len(SENTIMENT_MAP) + len(CONTEXT_MAP)
|
| 23 |
|
| 24 |
+
# --- Environment Class ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
class EmailTriageEnv(gym.Env):
|
| 26 |
+
def __init__(self, task="all", batch=None, shuffle=True):
|
| 27 |
super().__init__()
|
| 28 |
+
|
| 29 |
+
# Dataset ko import karna (app.py se load hoga)
|
| 30 |
+
try:
|
| 31 |
+
from app import EMAIL_DATASET
|
| 32 |
+
dataset_to_use = EMAIL_DATASET
|
| 33 |
+
except ImportError:
|
| 34 |
+
dataset_to_use = [] # Fallback agar dataset na mile
|
| 35 |
+
|
| 36 |
+
if batch is not None:
|
| 37 |
+
self.email_batch = batch
|
| 38 |
+
elif task != "all":
|
| 39 |
+
self.email_batch = [e for e in dataset_to_use if e.get("difficulty") == task]
|
| 40 |
+
else:
|
| 41 |
+
self.email_batch = dataset_to_use
|
| 42 |
+
|
| 43 |
+
self.shuffle = shuffle
|
| 44 |
self.action_space = spaces.MultiDiscrete([3, 3, 3])
|
| 45 |
self.observation_space = spaces.Box(low=0.0, high=1.0, shape=(OBS_DIM,), dtype=np.float32)
|
| 46 |
self._step_idx = 0
|
| 47 |
|
| 48 |
def _encode(self, email):
|
| 49 |
+
kw_flags = np.array([1.0 if kw in email.get("keywords", []) else 0.0 for kw in KEYWORD_VOCAB])
|
| 50 |
+
sent_idx = SENTIMENT_MAP.get(email.get("sentiment", "neutral"), 1)
|
| 51 |
+
sentiment_vec = np.zeros(len(SENTIMENT_MAP)); sentiment_vec[sent_idx] = 1.0
|
| 52 |
+
|
| 53 |
+
ctx_idx = CONTEXT_MAP.get(email.get("context", "spam"), 0)
|
| 54 |
+
context_vec = np.zeros(len(CONTEXT_MAP)); context_vec[ctx_idx] = 1.0
|
| 55 |
+
|
| 56 |
+
return np.concatenate([kw_flags, sentiment_vec, context_vec]).astype(np.float32)
|
| 57 |
|
| 58 |
def reset(self, seed=None, options=None):
|
| 59 |
super().reset(seed=seed)
|
| 60 |
self._step_idx = 0
|
| 61 |
+
if not self.email_batch:
|
| 62 |
+
return np.zeros(OBS_DIM, dtype=np.float32), {}
|
| 63 |
+
|
| 64 |
+
obs = self._encode(self.email_batch[0])
|
| 65 |
+
return obs, {"description": self.email_batch[0].get("description", "")}
|
| 66 |
|
| 67 |
def step(self, action):
|
| 68 |
email = self.email_batch[self._step_idx]
|
| 69 |
correct = email["correct_actions"]
|
| 70 |
+
|
| 71 |
+
# Reward Logic (Score sudharne ke liye)
|
| 72 |
reward = 0.0
|
|
|
|
|
|
|
| 73 |
if correct[0] == 2 and action[0] != 2:
|
| 74 |
+
reward = -2.0 # Security missed penalty
|
| 75 |
elif tuple(action) == correct:
|
| 76 |
reward = 1.0
|
| 77 |
elif action[0] == correct[0]:
|
| 78 |
reward = 0.2
|
| 79 |
|
| 80 |
self._step_idx += 1
|
| 81 |
+
terminated = self._step_idx >= len(self.email_batch)
|
|
|
|
| 82 |
|
| 83 |
+
# Next observation
|
| 84 |
+
if not terminated:
|
| 85 |
+
next_email = self.email_batch[self._step_idx]
|
| 86 |
+
obs = self._encode(next_email)
|
| 87 |
+
else:
|
| 88 |
+
obs = self._encode(email)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
info = {
|
| 91 |
+
"description": email.get("description", ""),
|
| 92 |
+
"correct_actions": correct,
|
| 93 |
+
"raw_reward": reward
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
return obs, float(reward), terminated, False, info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|