jisaacso219 commited on
Commit
70b3f00
·
verified ·
1 Parent(s): 739af6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -73,12 +73,10 @@ class Me:
73
  self.session_log = []
74
  Path("chat_logs").mkdir(exist_ok=True)
75
 
76
- # Download LinkedIn PDF
77
  gdown.download("https://drive.google.com/uc?id=1xz2RowkImpI8odYv8zvKdlRHaKfILn40", "linkedin.pdf", quiet=False)
78
  reader = PdfReader("linkedin.pdf")
79
  self.linkedin = "".join(page.extract_text() or "" for page in reader.pages)
80
 
81
- # Download summary.txt
82
  gdown.download("https://drive.google.com/uc?id=1hjJz082YFSVjFtpO0pwT6Tyy3eLYYj6-", "summary.txt", quiet=False)
83
  with open("summary.txt", "r", encoding="utf-8") as f:
84
  self.summary = f.read()
@@ -109,34 +107,33 @@ If you can't answer something, call `record_unknown_question`. If a user seems i
109
  return results
110
 
111
  def chat_stream(self, message, history):
112
- messages = [{"role": "system", "content": self.system_prompt()}]
113
 
114
- for msg in history:
115
- if msg["role"] in ["user", "assistant"]:
116
- messages.append(msg)
117
 
118
- messages.append({"role": "user", "content": message})
119
- self.session_log.append({"role": "user", "content": message})
120
 
121
- stream = self.openai.chat.completions.create(
122
- model="gpt-4o",
123
- messages=messages,
124
- tools=tools,
125
- stream=True
126
- )
127
-
128
- full_response = ""
129
- for chunk in stream:
130
- delta = chunk.choices[0].delta
131
- if hasattr(delta, "content") and delta.content:
132
- full_response += delta.content
133
- yield full_response
134
 
135
- full_response += "\n\n💬 Let me know if you’d like to follow up or need help connecting with Jacob."
 
 
 
 
 
136
 
137
- self.session_log.append({"role": "assistant", "content": full_response})
138
- self.save_session_log()
139
 
 
 
140
 
141
  def save_session_log(self):
142
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
@@ -145,16 +142,13 @@ If you can't answer something, call `record_unknown_question`. If a user seems i
145
  json.dump(self.session_log, f, indent=2)
146
 
147
  def archive_logs(self):
148
- """Create a zip archive of all previous logs — replaceable for weekly automation"""
149
  zip_path = "chat_logs/weekly_archive.zip"
150
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as archive:
151
  for log_file in Path("chat_logs").glob("session_*.json"):
152
  archive.write(log_file, arcname=log_file.name)
153
 
154
- # Instantiate assistant
155
  me = Me()
156
 
157
- # Build UI
158
  with gr.Blocks(title="Jacob Isaacson Chatbot") as iface:
159
  with gr.Row():
160
  gr.Image("jacob.png", width=100, show_label=False)
 
73
  self.session_log = []
74
  Path("chat_logs").mkdir(exist_ok=True)
75
 
 
76
  gdown.download("https://drive.google.com/uc?id=1xz2RowkImpI8odYv8zvKdlRHaKfILn40", "linkedin.pdf", quiet=False)
77
  reader = PdfReader("linkedin.pdf")
78
  self.linkedin = "".join(page.extract_text() or "" for page in reader.pages)
79
 
 
80
  gdown.download("https://drive.google.com/uc?id=1hjJz082YFSVjFtpO0pwT6Tyy3eLYYj6-", "summary.txt", quiet=False)
81
  with open("summary.txt", "r", encoding="utf-8") as f:
82
  self.summary = f.read()
 
107
  return results
108
 
109
  def chat_stream(self, message, history):
110
+ messages = [{"role": "system", "content": self.system_prompt()}]
111
 
112
+ for msg in history:
113
+ if isinstance(msg, dict) and msg.get("role") in ["user", "assistant"]:
114
+ messages.append(msg)
115
 
116
+ messages.append({"role": "user", "content": message})
117
+ self.session_log.append({"role": "user", "content": message})
118
 
119
+ stream = self.openai.chat.completions.create(
120
+ model="gpt-4o",
121
+ messages=messages,
122
+ tools=tools,
123
+ stream=True
124
+ )
 
 
 
 
 
 
 
125
 
126
+ full_response = ""
127
+ for chunk in stream:
128
+ delta = chunk.choices[0].delta
129
+ if hasattr(delta, "content") and delta.content:
130
+ full_response += delta.content
131
+ yield full_response
132
 
133
+ full_response += "\n\n💬 Let me know if you’d like to follow up or need help connecting with Jacob."
 
134
 
135
+ self.session_log.append({"role": "assistant", "content": full_response})
136
+ self.save_session_log()
137
 
138
  def save_session_log(self):
139
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
 
142
  json.dump(self.session_log, f, indent=2)
143
 
144
  def archive_logs(self):
 
145
  zip_path = "chat_logs/weekly_archive.zip"
146
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as archive:
147
  for log_file in Path("chat_logs").glob("session_*.json"):
148
  archive.write(log_file, arcname=log_file.name)
149
 
 
150
  me = Me()
151
 
 
152
  with gr.Blocks(title="Jacob Isaacson Chatbot") as iface:
153
  with gr.Row():
154
  gr.Image("jacob.png", width=100, show_label=False)