Turn any text-based PDF into a high-yield study outline and an interactive-ready MCQ quiz using any OpenAI-compatible LLM (OpenAI, OpenRouter, Ollama, LM Studio, …).
PDF → extracted text → study outline (Markdown) → quiz (Markdown or JSON)
Use it as a web app (no coding) or as a CLI / Python library.
- High-yield outline — core frameworks, definitions, methodologies, causal links; fluff removed.
- Two quiz modes
markdown— human-readable Q&A with answer key (study sheets, printing).json— pure JSON for frontends, validated against a strict schema (no prose, no code fences).
- Rigorous quiz prompt — truth-to-source constraint: every question verifiable from the text, plausible distractors, educational explanations.
--strictStructured Outputs — passes a PydanticQuizmodel asresponse_formatfor 100% schema compliance (OpenAIgpt-4o-miniand newer); automatic fallback to JSON mode on servers without.parsesupport.- Long-PDF handling — outlines are chunked (~12k chars) and merged; quiz input capped at ~15k chars (or use
--quiz-from-outline). - Pluggable backend — one client, configured entirely by
LLM_BASE_URL/LLM_API_KEY/LLM_MODEL.
- Python 3.10+
- An API key for your provider (or a local server needing no key)
# 1. Clone and set up git clone https://github.com/Flynntaggart26/PDF-quiz.git Set-Location PDF-quiz python -m venv .venv; .\.venv\Scripts\Activate.ps1 pip install -e . # 2. Configure the model Copy-Item .env.example .env # edit .env: LLM_API_KEY, LLM_BASE_URL, LLM_MODEL # 3. Run python -m pdf_quiz.cli "path\to\lecture.pdf" --format json -n 10 # (in a venv, the shorter `pdf-quiz` command works too)
streamlit run src/pdf_quiz/app.py
Upload a PDF in the browser, tune questions in the sidebar, read the outline, answer the quiz interactively with instant scoring, and download the JSON. Your API key can be entered in the sidebar — it's never written to disk.
Docker alternative (full guide: docs/DOCKER.md):
docker build -t pdf-quiz . docker run -p 8501:8501 -e LLM_API_KEY=sk-... pdf-quiz
Outputs land in output/:
| File | Contents |
|---|---|
<name>_extracted.txt |
Raw text pulled from the PDF |
<name>_outline.md |
High-yield study guide (Prompt #1) |
<name>_quiz.md |
Markdown quiz + answer key (--format markdown) |
<name>_quiz.json |
Validated quiz JSON (--format json) |
All settings come from the environment (.env supported):
| Variable | Default | Example |
|---|---|---|
LLM_MODEL |
gpt-4o-mini |
gpt-4o, llama3.1:8b |
LLM_API_KEY |
not-needed |
sk-… |
LLM_BASE_URL |
OpenAI default | https://openrouter.ai/api/v1, http://localhost:11434/v1 |
Provider presets (set in .env):
# OpenAI LLM_BASE_URL=https://api.openai.com/v1 LLM_MODEL=gpt-4o-mini LLM_API_KEY=sk-your-key # OpenRouter LLM_BASE_URL=https://openrouter.ai/api/v1 LLM_MODEL=openai/gpt-4o-mini LLM_API_KEY=sk-or-your-key # Local Ollama (no key needed) LLM_BASE_URL=http://localhost:11434/v1 LLM_MODEL=llama3.1:8b LLM_API_KEY=not-needed
--strict(Structured Outputs) requires a server that supportschat.completions.parse— OpenAIgpt-4o-mini/gpt-4oand newer. Ollama and most proxies fall back to JSON mode automatically.
# Full pipeline: outline + quiz python -m pdf_quiz.cli "lecture.pdf" -n 10 # Frontend-ready JSON quiz python -m pdf_quiz.cli "lecture.pdf" --format json -n 10 # Guaranteed schema via Structured Outputs (with fallback) python -m pdf_quiz.cli "lecture.pdf" --format json --strict -n 10 # Outline only / quiz only python -m pdf_quiz.cli "lecture.pdf" --outline-only python -m pdf_quiz.cli "lecture.pdf" --quiz-only -n 15 # Cheaper quiz grounded in the outline instead of raw text python -m pdf_quiz.cli "lecture.pdf" --format json --quiz-from-outline # Custom output dir / extra retries on flaky models python -m pdf_quiz.cli "lecture.pdf" -o my_notes --format json --retries 4
Full flags: python -m pdf_quiz.cli --help (or pdf-quiz --help in a venv).
Quiz generation retries automatically
(default 2) when the model returns invalid JSON, feeding the validation
errors back for correction.
pytest -qCI (.github/workflows/ci.yml) runs the suite on Python 3.10–3.12 for every
push and pull request.
--format json emits exactly this shape — validated before it's written, so your UI can trust it:
{
"quiz_title": "Cell Biology Basics",
"questions": [
{
"id": 1,
"question": "Which organelle produces most of the cell's ATP?",
"options": ["Nucleus", "Mitochondrion", "Ribosome", "Golgi apparatus"],
"correct_answer": "Mitochondrion",
"explanation": "The text states mitochondria carry out oxidative phosphorylation to produce ATP."
}
]
}Rules enforced by validate_quiz_json() (src/pdf_quiz/generator.py:67):
quiz_title: non-empty string.- Exactly N questions,
idsequential from 1. options: exactly 4 distinct non-empty strings.correct_answer: byte-exact match of one option.explanation: non-empty.- Markdown fences (
```json) stripped automatically; non-JSON output rejected with a clear error.
Minimal frontend consumption:
const quiz = await fetch("lecture_quiz.json").then(r => r.json()); quiz.questions.forEach(q => { // q.question, q.options[4], q.correct_answer, q.explanation });
from pdf_quiz.extractor import extract_pdf_text from pdf_quiz.generator import generate_outline, generate_quiz_json, generate_quiz_structured text = extract_pdf_text("lecture.pdf") outline = generate_outline(text) quiz = generate_quiz_json(text, num_questions=10) # works on any backend quiz = generate_quiz_structured(text, num_questions=10) # Structured Outputs + fallback
prompts/
01_high_yield_outline.md # Prompt #1: executive summary + core concepts
02_quiz_generator.md # Markdown quiz variant
02_quiz_generator_json.md # Prompt #2: rigorous psychometrician, pure-JSON
src/pdf_quiz/
app.py # Streamlit web UI
cli.py # argument parsing + pipeline orchestration (`pdf-quiz`)
extractor.py # pypdf text extraction + chunking
generator.py # outline map-reduce, quiz render/validate/parse + retries
llm.py # OpenAI-compatible client (json_mode + complete_structured)
schemas.py # Pydantic Question / Quiz contract
tests/ # pytest suite (no API calls) — run with `pytest -q`
Prompts are plain Markdown with {{PLACEHOLDERS}} — edit them without touching code.
MIT — see LICENSE.
- Text PDFs only. Scanned/image PDFs have no text layer and exit with
No extractable text found— run OCR (e.g.ocrmypdf) first. - Quiz input cap. Raw text over ~15k chars is truncated for the quiz call; use
--quiz-from-outlinefor long documents. --strictsupport. Only backends implementingchat.completions.parsehonor it; everything else usesresponse_format: json_object+ validation.- LLM accuracy. The prompt constrains the model to the source text, but always spot-check high-stakes material.