Rulga commited on
Commit
03a0824
Β·
1 Parent(s): 4123d86

feat: add directory permission checks for saving vector store and chat history

Browse files
Files changed (1) hide show
  1. app.py +62 -9
app.py CHANGED
@@ -188,28 +188,70 @@ def load_chat_history():
188
  return []
189
  return []
190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  def force_save_vector_store(vector_store):
192
  """Ensures vector store is properly saved to disk"""
193
  try:
194
- # Ensure directory exists
195
- os.makedirs(VECTOR_STORE_PATH, exist_ok=True)
 
 
196
 
197
  # Save vector store
198
  vector_store.save_local(VECTOR_STORE_PATH)
199
 
200
- # Verify files were created
201
- if not os.path.exists(os.path.join(VECTOR_STORE_PATH, "index.faiss")):
 
202
  raise Exception("Vector store files were not created")
203
 
 
 
 
 
204
  st.caption("βœ… Vector store saved successfully")
 
205
  except Exception as e:
206
- st.caption(f"❌ Failed to save vector store: {e}")
 
 
 
207
 
208
  def force_save_chat_history(chat_entry):
209
  """Ensures chat history is properly saved to disk"""
210
  try:
211
- # Ensure directory exists
212
- os.makedirs(CHAT_HISTORY_DIR, exist_ok=True)
 
 
213
 
214
  current_date = datetime.now().strftime("%Y-%m-%d")
215
  filename = os.path.join(CHAT_HISTORY_DIR, f"chat_history_{current_date}.json")
@@ -228,10 +270,21 @@ def force_save_chat_history(chat_entry):
228
  json.dump(existing_history, f, ensure_ascii=False, indent=2)
229
  f.flush()
230
  os.fsync(f.fileno())
231
-
 
 
 
 
 
 
 
232
  st.caption("βœ… Chat history saved successfully")
 
233
  except Exception as e:
234
- st.caption(f"❌ Failed to save chat history: {e}")
 
 
 
235
 
236
  # Main function
237
  def main():
 
188
  return []
189
  return []
190
 
191
+ def check_directory_permissions(directory):
192
+ """Check if directory has proper read/write permissions"""
193
+ try:
194
+ # Check if directory exists and create if not
195
+ os.makedirs(directory, exist_ok=True)
196
+
197
+ # Try to create a test file
198
+ test_file = os.path.join(directory, "write_test.txt")
199
+ with open(test_file, "w") as f:
200
+ f.write("test")
201
+ f.flush()
202
+ os.fsync(f.fileno()) # Force write to disk
203
+
204
+ # Try to read the test file
205
+ with open(test_file, "r") as f:
206
+ content = f.read()
207
+ if content != "test":
208
+ raise Exception("File content verification failed")
209
+
210
+ # Clean up
211
+ os.remove(test_file)
212
+
213
+ return True, None
214
+
215
+ except Exception as e:
216
+ permissions = oct(os.stat(directory).st_mode)[-3:] if os.path.exists(directory) else "N/A"
217
+ error_msg = f"Permission error: {str(e)} (Directory permissions: {permissions})"
218
+ return False, error_msg
219
+
220
  def force_save_vector_store(vector_store):
221
  """Ensures vector store is properly saved to disk"""
222
  try:
223
+ # Check directory permissions
224
+ success, error_msg = check_directory_permissions(VECTOR_STORE_PATH)
225
+ if not success:
226
+ raise Exception(error_msg)
227
 
228
  # Save vector store
229
  vector_store.save_local(VECTOR_STORE_PATH)
230
 
231
+ # Verify vector store files were created
232
+ index_file = os.path.join(VECTOR_STORE_PATH, "index.faiss")
233
+ if not os.path.exists(index_file):
234
  raise Exception("Vector store files were not created")
235
 
236
+ # Verify file permissions
237
+ if not os.access(index_file, os.R_OK | os.W_OK):
238
+ raise Exception(f"Insufficient permissions for vector store files")
239
+
240
  st.caption("βœ… Vector store saved successfully")
241
+
242
  except Exception as e:
243
+ error_msg = f"❌ Failed to save vector store: {str(e)}"
244
+ st.caption(error_msg)
245
+ st.error(error_msg) # Also show as error message
246
+ raise Exception(error_msg)
247
 
248
  def force_save_chat_history(chat_entry):
249
  """Ensures chat history is properly saved to disk"""
250
  try:
251
+ # Check directory permissions
252
+ success, error_msg = check_directory_permissions(CHAT_HISTORY_DIR)
253
+ if not success:
254
+ raise Exception(error_msg)
255
 
256
  current_date = datetime.now().strftime("%Y-%m-%d")
257
  filename = os.path.join(CHAT_HISTORY_DIR, f"chat_history_{current_date}.json")
 
270
  json.dump(existing_history, f, ensure_ascii=False, indent=2)
271
  f.flush()
272
  os.fsync(f.fileno())
273
+
274
+ # Verify file was created and is readable
275
+ if not os.path.exists(filename):
276
+ raise Exception("Chat history file was not created")
277
+
278
+ if not os.access(filename, os.R_OK | os.W_OK):
279
+ raise Exception(f"Insufficient permissions for chat history file")
280
+
281
  st.caption("βœ… Chat history saved successfully")
282
+
283
  except Exception as e:
284
+ error_msg = f"❌ Failed to save chat history: {str(e)}"
285
+ st.caption(error_msg)
286
+ st.error(error_msg) # Also show as error message
287
+ raise Exception(error_msg)
288
 
289
  # Main function
290
  def main():