Anne31415 commited on
Commit
e1abf0c
·
verified ·
1 Parent(s): 3031f8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -56
app.py CHANGED
@@ -176,23 +176,28 @@ def ask_bot(query):
176
  full_query = standard_prompt + query
177
  return full_query
178
 
179
- def add_to_chat_history(speaker, message, history_key):
180
- if history_key in st.session_state:
181
- st.session_state[history_key].append({"speaker": speaker, "message": message})
182
-
183
- def construct_context(chat_history):
184
- # Limit the context to the last few interactions (e.g., last 5)
185
- limited_history = chat_history[-5:]
186
- return " ".join([f"{entry['speaker']}: {entry['message']}" for entry in limited_history])
187
-
188
- def display_latest_messages(chat_history):
189
- latest_messages = chat_history[-2:]
190
- for chat_entry in latest_messages:
191
- speaker, message = chat_entry['speaker'], chat_entry['message']
192
- background_color = "#ffeecf" if speaker == "User" else "#e0ecff"
193
- st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{speaker}: {message}</div>", unsafe_allow_html=True)
194
 
195
-
 
 
 
 
 
 
 
196
 
197
  def page1():
198
  try:
@@ -258,49 +263,27 @@ def page1():
258
 
259
 
260
 
261
- if query:
262
- full_query = ask_bot(query) # Generate full query using ask_bot function
263
-
264
- # Start timing
265
- start_time = time.time()
266
-
267
- with st.spinner('Bot is thinking...'):
268
- # Load the chatbot model
269
- chain = load_chatbot()
270
-
271
- # Perform the similarity search and get the response
272
- docs = VectorStore.similarity_search(query=full_query, k=5)
273
- with get_openai_callback() as cb:
274
- response = chain.run(input_documents=docs, question=full_query)
275
- response = handle_no_answer(response)
276
-
277
- # Stop timing
278
- end_time = time.time()
279
-
280
- # Calculate duration
281
- duration = end_time - start_time
282
- st.text(f"Response time: {duration:.2f} seconds")
283
-
284
- # Update chat history after the bot's response
285
- st.session_state['chat_history_page1'].append(("User", query))
286
- st.session_state['chat_history_page1'].append(("Bot", response))
287
-
288
- # Display new messages at the bottom
289
- new_messages = st.session_state['chat_history_page1'][-2:]
290
- for chat in new_messages:
291
- background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
292
- 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)
293
-
294
- # Clear the input field after the query is made
295
- query = ""
296
-
297
- # Mark all messages as old after displaying
298
- st.session_state['chat_history_page1'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page1']]
299
 
300
  except Exception as e:
301
  st.error(f"Upsi, an unexpected error occurred: {e}")
302
- # Optionally log the exception details to a file or error tracking service
303
-
304
 
305
 
306
  def page2():
 
176
  full_query = standard_prompt + query
177
  return full_query
178
 
179
+ def get_chatbot_response(query, VectorStore):
180
+ chain = load_chatbot()
181
+ docs = VectorStore.similarity_search(query=query, k=5)
182
+ with get_openai_callback() as cb:
183
+ response = chain.run(input_documents=docs, question=query)
184
+ return handle_no_answer(response)
185
+
186
+ def combine_chat_history_with_query(chat_history, query):
187
+ combined_history = ""
188
+ for chat in chat_history:
189
+ if len(chat) >= 2:
190
+ combined_history += f"{chat[0]}: {chat[1]} "
191
+ return combined_history + query
 
 
192
 
193
+ def display_new_messages(placeholder, chat_history):
194
+ new_messages = chat_history[-2:]
195
+ for chat in new_messages:
196
+ background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
197
+ 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)
198
+
199
+ def mark_messages_as_old(chat_history):
200
+ return [(sender, msg, "old") for sender, msg, _ in chat_history]
201
 
202
  def page1():
203
  try:
 
263
 
264
 
265
 
266
+ if query:
267
+ full_query = ask_bot(query)
268
+ combined_query = combine_chat_history_with_query(st.session_state['chat_history_page1'], full_query)
269
+ st.session_state['chat_history_page1'].append(("User", query, "new"))
270
+
271
+ start_time = time.time()
272
+ with st.spinner('Bot is thinking...'):
273
+ response = get_chatbot_response(combined_query, VectorStore)
274
+ st.session_state['chat_history_page1'].append(("Bot", response, "new"))
275
+
276
+ end_time = time.time()
277
+ duration = end_time - start_time
278
+ st.text(f"Response time: {duration:.2f} seconds")
279
+
280
+ display_new_messages(new_messages_placeholder, st.session_state['chat_history_page1'])
281
+ query = ""
282
+
283
+ st.session_state['chat_history_page1'] = mark_messages_as_old(st.session_state['chat_history_page1'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
 
285
  except Exception as e:
286
  st.error(f"Upsi, an unexpected error occurred: {e}")
 
 
287
 
288
 
289
  def page2():