Anne31415 commited on
Commit
b160f83
·
1 Parent(s): 48235eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -20,8 +20,14 @@ import pydeck as pdk
20
  from urllib.error import URLError
21
 
22
  # Initialize session state variables
23
- if 'chat_history' not in st.session_state:
24
- st.session_state['chat_history'] = []
 
 
 
 
 
 
25
 
26
  st.set_page_config(layout="wide")
27
 
@@ -49,6 +55,7 @@ api_key = os.getenv("OPENAI_API_KEY")
49
  # Updated caching mechanism using st.cache_data
50
  @st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
51
  def load_vector_store(file_path, store_name, force_reload=False):
 
52
  # Check if we need to force reload the vector store (e.g., when the PDF changes)
53
  if force_reload or not os.path.exists(f"{store_name}.pkl"):
54
  text_splitter = RecursiveCharacterTextSplitter(
@@ -118,13 +125,9 @@ def page1():
118
  st.error("File not found. Please check the file path.")
119
  return
120
 
121
- VectorStore = load_vector_store(pdf_path, "my_vector_store", force_reload=False)
122
-
123
-
124
- if "chat_history" not in st.session_state:
125
- st.session_state['chat_history'] = []
126
-
127
- display_chat_history(st.session_state['chat_history'])
128
 
129
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
130
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
@@ -180,11 +183,11 @@ def page1():
180
  # You can use Streamlit's text function to display the timing
181
  st.text(f"Response time: {duration:.2f} seconds")
182
 
183
- st.session_state['chat_history'].append(("Bot", response, "new"))
184
 
185
 
186
  # Display new messages at the bottom
187
- new_messages = st.session_state['chat_history'][-2:]
188
  for chat in new_messages:
189
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
190
  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)
@@ -194,7 +197,7 @@ def page1():
194
  query = ""
195
 
196
  # Mark all messages as old after displaying
197
- st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
198
 
199
  except Exception as e:
200
  st.error(f"Upsi, an unexpected error occurred: {e}")
@@ -232,13 +235,11 @@ def page2():
232
  st.error("File not found. Please check the file path.")
233
  return
234
 
235
- VectorStore = load_vector_store(pdf_path2, "my_vector_store", force_reload=False)
236
-
237
 
238
- if "chat_history" not in st.session_state:
239
- st.session_state['chat_history'] = []
240
 
241
- display_chat_history(st.session_state['chat_history'])
242
 
243
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
244
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
@@ -272,7 +273,7 @@ def page2():
272
 
273
 
274
  if query:
275
- st.session_state['chat_history'].append(("User", query, "new"))
276
 
277
  # Start timing
278
  start_time = time.time()
@@ -294,11 +295,11 @@ def page2():
294
  # You can use Streamlit's text function to display the timing
295
  st.text(f"Response time: {duration:.2f} seconds")
296
 
297
- st.session_state['chat_history'].append(("Bot", response, "new"))
298
 
299
 
300
  # Display new messages at the bottom
301
- new_messages = st.session_state['chat_history'][-2:]
302
  for chat in new_messages:
303
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
304
  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)
@@ -308,7 +309,7 @@ def page2():
308
  query = ""
309
 
310
  # Mark all messages as old after displaying
311
- st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
312
 
313
  except Exception as e:
314
  st.error(f"Upsi, an unexpected error occurred: {e}")
@@ -329,9 +330,9 @@ def main():
329
  st.write('Made with ❤️ by BinDoc GmbH')
330
 
331
  # Main area content based on page selection
332
- if page == "Main ChatBot":
333
  page1()
334
- elif page == "Kodierhilfe":
335
  page2()
336
 
337
  if __name__ == "__main__":
 
20
  from urllib.error import URLError
21
 
22
  # Initialize session state variables
23
+ if 'chat_history_page1' not in st.session_state:
24
+ st.session_state['chat_history_page1'] = []
25
+
26
+ if 'chat_history_page2' not in st.session_state:
27
+ st.session_state['chat_history_page2'] = []
28
+
29
+ # Then use st.session_state['chat_history_page1'] within page1()
30
+
31
 
32
  st.set_page_config(layout="wide")
33
 
 
55
  # Updated caching mechanism using st.cache_data
56
  @st.cache_data(persist="disk") # Using persist="disk" to save cache across sessions
57
  def load_vector_store(file_path, store_name, force_reload=False):
58
+
59
  # Check if we need to force reload the vector store (e.g., when the PDF changes)
60
  if force_reload or not os.path.exists(f"{store_name}.pkl"):
61
  text_splitter = RecursiveCharacterTextSplitter(
 
125
  st.error("File not found. Please check the file path.")
126
  return
127
 
128
+ VectorStore = load_vector_store(pdf_path, "vector_store_page1", force_reload=False)
129
+
130
+ display_chat_history(st.session_state['chat_history1'])
 
 
 
 
131
 
132
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
133
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
 
183
  # You can use Streamlit's text function to display the timing
184
  st.text(f"Response time: {duration:.2f} seconds")
185
 
186
+ st.session_state['chat_history1'].append(("Bot", response, "new"))
187
 
188
 
189
  # Display new messages at the bottom
190
+ new_messages = st.session_state['chat_history1'][-2:]
191
  for chat in new_messages:
192
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
193
  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)
 
197
  query = ""
198
 
199
  # Mark all messages as old after displaying
200
+ st.session_state['chat_history_page1'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page1']]
201
 
202
  except Exception as e:
203
  st.error(f"Upsi, an unexpected error occurred: {e}")
 
235
  st.error("File not found. Please check the file path.")
236
  return
237
 
238
+ VectorStore = load_vector_store(pdf_path2, "vector_store_page2", force_reload=False)
 
239
 
240
+
 
241
 
242
+ display_chat_history(st.session_state['chat_history_page1'])
243
 
244
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
245
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
 
273
 
274
 
275
  if query:
276
+ st.session_state['chat_history_page2'].append(("User", query, "new"))
277
 
278
  # Start timing
279
  start_time = time.time()
 
295
  # You can use Streamlit's text function to display the timing
296
  st.text(f"Response time: {duration:.2f} seconds")
297
 
298
+ st.session_state['chat_history_page2'].append(("Bot", response, "new"))
299
 
300
 
301
  # Display new messages at the bottom
302
+ new_messages = st.session_state['chat_history_page2'][-2:]
303
  for chat in new_messages:
304
  background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
305
  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)
 
309
  query = ""
310
 
311
  # Mark all messages as old after displaying
312
+ st.session_state['chat_history_page2'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page2']]
313
 
314
  except Exception as e:
315
  st.error(f"Upsi, an unexpected error occurred: {e}")
 
330
  st.write('Made with ❤️ by BinDoc GmbH')
331
 
332
  # Main area content based on page selection
333
+ if page == "Page 1":
334
  page1()
335
+ elif page == "Page 2":
336
  page2()
337
 
338
  if __name__ == "__main__":