Ramanjit commited on
Commit
e80d7b7
·
1 Parent(s): 056b4f0

Implement chat clearing functionality with confirmation and initialize session state variables

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +41 -1
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  chat.db
2
  .env
 
 
1
  chat.db
2
  .env
3
+ .venv
app.py CHANGED
@@ -107,6 +107,11 @@ PLIVO_PHONE_NUMBER = st.text_input(
107
  "Enter your phone number to chat with KORA", value="+13602304837", max_chars=15
108
  )
109
 
 
 
 
 
 
110
 
111
  # Display chat history
112
  try:
@@ -124,6 +129,34 @@ try:
124
  except sqlite3.Error as e:
125
  st.error(f"Error fetching messages: {e}")
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  # Input + Send
128
  if prompt := st.chat_input("Type your message..."):
129
  try:
@@ -170,4 +203,11 @@ def check_for_new_messages():
170
  time.sleep(5)
171
 
172
 
173
- check_for_new_messages()
 
 
 
 
 
 
 
 
107
  "Enter your phone number to chat with KORA", value="+13602304837", max_chars=15
108
  )
109
 
110
+ # Initialize session state variables if they don't exist
111
+ if "show_confirmation" not in st.session_state:
112
+ st.session_state.show_confirmation = False
113
+ if "last_message" not in st.session_state:
114
+ st.session_state.last_message = ""
115
 
116
  # Display chat history
117
  try:
 
129
  except sqlite3.Error as e:
130
  st.error(f"Error fetching messages: {e}")
131
 
132
+ # Add a "Clear Chat" button with confirmation
133
+ if not st.session_state.show_confirmation:
134
+ clear_chat_button = st.button("Clear Chat")
135
+ if clear_chat_button:
136
+ st.session_state.show_confirmation = True
137
+ st.rerun()
138
+ else:
139
+ confirmation_col1, confirmation_col2 = st.columns(2)
140
+ with confirmation_col1:
141
+ confirm_clear = st.button("Yes, clear chat")
142
+ with confirmation_col2:
143
+ cancel_clear = st.button("Cancel")
144
+
145
+ if confirm_clear:
146
+ try:
147
+ with db_lock:
148
+ c.execute("DELETE FROM messages")
149
+ conn.commit()
150
+ st.session_state.show_confirmation = False
151
+ st.session_state.last_message = ""
152
+ st.success("Chat history cleared successfully.")
153
+ st.rerun() # Refresh the page to show empty chat
154
+ except sqlite3.Error as e:
155
+ st.error(f"Error clearing chat history: {e}")
156
+ elif cancel_clear:
157
+ st.session_state.show_confirmation = False
158
+ st.rerun() # Refresh the page to remove confirmation buttons
159
+
160
  # Input + Send
161
  if prompt := st.chat_input("Type your message..."):
162
  try:
 
203
  time.sleep(5)
204
 
205
 
206
+ # Start the message checking thread only once as a daemon thread
207
+ if not hasattr(st, "message_checker_started"):
208
+ try:
209
+ message_thread = threading.Thread(target=check_for_new_messages, daemon=True)
210
+ message_thread.start()
211
+ st.message_checker_started = True
212
+ except Exception as e:
213
+ logging.error(f"Error starting message checker thread: {e}")