-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui.py
More file actions
264 lines (217 loc) · 8.82 KB
/
Copy pathui.py
File metadata and controls
264 lines (217 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""Minimal Gradio UI for the EETRE OpenEnv environment.
Designed for HF Space judges:
- Click "Run Episode" -> watch the agent process emails live.
- Each cycle prints: Reset -> Email -> Reasoning -> Decision -> Reward -> Next.
- Talks to the OpenEnv-compliant FastAPI endpoints (`/reset`, `/step`, `/health`)
so the UI itself is just another OpenEnv client.
Mounted at `/ui` by `app.py`.
"""
from __future__ import annotations
import os
import time
from typing import Generator, List
import gradio as gr
import requests
BASE_URL = os.environ.get("EETRE_BASE_URL", "http://localhost:7860")
PUBLIC_URL = "https://Vetri17-openenv-email-triage-benchmark.hf.space"
VALID_ACTIONS = ["mark_spam", "escalate", "reply", "archive"]
PHISHING_KEYWORDS = ("credentials", "click now", "click here", "prize", "gift", "winner", "lottery")
TECHNICAL_KEYWORDS = ("timeout", "failed", "error", "production", "down", "outage", "5xx")
NEWSLETTER_KEYWORDS = ("unsubscribe", "digest", "newsletter", "weekly", "subscribe")
SECURITY_KEYWORDS = ("oauth", "security", "suspicious", "unusual", "breach", "mfa")
def _decide_action(email: dict) -> str:
body = (email.get("body") or "").lower()
subject = (email.get("subject") or "").lower()
text = f"{subject} {body}"
if any(w in text for w in PHISHING_KEYWORDS):
return "mark_spam"
if any(w in text for w in TECHNICAL_KEYWORDS):
return "reply"
if any(w in text for w in NEWSLETTER_KEYWORDS):
return "archive"
if any(w in text for w in SECURITY_KEYWORDS):
return "escalate"
if email.get("priority") in ("high", "critical"):
return "escalate"
return "reply"
def _reasoning_signals(email: dict) -> List[str]:
body = (email.get("body") or "").lower()
subject = (email.get("subject") or "").lower()
text = f"{subject} {body}"
signals: List[str] = []
if any(w in text for w in PHISHING_KEYWORDS):
signals.append("phishing signal detected")
if any(w in text for w in TECHNICAL_KEYWORDS):
signals.append("technical issue detected")
if any(w in text for w in NEWSLETTER_KEYWORDS):
signals.append("newsletter / low priority")
if any(w in text for w in SECURITY_KEYWORDS):
signals.append("security alert detected")
if email.get("priority") in ("high", "critical"):
signals.append(f"priority={email.get('priority')}")
if not signals:
signals.append("standard email")
return signals
def check_health() -> str:
try:
res = requests.get(f"{BASE_URL}/health", timeout=10).json()
return f"OK - environment status: {res.get('status', 'unknown')} ({BASE_URL})"
except Exception as exc:
return f"OFFLINE - {exc}"
def run_episode(task_id: str, pacing: float) -> Generator[str, None, None]:
"""Stream the episode line-by-line so judges see it run live."""
pacing = max(0.0, min(float(pacing or 0.0), 2.0))
logs: List[str] = []
def push(line: str = "") -> str:
logs.append(line)
return "\n".join(logs)
yield push("=" * 60)
yield push(f"TASK: {task_id.upper()}")
yield push("=" * 60)
yield push("")
try:
res = requests.post(
f"{BASE_URL}/reset",
json={"task_id": task_id},
timeout=20,
).json()
obs = res["observation"]
except Exception as exc:
yield push(f"Reset failed: {exc}")
return
yield push(f"Objective : {obs.get('objective', '')}")
yield push(f"Difficulty: {obs.get('difficulty', task_id)}")
yield push(f"Inbox size: {len(obs.get('inbox', []))}")
yield push(f"Max steps : {obs.get('max_steps', '?')}")
yield push("-" * 60)
total_reward = 0.0
inbox = obs.get("inbox", [])
for idx, email in enumerate(inbox, start=1):
if pacing:
time.sleep(pacing)
yield push("")
yield push(f"[{idx}/{len(inbox)}] EMAIL")
yield push(f" id : {email.get('id')}")
yield push(f" from : {email.get('sender', '')}")
yield push(f" subject : {email.get('subject', '')}")
yield push(f" priority: {email.get('priority', 'normal')}")
body_preview = (email.get("body") or "").replace("\n", " ")[:140]
yield push(f" body : {body_preview}...")
if pacing:
time.sleep(pacing)
yield push("")
yield push(" REASONING AGENT")
for signal in _reasoning_signals(email):
yield push(f" -> {signal}")
action = _decide_action(email)
if pacing:
time.sleep(pacing)
yield push("")
yield push(f" DECISION AGENT: {action.upper()}")
try:
step_res = requests.post(
f"{BASE_URL}/step",
json={
"email_id": email["id"],
"action_type": action,
"response": "Automated response",
},
timeout=20,
).json()
reward = step_res.get("reward", {}) or {}
score = float(reward.get("score", 0.0) or 0.0)
total_reward += score
yield push("")
yield push(" EVALUATOR AGENT")
yield push(f" score : {score:.3f}")
yield push(f" correct : {reward.get('action_correctness', 0.0):.3f}")
yield push(f" quality : {reward.get('response_quality', 0.0):.3f}")
feedback = reward.get("feedback")
if feedback:
yield push(f" note : {feedback}")
yield push(f" done : {step_res.get('done', False)}")
if step_res.get("done"):
yield push("")
yield push(" environment signalled DONE")
yield push("-" * 60)
break
except Exception as exc:
yield push(f" step error: {exc}")
yield push("-" * 60)
avg = total_reward / max(len(inbox), 1)
yield push("")
yield push("=" * 60)
yield push("EPISODE COMPLETE")
yield push(f" emails processed : {len(inbox)}")
yield push(f" total reward : {total_reward:.3f}")
yield push(f" avg reward : {avg:.3f}")
yield push("=" * 60)
with gr.Blocks(title="EETRE - Email Triage RL Environment") as demo:
gr.Markdown(
"""
# EETRE - Enterprise Email Triage & Response Environment
**OpenEnv-compliant RL environment** · 3-Agent System · Live Slack/SMTP Integration
| | |
|---|---|
| HF Space | https://huggingface.co/spaces/Vetri17/openenv-email-triage-benchmark |
| Colab | https://colab.research.google.com/drive/1s7hBuQe93gA1yzKJ0_tcNauOEFeq4yEF |
| GitHub | https://github.com/Vetri1706/openenv-email-triage-benchmark |
| Blog | See `Blog.md` in the Space repo |
Endpoints exercised by this UI: `POST /reset`, `POST /step`, `GET /health`.
"""
)
gr.Markdown("---")
with gr.Row():
health_btn = gr.Button("Check environment health", variant="secondary")
health_out = gr.Textbox(label="Status", lines=1, interactive=False)
health_btn.click(check_health, outputs=health_out)
gr.Markdown("---")
gr.Markdown("## Run Episode")
gr.Markdown(
"Pick a task, then watch the 3-agent loop process each email in real time. "
"Every cycle prints the email, reasoning, decision, and reward — exactly what "
"the environment returns over `/reset` and `/step`."
)
with gr.Row():
task_dropdown = gr.Dropdown(
choices=["easy", "medium", "hard"],
value="medium",
label="Task difficulty",
info="easy = basic support | medium = mixed inbox | hard = phishing + escalation",
)
pacing_slider = gr.Slider(
minimum=0.0,
maximum=1.5,
value=0.4,
step=0.1,
label="Pacing (seconds between agent steps)",
info="Set to 0 for max speed; ~0.4s reads nicely for a live demo.",
)
run_btn = gr.Button("Run episode", variant="primary", scale=2)
logs_output = gr.Textbox(
label="Live agent logs",
lines=32,
max_lines=60,
placeholder="Click 'Run episode' to see the agent process emails in real time...",
)
run_btn.click(
fn=run_episode,
inputs=[task_dropdown, pacing_slider],
outputs=logs_output,
)
gr.Markdown("---")
gr.Markdown(
"""
## Training Results
- SFT Loss: 37.5 -> 18.1 (51% reduction)
- GRPO Reward: 0.608 -> 0.833 (37% improvement)
- Model: Qwen2.5-0.5B + LoRA r=16 via Unsloth
- Data: 180 simulated + 11 live Gmail emails
## Architecture
```
Reasoning Agent -> Decision Agent -> Auditor -> Evaluator -> Slack/SMTP
```
"""
)
if __name__ == "__main__":
demo.launch()