Omkar1806 commited on
Commit
67147d6
·
verified ·
1 Parent(s): 6314ebe

Update server/app.py

Browse files
Files changed (1) hide show
  1. server/app.py +46 -20
server/app.py CHANGED
@@ -2,17 +2,26 @@ import sys
2
  import os
3
  import uvicorn
4
  import numpy as np
5
- import re
6
  from fastapi import FastAPI
7
  import gradio as gr
8
 
9
- # Setup path and imports
10
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
11
- from env import EmailTriageEnv, URGENCY_LABELS, ROUTING_LABELS, RESOLUTION_LABELS
 
 
 
 
 
 
 
 
 
 
12
 
13
  app = FastAPI()
14
 
15
- # --- Shared Dataset ---
16
  EMAIL_DATASET = [
17
  {"difficulty": "easy", "description": "Spam promo", "keywords": ["free", "offer"], "sentiment": "positive", "context": "spam", "correct_actions": (0, 0, 0)},
18
  {"difficulty": "easy", "description": "Routine support", "keywords": ["slow", "error"], "sentiment": "neutral", "context": "tech", "correct_actions": (0, 1, 1)},
@@ -27,30 +36,34 @@ def _classify_with_llm(email: dict) -> np.ndarray:
27
  desc = email.get('description', '').lower()
28
  kws = [k.lower() for k in email.get('keywords', [])]
29
 
30
- # Check for Security Threats (Highest Reward/Risk)
31
- sec_triggers = ["password", "hacked", "breach", "unauthorized", "urgent", "security", "credential"]
32
  if any(t in desc for t in sec_triggers) or any(k in sec_triggers for k in kws):
33
- # Legal + Security = Legal Routing
34
- if any(l in desc for l in ["legal", "lawsuit", "attorney", "threat", "audit"]):
35
  return np.array([2, 2, 2])
36
  return np.array([2, 1, 2]) # Security + Tech
37
 
38
  # Check for Legal
39
- if "legal" in desc or "lawsuit" in desc:
40
  return np.array([2, 2, 2])
41
 
42
  # Check for Billing
43
- if any(b in desc for b in ["invoice", "payment", "refund", "billing"]):
44
  if "dispute" in desc or "refund" in desc:
45
  return np.array([1, 2, 2])
46
  return np.array([1, 0, 1])
47
 
 
48
  return np.array([0, 0, 0])
49
 
50
  def run_task_demo(task: str) -> str:
51
  try:
 
52
  env = EmailTriageEnv(task=task, shuffle=False)
53
  env.reset()
 
 
54
  email_queue = list(env._queue)
55
  lines = []
56
  cumulative_score = 0.0
@@ -58,29 +71,42 @@ def run_task_demo(task: str) -> str:
58
  for i, email in enumerate(email_queue):
59
  action = _classify_with_llm(email)
60
  _, _, _, _, info = env.step(action)
 
61
  reward = info.get("raw_reward", 0)
62
  cumulative_score += reward
63
 
64
  status = "✅ EXACT MATCH (+1.0)" if reward >= 0.9 else "❌ MISMATCH"
 
 
 
 
 
 
65
  lines.append(f"#{i+1:02d} [{task.upper()}] {email['description'][:40]}...\n"
66
- f" ▶ Agent: {URGENCY_LABELS[action[0]]} | {ROUTING_LABELS[action[1]]} | {RESOLUTION_LABELS[action[2]]}\n"
67
  f" 🏆 Status: {status}\n" + "-"*40)
68
 
69
- final = max(0.0, min(1.0, cumulative_score / len(email_queue)))
70
  lines.append(f"\nTOTAL EPISODE SCORE: {final:.3f} / 1.000")
71
  return "\n".join(lines)
72
  except Exception as e:
73
- return f"Error: {str(e)}"
74
 
75
- # UI Layout
76
- with gr.Blocks() as demo:
77
- gr.Markdown("# 📧 Email Gatekeeper")
78
- task_dropdown = gr.Dropdown(choices=["easy", "medium", "hard"], value="easy", label="Select Difficulty")
79
- run_btn = gr.Button("Analyze Emails")
80
- output_box = gr.Textbox(lines=20, label="Reward Breakdown")
 
 
 
 
 
81
  run_btn.click(fn=run_task_demo, inputs=task_dropdown, outputs=output_box)
82
 
83
  app = gr.mount_gradio_app(app, demo, path="/")
84
 
85
  if __name__ == "__main__":
 
86
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
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
+ # --- Full Hackathon Dataset ---
25
  EMAIL_DATASET = [
26
  {"difficulty": "easy", "description": "Spam promo", "keywords": ["free", "offer"], "sentiment": "positive", "context": "spam", "correct_actions": (0, 0, 0)},
27
  {"difficulty": "easy", "description": "Routine support", "keywords": ["slow", "error"], "sentiment": "neutral", "context": "tech", "correct_actions": (0, 1, 1)},
 
36
  desc = email.get('description', '').lower()
37
  kws = [k.lower() for k in email.get('keywords', [])]
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
  # Check for Billing
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
+ # Default
58
  return np.array([0, 0, 0])
59
 
60
  def run_task_demo(task: str) -> str:
61
  try:
62
+ # Env initialization
63
  env = EmailTriageEnv(task=task, shuffle=False)
64
  env.reset()
65
+
66
+ # Accessing the queue fixed in env.py
67
  email_queue = list(env._queue)
68
  lines = []
69
  cumulative_score = 0.0
 
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
+ # Action labels safety check
81
+ u_lab = URGENCY_LABELS[action[0]]
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
+ final = max(0.0, min(1.0, cumulative_score / len(email_queue))) if email_queue else 0.0
90
  lines.append(f"\nTOTAL EPISODE SCORE: {final:.3f} / 1.000")
91
  return "\n".join(lines)
92
  except Exception as e:
93
+ return f"System Error: {str(e)}"
94
 
95
+ # Gradio Dashboard
96
+ with gr.Blocks(title="Email Gatekeeper") as demo:
97
+ gr.Markdown("# 📧 Email Gatekeeper AI")
98
+ gr.Markdown("Select difficulty and click Analyze to evaluate the Agent.")
99
+
100
+ with gr.Row():
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
  run_btn.click(fn=run_task_demo, inputs=task_dropdown, outputs=output_box)
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)