Spaces:
Sleeping
Sleeping
File size: 12,514 Bytes
7786671 | 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | #!/usr/bin/env python3
"""
Document ingestion script for Bahá'í Writings semantic search.
Processes .docx files, chunks by paragraphs, and stores embeddings in Pinecone.
"""
import os
import logging
import shutil
from pathlib import Path
from typing import List, Dict, Any
from pinecone import Pinecone, ServerlessSpec
from docx import Document
from openai import OpenAI
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class BahaiWritingsIngestor:
def __init__(self, openai_api_key: str, pinecone_api_key: str):
"""Initialize the ingestion pipeline."""
self.openai_client = OpenAI(api_key=openai_api_key)
self.pc = Pinecone(api_key=pinecone_api_key)
self.index_name = "bahai-writings"
self.dimension = 3072 # Dimension for text-embedding-3-large
# Create or get index
try:
# Check if index exists
if self.index_name not in [index.name for index in self.pc.list_indexes()]:
logger.info(f"Creating new index: {self.index_name}")
self.pc.create_index(
name=self.index_name,
dimension=self.dimension,
metric="cosine",
spec=ServerlessSpec(
cloud="aws",
region="us-east-1"
)
)
else:
logger.info(f"Using existing index: {self.index_name}")
self.index = self.pc.Index(self.index_name)
except Exception as e:
logger.error(f"Error initializing Pinecone index: {e}")
raise
def get_author_from_filename(self, filename: str) -> str:
"""Determine the author based on filename."""
filename_lower = filename.lower().replace('.docx', '')
# Bahá'u'lláh
bahaullah_works = [
'kitab-i-iqan', 'hidden-words', 'gleanings-writings-bahaullah',
'kitab-i-aqdas-2', 'epistle-son-wolf', 'gems-divine-mysteries',
'summons-lord-hosts', 'tablets-bahaullah', 'tabernacle-unity',
'prayers-meditations'
]
# 'Abdu'l-Bahá
abdul_baha_works = [
'some-answered-questions', 'paris-talks', 'promulgation-universal-peace',
'memorials-faithful', 'selections-writings-abdul-baha',
'secret-divine-civilization', 'travelers-narrative',
'will-testament-abdul-baha', 'tablets-divine-plan', 'tablet-auguste-forel'
]
# The Báb
bab_works = ['selections-writings-bab']
# Shoghi Effendi
shoghi_works = [
'advent-divine-justice', 'god-passes-by', 'promised-day-come',
'world-order-bahaullah'
]
# Universal House of Justice (dated documents and institutional)
uhj_works = [
'the-institution-of-the-counsellors', 'turning-point', 'muhj-1963-1986'
]
# Compilations
compilation_works = ['days-remembrance', 'light-of-the-world']
# Check filename against each category
if filename_lower in bahaullah_works:
return "Bahá'u'lláh"
elif filename_lower in abdul_baha_works:
return "'Abdu'l-Bahá"
elif filename_lower in bab_works:
return "The Báb"
elif filename_lower in shoghi_works:
return "Shoghi Effendi"
elif filename_lower in uhj_works:
return "Universal House of Justice"
elif filename_lower in compilation_works:
return "Compilations"
# Handle dated documents (likely UHJ messages)
elif filename_lower.startswith(('19', '20')) and '_' in filename_lower:
return "Universal House of Justice"
else:
return "Other"
def extract_paragraphs_from_docx(self, file_path: str) -> List[Dict[str, Any]]:
"""Extract paragraphs from a .docx file, combining short paragraphs."""
logger.info(f"Processing document: {file_path}")
doc = Document(file_path)
raw_paragraphs = []
# First, extract all non-empty paragraphs
for i, paragraph in enumerate(doc.paragraphs):
text = paragraph.text.strip()
if text: # Only keep non-empty paragraphs
raw_paragraphs.append({
"text": text,
"original_id": i
})
# Now combine paragraphs with <min_words words
min_words = 100
combined_paragraphs = []
i = 0
while i < len(raw_paragraphs):
current_text = raw_paragraphs[i]["text"]
current_word_count = len(current_text.split())
start_id = raw_paragraphs[i]["original_id"]
combined_ids = [start_id]
# If current paragraph has <min_words words, combine with next paragraphs
if current_word_count < min_words and i < len(raw_paragraphs) - 1:
j = i + 1
while j < len(raw_paragraphs) and len(current_text.split()) < min_words:
next_text = raw_paragraphs[j]["text"]
current_text += " " + next_text
combined_ids.append(raw_paragraphs[j]["original_id"])
j += 1
i = j # Skip the paragraphs we just combined
else:
i += 1 # Move to next paragraph
# Create the combined paragraph entry
if len(combined_ids) == 1:
document_id = f"{Path(file_path).stem}_para_{start_id}"
else:
document_id = f"{Path(file_path).stem}_para_{combined_ids[0]}-{combined_ids[-1]}"
combined_paragraphs.append({
"text": current_text,
"paragraph_id": start_id, # Use the ID of the first paragraph
"source_file": Path(file_path).name,
"document_id": document_id,
"author": self.get_author_from_filename(Path(file_path).name)
})
logger.info(f"Extracted {len(raw_paragraphs)} raw paragraphs, combined into {len(combined_paragraphs)} final paragraphs")
return combined_paragraphs
def generate_embedding(self, text: str) -> List[float]:
"""Generate embedding for text using OpenAI's text-embedding-3-large model."""
try:
response = self.openai_client.embeddings.create(
model="text-embedding-3-large",
input=text,
encoding_format="float"
)
return response.data[0].embedding
except Exception as e:
logger.error(f"Error generating embedding: {e}")
raise
def ingest_paragraphs(self, paragraphs: List[Dict[str, Any]]):
"""Generate embeddings and store paragraphs in Pinecone."""
logger.info(f"Generating embeddings for {len(paragraphs)} paragraphs...")
vectors = []
for paragraph in paragraphs:
try:
# Generate embedding
embedding = self.generate_embedding(paragraph["text"])
# Create vector for Pinecone
vector = {
"id": paragraph["document_id"],
"values": embedding,
"metadata": {
"text": paragraph["text"],
"source_file": paragraph["source_file"],
"paragraph_id": paragraph["paragraph_id"],
"author": paragraph["author"]
}
}
vectors.append(vector)
logger.info(f"Processed paragraph {paragraph['paragraph_id']}")
except Exception as e:
logger.error(f"Error processing paragraph {paragraph['paragraph_id']}: {e}")
continue
# Upsert to Pinecone index
if vectors:
# Pinecone has a limit on batch size, so we'll process in chunks
batch_size = 100
for i in range(0, len(vectors), batch_size):
batch = vectors[i:i + batch_size]
self.index.upsert(vectors=batch)
logger.info(f"Upserted batch {i//batch_size + 1} of {(len(vectors) + batch_size - 1)//batch_size}")
logger.info(f"Successfully ingested {len(vectors)} paragraphs into Pinecone")
def move_document_to_indexed(self, file_path: str):
"""Move document from corpus to corpus_indexed folder after successful ingestion."""
source_path = Path(file_path)
indexed_dir = Path("./corpus_indexed")
# Create corpus_indexed directory if it doesn't exist
indexed_dir.mkdir(exist_ok=True)
# Define destination path
destination_path = indexed_dir / source_path.name
try:
shutil.move(str(source_path), str(destination_path))
logger.info(f"Moved {source_path.name} to corpus_indexed/")
except Exception as e:
logger.error(f"Failed to move {source_path.name}: {e}")
raise
def ingest_document(self, file_path: str):
"""Complete ingestion pipeline for a single document."""
if not os.path.exists(file_path):
raise FileNotFoundError(f"Document not found: {file_path}")
# Extract paragraphs
paragraphs = self.extract_paragraphs_from_docx(file_path)
# Ingest into ChromaDB
self.ingest_paragraphs(paragraphs)
# Move document to indexed folder after successful ingestion
self.move_document_to_indexed(file_path)
logger.info(f"Completed ingestion of {file_path}")
def clear_index(self):
"""Clear all vectors from the existing index."""
try:
logger.info(f"Clearing all vectors from index '{self.index_name}'...")
self.index.delete(delete_all=True)
logger.info("Index cleared successfully")
except Exception as e:
logger.error(f"Error clearing index: {e}")
raise
def get_index_stats(self):
"""Get statistics about the index."""
stats = self.index.describe_index_stats()
count = stats['total_vector_count']
logger.info(f"Index '{self.index_name}' contains {count} vectors")
return count
def main():
"""Main ingestion function."""
# Check for required environment variables
openai_api_key = os.environ.get("OPENAI_API_KEY")
pinecone_api_key = os.environ.get("PINECONE_API_KEY")
if not openai_api_key:
raise ValueError("OPENAI_API_KEY environment variable is required")
if not pinecone_api_key:
raise ValueError("PINECONE_API_KEY environment variable is required")
# Initialize ingestor
ingestor = BahaiWritingsIngestor(openai_api_key, pinecone_api_key)
# Check if index has existing data
stats = ingestor.get_index_stats()
if stats > 0:
logger.info(f"Found {stats} existing vectors in the database.")
# Ask for user confirmation before clearing
confirmation = input(f"This will delete all {stats} existing vectors and re-ingest with new metadata. Continue? (y/N): ").strip().lower()
if confirmation in ['y', 'yes']:
logger.info("User confirmed. Clearing existing vectors from the database...")
ingestor.clear_index()
else:
logger.info("Operation cancelled by user.")
return
else:
logger.info("No existing vectors found. Proceeding with fresh ingestion.")
# Find and process .docx files in current directory
docx_files = list(Path("./corpus").glob("*.docx"))
if not docx_files:
logger.warning("No .docx files found in current directory")
return
for docx_file in docx_files:
try:
ingestor.ingest_document(str(docx_file))
except Exception as e:
logger.error(f"Failed to ingest {docx_file}: {e}")
# Print final statistics
ingestor.get_index_stats()
if __name__ == "__main__":
main() |