@ -1,6 +1,6 @@
import atexit
import os
import sys
import atexit
sys . path . append ( os . path . join ( os . path . dirname ( __file__ ) , os . path . pardir ) )
@ -9,9 +9,9 @@ from streamlit_chat import message
from query_methods import query , avail_query_methods
import pickle
conversations_file = " conversations.pkl "
def load_conversations ( ) :
try :
with open ( conversations_file , " rb " ) as f :
@ -44,10 +44,10 @@ def exit_handler():
# Perform cleanup operations here, like saving data or closing open files.
save_conversations ( st . session_state . conversations , st . session_state . current_conversation )
# Register the exit_handler function to be called when the program is closing.
atexit . register ( exit_handler )
st . header ( " Chat Placeholder " )
if ' conversations ' not in st . session_state :
@ -69,9 +69,10 @@ if 'query_method' not in st.session_state:
if ' current_conversation ' not in st . session_state or st . session_state [ ' current_conversation ' ] is None :
st . session_state [ ' current_conversation ' ] = { ' user_inputs ' : [ ] , ' generated_responses ' : [ ] }
input_placeholder = st . empty ( )
user_input = input_placeholder . text_input ( ' You: ' , key = f ' input_text_ { len ( st . session_state [ " current_conversation " ] [ " user_inputs " ] ) } ' )
user_input = input_placeholder . text_input (
' You: ' , key = f ' input_text_ { len ( st . session_state [ " current_conversation " ] [ " user_inputs " ] ) } '
)
submit_button = st . button ( " Submit " )
if user_input or submit_button :
@ -81,8 +82,9 @@ if user_input or submit_button:
st . session_state . current_conversation [ ' user_inputs ' ] . append ( user_input )
st . session_state . current_conversation [ ' generated_responses ' ] . append ( escaped_output )
save_conversations ( st . session_state . conversations , st . session_state . current_conversation )
user_input = input_placeholder . text_input ( ' You: ' , value = ' ' , key = f ' input_text_ { len ( st . session_state [ " current_conversation " ] [ " user_inputs " ] ) } ' ) # Clear the input field
user_input = input_placeholder . text_input (
' You: ' , value = ' ' , key = f ' input_text_ { len ( st . session_state [ " current_conversation " ] [ " user_inputs " ] ) } '
) # Clear the input field
# Add a button to create a new conversation
if st . sidebar . button ( " New Conversation " ) :
@ -90,11 +92,7 @@ if st.sidebar.button("New Conversation"):
st . session_state [ ' current_conversation ' ] = { ' user_inputs ' : [ ] , ' generated_responses ' : [ ] }
st . session_state [ ' input_field_key ' ] + = 1
st . session_state [ ' query_method ' ] = st . sidebar . selectbox (
" Select API: " ,
options = avail_query_methods ,
index = 0
)
st . session_state [ ' query_method ' ] = st . sidebar . selectbox ( " Select API: " , options = avail_query_methods , index = 0 )
# Sidebar
st . sidebar . header ( " Conversation History " )