pradeepsengarr commited on
Commit
1eda1ed
·
verified ·
1 Parent(s): 5256379

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
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
- # --- Create Vector DB ---
479
- def create_vectorstore(documents):
480
- # Setup a writable directory for Chroma
481
- chroma_dir = os.path.join(tempfile.gettempdir(), "chroma_db")
482
- if os.path.exists(chroma_dir):
483
- shutil.rmtree(chroma_dir) # Clear any old data
484
- os.makedirs(chroma_dir, exist_ok=True)
485
 
486
- # Initialize the model and embeddings
487
- model = SentenceTransformer("all-MiniLM-L6-v2", device='cpu')
488
- embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
489
-
490
- # Create the Chroma database
491
- db = Chroma.from_documents(documents, embeddings, persist_directory=chroma_dir)
492
 
493
- # Persist the Chroma database
 
 
494
  db.persist()
495
- return db
 
 
 
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):