Anne31415 commited on
Commit
eaccf69
1 Parent(s): 823a7d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -12
app.py CHANGED
@@ -191,24 +191,25 @@ def ask_bot(query):
191
  full_query = standard_prompt + query
192
  return full_query
193
 
194
- def save_conversation(chat_history, session_id, page_number):
195
  base_path = "Chat_Store/conversation_logs"
196
  if not os.path.exists(base_path):
197
  os.makedirs(base_path)
198
 
199
- filename = f"{base_path}/{session_id}_page{page_number}.json"
200
 
201
  # Check if the log file already exists
202
- existing_data = []
203
  if os.path.exists(filename):
204
  with open(filename, 'r', encoding='utf-8') as file:
205
  existing_data = json.load(file)
206
 
207
- # Append the new chat history to the existing data
208
- full_chat_history = existing_data + chat_history
 
209
 
210
  with open(filename, 'w', encoding='utf-8') as file:
211
- json.dump(full_chat_history, file, indent=4, ensure_ascii=False)
212
 
213
  # Git operations
214
  try:
@@ -217,7 +218,7 @@ def save_conversation(chat_history, session_id, page_number):
217
  os.chdir('Chat_Store')
218
 
219
  # Correct file path relative to the Git repository's root
220
- git_file_path = f"conversation_logs/{session_id}_page{page_number}.json"
221
 
222
  repo2.git_add(git_file_path)
223
  repo2.git_commit(f"Add/update conversation log for session {session_id}")
@@ -326,8 +327,17 @@ def page1():
326
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
327
  new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
328
 
329
- # Save conversation after chat interaction
330
- save_conversation(st.session_state['chat_history_page1'], st.session_state['session_id'], 1)
 
 
 
 
 
 
 
 
 
331
 
332
 
333
  # Display the current working directory after save_conversation
@@ -435,7 +445,14 @@ def page2():
435
 
436
  st.session_state['chat_history_page2'].append(("Bot", response, "new"))
437
 
438
- #save_conversation(st.session_state['chat_history_page2'], st.session_state['session_id'], 2)
 
 
 
 
 
 
 
439
 
440
  # Display new messages at the bottom
441
  new_messages = st.session_state['chat_history_page2'][-2:]
@@ -551,8 +568,15 @@ def page3():
551
  for chat in new_messages:
552
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
553
  new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
554
-
555
- #save_conversation(st.session_state['chat_history_page3'], st.session_state['session_id'], 3)
 
 
 
 
 
 
 
556
 
557
  # Clear the input field after the query is made
558
  query = ""
 
191
  full_query = standard_prompt + query
192
  return full_query
193
 
194
+ def save_conversation(chat_histories, session_id):
195
  base_path = "Chat_Store/conversation_logs"
196
  if not os.path.exists(base_path):
197
  os.makedirs(base_path)
198
 
199
+ filename = f"{base_path}/{session_id}.json"
200
 
201
  # Check if the log file already exists
202
+ existing_data = {"page1": [], "page2": [], "page3": []}
203
  if os.path.exists(filename):
204
  with open(filename, 'r', encoding='utf-8') as file:
205
  existing_data = json.load(file)
206
 
207
+ # Append the new chat history to the existing data for each page
208
+ for page_number, chat_history in enumerate(chat_histories, start=1):
209
+ existing_data[f"page{page_number}"] += chat_history
210
 
211
  with open(filename, 'w', encoding='utf-8') as file:
212
+ json.dump(existing_data, file, indent=4, ensure_ascii=False)
213
 
214
  # Git operations
215
  try:
 
218
  os.chdir('Chat_Store')
219
 
220
  # Correct file path relative to the Git repository's root
221
+ git_file_path = f"conversation_logs/{session_id}.json"
222
 
223
  repo2.git_add(git_file_path)
224
  repo2.git_commit(f"Add/update conversation log for session {session_id}")
 
327
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
328
  new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
329
 
330
+
331
+ # Combine chat histories from all pages
332
+ all_chat_histories = [
333
+ st.session_state['chat_history_page1'],
334
+ st.session_state['chat_history_page2'],
335
+ st.session_state['chat_history_page3']
336
+ ]
337
+
338
+ # Save the combined chat histories
339
+ save_conversation(all_chat_histories, st.session_state['session_id'])
340
+
341
 
342
 
343
  # Display the current working directory after save_conversation
 
445
 
446
  st.session_state['chat_history_page2'].append(("Bot", response, "new"))
447
 
448
+ # Combine chat histories from all pages
449
+ all_chat_histories = [
450
+ st.session_state['chat_history_page1'],
451
+ st.session_state['chat_history_page2'],
452
+ st.session_state['chat_history_page3']
453
+ ]
454
+
455
+ save_conversation(st.session_state['chat_history_page2'], st.session_state['session_id'], 2)
456
 
457
  # Display new messages at the bottom
458
  new_messages = st.session_state['chat_history_page2'][-2:]
 
568
  for chat in new_messages:
569
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
570
  new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
571
+
572
+ # Combine chat histories from all pages
573
+ all_chat_histories = [
574
+ st.session_state['chat_history_page1'],
575
+ st.session_state['chat_history_page2'],
576
+ st.session_state['chat_history_page3']
577
+ ]
578
+
579
+ save_conversation(st.session_state['chat_history_page3'], st.session_state['session_id'], 3)
580
 
581
  # Clear the input field after the query is made
582
  query = ""