Spaces:
Sleeping
Sleeping
Update src/ingest_documents.py
Browse files- src/ingest_documents.py +217 -130
src/ingest_documents.py
CHANGED
|
@@ -1,130 +1,217 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
import
|
| 12 |
-
import
|
| 13 |
-
|
| 14 |
-
import
|
| 15 |
-
from
|
| 16 |
-
from
|
| 17 |
-
from
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Complete ingestion script - processes all documents, extracts sections,
|
| 4 |
+
and creates a unified collection with section-aware metadata.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import shutil
|
| 10 |
+
import re
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 13 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 14 |
+
from langchain_community.vectorstores import Chroma
|
| 15 |
+
from langchain.docstore.document import Document
|
| 16 |
+
from dotenv import load_dotenv
|
| 17 |
+
from huggingface_hub import login
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def extract_section_reference(text: str) -> str:
|
| 21 |
+
"""
|
| 22 |
+
Extract section/article/part reference from a chunk of text.
|
| 23 |
+
Handles variations like "Section 13", "Sec. 13", "Article 45", "Part IV", "Chapter 2".
|
| 24 |
+
"""
|
| 25 |
+
patterns = [
|
| 26 |
+
r"(Section\s+\d+[A-Za-z0-9\-]*)",
|
| 27 |
+
r"(Sec\.\s*\d+[A-Za-z0-9\-]*)",
|
| 28 |
+
r"(Article\s+\d+[A-Za-z0-9\-]*)",
|
| 29 |
+
r"(Art\.\s*\d+[A-Za-z0-9\-]*)",
|
| 30 |
+
r"(Part\s+[IVXLC]+)",
|
| 31 |
+
r"(Chapter\s+\d+)",
|
| 32 |
+
r"(Cap\.\s*[A-Za-z0-9\-]+)"
|
| 33 |
+
]
|
| 34 |
+
for pattern in patterns:
|
| 35 |
+
match = re.search(pattern, text, re.IGNORECASE)
|
| 36 |
+
if match:
|
| 37 |
+
return match.group(1).strip()
|
| 38 |
+
return "Unknown Section"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def main():
|
| 42 |
+
"""Complete ingestion with section-aware metadata"""
|
| 43 |
+
|
| 44 |
+
print("COMPLETE LEGAL DOCUMENT INGESTION (Section-Aware)")
|
| 45 |
+
print("="*60)
|
| 46 |
+
|
| 47 |
+
# Load environment
|
| 48 |
+
load_dotenv()
|
| 49 |
+
hf_token = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 50 |
+
|
| 51 |
+
if not hf_token:
|
| 52 |
+
print("ERROR: Missing HUGGINGFACE_HUB_TOKEN in .env file")
|
| 53 |
+
sys.exit(1)
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
login(token=hf_token)
|
| 57 |
+
print("✓ Logged in to Hugging Face")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"ERROR: Hugging Face login failed: {e}")
|
| 60 |
+
sys.exit(1)
|
| 61 |
+
|
| 62 |
+
# Find processed text files
|
| 63 |
+
processed_dir = Path("data/processed")
|
| 64 |
+
if not processed_dir.exists():
|
| 65 |
+
print("ERROR: data/processed directory not found")
|
| 66 |
+
sys.exit(1)
|
| 67 |
+
|
| 68 |
+
text_files = list(processed_dir.glob("*_text.txt"))
|
| 69 |
+
if not text_files:
|
| 70 |
+
print("ERROR: No processed text files found")
|
| 71 |
+
sys.exit(1)
|
| 72 |
+
|
| 73 |
+
print(f"Found {len(text_files)} files to process:")
|
| 74 |
+
|
| 75 |
+
for file in text_files:
|
| 76 |
+
size = file.stat().st_size
|
| 77 |
+
print(f" {file.name}: {size:,} bytes")
|
| 78 |
+
|
| 79 |
+
# Process files into documents
|
| 80 |
+
all_documents = []
|
| 81 |
+
|
| 82 |
+
for text_file in text_files:
|
| 83 |
+
print(f"\nProcessing: {text_file.name}")
|
| 84 |
+
|
| 85 |
+
try:
|
| 86 |
+
with open(text_file, 'r', encoding='utf-8') as f:
|
| 87 |
+
content = f.read()
|
| 88 |
+
|
| 89 |
+
if not content.strip():
|
| 90 |
+
print(f" Skipping empty file: {text_file.name}")
|
| 91 |
+
continue
|
| 92 |
+
|
| 93 |
+
# Split into manageable chunks
|
| 94 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 95 |
+
chunk_size=800,
|
| 96 |
+
chunk_overlap=150,
|
| 97 |
+
separators=['\n\n', '\n', '. ', ' ']
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
chunks = text_splitter.split_text(content)
|
| 101 |
+
|
| 102 |
+
# Clean human-readable source name (remove _text, underscores, .pdf)
|
| 103 |
+
source_name = (
|
| 104 |
+
text_file.stem.replace('_text', '')
|
| 105 |
+
.replace('_', ' ')
|
| 106 |
+
.replace('-', ' ')
|
| 107 |
+
.strip()
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
print(f" Created {len(chunks)} chunks from {len(content):,} characters")
|
| 111 |
+
|
| 112 |
+
# Create documents with section-aware metadata
|
| 113 |
+
for i, chunk in enumerate(chunks):
|
| 114 |
+
if len(chunk.strip()) > 20: # Only meaningful chunks
|
| 115 |
+
section_ref = extract_section_reference(chunk)
|
| 116 |
+
|
| 117 |
+
# Infer doc type
|
| 118 |
+
doc_type = "general"
|
| 119 |
+
if "constitution" in text_file.name.lower():
|
| 120 |
+
doc_type = "constitution"
|
| 121 |
+
elif "labour" in text_file.name.lower():
|
| 122 |
+
doc_type = "labour_law"
|
| 123 |
+
elif "fccpa" in text_file.name.lower():
|
| 124 |
+
doc_type = "consumer_protection"
|
| 125 |
+
elif "data_protection" in text_file.name.lower():
|
| 126 |
+
doc_type = "data_protection"
|
| 127 |
+
|
| 128 |
+
doc = Document(
|
| 129 |
+
page_content=chunk.strip(),
|
| 130 |
+
metadata={
|
| 131 |
+
'source': source_name, # clean name, no .pdf
|
| 132 |
+
'document_type': doc_type,
|
| 133 |
+
'chunk_index': i,
|
| 134 |
+
'total_chunks': len(chunks),
|
| 135 |
+
'file_path': str(text_file),
|
| 136 |
+
'content_length': len(chunk.strip()),
|
| 137 |
+
'section': section_ref
|
| 138 |
+
}
|
| 139 |
+
)
|
| 140 |
+
all_documents.append(doc)
|
| 141 |
+
|
| 142 |
+
except Exception as e:
|
| 143 |
+
print(f" ERROR processing {text_file.name}: {e}")
|
| 144 |
+
continue
|
| 145 |
+
|
| 146 |
+
print(f"\nTotal documents prepared: {len(all_documents)}")
|
| 147 |
+
|
| 148 |
+
if not all_documents:
|
| 149 |
+
print("ERROR: No documents prepared for ingestion")
|
| 150 |
+
sys.exit(1)
|
| 151 |
+
|
| 152 |
+
# Initialize embedding model
|
| 153 |
+
try:
|
| 154 |
+
print("\nInitializing embedding model...")
|
| 155 |
+
embedding_model = HuggingFaceEmbeddings(
|
| 156 |
+
model_name="BAAI/bge-small-en",
|
| 157 |
+
model_kwargs={'device': 'cpu'}
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
test_embedding = embedding_model.embed_query("test legal document")
|
| 161 |
+
print(f"✓ Embedding model ready (dimension: {len(test_embedding)})")
|
| 162 |
+
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f"ERROR: Embedding model initialization failed: {e}")
|
| 165 |
+
sys.exit(1)
|
| 166 |
+
|
| 167 |
+
# Create vector database
|
| 168 |
+
try:
|
| 169 |
+
print("\nCreating complete vector database (with sections)...")
|
| 170 |
+
|
| 171 |
+
vector_db_path = Path("vector_db")
|
| 172 |
+
if vector_db_path.exists():
|
| 173 |
+
shutil.rmtree(vector_db_path)
|
| 174 |
+
print(" Removed existing database")
|
| 175 |
+
|
| 176 |
+
vectordb = Chroma.from_documents(
|
| 177 |
+
documents=all_documents,
|
| 178 |
+
embedding=embedding_model,
|
| 179 |
+
persist_directory="vector_db",
|
| 180 |
+
collection_name="legal_documents"
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
count = vectordb._collection.count()
|
| 184 |
+
print(f"✓ Successfully stored {count} documents")
|
| 185 |
+
|
| 186 |
+
# Test search functionality
|
| 187 |
+
print("\nTesting search functionality (showing sections)...")
|
| 188 |
+
|
| 189 |
+
test_queries = [
|
| 190 |
+
"constitutional rights",
|
| 191 |
+
"FCCPA tribunal consumer protection",
|
| 192 |
+
"labour law employment worker",
|
| 193 |
+
"data protection privacy"
|
| 194 |
+
]
|
| 195 |
+
|
| 196 |
+
for query in test_queries:
|
| 197 |
+
results = vectordb.similarity_search(query, k=3)
|
| 198 |
+
print(f"\n'{query}': {len(results)} results")
|
| 199 |
+
for doc in results:
|
| 200 |
+
print(f" [{doc.metadata.get('document_type')}] "
|
| 201 |
+
f"{doc.metadata.get('section', 'Unknown Section')} — "
|
| 202 |
+
f"{doc.metadata.get('source')}")
|
| 203 |
+
print(f" Preview: {doc.page_content[:120]}...")
|
| 204 |
+
|
| 205 |
+
print(f"\n✓ Complete ingestion successful!")
|
| 206 |
+
print(f"✓ Database contains {count} legal document chunks")
|
| 207 |
+
print(f"✓ Ready for legal question answering with section references")
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
print(f"ERROR: Vector database creation failed: {e}")
|
| 211 |
+
import traceback
|
| 212 |
+
traceback.print_exc()
|
| 213 |
+
sys.exit(1)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
if __name__ == "__main__":
|
| 217 |
+
main()
|