scmlewis commited on
Commit
598d62c
·
verified ·
1 Parent(s): ecb1f7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -2
app.py CHANGED
@@ -2,11 +2,14 @@ import os
2
  import gradio as gr
3
  import requests
4
  import json
 
5
 
6
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
7
  GROQ_COMPLETION_URL = "https://api.groq.com/openai/v1/chat/completions"
8
  GROQ_MODEL = "llama-3.1-8b-instant"
9
 
 
 
10
  def groq_completion(prompt, sys_prompt=None):
11
  headers = {
12
  "Authorization": f"Bearer {GROQ_API_KEY}",
@@ -60,6 +63,33 @@ def email_classifier_router(raw_email):
60
  }
61
  return output
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
64
  gr.Markdown("## 📨 AI Email Classifier & Router\nEasily classify, route, and summarize business emails.")
65
 
@@ -97,6 +127,9 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
97
  gr.Markdown("#### 🟣 Extracted Action Items")
98
  action_items = gr.Textbox(lines=4, label="Extracted Action Items", elem_id='actions')
99
 
 
 
 
100
  support_button.click(lambda: ex1, None, email_box)
101
  sales_button.click(lambda: ex2, None, email_box)
102
  finance_button.click(lambda: ex3, None, email_box)
@@ -112,12 +145,13 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
112
  summ = result.get("Summary", "")
113
  acts = result.get("Action Items", [])
114
  acts = "\n".join(acts) if isinstance(acts, list) else acts
115
- return cat, pri, rec, dra, summ, acts
 
116
 
117
  classify_btn.click(
118
  classify_and_render,
119
  inputs=[email_box],
120
- outputs=[category, priority, recipient, draft_response, summary, action_items]
121
  )
122
 
123
  if __name__ == "__main__":
 
2
  import gradio as gr
3
  import requests
4
  import json
5
+ from datetime import datetime
6
 
7
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
8
  GROQ_COMPLETION_URL = "https://api.groq.com/openai/v1/chat/completions"
9
  GROQ_MODEL = "llama-3.1-8b-instant"
10
 
11
+ classification_history = []
12
+
13
  def groq_completion(prompt, sys_prompt=None):
14
  headers = {
15
  "Authorization": f"Bearer {GROQ_API_KEY}",
 
63
  }
64
  return output
65
 
66
+ def add_to_history(email, cat, pri, summ):
67
+ entry = {
68
+ "datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
69
+ "email": email,
70
+ "category": cat,
71
+ "priority": pri,
72
+ "summary": summ
73
+ }
74
+ classification_history.append(entry)
75
+
76
+ def render_history():
77
+ # Returns list of (card_html, index) for Gradio interpretation
78
+ cards = []
79
+ for idx, h in enumerate(reversed(classification_history)):
80
+ card = (
81
+ f"<div style='border:1px solid #333;margin:8px;padding:10px;border-radius:8px;'>"
82
+ f"<b>{h['datetime']}</b><br>"
83
+ f"<b>Category:</b> <span style='background:#448; color:white; padding:2px 8px; border-radius:4px;'>{h['category']}</span> "
84
+ f"<b>Priority:</b> <span style='background:#aa4; color:black; padding:2px 8px; border-radius:4px;'>{h['priority']}</span><br>"
85
+ f"<i>{h['summary']}</i><br><br>"
86
+ f"<button onclick='navigator.clipboard.writeText({json.dumps(h['email'])})'>Copy Email</button> "
87
+ f"<button onclick='window.parent.postMessage({json.dumps(h['email'])}, \"*\")'>Load</button>"
88
+ f"</div>"
89
+ )
90
+ cards.append(card)
91
+ return "".join(cards)
92
+
93
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
94
  gr.Markdown("## 📨 AI Email Classifier & Router\nEasily classify, route, and summarize business emails.")
95
 
 
127
  gr.Markdown("#### 🟣 Extracted Action Items")
128
  action_items = gr.Textbox(lines=4, label="Extracted Action Items", elem_id='actions')
129
 
130
+ # History display column
131
+ history_panel = gr.HTML(label="Classification History")
132
+
133
  support_button.click(lambda: ex1, None, email_box)
134
  sales_button.click(lambda: ex2, None, email_box)
135
  finance_button.click(lambda: ex3, None, email_box)
 
145
  summ = result.get("Summary", "")
146
  acts = result.get("Action Items", [])
147
  acts = "\n".join(acts) if isinstance(acts, list) else acts
148
+ add_to_history(email_text, cat, pri, summ)
149
+ return cat, pri, rec, dra, summ, acts, render_history()
150
 
151
  classify_btn.click(
152
  classify_and_render,
153
  inputs=[email_box],
154
+ outputs=[category, priority, recipient, draft_response, summary, action_items, history_panel]
155
  )
156
 
157
  if __name__ == "__main__":