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