Spaces:
Sleeping
Sleeping
Update env.py
Browse files
env.py
CHANGED
|
@@ -1,77 +1,83 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
from typing import Optional
|
| 3 |
-
from types import SimpleNamespace
|
| 4 |
-
from openenv.core.env_server import Environment
|
| 5 |
-
from models import MyEnvV4Observation, MyEnvV4Action
|
| 6 |
-
|
| 7 |
-
class MyEnvV4Env(Environment):
|
| 8 |
-
def __init__(self):
|
| 9 |
-
super().__init__()
|
| 10 |
-
# Realistic Dataset with Digital Seduction/Phishing markers
|
| 11 |
-
self.dataset = [
|
| 12 |
-
{
|
| 13 |
-
"sender": "dean.office@manipal.edu",
|
| 14 |
-
"subject": "B.Tech Lab Exam Schedule",
|
| 15 |
-
"body": "Please find the attached PDF for the upcoming CSE lab exams.",
|
| 16 |
-
"headers": ["SPF: Pass", "DKIM: Pass"],
|
| 17 |
-
"label": "INBOX"
|
| 18 |
-
},
|
| 19 |
-
{
|
| 20 |
-
"sender": "verify-account@security-amazon.net",
|
| 21 |
-
"subject": "Urgent: Your account is locked!",
|
| 22 |
-
"body": "Digital Seduction Alert: High urgency used. Click http://bit.ly/fake-link to unlock.",
|
| 23 |
-
"headers": ["SPF: Fail", "DMARC: Fail"],
|
| 24 |
-
"label": "QUARANTINE"
|
| 25 |
-
},
|
| 26 |
-
{
|
| 27 |
-
"sender": "prize@lottery-winner.co",
|
| 28 |
-
"subject": "Congratulations! You won $10,000",
|
| 29 |
-
"body": "Reply with your bank details to claim your cash prize immediately.",
|
| 30 |
-
"headers": ["SPF: Neutral"],
|
| 31 |
-
"label": "SPAM"
|
| 32 |
-
}
|
| 33 |
-
]
|
| 34 |
-
self.current_step = 0
|
| 35 |
-
|
| 36 |
-
async def reset(self):
|
| 37 |
-
self.current_step = 0
|
| 38 |
-
return self._get_result()
|
| 39 |
-
|
| 40 |
-
def _get_result(self, reward=0.0, done=False):
|
| 41 |
-
if self.current_step >= len(self.dataset):
|
| 42 |
-
obs = MyEnvV4Observation(
|
| 43 |
-
sender="N/A", subject="N/A", body="N/A",
|
| 44 |
-
headers=[], echoed_message="End of Data"
|
| 45 |
-
)
|
| 46 |
-
return SimpleNamespace(observation=obs, reward=reward, done=True)
|
| 47 |
-
|
| 48 |
-
data = self.dataset[self.current_step]
|
| 49 |
-
obs = MyEnvV4Observation(
|
| 50 |
-
sender=data["sender"],
|
| 51 |
-
subject=data["subject"],
|
| 52 |
-
body=data["body"],
|
| 53 |
-
headers=data["headers"],
|
| 54 |
-
echoed_message=f"Step {self.current_step + 1}"
|
| 55 |
-
)
|
| 56 |
-
return SimpleNamespace(observation=obs, reward=reward, done=done)
|
| 57 |
-
|
| 58 |
-
async def step(self, action: MyEnvV4Action):
|
| 59 |
-
if self.current_step >= len(self.dataset):
|
| 60 |
-
return self._get_result(done=True)
|
| 61 |
-
|
| 62 |
-
correct_label = self.dataset[self.current_step]["label"]
|
| 63 |
-
# Exact match reward logic for 0.0 - 1.0 range
|
| 64 |
-
reward = 1.0 if action.message.strip().upper() == correct_label else 0.0
|
| 65 |
-
|
| 66 |
-
self.current_step += 1
|
| 67 |
-
done = self.current_step >= len(self.dataset)
|
| 68 |
-
|
| 69 |
-
return self._get_result(reward=reward, done=done)
|
| 70 |
-
|
| 71 |
-
async def close(self):
|
| 72 |
-
pass
|
| 73 |
-
|
| 74 |
-
@classmethod
|
| 75 |
-
async def from_docker_image(cls, image_name: str):
|
| 76 |
-
"""Simulated helper for local/containerized runs."""
|
| 77 |
-
return cls()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from typing import Optional
|
| 3 |
+
from types import SimpleNamespace
|
| 4 |
+
from openenv.core.env_server import Environment
|
| 5 |
+
from models import MyEnvV4Observation, MyEnvV4Action
|
| 6 |
+
|
| 7 |
+
class MyEnvV4Env(Environment):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super().__init__()
|
| 10 |
+
# Realistic Dataset with Digital Seduction/Phishing markers
|
| 11 |
+
self.dataset = [
|
| 12 |
+
{
|
| 13 |
+
"sender": "dean.office@manipal.edu",
|
| 14 |
+
"subject": "B.Tech Lab Exam Schedule",
|
| 15 |
+
"body": "Please find the attached PDF for the upcoming CSE lab exams.",
|
| 16 |
+
"headers": ["SPF: Pass", "DKIM: Pass"],
|
| 17 |
+
"label": "INBOX"
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"sender": "verify-account@security-amazon.net",
|
| 21 |
+
"subject": "Urgent: Your account is locked!",
|
| 22 |
+
"body": "Digital Seduction Alert: High urgency used. Click http://bit.ly/fake-link to unlock.",
|
| 23 |
+
"headers": ["SPF: Fail", "DMARC: Fail"],
|
| 24 |
+
"label": "QUARANTINE"
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"sender": "prize@lottery-winner.co",
|
| 28 |
+
"subject": "Congratulations! You won $10,000",
|
| 29 |
+
"body": "Reply with your bank details to claim your cash prize immediately.",
|
| 30 |
+
"headers": ["SPF: Neutral"],
|
| 31 |
+
"label": "SPAM"
|
| 32 |
+
}
|
| 33 |
+
]
|
| 34 |
+
self.current_step = 0
|
| 35 |
+
|
| 36 |
+
async def reset(self):
|
| 37 |
+
self.current_step = 0
|
| 38 |
+
return self._get_result()
|
| 39 |
+
|
| 40 |
+
def _get_result(self, reward=0.0, done=False):
|
| 41 |
+
if self.current_step >= len(self.dataset):
|
| 42 |
+
obs = MyEnvV4Observation(
|
| 43 |
+
sender="N/A", subject="N/A", body="N/A",
|
| 44 |
+
headers=[], echoed_message="End of Data"
|
| 45 |
+
)
|
| 46 |
+
return SimpleNamespace(observation=obs, reward=reward, done=True)
|
| 47 |
+
|
| 48 |
+
data = self.dataset[self.current_step]
|
| 49 |
+
obs = MyEnvV4Observation(
|
| 50 |
+
sender=data["sender"],
|
| 51 |
+
subject=data["subject"],
|
| 52 |
+
body=data["body"],
|
| 53 |
+
headers=data["headers"],
|
| 54 |
+
echoed_message=f"Step {self.current_step + 1}"
|
| 55 |
+
)
|
| 56 |
+
return SimpleNamespace(observation=obs, reward=reward, done=done)
|
| 57 |
+
|
| 58 |
+
async def step(self, action: MyEnvV4Action):
|
| 59 |
+
if self.current_step >= len(self.dataset):
|
| 60 |
+
return self._get_result(done=True)
|
| 61 |
+
|
| 62 |
+
correct_label = self.dataset[self.current_step]["label"]
|
| 63 |
+
# Exact match reward logic for 0.0 - 1.0 range
|
| 64 |
+
reward = 1.0 if action.message.strip().upper() == correct_label else 0.0
|
| 65 |
+
|
| 66 |
+
self.current_step += 1
|
| 67 |
+
done = self.current_step >= len(self.dataset)
|
| 68 |
+
|
| 69 |
+
return self._get_result(reward=reward, done=done)
|
| 70 |
+
|
| 71 |
+
async def close(self):
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
@classmethod
|
| 75 |
+
async def from_docker_image(cls, image_name: str):
|
| 76 |
+
"""Simulated helper for local/containerized runs."""
|
| 77 |
+
return cls()
|
| 78 |
+
|
| 79 |
+
# Create the environment instance
|
| 80 |
+
env = MyEnvV4Env()
|
| 81 |
+
|
| 82 |
+
# This is the 'app' that Docker and Uvicorn are looking for
|
| 83 |
+
app = env.app
|