Shreyas094 commited on
Commit
298b773
·
verified ·
1 Parent(s): 50bdf58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -138,20 +138,30 @@ def delete_documents(selected_docs):
138
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
139
 
140
  deleted_docs = []
141
- for doc_name in selected_docs:
142
- # Remove from uploaded_documents list
143
- uploaded_documents = [doc for doc in uploaded_documents if doc["name"] != doc_name]
144
-
145
- # Remove from FAISS database
146
- docs_to_keep = [doc for doc in database.docstore._dict.values() if doc.metadata["source"] != doc_name]
 
 
 
 
 
 
 
 
 
 
 
147
  database = FAISS.from_documents(docs_to_keep, embed)
148
-
149
- deleted_docs.append(doc_name)
150
 
151
  # Save updated FAISS database
152
  database.save_local("faiss_database")
153
 
154
- # Save updated uploaded_documents list
 
155
  save_documents(uploaded_documents)
156
 
157
  return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()
 
138
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
139
 
140
  deleted_docs = []
141
+ docs_to_keep = []
142
+ for doc in database.docstore._dict.values():
143
+ if doc.metadata["source"] not in selected_docs:
144
+ docs_to_keep.append(doc)
145
+ else:
146
+ deleted_docs.append(doc.metadata["source"])
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
+ else:
157
+ # Create new FAISS index with remaining documents
158
  database = FAISS.from_documents(docs_to_keep, embed)
 
 
159
 
160
  # Save updated FAISS database
161
  database.save_local("faiss_database")
162
 
163
+ # Update uploaded_documents list
164
+ uploaded_documents = [doc for doc in uploaded_documents if doc["name"] not in deleted_docs]
165
  save_documents(uploaded_documents)
166
 
167
  return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()