jisaacso219 commited on
Commit
bceb0df
·
verified ·
1 Parent(s): 213e868

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -12,13 +12,12 @@ import zipfile
12
 
13
  load_dotenv(override=True)
14
 
15
- # Ensure chat_logs directory exists
16
- chat_logs_dir = Path("chat_logs")
17
- chat_logs_dir.mkdir(exist_ok=True)
18
- ( chat_logs_dir / ".keep" ).touch(exist_ok=True)
19
-
20
  def push(text):
21
  try:
 
 
 
 
22
  requests.post(
23
  "https://api.pushover.net/1/messages.json",
24
  data={
@@ -32,19 +31,27 @@ def push(text):
32
 
33
  def record_user_details(email, name="Name not provided", notes="not provided"):
34
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
35
- filename = chat_logs_dir / f"session_{timestamp}.json"
 
 
 
 
36
  with open(filename, "w", encoding="utf-8") as f:
37
  json.dump(me.session_log, f, indent=2)
38
- msg = f"[New Contact]\nName: {name}\nEmail: {email}\nNotes: {notes}\n\n🔗 View log: {filename.absolute()}"
39
  push(msg)
40
  return {"recorded": "ok"}
41
 
42
  def record_unknown_question(question):
43
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
44
- filename = chat_logs_dir / f"session_{timestamp}.json"
 
 
 
 
45
  with open(filename, "w", encoding="utf-8") as f:
46
  json.dump(me.session_log, f, indent=2)
47
- msg = f"[Unknown Question]\nQ: {question}\n\n🔗 View log: {filename.absolute()}"
48
  push(msg)
49
  return {"recorded": "ok"}
50
 
@@ -86,6 +93,7 @@ class Me:
86
  self.openai = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
87
  self.name = "Jacob Isaacson"
88
  self.session_log = []
 
89
 
90
  gdown.download("https://drive.google.com/uc?id=1xz2RowkImpI8odYv8zvKdlRHaKfILn40", "linkedin.pdf", quiet=False)
91
  reader = PdfReader("linkedin.pdf")
@@ -168,14 +176,14 @@ If you can't answer something, call `record_unknown_question`. If a user seems i
168
 
169
  def save_session_log(self):
170
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
171
- filename = chat_logs_dir / f"session_{timestamp}.json"
172
  with open(filename, "w", encoding="utf-8") as f:
173
  json.dump(self.session_log, f, indent=2)
174
 
175
  def archive_logs(self):
176
- zip_path = chat_logs_dir / "weekly_archive.zip"
177
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as archive:
178
- for log_file in chat_logs_dir.glob("session_*.json"):
179
  archive.write(log_file, arcname=log_file.name)
180
 
181
  me = Me()
@@ -191,7 +199,8 @@ with gr.Blocks(title="Jacob Isaacson Chatbot") as iface:
191
  show_copy_button=True,
192
  value=[
193
  {"role": "assistant", "content": "Hello, my name is Jacob Isaacson. Please ask me any questions about my professional career and I will do my best to respond."}
194
- ]
 
195
  ),
196
  type="messages",
197
  additional_inputs=[],
@@ -199,3 +208,4 @@ with gr.Blocks(title="Jacob Isaacson Chatbot") as iface:
199
 
200
  if __name__ == "__main__":
201
  iface.launch()
 
 
12
 
13
  load_dotenv(override=True)
14
 
 
 
 
 
 
15
  def push(text):
16
  try:
17
+ Path("chat_logs").mkdir(exist_ok=True)
18
+ keep_path = Path("chat_logs/.keep")
19
+ if not keep_path.exists():
20
+ keep_path.touch()
21
  requests.post(
22
  "https://api.pushover.net/1/messages.json",
23
  data={
 
31
 
32
  def record_user_details(email, name="Name not provided", notes="not provided"):
33
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
34
+ filename = f"chat_logs/session_{timestamp}.json"
35
+ latest_log = "\n".join([
36
+ f"{entry['role'].capitalize()}: {entry['content'][:200]}"
37
+ for entry in me.session_log[-6:]
38
+ ])
39
  with open(filename, "w", encoding="utf-8") as f:
40
  json.dump(me.session_log, f, indent=2)
41
+ msg = f"[New Contact]\nName: {name}\nEmail: {email}\nNotes: {notes}\n\n🔗 View log: {filename}"
42
  push(msg)
43
  return {"recorded": "ok"}
44
 
45
  def record_unknown_question(question):
46
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
47
+ filename = f"chat_logs/session_{timestamp}.json"
48
+ latest_log = "\n".join([
49
+ f"{entry['role'].capitalize()}: {entry['content'][:200]}"
50
+ for entry in me.session_log[-6:]
51
+ ])
52
  with open(filename, "w", encoding="utf-8") as f:
53
  json.dump(me.session_log, f, indent=2)
54
+ msg = f"[Unknown Question]\nQ: {question}\n\n🔗 View log: {filename}"
55
  push(msg)
56
  return {"recorded": "ok"}
57
 
 
93
  self.openai = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
94
  self.name = "Jacob Isaacson"
95
  self.session_log = []
96
+ Path("chat_logs").mkdir(exist_ok=True)
97
 
98
  gdown.download("https://drive.google.com/uc?id=1xz2RowkImpI8odYv8zvKdlRHaKfILn40", "linkedin.pdf", quiet=False)
99
  reader = PdfReader("linkedin.pdf")
 
176
 
177
  def save_session_log(self):
178
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
179
+ filename = f"chat_logs/session_{timestamp}.json"
180
  with open(filename, "w", encoding="utf-8") as f:
181
  json.dump(self.session_log, f, indent=2)
182
 
183
  def archive_logs(self):
184
+ zip_path = "chat_logs/weekly_archive.zip"
185
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as archive:
186
+ for log_file in Path("chat_logs").glob("session_*.json"):
187
  archive.write(log_file, arcname=log_file.name)
188
 
189
  me = Me()
 
199
  show_copy_button=True,
200
  value=[
201
  {"role": "assistant", "content": "Hello, my name is Jacob Isaacson. Please ask me any questions about my professional career and I will do my best to respond."}
202
+ ],
203
+ type="messages"
204
  ),
205
  type="messages",
206
  additional_inputs=[],
 
208
 
209
  if __name__ == "__main__":
210
  iface.launch()
211
+