Anne31415 commited on
Commit
713303c
·
verified ·
1 Parent(s): e1abf0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -38
app.py CHANGED
@@ -175,29 +175,7 @@ def ask_bot(query):
175
  # Kombiniere den standardmäßigen Prompt mit der Benutzeranfrage
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,27 +241,51 @@ def page1():
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():
 
175
  # Kombiniere den standardmäßigen Prompt mit der Benutzeranfrage
176
  full_query = standard_prompt + query
177
  return full_query
178
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  def page1():
181
  try:
 
241
 
242
 
243
 
244
+ if query:
245
+ full_query = ask_bot(query)
246
+ st.session_state['chat_history_page1'].append(("User", query, "new"))
247
+
248
+ # Start timing
249
+ start_time = time.time()
250
+
251
+ with st.spinner('Bot is thinking...'):
252
+ chain = load_chatbot()
253
+ docs = VectorStore.similarity_search(query=query, k=5)
254
+ with get_openai_callback() as cb:
255
+ response = chain.run(input_documents=docs, question=full_query)
256
+ response = handle_no_answer(response) # Process the response through the new function
257
 
 
 
 
258
 
259
+
260
+ # Stop timing
261
+ end_time = time.time()
262
+
263
+ # Calculate duration
264
+ duration = end_time - start_time
265
 
266
+ # You can use Streamlit's text function to display the timing
267
+ st.text(f"Response time: {duration:.2f} seconds")
268
+
269
+ st.session_state['chat_history_page1'].append(("Bot", response, "new"))
270
+
271
+
272
+ # Display new messages at the bottom
273
+ new_messages = st.session_state['chat_history_page1'][-2:]
274
+ for chat in new_messages:
275
+ background_color = "#ffeecf" if chat[2] == "new" else "#ffeecf" if chat[0] == "User" else "#ffeecf"
276
+ 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)
277
+
278
+
279
+ # Clear the input field after the query is made
280
+ query = ""
281
+
282
+ # Mark all messages as old after displaying
283
+ st.session_state['chat_history_page1'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history_page1']]
284
 
285
  except Exception as e:
286
  st.error(f"Upsi, an unexpected error occurred: {e}")
287
+ # Optionally log the exception details to a file or error tracking service
288
+
289
 
290
 
291
  def page2():