Spaces:
Sleeping
Sleeping
File size: 3,930 Bytes
5a642b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import json
import os
from langchain_chroma import Chroma
from langchain_ollama import OllamaEmbeddings
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter
from tqdm import tqdm
def run_indexing(textbooks, base_dir, persist_directory="vector_store", batch_size=50):
# Initialize embeddings
embeddings = OllamaEmbeddings(model="nomic-embed-text:latest")
# Initialize Text Splitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=150,
length_function=len,
is_separator_regex=False,
)
all_documents = []
for textbook in textbooks:
jsonl_path = os.path.join(base_dir, textbook)
if not os.path.exists(jsonl_path):
print(f"Skipping {textbook} (Not found)...")
continue
print(f"Loading data from {textbook}...")
with open(jsonl_path, "r", encoding="utf-8") as f:
for line in f:
try:
data = json.loads(line)
content = data.get("content", "")
if not content:
continue
base_metadata = {
"subject": data.get("subject"),
"book_name": data.get("book_name"),
"page_number": data.get("page_number"),
"category": data.get("category"),
"image_path": data.get("image_path"),
"track": "pharmacy"
}
header = f"SUBJECT: {data.get('subject')} | BOOK: {data.get('book_name')} | PAGE: {data.get('page_number')}\n"
chunks = text_splitter.split_text(content)
for chunk in chunks:
doc = Document(
page_content=header + chunk,
metadata=base_metadata
)
all_documents.append(doc)
except Exception as e:
print(f"Error parsing line: {e}")
total_docs = len(all_documents)
if total_docs == 0:
print("No documents to index.")
return
# Delete old vector store to ensure clean merge
if os.path.exists(persist_directory):
import shutil
print(f"Cleaning old vector store at {persist_directory}...")
try:
shutil.rmtree(persist_directory)
except Exception as e:
print(f"Warning: Could not delete old vector store: {e}. Please ensure no other process is using it.")
return
print(f"Starting batch indexing: {total_docs} chunks created, batch size {batch_size}...")
# Initialize the store with the first batch
first_batch = all_documents[:batch_size]
vectorstore = Chroma.from_documents(
documents=first_batch,
embedding=embeddings,
persist_directory=persist_directory
)
# Add remaining batches
for i in tqdm(range(batch_size, total_docs, batch_size), desc="Indexing progress"):
batch = all_documents[i : i + batch_size]
vectorstore.add_documents(batch)
print(f"\nIndexing complete! {total_docs} chunks indexed into: {persist_directory}")
if __name__ == "__main__":
import os
BASE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data", "processed")
VECTOR_DB_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "db", "vector_store")
# Dynamically find all jsonl files in the BASE_DIR
TEXTBOOKS = [f for f in os.listdir(BASE_DIR) if f.endswith(".jsonl")]
print(f"Found {len(TEXTBOOKS)} textbooks for indexing: {', '.join(TEXTBOOKS)}")
run_indexing(TEXTBOOKS, BASE_DIR, VECTOR_DB_DIR, batch_size=100)
|