Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions OCR.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ and apply the local AI review in one run:
./ocr-scanned-pdf.sh SOURCE.pdf NEW-AI-OCR.pdf --force-ocr --ai-review
```

To use different locally installed Ollama models for the two stages, add
`--extract-model MODEL` and `--review-model MODEL` to the same command. Both
models must support the image input and output format expected by their stage.
The default models are unchanged when the options are omitted.

This is the workflow used by the viewer's Enhance button. Force OCR rasterizes
pages and replaces existing text in the new copy; the source remains unchanged.
The AI pass reviews all pages with recognized blocks, applies reviewed text and
Expand Down
16 changes: 13 additions & 3 deletions ocr-scanned-pdf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euo pipefail

usage() {
cat <<'EOF'
Usage: ./ocr-scanned-pdf.sh INPUT.pdf [OUTPUT.pdf] [--skip-text|--force-ocr] [--ai-review] [metadata options]
Usage: ./ocr-scanned-pdf.sh INPUT.pdf [OUTPUT.pdf] [--skip-text|--force-ocr] [--ai-review] [--extract-model MODEL] [--review-model MODEL] [metadata options]

Create a searchable, tagged PDF from an English-language scanned PDF. If
OUTPUT.pdf is omitted, the output is written beside the input as
Expand All @@ -13,7 +13,8 @@ written beside the output for searching and review. OCR lines are grouped into
logical paragraphs, and likely section headings are tagged as H1 or H2.
Use --skip-text for mixed documents to preserve pages that already have text.
Use --force-ocr to replace the text layer on every page, including existing OCR.
Use --ai-review to apply the local DeepSeek and SEC OCR review models afterward.
Use --ai-review to apply local Ollama OCR extraction and review afterward.
Use --extract-model and --review-model to override the default Ollama models.
Use --source-url URL to preserve the original document link in PDF metadata.
Add optional --title, --author, --description, --tags (or --keywords),
--publisher, --rights, --language, and --identifier values during enhancement.
Expand All @@ -37,6 +38,7 @@ if [[ $# -gt 0 && "$1" != --* ]]; then
fi
ocr_options=()
metadata_options=()
review_options=()
ocr_mode=""
ai_review=false
while [[ $# -gt 0 ]]; do
Expand All @@ -50,6 +52,14 @@ while [[ $# -gt 0 ]]; do
ocr_options+=("$1")
;;
--ai-review) ai_review=true ;;
--extract-model|--review-model)
if [[ $# -lt 2 || -z "$2" || "$2" == --* ]]; then
printf "Provide a model after %s.\n" "$1" >&2
exit 2
fi
review_options+=("$1" "$2")
shift
;;
--source-url)
if [[ $# -lt 2 || -z "$2" || "$2" == --* ]]; then
printf 'Provide a URL after --source-url.\n' >&2
Expand Down Expand Up @@ -209,7 +219,7 @@ fi
if "$ai_review"; then
printf 'PDF_MAGIC_PROGRESS review %s\n' "$input_pages"
"${tagger_python[@]}" "$script_dir/ollama-review-ocr.py" \
"$tagged_output" "$initial_report" "$review_report" ${resume_review[@]+"${resume_review[@]}"}
"$tagged_output" "$initial_report" "$review_report" ${resume_review[@]+"${resume_review[@]}"} ${review_options[@]+"${review_options[@]}"}
reviewed_output="$tagging_dir/reviewed.pdf"
printf 'PDF_MAGIC_PROGRESS finalizing\n'
"${tagger_python[@]}" "$tagger" \
Expand Down
5 changes: 3 additions & 2 deletions tests/test_ocr_shell_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_interrupted_review_reuses_ocr_and_saved_pages(self):
(directory / "ollama-review-ocr.py").write_text(
"from pathlib import Path\n"
"import sys\n"
"assert sys.argv[4:] == ['--resume']\n"
"assert sys.argv[4:] == ['--resume', '--extract-model', 'custom-ocr:latest', '--review-model', 'custom-review:latest']\n"
"checkpoint = Path(sys.argv[3])\n"
"if not checkpoint.exists():\n"
" checkpoint.write_text('{\"page\":1}\\n')\n"
Expand All @@ -56,7 +56,8 @@ def test_interrupted_review_reuses_ocr_and_saved_pages(self):
)
env = {**os.environ, "PATH": str(commands) + os.pathsep + os.environ["PATH"],
"PDF_MAGIC_REVIEW_CHECKPOINT": str(checkpoint)}
command = ["/bin/bash", str(script), str(source), str(output), "--force-ocr", "--ai-review"]
command = ["/bin/bash", str(script), str(source), str(output), "--force-ocr", "--ai-review",
"--extract-model", "custom-ocr:latest", "--review-model", "custom-review:latest"]
first = subprocess.run(command, env=env, capture_output=True, text=True)
self.assertEqual(first.returncode, 47, first.stderr)
self.assertFalse(output.exists())
Expand Down
Loading