Spaces:
Paused
Paused
Shreyas094
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -112,16 +112,24 @@ def update_vectors(files, parser):
|
|
112 |
|
113 |
logging.info(f"Total chunks processed: {total_chunks}")
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
150 |
-
|
151 |
-
|
152 |
|
153 |
if not docs_to_keep:
|
154 |
-
# If all documents are deleted,
|
155 |
-
|
156 |
-
|
|
|
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)
|