Package: marker-pdf
MarkerMarker converts PDF, EPUB, and MOBI to markdown.  It's 10x faster than nougat, more accurate on most documents, and has low hallucination risk.Support for a range of PDF documents (optimized for books and scientific papers)Removes headers/footers/other artifactsConverts most equations to latexFormats code blocks and tablesSupport for multiple languages (although most testing is done in English).  Seesettings.pyfor a language list.Works on GPU, CPU, or MPSHow it worksMarker is a pipeline of deep learning models:Extract text, OCR if necessary (heuristics, tesseract)Detect page layout (layout segmenter,column detector)Clean and format each block (heuristics,nougat)Combine blocks and postprocess complete text (heuristics,pdf_postprocessor)Relying on autoregressive forward passes to generate text is slow and prone to hallucination/repetition.  From the nougat paper:We observed [repetition] in 1.5% of pages in the test set, but the frequency increases for out-of-domain documents.In my anecdotal testing, repetitions happen on 5%+ of out-of-domain (non-arXiv) pages.Nougat is an amazing model, but I wanted a faster and more general purpose solution. Marker is 10x faster and has low hallucination risk because it only passes equation blocks through an LLM forward pass.ExamplesPDFTypeMarkerNougatThink PythonTextbookViewViewThink OSTextbookViewViewSwitch TransformersarXiv paperViewViewMulti-column CNNarXiv paperViewViewPerformanceThe above results are with marker and nougat setup so they each take ~3GB of VRAM on an A6000.Seebelowfor detailed speed and accuracy benchmarks, and instructions on how to run your own benchmarks.CommunityDiscordis where we discuss future development.LimitationsPDF is a tricky format, so marker will not always work perfectly.  Here are some known limitations that are on the roadmap to address:Marker will convert fewer equations to latex than nougat.  This is because it has to first detect equations, then convert them without hallucation.Whitespace and indentations are not always respected.Not all lines/spans will be joined properly.Languages similar to English (Spanish, French, German, Russian, etc) have the best support. There is provisional support for Chinese, Japanese, Korean, and Hindi, but it may not work as well.This works best on digital PDFs that won't require a lot of OCR.  It's optimized for speed, and limited OCR is used to fix errors.InstallationThis has been tested on Mac and Linux (Ubuntu and Debian).  You'll need python 3.9+ andpoetry.First, clone the repo:git clone https://github.com/VikParuchuri/marker.gitcd markerLinuxInstall system requirementsOptional: Install tesseract 5 by followingthese instructionsor runningscripts/install/tesseract_5_install.sh.Install ghostscript > 9.55 by followingthese instructionsor runningscripts/install/ghostscript_install.sh.Install other requirements withcat scripts/install/apt-requirements.txt | xargs sudo apt-get install -ySet the tesseract data folder pathFind the tesseract data foldertessdatawithfind / -name tessdata.  Make sure to use the one corresponding to the latest tesseract version if you have multiple.Create alocal.envfile in the rootmarkerfolder withTESSDATA_PREFIX=/path/to/tessdatainside itInstall python requirementspoetry installpoetry shellto activate your poetry venvUpdate pytorch since poetry doesn't play nicely with itGPU only: runpip install torchto install other torch dependencies.CPU only: Uninstall torch withpoetry remove torch, then follow theCPU installinstructions.MacInstall system requirements fromscripts/install/brew-requirements.txtSet the tesseract data folder pathFind the tesseract data foldertessdatawithbrew list tesseractCreate alocal.envfile in the rootmarkerfolder withTESSDATA_PREFIX=/path/to/tessdatainside itInstall python requirementspoetry installpoetry shellto activate your poetry venvOn ARM macs (M1+), make sure to set theTORCH_DEVICEsetting tomps(more details below) for a speedupUsageFirst, some configuration:Set your torch device in thelocal.envfile.  For example,TORCH_DEVICE=cudaorTORCH_DEVICE=mps.cpuis the default.If using GPU, setINFERENCE_RAMto your GPU VRAM (per GPU).  For example, if you have 16 GB of VRAM, setINFERENCE_RAM=16.Depending on your document types, marker's average memory usage per task can vary slightly.  You can configureVRAM_PER_TASKto adjust this if you notice tasks failing with GPU out of memory errors.Inspect the other settings inmarker/settings.py.  You can override any settings in thelocal.envfile, or by setting environment variables.By default, the final editor model is off.  Turn it on withENABLE_EDITOR_MODEL.By default, marker will use ocrmypdf for OCR, which is slower than base tesseract, but higher quality.  You can change this with theOCR_ENGINEsetting.Convert a single fileRunconvert_single.py, like this:python convert_single.py /path/to/file.pdf /path/to/output.md --parallel_factor 2 --max_pages 10--parallel_factoris how much to increase batch size and parallel OCR workers by.  Higher numbers will take more VRAM and CPU, but process faster.  Set to 1 by default.--max_pagesis the maximum number of pages to process.  Omit this to convert the entire document.Make sure theDEFAULT_LANGsetting is set appropriately for your document.Convert multiple filesRunconvert.py, like this:python convert.py /path/to/input/folder /path/to/output/folder --workers 10 --max 10 --metadata_file /path/to/metadata.json --min_length 10000--workersis the number of pdfs to convert at once.  This is set to 1 by default, but you can increase it to increase throughput, at the cost of more CPU/GPU usage. Parallelism will not increase beyondINFERENCE_RAM / VRAM_PER_TASKif you're using GPU.--maxis the maximum number of pdfs to convert.  Omit this to convert all pdfs in the folder.--metadata_fileis an optional path to a json file with metadata about the pdfs.  If you provide it, it will be used to set the language for each pdf.  If not,DEFAULT_LANGwill be used. The format is:--min_lengthis the minimum number of characters that need to be extracted from a pdf before it will be considered for processing.  If you're processing a lot of pdfs, I recommend setting this to avoid OCRing pdfs that are mostly images. (slows everything down){
  "pdf1.pdf": {"language": "English"},
  "pdf2.pdf": {"language": "Spanish"},
  ...
}Convert multiple files on multiple GPUsRunchunk_convert.sh, like this:MIN_LENGTH=10000 METADATA_FILE=../pdf_meta.json NUM_DEVICES=4 NUM_WORKERS=15 bash chunk_convert.sh ../pdf_in ../md_outMETADATA_FILEis an optional path to a json file with metadata about the pdfs.  See above for the format.NUM_DEVICESis the number of GPUs to use.  Should be2or greater.NUM_WORKERSis the number of parallel processes to run on each GPU.  Per-GPU parallelism will not increase beyondINFERENCE_RAM / VRAM_PER_TASK.MIN_LENGTHis the minimum number of characters that need to be extracted from a pdf before it will be considered for processing.  If you're processing a lot of pdfs, I recommend setting this to avoid OCRing pdfs that are mostly images. (slows everything down)BenchmarksBenchmarking PDF extraction quality is hard.  I've created a test set by finding books and scientific papers that have a pdf version and a latex source.  I convert the latex to text, and compare the reference to the output of text extraction methods.Benchmarks show that marker is 10x faster than nougat, and more accurate outside arXiv (nougat was trained on arXiv data).  We show naive text extraction (pulling text out of the pdf with no processing) for comparison.SpeedMethodAverage ScoreTime per pageTime per documentnaive0.3507270.001523780.326524marker0.6410620.36062277.2762nougat0.6292113.77259808.413AccuracyFirst 3 are non-arXiv books, last 3 are arXiv papers.Methodswitch_trans.pdfcrowd.pdfmulticolcnn.pdfthinkos.pdfthinkdsp.pdfthinkpython.pdfnaive0.2441140.1406690.08682210.3668560.4125210.468281marker0.4820910.4668820.5370620.7543470.788250.779536nougat0.6964580.5523370.7350990.6550020.6457040.650282Peak GPU memory usage during the benchmark is3.3GBfor nougat, and3.1GBfor marker.  Benchmarks were run on an A6000.ThroughputMarker takes about 2GB of VRAM on average per task, so you can convert 24 documents in parallel on an A6000.Running your own benchmarksYou can benchmark the performance of marker on your machine.  First, download the benchmark datahereand unzip.Then runbenchmark.pylike this:python benchmark.py data/pdfs data/references report.json --nougatThis will benchmark marker against other text extraction methods.  It sets up batch sizes for nougat and marker to use a similar amount of GPU RAM for each.Omit--nougatto exclude nougat from the benchmark.  I don't recommend running nougat on CPU, since it is very slow.Commercial usageDue to the licensing of the underlying models like layoutlmv3 and nougat, this is only suitable for noncommercial usage.I'm building a version that can be used commercially, by stripping out the dependencies below. If you would like to get early access, email me atmarker@vikas.sh.Here are the non-commercial/restrictive dependencies:LayoutLMv3: CC BY-NC-SA 4.0 .SourceNougat: CC-BY-NC .SourcePyMuPDF - GPL .SourceOther dependencies/datasets are openly licensed (doclaynet, byt5), or used in a way that is compatible with commercial usage (ghostscript).ThanksThis work would not have been possible without amazing open source models and datasets, including (but not limited to):Nougat from MetaLayoutlmv3 from MicrosoftDocLayNet from IBMByT5 from GoogleThank you to the authors of these models and datasets for making them available to the community!