Spaces:
Sleeping
Sleeping
Download src/pipeline.py from veltrixcode/businesslawrag: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/veltrixcode/businesslawrag/resolve/main/src/pipeline.py
- Command line
-
hf download hf://spaces/veltrixcode/businesslawrag/src/pipeline.py
-
curl -L -o pipeline.py https://huggingface.co/spaces/veltrixcode/businesslawrag/resolve/main/src/pipeline.py
2.2 kB
| import os | |
| import sys | |
| import glob | |
| from typing import List, Dict, Any | |
| from src.downloader import download_all_books, ICAI_MANIFEST | |
| from src.parser import extract_pdf_pages | |
| from src.chunker import build_contextual_chunks | |
| from src.retriever import HybridLegalRetriever | |
| def run_ingestion_pipeline(books_dir: str = "data/books", force_reindex: bool = False): | |
| print("==================================================") | |
| print("π STEP 1: DOWNLOADING ALL ICAI BOOK CHAPTERS...") | |
| print("==================================================") | |
| downloaded_books = download_all_books(target_dir=books_dir) | |
| # Map filenames to metadata | |
| meta_map = {item["filename"]: item for item in ICAI_MANIFEST} | |
| print("\n==================================================") | |
| print("π STEP 2: PARSING PAGES FROM ALL PDFS...") | |
| print("==================================================") | |
| all_pages: List[Dict[str, Any]] = [] | |
| for book in downloaded_books: | |
| path = book["local_path"] | |
| fname = book["filename"] | |
| meta = meta_map.get(fname, {"title": fname, "chapter": "General"}) | |
| pages = extract_pdf_pages(path, metadata=meta) | |
| print(f"Extracted {len(pages)} text pages from {fname}") | |
| all_pages.extend(pages) | |
| print(f"\nTotal pages extracted across all chapters: {len(all_pages)}") | |
| print("\n==================================================") | |
| print("β STEP 3: CONTEXTUAL CHUNKING & ENRICHMENT...") | |
| print("==================================================") | |
| all_chunks = build_contextual_chunks(all_pages) | |
| print(f"Generated {len(all_chunks)} legal context chunks.") | |
| print("\n==================================================") | |
| print("π§ STEP 4: BUILDING HYBRID DENSE + BM25 INDEX...") | |
| print("==================================================") | |
| retriever = HybridLegalRetriever(persist_dir="data/index") | |
| retriever.index_chunks(all_chunks, force_reindex=force_reindex) | |
| print("\nπ INGESTION & PIPELINE SETUP COMPLETE!") | |
| return retriever | |
| if __name__ == "__main__": | |
| force = "--force" in sys.argv | |
| run_ingestion_pipeline(force_reindex=force) | |