shukdevdatta123 commited on
Commit
774ce77
·
verified ·
1 Parent(s): e52bc84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -47,6 +47,7 @@ def read_docx(file):
47
 
48
  @st.cache_resource(show_spinner=False)
49
  def load_data(uploaded_files):
 
50
  llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
51
  system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
52
 
@@ -65,21 +66,13 @@ def load_data(uploaded_files):
65
 
66
  # Function to save the conversation
67
  def save_conversation():
68
- conversations = load_conversations() # Load existing conversations
69
- conversation_count = len(conversations) + 1 # Determine the next conversation number
70
-
71
  with open("conversations.json", "a") as f:
72
  conversation_data = {
73
  "messages": st.session_state.messages,
74
  "file_names": st.session_state.uploaded_file_names
75
  }
76
- # Prepend conversation number to the saved data
77
- conversation_with_number = {
78
- "conversation_number": conversation_count,
79
- **conversation_data
80
- }
81
- if isinstance(conversation_with_number, dict):
82
- json.dump(conversation_with_number, f)
83
  f.write("\n")
84
 
85
  # Function to load previous conversations
@@ -87,13 +80,17 @@ def load_conversations():
87
  if os.path.exists("conversations.json"):
88
  with open("conversations.json", "r") as f:
89
  conversations = [json.loads(line) for line in f]
 
 
 
 
90
  return conversations
91
  return []
92
 
93
  # Function to delete selected conversations
94
- def delete_selected_conversations(selected_conversation_numbers):
95
  conversations = load_conversations()
96
- remaining_conversations = [conv for conv in conversations if conv.get('conversation_number') not in selected_conversation_numbers]
97
  with open("conversations.json", "w") as f:
98
  for conv in remaining_conversations:
99
  json.dump(conv, f)
@@ -161,16 +158,15 @@ if st.session_state.show_conversations:
161
 
162
  if conversations:
163
  selected_indices = []
164
- for conv in conversations:
165
  if isinstance(conv, dict): # Ensure conv is a dictionary
166
- conv_number = conv.get('conversation_number', 'Unknown')
167
- st.sidebar.write(f"Conversation {conv_number}:")
168
  for message in conv.get('messages', []): # Use get safely
169
  st.sidebar.write(f"{message['role']}: {message['content']}")
170
  st.sidebar.write(f"Files: {', '.join(conv.get('file_names', []))}")
171
  # Checkbox for selecting conversation to delete
172
- if st.sidebar.checkbox(f"Select Conversation {conv_number} for Deletion", key=f"delete_checkbox_{conv_number}"):
173
- selected_indices.append(conv_number)
174
  else:
175
  print("Warning: Encountered a non-dictionary conversation:", conv)
176
 
@@ -183,4 +179,4 @@ if st.session_state.show_conversations:
183
  else:
184
  st.sidebar.write("No previous conversations found.")
185
  else:
186
- st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")
 
47
 
48
  @st.cache_resource(show_spinner=False)
49
  def load_data(uploaded_files):
50
+ # Create the LLM instance outside the cache
51
  llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5,
52
  system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
53
 
 
66
 
67
  # Function to save the conversation
68
  def save_conversation():
 
 
 
69
  with open("conversations.json", "a") as f:
70
  conversation_data = {
71
  "messages": st.session_state.messages,
72
  "file_names": st.session_state.uploaded_file_names
73
  }
74
+ if isinstance(conversation_data, dict):
75
+ json.dump(conversation_data, f)
 
 
 
 
 
76
  f.write("\n")
77
 
78
  # Function to load previous conversations
 
80
  if os.path.exists("conversations.json"):
81
  with open("conversations.json", "r") as f:
82
  conversations = [json.loads(line) for line in f]
83
+ # Ensure each conversation is a dictionary
84
+ for conv in conversations:
85
+ if not isinstance(conv, dict):
86
+ print("Warning: Loaded conversation is not a dictionary:", conv)
87
  return conversations
88
  return []
89
 
90
  # Function to delete selected conversations
91
+ def delete_selected_conversations(selected_indices):
92
  conversations = load_conversations()
93
+ remaining_conversations = [conv for i, conv in enumerate(conversations) if i not in selected_indices]
94
  with open("conversations.json", "w") as f:
95
  for conv in remaining_conversations:
96
  json.dump(conv, f)
 
158
 
159
  if conversations:
160
  selected_indices = []
161
+ for i, conv in enumerate(conversations):
162
  if isinstance(conv, dict): # Ensure conv is a dictionary
163
+ st.sidebar.write(f"Conversation {i + 1}:")
 
164
  for message in conv.get('messages', []): # Use get safely
165
  st.sidebar.write(f"{message['role']}: {message['content']}")
166
  st.sidebar.write(f"Files: {', '.join(conv.get('file_names', []))}")
167
  # Checkbox for selecting conversation to delete
168
+ if st.sidebar.checkbox(f"Select Conversation {i + 1} for Deletion", key=f"delete_checkbox_{i}"):
169
+ selected_indices.append(i)
170
  else:
171
  print("Warning: Encountered a non-dictionary conversation:", conv)
172
 
 
179
  else:
180
  st.sidebar.write("No previous conversations found.")
181
  else:
182
+ st.sidebar.write("Previous conversations are hidden. Click 'Toggle Previous Conversations' to show.")