Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -176,23 +176,28 @@ def ask_bot(query):
|
|
| 176 |
full_query = standard_prompt + query
|
| 177 |
return full_query
|
| 178 |
|
| 179 |
-
def
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 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 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 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():
|