Update server/app.py
Browse files- server/app.py +84 -0
server/app.py
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
from fastapi import FastAPI, Request
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Environment ko dhoondne ke liye rasta sahi karna (agar env.py bahar hai)
|
| 8 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 9 |
+
|
| 10 |
+
from env import (
|
| 11 |
+
EmailTriageEnv, URGENCY_LABELS, ROUTING_LABELS, RESOLUTION_LABELS,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# 1. FastAPI instance banana
|
| 15 |
+
app = FastAPI()
|
| 16 |
+
|
| 17 |
+
# --- Hackathon Grader Endpoints ---
|
| 18 |
+
@app.post("/reset")
|
| 19 |
+
async def reset(request: Request):
|
| 20 |
+
return {"status": "success", "message": "Environment reset"}
|
| 21 |
+
|
| 22 |
+
@app.post("/step")
|
| 23 |
+
async def step(request: Request):
|
| 24 |
+
return {"status": "success"}
|
| 25 |
+
|
| 26 |
+
# 2. Logic Functions
|
| 27 |
+
_LEGAL_SECURITY_KW = {"lawsuit", "attorney", "sue", "ransomware", "extortion"}
|
| 28 |
+
_BILLING_ESCALATE_KW = {"refund"}
|
| 29 |
+
|
| 30 |
+
def _classify(email: dict) -> np.ndarray:
|
| 31 |
+
kw = set(email.get("keywords", []))
|
| 32 |
+
context = email.get("context", "").lower()
|
| 33 |
+
if context == "legal" or kw & {"lawsuit", "attorney", "sue"}:
|
| 34 |
+
return np.array([2, 2, 2], dtype=np.int64)
|
| 35 |
+
if context == "security":
|
| 36 |
+
if kw & _LEGAL_SECURITY_KW or ("hacked" in kw and "breach" in kw):
|
| 37 |
+
return np.array([2, 2, 2], dtype=np.int64)
|
| 38 |
+
return np.array([2, 1, 2], dtype=np.int64)
|
| 39 |
+
if context == "billing":
|
| 40 |
+
return np.array([1, 2, 2] if kw & _BILLING_ESCALATE_KW else [1, 0, 1], dtype=np.int64)
|
| 41 |
+
if context == "tech" or kw & {"crash", "error", "bug", "slow"}:
|
| 42 |
+
return np.array([0, 1, 1], dtype=np.int64)
|
| 43 |
+
return np.array([0, 0, 0], dtype=np.int64)
|
| 44 |
+
|
| 45 |
+
def run_task_demo(task: str) -> str:
|
| 46 |
+
env = EmailTriageEnv(task=task, shuffle=False)
|
| 47 |
+
env.reset(seed=42)
|
| 48 |
+
email_queue = list(env._queue)
|
| 49 |
+
lines = []
|
| 50 |
+
cumulative = 0.0
|
| 51 |
+
terminated = False
|
| 52 |
+
step = 0
|
| 53 |
+
while not terminated:
|
| 54 |
+
email = email_queue[step]
|
| 55 |
+
action = _classify(email)
|
| 56 |
+
_, norm_reward, terminated, _, info = env.step(action)
|
| 57 |
+
cumulative += norm_reward
|
| 58 |
+
raw = info["raw_reward"]
|
| 59 |
+
ca = info["correct_actions"]
|
| 60 |
+
verdict = ("✅ EXACT" if raw >= 1.0 else "🔶 PARTIAL" if raw > 0 else "🚨 SECURITY MISS" if raw < 0 else "❌ WRONG")
|
| 61 |
+
lines.append(f"#{step+1:02d} [{email['difficulty'].upper()}] {email['description'][:40]}\n"
|
| 62 |
+
f" Predicted : {URGENCY_LABELS[action[0]]} | {ROUTING_LABELS[action[1]]} | {RESOLUTION_LABELS[action[2]]}\n"
|
| 63 |
+
f" Correct : {URGENCY_LABELS[ca[0]]} | {ROUTING_LABELS[ca[1]]} | {RESOLUTION_LABELS[ca[2]]}\n"
|
| 64 |
+
f" Reward : {raw:+.1f} {verdict}\n")
|
| 65 |
+
step += 1
|
| 66 |
+
final = max(0.0, min(1.0, cumulative))
|
| 67 |
+
lines.append(f"\n{'─'*50}\nFinal Score : {final:.3f} / 1.0")
|
| 68 |
+
return "\n".join(lines)
|
| 69 |
+
|
| 70 |
+
# 3. Gradio Interface Setup
|
| 71 |
+
with gr.Blocks(title="Email Gatekeeper RL") as demo:
|
| 72 |
+
gr.Markdown("# 📧 Email Gatekeeper — RL Environment Demo")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
task_dropdown = gr.Dropdown(choices=["easy", "medium", "hard"], value="easy", label="Select Task")
|
| 75 |
+
run_btn = gr.Button("▶ Run Episode", variant="primary")
|
| 76 |
+
output_box = gr.Textbox(label="Episode Results", lines=25)
|
| 77 |
+
run_btn.click(fn=run_task_demo, inputs=task_dropdown, outputs=output_box)
|
| 78 |
+
|
| 79 |
+
# 4. FastAPI aur Gradio ko jodna (Isse 'app' variable finalize hoga)
|
| 80 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
import uvicorn
|
| 84 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|