shukdevdatta123 commited on
Commit
bc50dbe
·
verified ·
1 Parent(s): 164707b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -20,6 +20,12 @@ if 'openai_api_key' not in st.session_state:
20
  st.session_state.openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key:", type="password",
21
  value=st.session_state.openai_api_key)
22
 
 
 
 
 
 
 
23
  # Initialize session state for messages
24
  if "messages" not in st.session_state:
25
  st.session_state.messages = [
@@ -63,16 +69,24 @@ def load_data(uploaded_files):
63
 
64
  # Function to save the conversation
65
  def save_conversation():
66
- with open("conversations.json", "a") as f:
67
- json.dump(st.session_state.messages, f)
68
- f.write("\n")
 
 
 
 
 
 
 
69
 
70
  # Function to load previous conversations
71
  def load_conversations():
72
  if os.path.exists("conversations.json"):
73
  with open("conversations.json", "r") as f:
74
  conversations = [json.loads(line) for line in f]
75
- return conversations
 
76
  return []
77
 
78
  # Function to delete selected conversations
@@ -139,14 +153,14 @@ if st.sidebar.button("Toggle Previous Conversations"):
139
 
140
  # Show previous conversations if the toggle is enabled
141
  if st.session_state.show_conversations:
142
- st.sidebar.subheader("Previous Conversations")
143
  conversations = load_conversations()
144
 
145
  if conversations:
146
  selected_indices = []
147
  for i, conv in enumerate(conversations):
148
  st.sidebar.write(f"Conversation {i + 1}:")
149
- for message in conv:
150
  st.sidebar.write(f"{message['role']}: {message['content']}")
151
  # Checkbox for selecting conversation to delete
152
  if st.sidebar.checkbox(f"Select Conversation {i + 1} for Deletion", key=f"delete_checkbox_{i}"):
@@ -161,4 +175,4 @@ if st.session_state.show_conversations:
161
  else:
162
  st.sidebar.write("No previous conversations found.")
163
  else:
164
- st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")
 
20
  st.session_state.openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key:", type="password",
21
  value=st.session_state.openai_api_key)
22
 
23
+ # Add a text input for the username
24
+ if 'username' not in st.session_state:
25
+ st.session_state.username = ""
26
+
27
+ st.session_state.username = st.sidebar.text_input("Enter your username:")
28
+
29
  # Initialize session state for messages
30
  if "messages" not in st.session_state:
31
  st.session_state.messages = [
 
69
 
70
  # Function to save the conversation
71
  def save_conversation():
72
+ if st.session_state.username:
73
+ with open("conversations.json", "a") as f:
74
+ conversation_entry = {
75
+ "username": st.session_state.username,
76
+ "messages": st.session_state.messages
77
+ }
78
+ json.dump(conversation_entry, f)
79
+ f.write("\n")
80
+ else:
81
+ st.warning("Please enter a username before saving the conversation.")
82
 
83
  # Function to load previous conversations
84
  def load_conversations():
85
  if os.path.exists("conversations.json"):
86
  with open("conversations.json", "r") as f:
87
  conversations = [json.loads(line) for line in f]
88
+ # Filter conversations based on the current username
89
+ return [conv for conv in conversations if conv["username"] == st.session_state.username]
90
  return []
91
 
92
  # Function to delete selected conversations
 
153
 
154
  # Show previous conversations if the toggle is enabled
155
  if st.session_state.show_conversations:
156
+ st.sidebar.subheader("Your Previous Conversations")
157
  conversations = load_conversations()
158
 
159
  if conversations:
160
  selected_indices = []
161
  for i, conv in enumerate(conversations):
162
  st.sidebar.write(f"Conversation {i + 1}:")
163
+ for message in conv["messages"]:
164
  st.sidebar.write(f"{message['role']}: {message['content']}")
165
  # Checkbox for selecting conversation to delete
166
  if st.sidebar.checkbox(f"Select Conversation {i + 1} for Deletion", key=f"delete_checkbox_{i}"):
 
175
  else:
176
  st.sidebar.write("No previous conversations found.")
177
  else:
178
+ st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")