Anne31415 commited on
Commit
df93392
1 Parent(s): cf911b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -32,6 +32,10 @@ if 'chat_history_page2' not in st.session_state:
32
  if 'chat_history_page3' not in st.session_state:
33
  st.session_state['chat_history_page3'] = []
34
 
 
 
 
 
35
 
36
 
37
  # Step 1: Clone the Dataset Repository
@@ -189,18 +193,18 @@ def ask_bot(query):
189
  full_query = standard_prompt + query
190
  return
191
 
192
- def save_conversation(chat_history, session_id):
193
- # Define the base path for conversation logs
194
- base_path = "Private_Book/conversation_logs"
195
-
196
- # Create the directory if it doesn't exist
197
  if not os.path.exists(base_path):
198
  os.makedirs(base_path)
199
 
200
- # Create a unique filename using session_id
201
- filename = f"{base_path}/{session_id}.json"
 
 
 
 
202
 
203
- # Save the chat history as JSON
204
  with open(filename, 'w') as file:
205
  json.dump(chat_history, file, indent=4)
206
 
@@ -320,8 +324,7 @@ def page1():
320
  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)
321
 
322
  # Save conversation after chat interaction
323
- session_id = str(uuid.uuid4()) # Generate a unique session ID
324
- save_conversation(st.session_state['chat_history_page1'], session_id)
325
 
326
  # Clear the input field after the query is made
327
  query = ""
 
32
  if 'chat_history_page3' not in st.session_state:
33
  st.session_state['chat_history_page3'] = []
34
 
35
+ # This session ID will be unique per user session and consistent across all pages.
36
+ if 'session_id' not in st.session_state:
37
+ st.session_state['session_id'] = str(uuid.uuid4())
38
+
39
 
40
 
41
  # Step 1: Clone the Dataset Repository
 
193
  full_query = standard_prompt + query
194
  return
195
 
196
+ def save_conversation(chat_history, session_id, page_number):
197
+ base_path = "Chat_Store/conversation_logs"
 
 
 
198
  if not os.path.exists(base_path):
199
  os.makedirs(base_path)
200
 
201
+ filename = f"{base_path}/{session_id}_page{page_number}.json"
202
+
203
+ if os.path.exists(filename):
204
+ with open(filename, 'r') as file:
205
+ existing_data = json.load(file)
206
+ chat_history = existing_data + chat_history
207
 
 
208
  with open(filename, 'w') as file:
209
  json.dump(chat_history, file, indent=4)
210
 
 
324
  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)
325
 
326
  # Save conversation after chat interaction
327
+ save_conversation(st.session_state['chat_history_page1'], st.session_state['session_id'], 1)
 
328
 
329
  # Clear the input field after the query is made
330
  query = ""