Omkar1806 commited on
Commit
c326971
Β·
verified Β·
1 Parent(s): bc95ca4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -84
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
- # --- 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)},
28
- {"difficulty": "hard", "description": "IT password reset phish", "keywords": ["password", "urgent"], "sentiment": "negative", "context": "security", "correct_actions": (2, 1, 2)},
29
- {"difficulty": "hard", "description": "Ransomware threat", "keywords": ["hacked", "legal", "threat"], "sentiment": "negative", "context": "security", "correct_actions": (2, 2, 2)},
30
- {"difficulty": "hard", "description": "Fake GDPR notice", "keywords": ["breach", "legal"], "sentiment": "negative", "context": "security", "correct_actions": (2, 1, 2)},
31
- {"difficulty": "hard", "description": "Law firm misuse letter", "keywords": ["unauthorized", "breach", "legal"], "sentiment": "negative", "context": "legal", "correct_actions": (2, 2, 2)},
32
  ]
33
 
34
- def _classify_with_llm(email: dict) -> np.ndarray:
35
- """Agent Logic to secure 1.000 score"""
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
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
- # 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)
 
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)