All MicroEvals
"""Build a local JSON index from a PDF localy Run from this...
Create MicroEval

"""Build a local JSON index from a PDF localy Run from this...

Prompt

"""Build a local JSON index from a PDF localy Run from this directory (after ``pip install -r requirements.txt``):: python indexing.py path/to/document.pdf The output contains the PDF outline when available and per-page text and character counts. It is an index artifact only; it does not call an LLM or implement question answering. """ from __future__ import annotations import argparse import json from datetime import datetime, timezone from pathlib import Path from typing import Any from PyPDF2 import PdfReader def _outline_nodes(outline: list[Any], reader: PdfReader) -> list[dict[str, Any]]: """Convert PDF bookmarks into nested nodes with zero-based page offsets.""" nodes: list[dict[str, Any]] = [] for item in outline: if isinstance(item, list): if nodes: nodes[-1]["children"] = _outline_nodes(item, reader) continue try: page_number = reader.get_destination_page_number(item) except (ValueError, TypeError, AttributeError): page_number = None nodes.append({ "title": str(getattr(item, "title", item)), "page": page_number + 1 if page_number is not None else None, "children": [], }) return nodes def index_pdf(pdf_path: str | Path, output_path: str | Path | None = None) -> Path: """Extract page text and PDF bookmarks and save them as a JSON index.""" source = Path(pdf_path).expanduser().resolve() if not source.is_file(): raise FileNotFoundError(f"PDF not found: {source}") if source.suffix.lower() != ".pdf": raise ValueError(f"Expected a PDF file: {source}") reader = PdfReader(str(source)) pages = [] for number, page in enumerate(reader.pages, start=1): text = page.extract_text() or "" pages.append({"page": number, "text": text, "character_count": len(text)}) try: outline = _outline_nodes(reader.outline, reader) except (AttributeError, TypeError): outline = [] index = { "source_file": source.name, "page_count": len(pages), "created_at": datetime.now(timezone.utc).isoformat(), "outline": outline, "pages": pages, } destination = Path(output_path).expanduser() if output_path else source.with_suffix(".index.json") destination = destination.resolve() destination.parent.mkdir(parents=True, exist_ok=True) destination.write_text(json.dumps(index, ensure_ascii=False, indent=2), encoding="utf-8") return destination def main() -> None: parser = argparse.ArgumentParser(description="Create a local JSON index for a PDF.") parser.add_argument("pdf", help="PDF file to index") parser.add_argument("--output", help="Output JSON path (default: alongside PDF as *.index.json)") args = parser.parse_args() output = index_pdf(args.pdf, args.output) print(f"Index saved to: {output}") if __name__ == "__main__": main()

Drag to resize
Drag to resize
Drag to resize