Document RAG — Ask your lecture notes
2025AI engineering
A RAG system that reads scanned notes and PDFs, layout included, and answers questions with page citations.

Problem
Study material is mostly scanned PDFs and photographed notes. Plain text search loses the structure: which table a number came from, which figure a question is about, which page to go back to. Most RAG demos skip that by assuming clean text.
What I built
I built it twice. The first version was a straight pipeline: OCR, chunk, embed, FAISS, and a small local model (Flan-T5 or Phi-3) behind a Gradio chat. It worked, but it couldn't tell a table from a paragraph. The second version is a service:
- Ingestion: PDFs are split into pages, run through Tesseract for words and bounding boxes, then through LayoutLMv3, which labels tables, figures and headings.
- Layout-aware chunking: chunks follow the document's structure instead of a fixed token window, and keep their page and position as metadata.
- Two indexes: text embeddings (all-MiniLM-L6-v2, 384 dimensions) and layout features go into separate FAISS indexes, switching to IVF once there's enough data.
- Hybrid retrieval: a query is classified first. Questions about a table or figure lean on the layout index. Results are merged with weights (text 0.6, layout 0.4), reranked, and cut to the top 5.
- Generation: Gemini 2.5 Flash streams the answer back with citations to the page and element it came from.
- Serving: a FastAPI backend with a SQLite store, and a Streamlit front end with a document viewer that draws LayoutLM's boxes over each page.
What I learned
Retrieval quality mattered more than the model. Most bad answers came from the wrong chunk, not a weak LLM, which is why the second version spends its effort on chunking and on deciding which index to trust. The next step is a proper evaluation set, so retrieval changes are measured instead of eyeballed.