Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from huggingface_hub import HfApi, HfFolder
|
|
| 10 |
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
| 11 |
DATASET_REPO = "GovindRaj/faiss-vectorstore" # Your Hugging Face Dataset ID
|
| 12 |
|
| 13 |
-
# Function to create FAISS vector DB and upload to Hugging Face
|
| 14 |
def create_vector_db(uploaded_files):
|
| 15 |
# Create a temporary directory
|
| 16 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
@@ -42,8 +42,17 @@ def create_vector_db(uploaded_files):
|
|
| 42 |
model_kwargs={'device': 'cpu'}
|
| 43 |
)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
db.save_local(DB_FAISS_PATH)
|
| 48 |
|
| 49 |
# Retrieve the token from environment variables (Hugging Face Secrets)
|
|
@@ -52,7 +61,7 @@ def create_vector_db(uploaded_files):
|
|
| 52 |
if not hf_token:
|
| 53 |
raise ValueError("Hugging Face token not found. Please set the token in Hugging Face secrets.")
|
| 54 |
|
| 55 |
-
# Push the vector database to Hugging Face Dataset
|
| 56 |
HfFolder.save_token(hf_token)
|
| 57 |
api = HfApi()
|
| 58 |
api.upload_folder(
|
|
@@ -85,4 +94,4 @@ def main():
|
|
| 85 |
st.error(f"An error occurred: {str(e)}")
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
-
main()
|
|
|
|
| 10 |
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
| 11 |
DATASET_REPO = "GovindRaj/faiss-vectorstore" # Your Hugging Face Dataset ID
|
| 12 |
|
| 13 |
+
# Function to create or update FAISS vector DB and upload to Hugging Face
|
| 14 |
def create_vector_db(uploaded_files):
|
| 15 |
# Create a temporary directory
|
| 16 |
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
| 42 |
model_kwargs={'device': 'cpu'}
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Check if FAISS vectorstore already exists
|
| 46 |
+
if os.path.exists(DB_FAISS_PATH):
|
| 47 |
+
# Load existing FAISS database
|
| 48 |
+
db = FAISS.load_local(DB_FAISS_PATH, embeddings)
|
| 49 |
+
# Add new documents to the existing database
|
| 50 |
+
db.add_documents(texts)
|
| 51 |
+
else:
|
| 52 |
+
# Create a new FAISS database if none exists
|
| 53 |
+
db = FAISS.from_documents(texts, embeddings)
|
| 54 |
+
|
| 55 |
+
# Save the updated FAISS database locally
|
| 56 |
db.save_local(DB_FAISS_PATH)
|
| 57 |
|
| 58 |
# Retrieve the token from environment variables (Hugging Face Secrets)
|
|
|
|
| 61 |
if not hf_token:
|
| 62 |
raise ValueError("Hugging Face token not found. Please set the token in Hugging Face secrets.")
|
| 63 |
|
| 64 |
+
# Push the updated vector database to Hugging Face Dataset
|
| 65 |
HfFolder.save_token(hf_token)
|
| 66 |
api = HfApi()
|
| 67 |
api.upload_folder(
|
|
|
|
| 94 |
st.error(f"An error occurred: {str(e)}")
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
main()
|