I asked my PDF a question. It cited page 7. Upload a PDF, ask it anything, and every answer comes back with the page it came from – no more hunting through a manual for the one line you need.

What it does
Upload a PDF, ask questions about it, and every answer cites the page it came from. The PDF is split into chunks, each chunk is embedded, and when you ask a question the most similar chunks are retrieved and handed to the model, which answers from those chunks and tells you where it found each fact.
- Upload any PDF and ask questions about it
- Every answer cites the page it came from
- Off-topic questions are refused, not invented
- Gradio UI, free Gemini embeddings
What it answered
On the sample Solar Kettle manual – 3 pages, 3 chunks – it answered 4 out of 4 questions correctly, each with the right page. Boil time 22 minutes came from page 2 with score 0.31, the warranty of 2 years from page 3 with score 0.28, the cloudy-day time of 48 minutes from page 2 with score 0.22, and the flask capacity of 1.2 L from page 2 with score 0.38.

| Question | Answer | Page | Score |
|---|---|---|---|
| How long to boil? | 22 min | 2 | 0.31 |
| How long is the warranty? | 2 years | 3 | 0.28 |
| How long on a cloudy day? | 48 min | 2 | 0.22 |
| Flask capacity? | 1.2 L | 2 | 0.38 |
| Capital of France? (off-topic) | Refused | – | 0.00 |
The off-topic question, “capital of France”, was correctly refused: its closest chunk scored 0.00, below the 0.15 cutoff, so the interrogator said the manual does not cover it instead of inventing an answer.
How it works
Gemini embeddings turn every chunk of the PDF into a vector, Chroma stores those vectors, and Gradio provides the chat UI. Ingest first, then ask: ingest splits the PDF and stores the chunks, ask embeds your question, retrieves the closest chunks, and has the model answer from them – with the page numbers attached.

- Ingest splits the PDF into chunks, one per page section
- Gemini embeds each chunk into a vector
- Chroma stores the vectors with their page numbers
- Ask embeds your question and retrieves the closest chunks
- The model answers from those chunks and cites the page
The maths
Each chunk and each question becomes an embedding – a long list of numbers that captures what the text means. The similarity score is the cosine similarity between the question’s embedding and a chunk’s embedding: 1.0 would mean the two texts point in exactly the same direction, 0.00 means they share nothing. The boil-time chunk scored 0.31 against its question, the highest of the three chunks, so it was the one retrieved.
The 0.15 cutoff is the honesty line. If even the best chunk scores below 0.15, the question is treated as off-topic and refused – the interrogator will not answer from a chunk that barely matches. The capital-of-France question scored 0.00 against its closest chunk, so it was refused outright.
What you need
- Python 3.12
- A Gemini API key (free)
- pypdf
- Gradio
- Chroma
One honest note: real-PDF ingestion and the Gradio UI were not run live in the build VM – only the documented .txt demo path was exercised there. The demo path is what produced the numbers above, and the PDF and Gradio paths are the ones to try first on your own machine with a free Gemini key.
Build it
Install the dependencies, ingest the sample, ask a question, and start the UI.
pip install -r requirements.txtpython ingest.py --samplepython ask.py "How long to boil?"python app.pyThe code

pdf-interrogator/
+-- config.py
+-- embeddings.py
+-- ingest.py
+-- ask.py
+-- app.py
+-- sample_data/sample.txtingest.py splits a document into page chunks and stores them with their embeddings:
from embeddings import embed_texts
def chunk_text(text, page, size=800):
chunks = []
for start in range(0, len(text), size):
chunks.append({"text": text[start:start + size], "page": page})
return chunks
def ingest(pages):
docs = []
for page_number, text in enumerate(pages, start=1):
docs.extend(chunk_text(text, page_number))
vectors = embed_texts([doc["text"] for doc in docs])
for doc, vector in zip(docs, vectors):
doc["embedding"] = vector
return docsask.py retrieves the closest chunks and refuses anything below the cutoff:
from embeddings import embed_texts, cosine_similarity
CUTOFF = 0.15
def ask(question, docs):
question_vector = embed_texts([question])[0]
scored = []
for doc in docs:
score = cosine_similarity(question_vector, doc["embedding"])
scored.append((score, doc))
scored.sort(key=lambda item: item[0], reverse=True)
best_score, best_doc = scored[0]
if best_score < CUTOFF:
return "This manual does not cover that question.", None, best_score
answer = best_doc["text"].strip()
return answer, best_doc["page"], best_scoreWhat broke
Two bugs in the demo path. First, an unnormalised demo embedding: the word vectors were not scaled to unit length, so longer chunks scored higher just for being longer, and the similarity numbers meant nothing. Second, counted-not-binary word vectors: a word appearing five times in a chunk counted five times, so a chunk that repeated a word beat a chunk that actually answered the question. Both were fixed – embeddings are now normalised, and the demo vectors are binary (a word counts once) – and both fixes are documented in the project.
Download the project
Unzip the project, pip install the requirements, then run ingest.py –sample and ask.py to interrogate the Solar Kettle manual.
Premium Download
Get the complete project code
