-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_chromadb_simple.py
More file actions
101 lines (84 loc) · 3.47 KB
/
Copy pathbuild_chromadb_simple.py
File metadata and controls
101 lines (84 loc) · 3.47 KB
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
import os
import shutil
from pathlib import Path
from pypdf import PdfReader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain.embeddings import SentenceTransformerEmbeddings
# --- CONFIGURATION ---
DATA_FOLDER = "fons_knowledge_base"
DB_OUTPUT_FOLDER = "chroma_db_fons"
def load_pdfs_from_folder(folder_path):
"""Load text from all PDFs in a folder"""
print(f" 📚 Loading PDFs from '{folder_path}'...")
documents = []
pdf_files = list(Path(folder_path).glob("*.pdf"))
for i, pdf_file in enumerate(pdf_files):
try:
print(f" Processing {i+1}/{len(pdf_files)}: {pdf_file.name}")
reader = PdfReader(pdf_file)
for page_num, page in enumerate(reader.pages):
text = page.extract_text()
if text.strip():
documents.append({
"page_content": text,
"metadata": {
"source": pdf_file.name,
"page": page_num
}
})
except Exception as e:
print(f" ⚠️ Error processing {pdf_file.name}: {e}")
print(f" ✅ Loaded {len(documents)} pages.")
return documents
def build_the_brain():
print(f"🧠 Building ChromaDB from {DATA_FOLDER}...")
# Clean up old versions to ensure a fresh build
if os.path.exists(DB_OUTPUT_FOLDER):
print(f" 🧹 Removing old database...")
shutil.rmtree(DB_OUTPUT_FOLDER)
# 1. Load PDFs
documents = load_pdfs_from_folder(DATA_FOLDER)
if not documents:
print(" ❌ No documents found. Exiting.")
return
# 2. Split into chunks
print(" 🔄 Splitting text into chunks...")
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
chunks = []
for doc in documents:
split_texts = text_splitter.split_text(doc["page_content"])
for text in split_texts:
chunks.append({
"page_content": text,
"metadata": doc["metadata"]
})
print(f" ✅ Created {len(chunks)} text chunks.")
# 3. Load Embedding Model
print(" 🧠 Loading embedding model (using SentenceTransformers)...")
try:
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
except Exception as e:
print(f" ⚠️ Error loading embeddings: {e}")
print(" Using default embeddings instead...")
from langchain.embeddings import FakeEmbeddings
embeddings = FakeEmbeddings(model_name="default")
# 4. Create and Persist the Database
print(" 💾 Creating and saving the vector database (This will take several minutes)...")
try:
# Convert document format for Chroma
texts = [doc["page_content"] for doc in chunks]
metadatas = [doc["metadata"] for doc in chunks]
db = Chroma.from_texts(
texts=texts,
metadatas=metadatas,
embedding=embeddings,
persist_directory=DB_OUTPUT_FOLDER
)
print(f"\n🎉 SUCCESS! ChromaDB created in folder: '{DB_OUTPUT_FOLDER}'")
print(" You can now upload this folder to your Hugging Face Space.")
except Exception as e:
print(f"\n❌ Error creating database: {e}")
raise
if __name__ == "__main__":
build_the_brain()