Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,20 @@ import uvicorn
|
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
from fastapi import FastAPI
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
EMAIL_DATASET = [
|
| 12 |
-
{"difficulty": "easy", "description": "Spam promo", "correct_actions": (0, 0, 0)},
|
| 13 |
-
{"difficulty": "easy", "description": "Routine support", "correct_actions": (0, 1, 1)},
|
| 14 |
-
{"difficulty": "hard", "description": "IT password reset phish", "correct_actions": (2, 1, 2)},
|
| 15 |
-
{"difficulty": "hard", "description": "Ransomware threat", "correct_actions": (2, 2, 2)},
|
| 16 |
-
{"difficulty": "hard", "description": "Fake GDPR notice", "correct_actions": (2, 1, 2)},
|
| 17 |
-
]
|
| 18 |
-
|
| 19 |
-
# Labels from env
|
| 20 |
-
URGENCY_LABELS = ["General", "Billing", "Security Breach"]
|
| 21 |
-
ROUTING_LABELS = ["AI Auto-Reply", "Tech Support", "Legal"]
|
| 22 |
-
RESOLUTION_LABELS = ["Archive", "Draft Reply", "Escalate to Human"]
|
| 23 |
-
|
| 24 |
-
from env import EmailTriageEnv
|
| 25 |
-
|
| 26 |
-
def classify_logic(desc):
|
| 27 |
desc = desc.lower()
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
return [2, 1, 2]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return [0, 0, 0]
|
| 32 |
|
| 33 |
def run_demo(task):
|
|
@@ -37,25 +27,34 @@ def run_demo(task):
|
|
| 37 |
results = []
|
| 38 |
total_reward = 0
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
for i, email in enumerate(env._queue):
|
| 41 |
-
action =
|
| 42 |
_, reward, _, _, info = env.step(action)
|
| 43 |
total_reward += reward
|
| 44 |
|
| 45 |
status = "✅ MATCH" if reward >= 1.0 else "❌ MISMATCH"
|
| 46 |
-
results.append(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"Error: {str(e)}"
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
gr.Markdown("# 📧 Email Gatekeeper AI")
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
|
|
|
| 59 |
btn.click(run_demo, inputs=diff, outputs=out)
|
| 60 |
|
| 61 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
from fastapi import FastAPI
|
| 7 |
+
from env import EmailTriageEnv, URGENCY_LABELS, ROUTING_LABELS, RESOLUTION_LABELS
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
def smart_agent_logic(desc):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
desc = desc.lower()
|
| 13 |
+
# Hard logic
|
| 14 |
+
if any(x in desc for x in ["password", "hacked", "breach", "phish", "ransomware", "threat"]):
|
| 15 |
+
if "threat" in desc or "ransomware" in desc: return [2, 2, 2]
|
| 16 |
return [2, 1, 2]
|
| 17 |
+
# Medium logic (Billing)
|
| 18 |
+
if any(x in desc for x in ["billing", "refund", "dispute", "invoice"]):
|
| 19 |
+
return [1, 2, 2]
|
| 20 |
+
# Easy logic
|
| 21 |
return [0, 0, 0]
|
| 22 |
|
| 23 |
def run_demo(task):
|
|
|
|
| 27 |
results = []
|
| 28 |
total_reward = 0
|
| 29 |
|
| 30 |
+
if not env._queue:
|
| 31 |
+
return f"No emails found for {task} difficulty."
|
| 32 |
+
|
| 33 |
for i, email in enumerate(env._queue):
|
| 34 |
+
action = smart_agent_logic(email['description'])
|
| 35 |
_, reward, _, _, info = env.step(action)
|
| 36 |
total_reward += reward
|
| 37 |
|
| 38 |
status = "✅ MATCH" if reward >= 1.0 else "❌ MISMATCH"
|
| 39 |
+
results.append(
|
| 40 |
+
f"#{i+1} [{task.upper()}] {email['description']}\n"
|
| 41 |
+
f" Agent: {URGENCY_LABELS[action[0]]} | {ROUTING_LABELS[action[1]]} | {RESOLUTION_LABELS[action[2]]}\n"
|
| 42 |
+
f" Status: {status}"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
final_score = max(0.0, total_reward / len(env._queue))
|
| 46 |
+
return "\n\n".join(results) + f"\n\n--- FINAL EPISODE SCORE: {final_score:.3f} / 1.000 ---"
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"System Error: {str(e)}"
|
| 49 |
|
| 50 |
+
# UI Layout
|
| 51 |
+
with gr.Blocks(title="Email Gatekeeper AI") as demo:
|
| 52 |
gr.Markdown("# 📧 Email Gatekeeper AI")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
diff = gr.Dropdown(choices=["easy", "medium", "hard"], value="easy", label="Select Difficulty")
|
| 55 |
+
btn = gr.Button("Analyze Emails", variant="primary")
|
| 56 |
|
| 57 |
+
out = gr.Textbox(label="Logs", lines=15)
|
| 58 |
btn.click(run_demo, inputs=diff, outputs=out)
|
| 59 |
|
| 60 |
app = gr.mount_gradio_app(app, demo, path="/")
|