shukdevdatta123 commited on
Commit
c6657a0
Β·
verified Β·
1 Parent(s): 3480f17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -8,7 +8,7 @@ from dotenv import load_dotenv
8
  import json
9
 
10
  # Load environment variables from .env file
11
- load_dotenv("")
12
 
13
  st.header("Chat with the uploaded docs πŸ’¬ πŸ“š")
14
 
@@ -79,26 +79,25 @@ def save_conversation():
79
  else:
80
  st.warning("Please enter a phone number before saving the conversation.")
81
 
82
- # Function to load previous conversations
83
- def load_conversations():
84
  if os.path.exists("conversations.json"):
85
  with open("conversations.json", "r") as f:
86
  conversations = []
87
  for line in f:
88
  try:
89
  conversation = json.loads(line)
90
- conversations.append(conversation)
 
91
  except json.JSONDecodeError:
92
  st.error("Error decoding JSON from conversations file. Please check the file.")
93
  continue
94
-
95
- # Filter conversations based on the current phone number
96
- return [conv for conv in conversations if isinstance(conv, dict) and "phone_number" in conv and conv["phone_number"] == st.session_state.phone_number]
97
  return []
98
 
99
  # Function to delete selected conversations
100
- def delete_selected_conversations(selected_indices):
101
- conversations = load_conversations()
102
  remaining_conversations = [conv for i, conv in enumerate(conversations) if i not in selected_indices]
103
  with open("conversations.json", "w") as f:
104
  for conv in remaining_conversations:
@@ -158,10 +157,10 @@ if 'show_conversations' not in st.session_state:
158
  if st.sidebar.button("Toggle Previous Conversations"):
159
  st.session_state.show_conversations = not st.session_state.show_conversations
160
 
161
- # Show previous conversations if the toggle is enabled
162
- if st.session_state.show_conversations:
163
  st.sidebar.subheader("Your Previous Conversations")
164
- conversations = load_conversations()
165
 
166
  if conversations:
167
  selected_indices = []
@@ -175,11 +174,11 @@ if st.session_state.show_conversations:
175
 
176
  if st.sidebar.button("Delete Selected Conversations"):
177
  if selected_indices:
178
- delete_selected_conversations(selected_indices)
179
- st.success("Selected conversations deleted. Please Refresh to See the Effect!")
180
  st.session_state.messages = [] # Optional: reset messages for a fresh start
181
 
182
  else:
183
- st.sidebar.write("No previous conversations found.")
184
  else:
185
- st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")
 
8
  import json
9
 
10
  # Load environment variables from .env file
11
+ load_dotenv()
12
 
13
  st.header("Chat with the uploaded docs πŸ’¬ πŸ“š")
14
 
 
79
  else:
80
  st.warning("Please enter a phone number before saving the conversation.")
81
 
82
+ # Function to load previous conversations based on phone number
83
+ def load_conversations(phone_number):
84
  if os.path.exists("conversations.json"):
85
  with open("conversations.json", "r") as f:
86
  conversations = []
87
  for line in f:
88
  try:
89
  conversation = json.loads(line)
90
+ if conversation.get("phone_number") == phone_number:
91
+ conversations.append(conversation)
92
  except json.JSONDecodeError:
93
  st.error("Error decoding JSON from conversations file. Please check the file.")
94
  continue
95
+ return conversations
 
 
96
  return []
97
 
98
  # Function to delete selected conversations
99
+ def delete_selected_conversations(selected_indices, phone_number):
100
+ conversations = load_conversations(phone_number)
101
  remaining_conversations = [conv for i, conv in enumerate(conversations) if i not in selected_indices]
102
  with open("conversations.json", "w") as f:
103
  for conv in remaining_conversations:
 
157
  if st.sidebar.button("Toggle Previous Conversations"):
158
  st.session_state.show_conversations = not st.session_state.show_conversations
159
 
160
+ # Show previous conversations if the toggle is enabled and phone number is provided
161
+ if st.session_state.show_conversations and st.session_state.phone_number:
162
  st.sidebar.subheader("Your Previous Conversations")
163
+ conversations = load_conversations(st.session_state.phone_number)
164
 
165
  if conversations:
166
  selected_indices = []
 
174
 
175
  if st.sidebar.button("Delete Selected Conversations"):
176
  if selected_indices:
177
+ delete_selected_conversations(selected_indices, st.session_state.phone_number)
178
+ st.success("Selected conversations deleted. Please refresh to see the effect!")
179
  st.session_state.messages = [] # Optional: reset messages for a fresh start
180
 
181
  else:
182
+ st.sidebar.write("No previous conversations found for this phone number.")
183
  else:
184
+ st.sidebar.write("Previous conversations are hidden. Enter your phone number to view them.")