Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -469,30 +469,29 @@ def split_text_into_chunks(text):
|
|
469 |
return splitter.create_documents([text])
|
470 |
|
471 |
import os
|
472 |
-
import tempfile
|
473 |
import shutil
|
474 |
-
from langchain.vectorstores import Chroma
|
475 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
476 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
477 |
|
478 |
-
#
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
shutil.rmtree(chroma_dir) # Clear any old data
|
484 |
-
os.makedirs(chroma_dir, exist_ok=True)
|
485 |
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
# Create the Chroma database
|
491 |
-
db = Chroma.from_documents(documents, embeddings, persist_directory=chroma_dir)
|
492 |
|
493 |
-
|
|
|
|
|
494 |
db.persist()
|
495 |
-
|
|
|
|
|
|
|
496 |
|
497 |
# --- Setup QA Chain ---
|
498 |
def setup_qa(db):
|
|
|
469 |
return splitter.create_documents([text])
|
470 |
|
471 |
import os
|
|
|
472 |
import shutil
|
|
|
|
|
473 |
from sentence_transformers import SentenceTransformer
|
474 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
475 |
+
from langchain.vectorstores import Chroma
|
476 |
|
477 |
+
# Setup a writable directory for Chroma
|
478 |
+
chroma_dir = "/home/user/app/chroma_db" # Change this to an absolute writable directory
|
479 |
+
if os.path.exists(chroma_dir):
|
480 |
+
shutil.rmtree(chroma_dir) # Clear any old data
|
481 |
+
os.makedirs(chroma_dir, exist_ok=True)
|
|
|
|
|
482 |
|
483 |
+
# Initialize the model and embeddings
|
484 |
+
model = SentenceTransformer("all-MiniLM-L6-v2", device='cpu')
|
485 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
|
|
|
|
|
|
486 |
|
487 |
+
# Create the Chroma database
|
488 |
+
try:
|
489 |
+
db = Chroma.from_documents(splits, embeddings, persist_directory=chroma_dir)
|
490 |
db.persist()
|
491 |
+
print(f"Vectorstore created successfully at {chroma_dir}")
|
492 |
+
except Exception as e:
|
493 |
+
print(f"Error creating vectorstore: {e}")
|
494 |
+
|
495 |
|
496 |
# --- Setup QA Chain ---
|
497 |
def setup_qa(db):
|