Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ from langchain_core.documents import Document
|
|
| 15 |
from huggingface_hub import InferenceClient
|
| 16 |
import inspect
|
| 17 |
import logging
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# Set up basic configuration for logging
|
|
@@ -116,11 +117,7 @@ def update_vectors(files, parser):
|
|
| 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 |
-
|
| 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)
|
|
@@ -159,14 +156,14 @@ def delete_documents(selected_docs):
|
|
| 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
|
| 163 |
if os.path.exists("faiss_database"):
|
| 164 |
-
|
| 165 |
-
logging.info("All documents deleted. Removed FAISS database
|
| 166 |
else:
|
| 167 |
# Create new FAISS index with remaining documents
|
| 168 |
-
|
| 169 |
-
|
| 170 |
logging.info(f"Created new FAISS index with {len(docs_to_keep)} documents.")
|
| 171 |
|
| 172 |
# Update uploaded_documents list
|
|
|
|
| 15 |
from huggingface_hub import InferenceClient
|
| 16 |
import inspect
|
| 17 |
import logging
|
| 18 |
+
import shutil
|
| 19 |
|
| 20 |
|
| 21 |
# Set up basic configuration for logging
|
|
|
|
| 117 |
if os.path.exists("faiss_database"):
|
| 118 |
logging.info("Updating existing FAISS database")
|
| 119 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
| 120 |
+
database.add_documents(all_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
else:
|
| 122 |
logging.info("Creating new FAISS database")
|
| 123 |
database = FAISS.from_documents(all_data, embed)
|
|
|
|
| 156 |
logging.info(f"Documents to delete: {len(deleted_docs)}")
|
| 157 |
|
| 158 |
if not docs_to_keep:
|
| 159 |
+
# If all documents are deleted, remove the FAISS database directory
|
| 160 |
if os.path.exists("faiss_database"):
|
| 161 |
+
shutil.rmtree("faiss_database")
|
| 162 |
+
logging.info("All documents deleted. Removed FAISS database directory.")
|
| 163 |
else:
|
| 164 |
# Create new FAISS index with remaining documents
|
| 165 |
+
new_database = FAISS.from_documents(docs_to_keep, embed)
|
| 166 |
+
new_database.save_local("faiss_database")
|
| 167 |
logging.info(f"Created new FAISS index with {len(docs_to_keep)} documents.")
|
| 168 |
|
| 169 |
# Update uploaded_documents list
|