book/tools/scripts/docs/FIGURE_CAPTIONS.md
This script improves figure and table captions in the ML Systems textbook using local Ollama LLM models. It provides automated caption enhancement with strong, educational language while maintaining proper formatting.
# Python dependencies (included in main requirements.txt)
pip install pypandoc pyyaml requests pillow
# Ollama for LLM caption improvement
brew install ollama # macOS
# or: curl -fsSL https://ollama.ai/install.sh | sh # Linux
# Download recommended models
ollama pull qwen2.5:7b # Default model (good balance)
ollama pull gemma2:9b # High quality alternative
ollama pull llama3.2:3b # Fast lightweight option
# Process all core chapters with default model
python3 scripts/improve_figure_captions.py -d contents/core/
# Use specific model
python3 scripts/improve_figure_captions.py -d contents/core/ -m gemma2:9b
# Process specific files
python3 scripts/improve_figure_captions.py -f contents/core/introduction/introduction.qmd
All main options have both short and long forms:
| Option | Short | Purpose |
|---|---|---|
--improve | -i | LLM caption improvement (default mode) |
--build-map | -b | Build content map and save to JSON |
--analyze | -a | Quality analysis + file validation |
--repair | -r | Fix formatting issues only |
| Option | Short | Purpose |
|---|---|---|
--model | -m | Specify Ollama model (default: qwen2.5:7b) |
--files | -f | Process specific QMD files |
--directories | -d | Process directories (follows _quarto-html.yml order) |
--save-json | Save detailed content map to JSON | |
--list-models | List available Ollama models |
# Default workflow - improve all captions
python3 scripts/improve_figure_captions.py -d contents/core/
# Equivalent explicit command
python3 scripts/improve_figure_captions.py --improve -d contents/core/
# With different model
python3 scripts/improve_figure_captions.py -i -d contents/core/ -m gemma2:9b
# Multiple directories
python3 scripts/improve_figure_captions.py -d contents/core/ -d contents/frontmatter/
# Build content map only
python3 scripts/improve_figure_captions.py --build-map -d contents/core/
python3 scripts/improve_figure_captions.py -b -d contents/core/
# Analyze caption quality and validate structure
python3 scripts/improve_figure_captions.py --analyze -d contents/core/
python3 scripts/improve_figure_captions.py -a -d contents/core/
# Fix formatting issues only (no LLM)
python3 scripts/improve_figure_captions.py --repair -d contents/core/
python3 scripts/improve_figure_captions.py -r -d contents/core/
# Save detailed JSON output for inspection
python3 scripts/improve_figure_captions.py -d contents/core/ --save-json
# List available Ollama models
python3 scripts/improve_figure_captions.py --list-models
# Process single file for testing
python3 scripts/improve_figure_captions.py -f contents/core/introduction/introduction.qmd -m gemma2:9b
| Model | Speed | Quality | Use Case |
|---|---|---|---|
| qwen2.5:7b | ⭐⭐⭐ | ⭐⭐⭐⭐ | Default - best balance |
| gemma2:9b | ⭐⭐ | ⭐⭐⭐⭐⭐ | High quality output |
| llama3.2:3b | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | Fast processing |
| mistral:7b | ⭐⭐⭐ | ⭐⭐⭐⭐ | Alternative option |
# Install specific models
ollama pull qwen2.5:7b
ollama pull gemma2:9b
ollama pull llama3.2:3b
# Check installed models
ollama list
**Bold Title**: Sentence case explanation.: **Bold Title**: Sentence case explanation. (note colon prefix)The script automatically:
Before (weak):
Illustrates how machine learning models can serve as amplifiers.
After (strong):
**Amplification Effects**: Machine learning models enable threat actors to scale attacks by automating target identification and payload generation.
The script builds a comprehensive map including:
# Check if Ollama is running
curl http://localhost:11434/api/tags
# Start Ollama service
ollama serve
# Check available models
ollama list
# Analyze extraction issues
python3 scripts/improve_figure_captions.py --analyze -d contents/core/
# Build content map to see details
python3 scripts/improve_figure_captions.py --build-map -d contents/core/
# Try different model
python3 scripts/improve_figure_captions.py -d contents/core/ -m gemma2:9b
# Check specific file
python3 scripts/improve_figure_captions.py -f problematic_file.qmd --save-json
-f filename.qmd--save-jsoncontent_map.json # Detailed content structure (if --save-json)
improvements_YYYYMMDD_HHMMSS.json # Summary of changes made
{
"figures": {
"fig-ai-timeline": {
"qmd_file": "contents/core/introduction/introduction.qmd",
"type": "tikz",
"original_caption": "...",
"new_caption": "...",
"improved": true
}
},
"tables": { ... },
"metadata": {
"extraction_stats": {
"figures_found": 270,
"tables_found": 92,
"extraction_failures": 0,
"success_rate": 100.0
}
}
}
The script works seamlessly with Quarto's build process:
{#fig-id .class})# 1. Improve captions
python3 scripts/improve_figure_captions.py -d contents/core/
# 2. Build book normally
quarto render
# 3. Check results
open build/html/index.html
-f filename.qmd--analyze--save-json-d contents/core/: **bold**: format)Last Updated: December 2024 Tested With: Quarto 1.5+, Ollama 0.3+, Python 3.8+ Script Version: 2.0 (streamlined options)