Update app.py
Browse files
app.py
CHANGED
|
@@ -2,111 +2,63 @@ import sys
|
|
| 2 |
import os
|
| 3 |
import uvicorn
|
| 4 |
import numpy as np
|
| 5 |
-
from fastapi import FastAPI
|
| 6 |
import gradio as gr
|
| 7 |
-
|
| 8 |
-
# --- PATH FIX: Sabse pehle current directory ko path mein add karte hain ---
|
| 9 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
-
if current_dir not in sys.path:
|
| 11 |
-
sys.path.append(current_dir)
|
| 12 |
-
|
| 13 |
-
# Ab import kaam karega
|
| 14 |
-
try:
|
| 15 |
-
from env import EmailTriageEnv, URGENCY_LABELS, ROUTING_LABELS, RESOLUTION_LABELS
|
| 16 |
-
except ImportError:
|
| 17 |
-
# Fallback labels agar env.py na mile (Error se bachne ke liye)
|
| 18 |
-
URGENCY_LABELS = ["General", "Billing", "Security Breach"]
|
| 19 |
-
ROUTING_LABELS = ["AI Auto-Reply", "Tech Support", "Legal"]
|
| 20 |
-
RESOLUTION_LABELS = ["Archive", "Draft Reply", "Escalate to Human"]
|
| 21 |
|
| 22 |
app = FastAPI()
|
| 23 |
|
| 24 |
-
#
|
| 25 |
EMAIL_DATASET = [
|
| 26 |
-
{"difficulty": "easy", "description": "Spam promo", "
|
| 27 |
-
{"difficulty": "easy", "description": "Routine support", "
|
| 28 |
-
{"difficulty": "hard", "description": "IT password reset phish", "
|
| 29 |
-
{"difficulty": "hard", "description": "Ransomware threat", "
|
| 30 |
-
{"difficulty": "hard", "description": "Fake GDPR notice", "
|
| 31 |
-
{"difficulty": "hard", "description": "Law firm misuse letter", "keywords": ["unauthorized", "breach", "legal"], "sentiment": "negative", "context": "legal", "correct_actions": (2, 2, 2)},
|
| 32 |
]
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# Check for Security Threats
|
| 40 |
-
sec_triggers = ["password", "hacked", "breach", "unauthorized", "urgent", "security", "credential", "phish"]
|
| 41 |
-
if any(t in desc for t in sec_triggers) or any(k in sec_triggers for k in kws):
|
| 42 |
-
# Security + Legal/Threat
|
| 43 |
-
if any(l in desc for l in ["legal", "lawsuit", "attorney", "threat", "audit", "court"]):
|
| 44 |
-
return np.array([2, 2, 2])
|
| 45 |
-
return np.array([2, 1, 2]) # Security + Tech
|
| 46 |
-
|
| 47 |
-
# Check for Legal
|
| 48 |
-
if "legal" in desc or "lawsuit" in desc or "attorney" in desc:
|
| 49 |
-
return np.array([2, 2, 2])
|
| 50 |
|
| 51 |
-
|
| 52 |
-
if any(b in desc for b in ["invoice", "payment", "refund", "billing", "overdue"]):
|
| 53 |
-
if "dispute" in desc or "refund" in desc:
|
| 54 |
-
return np.array([1, 2, 2])
|
| 55 |
-
return np.array([1, 0, 1])
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
def
|
| 61 |
try:
|
| 62 |
-
|
| 63 |
-
env = EmailTriageEnv(task=task, shuffle=False)
|
| 64 |
env.reset()
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
for i, email in enumerate(email_queue):
|
| 72 |
-
action = _classify_with_llm(email)
|
| 73 |
-
_, _, _, _, info = env.step(action)
|
| 74 |
-
|
| 75 |
-
reward = info.get("raw_reward", 0)
|
| 76 |
-
cumulative_score += reward
|
| 77 |
-
|
| 78 |
-
status = "β
EXACT MATCH (+1.0)" if reward >= 0.9 else "β MISMATCH"
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
ro_lab = ROUTING_LABELS[action[1]]
|
| 83 |
-
re_lab = RESOLUTION_LABELS[action[2]]
|
| 84 |
-
|
| 85 |
-
lines.append(f"#{i+1:02d} [{task.upper()}] {email['description'][:40]}...\n"
|
| 86 |
-
f" βΆ Agent: {u_lab} | {ro_lab} | {re_lab}\n"
|
| 87 |
-
f" π Status: {status}\n" + "-"*40)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
return "\n".join(lines)
|
| 92 |
except Exception as e:
|
| 93 |
-
return f"
|
| 94 |
|
| 95 |
-
|
| 96 |
-
with gr.Blocks(title="Email Gatekeeper") as demo:
|
| 97 |
gr.Markdown("# π§ Email Gatekeeper AI")
|
| 98 |
-
gr.
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
task_dropdown = gr.Dropdown(choices=["easy", "medium", "hard"], value="easy", label="Select Difficulty")
|
| 102 |
-
run_btn = gr.Button("Analyze Emails", variant="primary")
|
| 103 |
-
|
| 104 |
-
output_box = gr.Textbox(lines=20, label="Evaluation Logs", placeholder="Results will appear here...")
|
| 105 |
|
| 106 |
-
|
| 107 |
|
| 108 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
-
# Port 7860 is mandatory for Hugging Face Spaces
|
| 112 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
import os
|
| 3 |
import uvicorn
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
+
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Shared Dataset
|
| 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 |
+
if any(x in desc for x in ["password", "hacked", "breach", "phish"]):
|
| 29 |
+
if "threat" in desc or "ransom" in desc: return [2, 2, 2]
|
| 30 |
+
return [2, 1, 2]
|
| 31 |
+
return [0, 0, 0]
|
| 32 |
|
| 33 |
+
def run_demo(task):
|
| 34 |
try:
|
| 35 |
+
env = EmailTriageEnv(task=task)
|
|
|
|
| 36 |
env.reset()
|
| 37 |
+
results = []
|
| 38 |
+
total_reward = 0
|
| 39 |
|
| 40 |
+
for i, email in enumerate(env._queue):
|
| 41 |
+
action = classify_logic(email['description'])
|
| 42 |
+
_, reward, _, _, info = env.step(action)
|
| 43 |
+
total_reward += reward
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
status = "β
MATCH" if reward >= 1.0 else "β MISMATCH"
|
| 46 |
+
results.append(f"#{i+1} {email['description'][:30]}...\n Agent: {URGENCY_LABELS[action[0]]} | Status: {status}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
score = max(0, total_reward / len(env._queue)) if env._queue else 0
|
| 49 |
+
return "\n\n".join(results) + f"\n\nFINAL SCORE: {score:.3f}"
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
+
return f"Error: {str(e)}"
|
| 52 |
|
| 53 |
+
with gr.Blocks() as demo:
|
|
|
|
| 54 |
gr.Markdown("# π§ Email Gatekeeper AI")
|
| 55 |
+
diff = gr.Dropdown(["easy", "hard"], value="easy", label="Difficulty")
|
| 56 |
+
btn = gr.Button("Analyze Emails")
|
| 57 |
+
out = gr.Textbox(label="Logs", lines=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
btn.click(run_demo, inputs=diff, outputs=out)
|
| 60 |
|
| 61 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 64 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|