AlanOC commited on
Commit
1ba5adb
1 Parent(s): 6b90e20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -223,11 +223,18 @@ def main():
223
 
224
 
225
 
226
-
227
-
228
  # Initialize session state variables
229
- if 'chat_history' not in st.session_state:
230
- st.session_state['chat_history'] = []
 
 
 
 
 
 
 
 
 
231
 
232
  # Function to handle user queries
233
  def handle_query(user_query):
@@ -250,25 +257,12 @@ def main():
250
  st.write("**Answer:**")
251
  st.write(answer)
252
 
253
- # Input field for new question
254
- user_query = st.text_input("**Question**", key='new_user_query')
255
-
256
  # Handle new query
257
  if st.button("Ask", key='new_ask_button') and user_query:
258
  with st.spinner('Thinking...'):
259
  ai_response = ask_alans_ai(user_query, 'vectordb_placeholder', st.session_state['chat_history'])
260
- # Append response to chat history and display immediately
261
  st.session_state['chat_history'].append((user_query, ai_response))
262
- # Display the latest question-answer pair immediately
263
- with st.chat_message("user", avatar='./user.png'):
264
- st.write("**Question:**")
265
- st.write(user_query)
266
- with st.chat_message("assistant", avatar='./ci.png'):
267
- st.write("**Answer:**")
268
- st.write(ai_response)
269
- # Clear the input field
270
- st.session_state['new_user_query'] = ""
271
- # Rerun the script to update the chat history display
272
  st.experimental_rerun()
273
 
274
  # Clear chat history button
@@ -295,6 +289,7 @@ def main():
295
 
296
 
297
 
 
298
  # Run the Streamlit app
299
  if __name__ == "__main__":
300
  main()
 
223
 
224
 
225
 
 
 
226
  # Initialize session state variables
227
+ st.session_state.setdefault('chat_history', [])
228
+ st.session_state.setdefault('ask_pressed', False)
229
+
230
+ # Clear the input field if 'Ask' was pressed in the previous run
231
+ if st.session_state['ask_pressed']:
232
+ user_query = ""
233
+ st.session_state['ask_pressed'] = False
234
+ else:
235
+ user_query = st.text_input("**Question**", key='new_user_query')
236
+
237
+
238
 
239
  # Function to handle user queries
240
  def handle_query(user_query):
 
257
  st.write("**Answer:**")
258
  st.write(answer)
259
 
 
 
 
260
  # Handle new query
261
  if st.button("Ask", key='new_ask_button') and user_query:
262
  with st.spinner('Thinking...'):
263
  ai_response = ask_alans_ai(user_query, 'vectordb_placeholder', st.session_state['chat_history'])
 
264
  st.session_state['chat_history'].append((user_query, ai_response))
265
+ st.session_state['ask_pressed'] = True
 
 
 
 
 
 
 
 
 
266
  st.experimental_rerun()
267
 
268
  # Clear chat history button
 
289
 
290
 
291
 
292
+
293
  # Run the Streamlit app
294
  if __name__ == "__main__":
295
  main()