Anne31415 commited on
Commit
35f13e1
·
1 Parent(s): 0cba2f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -24
app.py CHANGED
@@ -174,37 +174,38 @@ def main():
174
  for label, query, color in cloud_buttons:
175
  cloud_button(label, query, color=color)
176
 
 
177
  if st.session_state['button_clicked']:
178
  query = st.session_state['button_clicked']['query']
179
  st.session_state['chat_history'].append(("User", query, "new"))
180
  st.session_state['button_clicked'] = None
181
-
 
 
182
  if st.button("Ask") or (query and query != st.session_state.get('last_query')):
183
  st.session_state['chat_history'].append(("User", query, "new"))
184
  st.session_state['last_query'] = query
185
-
186
- if st.session_state['chat_history']:
187
- loading_message = st.empty()
188
- loading_message.text('Bot is thinking...')
189
-
190
- VectorStore = load_pdf(pdf_path)
191
- chain = load_chatbot()
192
- docs = VectorStore.similarity_search(query=query, k=3)
193
- with get_openai_callback() as cb:
194
- response = chain.run(input_documents=docs, question=query)
195
-
196
- st.session_state['chat_history'].append(("Bot", response, "new"))
197
-
198
- new_messages = st.session_state['chat_history'][-2:]
199
- for chat in new_messages:
200
- background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
201
- st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
202
-
203
- st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
204
-
205
- loading_message.empty()
206
-
207
- st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
208
 
209
  if __name__ == "__main__":
210
  main()
 
174
  for label, query, color in cloud_buttons:
175
  cloud_button(label, query, color=color)
176
 
177
+ # Handle button click from cloud buttons
178
  if st.session_state['button_clicked']:
179
  query = st.session_state['button_clicked']['query']
180
  st.session_state['chat_history'].append(("User", query, "new"))
181
  st.session_state['button_clicked'] = None
182
+ handle_response(query, pdf_path)
183
+
184
+ # Handle text input submission
185
  if st.button("Ask") or (query and query != st.session_state.get('last_query')):
186
  st.session_state['chat_history'].append(("User", query, "new"))
187
  st.session_state['last_query'] = query
188
+ handle_response(query, pdf_path)
189
+
190
+ def handle_response(query, pdf_path):
191
+ loading_message = st.empty()
192
+ loading_message.text('Bot is thinking...')
193
+
194
+ VectorStore = load_pdf(pdf_path)
195
+ chain = load_chatbot()
196
+ docs = VectorStore.similarity_search(query=query, k=3)
197
+ with get_openai_callback() as cb:
198
+ response = chain.run(input_documents=docs, question=query)
199
+
200
+ st.session_state['chat_history'].append(("Bot", response, "new"))
201
+ new_messages = st.session_state['chat_history'][-2:]
202
+ for chat in new_messages:
203
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
204
+ st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
205
+
206
+ st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
207
+ loading_message.empty()
208
+ st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
 
 
209
 
210
  if __name__ == "__main__":
211
  main()