Shreyas094 commited on
Commit
1387ba4
·
verified ·
1 Parent(s): 76b11d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -112,16 +112,24 @@ def update_vectors(files, parser):
112
 
113
  logging.info(f"Total chunks processed: {total_chunks}")
114
 
115
- if os.path.exists("faiss_database"):
116
- logging.info("Updating existing FAISS database")
117
- database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
118
- database.add_documents(all_data)
119
- else:
120
- logging.info("Creating new FAISS database")
121
- database = FAISS.from_documents(all_data, embed)
122
-
123
- database.save_local("faiss_database")
124
- logging.info("FAISS database saved")
 
 
 
 
 
 
 
 
125
 
126
  # Save the updated list of documents
127
  save_documents(uploaded_documents)
@@ -146,22 +154,21 @@ def delete_documents(selected_docs):
146
  deleted_docs.append(doc.metadata.get("source", "Unknown"))
147
 
148
  # Print debugging information
149
- print(f"Total documents before deletion: {len(database.docstore._dict)}")
150
- print(f"Documents to keep: {len(docs_to_keep)}")
151
- print(f"Documents to delete: {len(deleted_docs)}")
152
 
153
  if not docs_to_keep:
154
- # If all documents are deleted, create an empty FAISS index
155
- database = FAISS.from_texts([""], embed)
156
- logging.info("All documents deleted. Created an empty FAISS index.")
 
157
  else:
158
  # Create new FAISS index with remaining documents
159
  database = FAISS.from_documents(docs_to_keep, embed)
 
160
  logging.info(f"Created new FAISS index with {len(docs_to_keep)} documents.")
161
 
162
- # Save updated FAISS database
163
- database.save_local("faiss_database")
164
-
165
  # Update uploaded_documents list
166
  uploaded_documents = [doc for doc in uploaded_documents if doc["name"] not in deleted_docs]
167
  save_documents(uploaded_documents)
 
112
 
113
  logging.info(f"Total chunks processed: {total_chunks}")
114
 
115
+ try:
116
+ if os.path.exists("faiss_database"):
117
+ logging.info("Updating existing FAISS database")
118
+ database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
119
+ if len(database.docstore._dict) == 0:
120
+ logging.info("Existing database is empty. Creating new FAISS database")
121
+ database = FAISS.from_documents(all_data, embed)
122
+ else:
123
+ database.add_documents(all_data)
124
+ else:
125
+ logging.info("Creating new FAISS database")
126
+ database = FAISS.from_documents(all_data, embed)
127
+
128
+ database.save_local("faiss_database")
129
+ logging.info("FAISS database saved")
130
+ except Exception as e:
131
+ logging.error(f"Error updating FAISS database: {str(e)}")
132
+ return f"Error updating vector store: {str(e)}", display_documents()
133
 
134
  # Save the updated list of documents
135
  save_documents(uploaded_documents)
 
154
  deleted_docs.append(doc.metadata.get("source", "Unknown"))
155
 
156
  # Print debugging information
157
+ logging.info(f"Total documents before deletion: {len(database.docstore._dict)}")
158
+ logging.info(f"Documents to keep: {len(docs_to_keep)}")
159
+ logging.info(f"Documents to delete: {len(deleted_docs)}")
160
 
161
  if not docs_to_keep:
162
+ # If all documents are deleted, remove the FAISS database file
163
+ if os.path.exists("faiss_database"):
164
+ os.remove("faiss_database")
165
+ logging.info("All documents deleted. Removed FAISS database file.")
166
  else:
167
  # Create new FAISS index with remaining documents
168
  database = FAISS.from_documents(docs_to_keep, embed)
169
+ database.save_local("faiss_database")
170
  logging.info(f"Created new FAISS index with {len(docs_to_keep)} documents.")
171
 
 
 
 
172
  # Update uploaded_documents list
173
  uploaded_documents = [doc for doc in uploaded_documents if doc["name"] not in deleted_docs]
174
  save_documents(uploaded_documents)