Update server/app.py
Browse files- server/app.py +37 -21
server/app.py
CHANGED
|
@@ -1,17 +1,12 @@
|
|
| 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,
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# 1. FastAPI
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
# --- Hackathon Grader Endpoints ---
|
|
@@ -37,7 +32,8 @@ def _classify(email: dict) -> np.ndarray:
|
|
| 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
|
|
|
|
| 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)
|
|
@@ -57,28 +53,48 @@ def run_task_demo(task: str) -> str:
|
|
| 57 |
cumulative += norm_reward
|
| 58 |
raw = info["raw_reward"]
|
| 59 |
ca = info["correct_actions"]
|
| 60 |
-
verdict = ("β
EXACT" if raw >= 1.0 else
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
step += 1
|
| 66 |
final = max(0.0, min(1.0, cumulative))
|
| 67 |
-
lines.append(f"\n{'β'*50}
|
|
|
|
| 68 |
return "\n".join(lines)
|
| 69 |
|
| 70 |
-
# 3. Gradio Interface
|
| 71 |
with gr.Blocks(title="Email Gatekeeper RL") as demo:
|
| 72 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
| 73 |
with gr.Row():
|
| 74 |
-
task_dropdown = gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
run_btn = gr.Button("βΆ Run Episode", variant="primary")
|
| 76 |
-
output_box = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
run_btn.click(fn=run_task_demo, inputs=task_dropdown, outputs=output_box)
|
| 78 |
|
| 79 |
-
# 4.
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ---
|
|
|
|
| 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)
|
|
|
|
| 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)
|