1 of 9

AI-Powered Document Insights

Open Source RAG for Digital and Scanned PDFs

Sok Leng Chan (Sandra)

2 of 9

Problem Statement

Reliable answers from mixed-quality pharmaceutical PDFs

Challenge

Solution

Pharmaceutical teams receive both searchable PDFs and image-only scans. Poor scan quality, inconsistent layouts, and long documents make manual review slow and can hide important safety details.

The pipeline detects missing text, applies Tesseract OCR when needed, creates page-aware chunks, retrieves evidence with MiniLM and FAISS, then asks Qwen to answer only from the retrieved context.

Pain Point

Pipeline Response

Image-only scans

OCR fallback with deskewing, denoising, and Otsu thresholding

Long documents

360-word chunks with 70-word overlap and page metadata

Wrong evidence

Dense retrieval with Top K = 4 and document-type routing

Low trust

Visible source file, page range, similarity, and confidence

​

​

3 of 9

System Architecture

Conditional OCR and metadata-aware retrieval

Component Specifications

Component

Technology

Configuration

PDF extraction

PyMuPDF

Digital text first; OCR under 40 characters

OCR

OpenCV + Tesseract

3x render, deskew, denoise, Otsu, OEM 3, PSM 6

Chunking

Page-aware sliding window

360 words; 70-word overlap

Embeddings

all-MiniLM-L6-v2

384 dimensions; normalized vectors

Vector index

FAISS IndexFlatIP

Cosine-style inner-product search

Retriever

Dense + metadata routing

Top K = 4; global fallback

Answer model

Qwen2.5-0.5B-Instruct

Open source; deterministic; context only

Output

Gradio 6

Answer, sources, confidence, chunk count, chat export

PDF

OCR

Chunk

Embed

FAISS

Retrieve

Qwen

4 of 9

Pipeline Performance Metrics

Controlled five-page FDA test pair, 8 retrieval queries and 5 answer queries per format

Metric

Digital PDF

Scanned PDF

Processing time

0.55 s

11.53 s

OCR token F1

100.0%

98.8%

Recall@4

100.0%

100.0%

Precision@4

75.0%

81.3%

MRR

0.938

0.938

Answer accuracy

80.0%

80.0%

Average response

5.84 s

6.06 s

Inline citation compliance

0.0%

0.0%

What the test shows

• OCR preserved retrieval quality, but made processing about 21x slower.

• Both formats answered 4 of 5 questions correctly.

• The source panel was relevant, but Qwen omitted inline citation tags.

Test source: FDA Drug Safety Communication, June 16, 2021. The scanned PDF was generated from the same five pages to isolate OCR impact.

5 of 9

Pipeline in Action | Correct Factual Lookup

Actual Gradio output · digital PDF test

EXAMPLE QUERY 1 · DIGITAL PDF

QUESTION

How many serious adverse event cases did FDA identify in FAERS from 2010 through 2020?

RETRIEVED EVIDENCE

Pages 2–3 · similarity 52.7%

Page 4 · similarity 52.4%

​

ANSWER

FDA identified 50 serious adverse event cases.

​

EVALUATION

Correct answer · relevant chunk ranked first · retrieval 0.015 s

Sources panel shows the evidence; inline [1] citation was omitted.

6 of 9

Pipeline in Action | OCR Success and Answer Limitation

Actual Gradio output · scanned PDF test

EXAMPLE QUERY 2 · SCANNED PDF

QUESTION

What should a consumer do when using hand sanitizer inside a car?

OCR + RETRIEVAL

5 of 5 pages used OCR · token F1 98.8% · processing 11.53 s

Relevant source ranked first · similarity 71.6%

​

ANSWER LIMITATION

Qwen changed the instruction order, did not clearly say to open the window to improve ventilation before vapors clear, and added unsupported details.

​

LESSON

Successful retrieval does not guarantee a grounded final answer.

7 of 9

Design Decision Analysis | Fast, Local, and Traceable

Constraints: CPU-based evaluation, no paid API, and one controlled five-page document pair. Results demonstrate pipeline behaviour, not production readiness.

SPEED vs. ACCURACY

  • • MiniLM and Qwen 0.5B keep the demo local and responsive.
  • • The smaller LLM reduced answer quality and failed the citation format.

​

  • COMPLEXITY vs. MAINTAINABILITY
  • • A single FAISS index is easy to explain and debug.

• Conditional OCR adds complexity only when embedded PDF text is missing.

​

TEST NOTE

The local evaluator used bfloat16; the Colab notebook uses float16 on GPU or float32 on CPU.

Model and Retrieval Decisions

Key Trade-offs

Decision

Rationale

Trade-off

MiniLM embedding

Small, fast, CPU-friendly

Less domain-specific than biomedical models

360 / 70 chunking

Preserves page context

Overlap increases index size

Qwen 0.5B

Open source and local

Weaker instruction following; missed citations

FAISS flat IP

Simple exact vector search

In-memory; not designed for 1,000+ PDFs

8 of 9

Current Limitations & Next Steps | Reliability Before Scale

CURRENT LIMITATIONS

1 · Citation compliance

Qwen omitted inline [1] citations in every tested answer, although the Sources panel remained relevant.

​

2 · Answer grounding

The car-ventilation question produced an incorrect sequence and unsupported details.

​

3 · OCR latency

The scanned PDF took 11.53 s to process versus 0.55 s for the digital PDF — about 21× slower.

​

4 · Evaluation scope

One paired five-page FDA document was tested. Tables, multilingual scans, and complex layouts were not included.

PROPOSED ENHANCEMENTS

SHORT TERM

• Enforce a structured answer-and-citation schema.

• Regenerate answers that lack source tags.

• Add a grounding check against retrieved chunks.

​

MEDIUM TERM

• Add hybrid BM25 + dense retrieval and a cross-encoder reranker.

• Improve table/layout OCR and expand the test set.

• Track latency, Recall@K, answer accuracy, and failures automatically.

​

LONG TERM

• Use a persistent vector database with access control.

• Add monitoring and human review for high-risk documents.

• Validate on 1,000+ PDFs and vague user queries.

9 of 9

Project Impact & Learning Outcomes

100%

Recall@4

80%

Answer accuracy

98.8%

Scanned OCR token F1

WHAT I BUILT

An end-to-end open-source RAG chatbot for digital and scanned PDFs: conditional OCR → page-aware chunks → MiniLM embeddings → FAISS retrieval → Qwen answers → visible sources and confidence.

​

WHAT I LEARNED

High retrieval scores do not guarantee a grounded answer. OCR can preserve retrieval quality, but it adds major processing time. Citation enforcement and answer validation are the next reliability priorities.

​

BUSINESS VALUE

The system can speed up first-pass document review while keeping evidence visible. Human review is still required for important safety, legal, or pharmaceutical decisions.

Sok Leng Chan (Sandra) | s.leng.chan@outlook.com | Next milestone: larger pharmaceutical test set