Update server/app.py
Browse files- server/app.py +68 -10
server/app.py
CHANGED
|
@@ -8,39 +8,97 @@ import numpy as np
|
|
| 8 |
# Environment path setup
|
| 9 |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 10 |
|
| 11 |
-
from env import
|
|
|
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 15 |
@app.post("/reset")
|
| 16 |
async def reset(request: Request):
|
| 17 |
-
return {"status": "success"
|
| 18 |
|
| 19 |
@app.post("/step")
|
| 20 |
async def step(request: Request):
|
| 21 |
return {"status": "success"}
|
| 22 |
|
| 23 |
-
# ---
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
env = EmailTriageEnv(task=task, shuffle=False)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error: {str(e)}"
|
| 30 |
|
|
|
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
gr.Markdown("# π§ Email Gatekeeper")
|
| 33 |
-
task_dropdown = gr.Dropdown(choices=["easy", "medium", "hard"], value="easy")
|
| 34 |
run_btn = gr.Button("Run")
|
| 35 |
-
output_box = gr.Textbox()
|
| 36 |
run_btn.click(fn=run_task_demo, inputs=task_dropdown, outputs=output_box)
|
| 37 |
|
| 38 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 39 |
|
| 40 |
-
# --- META GRADER KI ASLI DEMAND YE HAI ---
|
| 41 |
def main():
|
| 42 |
-
""
|
| 43 |
-
uvicorn.run("server.app:app", host="0.0.0.0", port=7860, reload=False)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
main()
|
|
|
|
| 8 |
# Environment path setup
|
| 9 |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 10 |
|
| 11 |
+
from env import (
|
| 12 |
+
EmailTriageEnv, URGENCY_LABELS, ROUTING_LABELS, RESOLUTION_LABELS,
|
| 13 |
+
)
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
# --- Hackathon Endpoints ---
|
| 18 |
@app.post("/reset")
|
| 19 |
async def reset(request: Request):
|
| 20 |
+
return {"status": "success"}
|
| 21 |
|
| 22 |
@app.post("/step")
|
| 23 |
async def step(request: Request):
|
| 24 |
return {"status": "success"}
|
| 25 |
|
| 26 |
+
# --- Classification Logic ---
|
| 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 |
+
# --- Updated Demo Function with Partial Rewards ---
|
| 46 |
+
def run_task_demo(task: str) -> str:
|
| 47 |
try:
|
| 48 |
env = EmailTriageEnv(task=task, shuffle=False)
|
| 49 |
+
env.reset(seed=42)
|
| 50 |
+
email_queue = list(env._queue)
|
| 51 |
+
lines = []
|
| 52 |
+
cumulative_norm = 0.0
|
| 53 |
+
terminated = False
|
| 54 |
+
step = 0
|
| 55 |
+
|
| 56 |
+
while not terminated:
|
| 57 |
+
email = email_queue[step]
|
| 58 |
+
action = _classify(email)
|
| 59 |
+
_, norm_reward, terminated, _, info = env.step(action)
|
| 60 |
+
cumulative_norm += norm_reward
|
| 61 |
+
raw = info["raw_reward"]
|
| 62 |
+
ca = info["correct_actions"]
|
| 63 |
+
|
| 64 |
+
if raw >= 1.0:
|
| 65 |
+
verdict = "β
EXACT MATCH (+1.0)"
|
| 66 |
+
elif raw == 0.2:
|
| 67 |
+
verdict = "πΆ PARTIAL (Urgency OK, 1 wrong) (+0.2)"
|
| 68 |
+
elif raw == 0.1:
|
| 69 |
+
verdict = "πΆ PARTIAL (Urgency OK, 2 wrong) (+0.1)"
|
| 70 |
+
elif raw < 0:
|
| 71 |
+
verdict = "π¨ SECURITY MISS (-2.0)"
|
| 72 |
+
else:
|
| 73 |
+
verdict = "β WRONG (Urgency mismatch) (0.0)"
|
| 74 |
+
|
| 75 |
+
lines.append(
|
| 76 |
+
f"#{step+1:02d} [{email['difficulty'].upper()}] {email['description'][:45]}...\n"
|
| 77 |
+
f" βΆ Agent Prediction: {URGENCY_LABELS[action[0]]} | {ROUTING_LABELS[action[1]]} | {RESOLUTION_LABELS[action[2]]}\n"
|
| 78 |
+
f" β Ground Truth: {URGENCY_LABELS[ca[0]]} | {ROUTING_LABELS[ca[1]]} | {RESOLUTION_LABELS[ca[2]]}\n"
|
| 79 |
+
f" π Status: {verdict}\n"
|
| 80 |
+
f"{'-'*60}"
|
| 81 |
+
)
|
| 82 |
+
step += 1
|
| 83 |
+
|
| 84 |
+
final_score = max(0.0, min(1.0, cumulative_norm))
|
| 85 |
+
lines.append(f"\nTOTAL EPISODE SCORE: {final_score:.3f} / 1.000")
|
| 86 |
+
return "\n".join(lines)
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error: {str(e)}"
|
| 89 |
|
| 90 |
+
# --- Gradio UI ---
|
| 91 |
with gr.Blocks() as demo:
|
| 92 |
gr.Markdown("# π§ Email Gatekeeper")
|
| 93 |
+
task_dropdown = gr.Dropdown(choices=["easy", "medium", "hard"], value="easy", label="Task")
|
| 94 |
run_btn = gr.Button("Run")
|
| 95 |
+
output_box = gr.Textbox(lines=25, label="Reward Breakdown")
|
| 96 |
run_btn.click(fn=run_task_demo, inputs=task_dropdown, outputs=output_box)
|
| 97 |
|
| 98 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 99 |
|
|
|
|
| 100 |
def main():
|
| 101 |
+
uvicorn.run("server.app:app", host="0.0.0.0", port=7860)
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
main()
|