Omkar1806 commited on
Commit
fb62d21
·
verified ·
1 Parent(s): 38fd83c

Update server/app.py

Browse files
Files changed (1) hide show
  1. server/app.py +21 -20
server/app.py CHANGED
@@ -4,11 +4,13 @@ import uvicorn
4
  import numpy as np
5
  import gradio as gr
6
  from fastapi import FastAPI
 
 
 
7
  from env import EmailTriageEnv, URGENCY_LABELS, ROUTING_LABELS, RESOLUTION_LABELS
8
 
9
  app = FastAPI()
10
 
11
- # --- Agent Logic ---
12
  def smart_agent_logic(desc):
13
  desc = desc.lower()
14
  if any(x in desc for x in ["password", "hacked", "breach", "phish", "threat", "ransomware"]):
@@ -19,13 +21,11 @@ def smart_agent_logic(desc):
19
  return [0, 1, 1]
20
  return [0, 0, 0]
21
 
22
- # --- Core Function ---
23
  def run_demo(task):
24
  try:
25
  env = EmailTriageEnv(task=task)
26
  env.reset()
27
- results = []
28
- total_reward = 0
29
  print(f"[START] Task: {task}")
30
  for i, email in enumerate(env._queue):
31
  action = smart_agent_logic(email['description'])
@@ -36,31 +36,32 @@ def run_demo(task):
36
  results.append(f"#{i+1} [{task.upper()}] {email['description'][:30]}... | {status}")
37
  score = total_reward / len(env._queue) if env._queue else 0
38
  print(f"[END] Final Score: {score}")
39
- return "\n".join(results) + f"\n\n--- FINAL SCORE: {score:.3f} / 1.000 ---"
40
  except Exception as e:
41
  return f"Error: {str(e)}"
42
 
43
- # --- API for Validator ---
 
 
 
 
 
44
  @app.post("/reset")
45
  async def reset_endpoint():
46
- return {"status": "success", "message": "Environment Reset OK"}
47
-
48
- @app.get("/status")
49
- async def health_check():
50
- return {"status": "online"}
51
 
52
- # --- Gradio UI ---
53
- with gr.Blocks(title="Email Gatekeeper AI") as demo:
54
  gr.Markdown("# 📧 Email Gatekeeper AI")
55
- with gr.Row():
56
- diff = gr.Dropdown(choices=["easy", "medium", "hard"], value="easy", label="Select Difficulty")
57
- btn = gr.Button("Analyze Emails", variant="primary")
58
- out = gr.Textbox(label="Evaluation Logs", lines=12)
59
  btn.click(run_demo, inputs=diff, outputs=out)
60
 
61
- # IS LINE KO DHAYAN SE DEKHO: Humne path="/" kar diya hai
62
  app = gr.mount_gradio_app(app, demo, path="/")
63
 
64
  if __name__ == "__main__":
65
- # Hugging Face hamesha port 7860 use karta hai
66
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
4
  import numpy as np
5
  import gradio as gr
6
  from fastapi import FastAPI
7
+
8
+ # Path fix taaki env.py mil jaye
9
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
10
  from env import EmailTriageEnv, URGENCY_LABELS, ROUTING_LABELS, RESOLUTION_LABELS
11
 
12
  app = FastAPI()
13
 
 
14
  def smart_agent_logic(desc):
15
  desc = desc.lower()
16
  if any(x in desc for x in ["password", "hacked", "breach", "phish", "threat", "ransomware"]):
 
21
  return [0, 1, 1]
22
  return [0, 0, 0]
23
 
 
24
  def run_demo(task):
25
  try:
26
  env = EmailTriageEnv(task=task)
27
  env.reset()
28
+ results = []; total_reward = 0
 
29
  print(f"[START] Task: {task}")
30
  for i, email in enumerate(env._queue):
31
  action = smart_agent_logic(email['description'])
 
36
  results.append(f"#{i+1} [{task.upper()}] {email['description'][:30]}... | {status}")
37
  score = total_reward / len(env._queue) if env._queue else 0
38
  print(f"[END] Final Score: {score}")
39
+ return "\n".join(results) + f"\n\n--- FINAL SCORE: {score:.3f} ---"
40
  except Exception as e:
41
  return f"Error: {str(e)}"
42
 
43
+ # --- REQUIRED BY VALIDATOR ---
44
+ def main():
45
+ print("--- 🚀 STARTING MULTI-MODE DEPLOYMENT TEST ---")
46
+ for level in ["easy", "medium", "hard"]:
47
+ print(run_demo(level))
48
+
49
  @app.post("/reset")
50
  async def reset_endpoint():
51
+ return {"status": "success"}
 
 
 
 
52
 
53
+ # Gradio Setup
54
+ with gr.Blocks() as demo:
55
  gr.Markdown("# 📧 Email Gatekeeper AI")
56
+ diff = gr.Dropdown(["easy", "medium", "hard"], value="easy", label="Difficulty")
57
+ btn = gr.Button("Analyze Emails")
58
+ out = gr.Textbox(label="Logs", lines=10)
 
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
+ if len(sys.argv) > 1 and sys.argv[1] == "--test":
65
+ main()
66
+ else:
67
+ uvicorn.run(app, host="0.0.0.0", port=7860)