### Installation Source: https://context7.com/facebookresearch/nougat/llms.txt Instructions for installing Nougat using pip, including optional dependencies for API support. ```APIDOC ## Installation ### Install from pip ```bash pip install nougat-ocr ``` ### Install with optional dependencies ```bash # For API support pip install "nougat-ocr[api]" # For dataset generation pip install "nougat-ocr[dataset]" # Install from repository pip install git+https://github.com/facebookresearch/nougat ``` ``` -------------------------------- ### Start Nougat REST API Server Source: https://context7.com/facebookresearch/nougat/llms.txt Instructions for starting the Nougat REST API server. This involves setting the checkpoint path environment variable and running the `nougat_api` command. ```bash # Set checkpoint path export NOUGAT_CHECKPOINT=/path/to/checkpoint # Start server on port 8503 nougat_api ``` -------------------------------- ### Python API Client Example Source: https://context7.com/facebookresearch/nougat/llms.txt Example Python code demonstrating how to use the Nougat API for PDF to Markdown conversion. ```APIDOC ## Python Client for PDF to Markdown Conversion ### Description This Python script utilizes the `requests` library to interact with the Nougat API's `/predict/` endpoint for converting PDF files to Markdown. ### Code Example ```python import requests def convert_pdf_to_markdown(pdf_path: str, start: int = None, stop: int = None) -> str: """Convert PDF to Markdown using Nougat API.""" url = "http://127.0.0.1:8503/predict/" params = {} if start is not None: params["start"] = start if stop is not None: params["stop"] = stop with open(pdf_path, "rb") as f: files = {"file": (pdf_path, f, "application/pdf")} response = requests.post(url, files=files, params=params) if response.status_code == 200: return response.text else: raise Exception(f"Error: {response.status_code}") # Usage: Convert entire PDF try: markdown_text = convert_pdf_to_markdown("paper.pdf") print(markdown_text) except Exception as e: print(e) # Usage: Process only first 3 pages try: markdown_text = convert_pdf_to_markdown("paper.pdf", start=1, stop=3) print(markdown_text) except Exception as e: print(e) ``` ``` -------------------------------- ### Install Nougat OCR Source: https://github.com/facebookresearch/nougat/blob/main/README.md Installs the Nougat OCR package from PyPI or directly from the GitHub repository. Extra dependencies for API or dataset usage can be installed with specific extras. ```bash pip install nougat-ocr ``` ```bash pip install git+https://github.com/facebookresearch/nougat ``` ```bash pip install "nougat-ocr[api]" ``` ```bash pip install "nougat-ocr[dataset]" ``` -------------------------------- ### REST API Usage Source: https://context7.com/facebookresearch/nougat/llms.txt Instructions on how to start the Nougat REST API server. ```APIDOC ## REST API Usage ### Start the API server ```bash # Set checkpoint path export NOUGAT_CHECKPOINT=/path/to/checkpoint # Start server on port 8503 nougat_api ``` ``` -------------------------------- ### Build and Run Nougat Docker Container Source: https://github.com/facebookresearch/nougat/blob/main/docker/README.md Commands to build the Docker image from the repository and start the container with GPU support enabled. ```shell docker build -t . docker run -it -d -p :8503 --gpus all ``` -------------------------------- ### Run Nougat API Service Source: https://context7.com/facebookresearch/nougat/llms.txt Commands to start the Nougat API service with specific configuration options like batch size. ```bash NOUGAT_BATCHSIZE=4 nougat_api ``` -------------------------------- ### Install Nougat OCR via pip Source: https://context7.com/facebookresearch/nougat/llms.txt Installs the Nougat OCR package using pip. Optional dependencies for API support or dataset generation can be included. ```bash pip install nougat-ocr # For API support pip install "nougat-ocr[api]" # For dataset generation pip install "nougat-ocr[dataset]" # Install from repository pip install git+https://github.com/facebookresearch/nougat ``` -------------------------------- ### Start Nougat Prediction API Source: https://github.com/facebookresearch/nougat/blob/main/README.md Starts the Nougat API service, allowing PDF predictions via HTTP POST requests. The API can process a PDF file and return markdown text. It supports optional 'start' and 'stop' parameters to limit the page range for conversion. ```bash $ nougat_api ``` ```bash curl -X 'POST' \ 'http://127.0.0.1:8503/predict/' \ -H 'accept: application/json' \ -H 'Content-Type: multipart/form-data' \ -F 'file=@;type=application/pdf' ``` ```bash http://127.0.0.1:8503/predict/?start=1&stop=5 ``` -------------------------------- ### Metrics Computation Source: https://context7.com/facebookresearch/nougat/llms.txt Provides examples for computing detailed metrics from evaluation results using the command line and Python. ```APIDOC ## Compute Detailed Metrics (Command Line) ### Description Computes detailed metrics by modality (text, math, tables) from a JSON results file. ### Command ```bash python -m nougat.metrics results/output.json ``` ## Compute Metrics for First N Samples (Command Line) ### Description Computes metrics from the results file, limiting the computation to the first N samples. ### Command ```bash python -m nougat.metrics results/output.json -N 100 ``` ## Metrics Computation in Python ### Description Demonstrates how to compute metrics for a single prediction-ground truth pair and how to split text into modalities for separate evaluation using Python. ### Python Code ```python from nougat.metrics import compute_metrics, get_metrics, split_text # Compute metrics for single prediction-ground truth pair pred = "# Introduction\n\nThis paper presents..." gt = "# Introduction\n\nThis paper presents..." metrics = compute_metrics(pred, gt) # Returns: { # "edit_dist": 0.05, # Normalized edit distance # "bleu": 0.95, # BLEU score # "meteor": 0.92, # METEOR score # "precision": 0.98, # Token precision # "recall": 0.97, # Token recall # "f_measure": 0.975 # F1 score # } # Split text into modalities for separate evaluation predictions = ["# Title\n\[E=mc^2\]\n\\begin{tabular}...\\end{tabular}"] ground_truths = ["# Title\n\[E=mc^2\]\n\\begin{tabular}...\\end{tabular}"] pred_text, pred_math, pred_tables = split_text(predictions) gt_text, gt_math, gt_tables = split_text(ground_truths) # Get metrics with multiprocessing text_metrics = get_metrics(gt_text, pred_text, pool=True) math_metrics = get_metrics(gt_math, pred_math, pool=True) table_metrics = get_metrics(gt_tables, pred_tables, pool=True) # Average metrics for name, m in [("Text", text_metrics), ("Math", math_metrics), ("Tables", table_metrics)]: print(f"{name}: {sum(m['bleu'])/len(m['bleu']):.4f} BLEU") ``` ``` -------------------------------- ### GET / Source: https://github.com/facebookresearch/nougat/blob/main/docker/README.md Checks the health status of the Nougat API server. ```APIDOC ## GET / ### Description Verifies that the Nougat API server is running and reachable. ### Method GET ### Endpoint / ### Response #### Success Response (200) - **status-code** (integer) - The HTTP status code. - **data** (object) - Empty object indicating successful connection. #### Response Example { "status-code": 200, "data": {} } ``` -------------------------------- ### Python: LazyDataset for Efficient PDF Batch Processing Source: https://context7.com/facebookresearch/nougat/llms.txt Demonstrates the use of `LazyDataset` for efficient, lazy loading of PDF documents. This enables batch processing with automatic page rasterization, suitable for large numbers of documents or pages. It includes setup for the model, dataset creation, dataloader configuration, and batch inference. ```python from nougat import NougatModel from nougat.utils.dataset import LazyDataset from nougat.utils.checkpoint import get_checkpoint from nougat.utils.device import move_to_device from functools import partial import torch # Setup model checkpoint = get_checkpoint(model_tag="0.1.0-small") model = NougatModel.from_pretrained(checkpoint) model = move_to_device(model, cuda=True) model.eval() # Create dataset from PDF pdf_path = "paper.pdf" dataset = LazyDataset( pdf=pdf_path, prepare=partial(model.encoder.prepare_input, random_padding=False), pages=None # Process all pages, or pass list like [0, 1, 2] ) print(f"Processing {dataset.name} with {dataset.size} pages") # Create dataloader for batch processing dataloader = torch.utils.data.DataLoader( dataset, batch_size=4, shuffle=False, collate_fn=LazyDataset.ignore_none_collate ) # Process batches predictions = [] for sample, is_last_page in dataloader: if sample is None: continue output = model.inference(image_tensors=sample, early_stopping=True) for j, pred in enumerate(output["predictions"]): predictions.append(pred) if is_last_page[j]: # End of document reached full_text = "".join(predictions) print(full_text) predictions = [] ``` -------------------------------- ### Check CUDA Version Source: https://github.com/facebookresearch/nougat/blob/main/docker/README.md Verifies the installed CUDA version to ensure compatibility with the Docker base image and PyTorch version. ```shell nvcc -V ``` -------------------------------- ### Training Nougat Model Source: https://github.com/facebookresearch/nougat/blob/main/README.md This command initiates the training or fine-tuning process for a Nougat model. It requires a configuration file specifying training parameters. ```python python train.py --config config/train_nougat.yaml ``` -------------------------------- ### CLI: Process Single PDF with Nougat Source: https://context7.com/facebookresearch/nougat/llms.txt Demonstrates various command-line options for processing a single PDF file with Nougat. This includes specifying output directories, using different model variants, selecting specific pages, and controlling processing heuristics. ```bash # Basic usage - process PDF and output to directory nougat path/to/document.pdf -o output_directory # Use base model instead of default small model nougat path/to/document.pdf -o output_directory -m 0.1.0-base # Process specific pages (1-4 and page 7) nougat path/to/document.pdf -o output_directory -p "1-4,7" # Process with full precision (useful for CPU) nougat path/to/document.pdf -o output_directory --full-precision # Disable failure detection heuristic nougat path/to/document.pdf -o output_directory --no-skipping # Skip markdown postprocessing nougat path/to/document.pdf -o output_directory --no-markdown ``` -------------------------------- ### Basic and Custom Evaluation Source: https://context7.com/facebookresearch/nougat/llms.txt Demonstrates how to run the evaluation script with basic and custom parameters. ```APIDOC ## Basic Evaluation ### Description Runs the evaluation script with default parameters. ### Command ```bash python test.py --checkpoint path/to/checkpoint --dataset path/to/test.jsonl --save_path results/output.json ``` ## Custom Evaluation Parameters ### Description Runs the evaluation script with custom parameters for split, batch size, number of samples, and shuffle. ### Command ```bash python test.py --checkpoint path/to/checkpoint --dataset path/to/test.jsonl --save_path results/output.json --split test --batch_size 10 --num_samples 1000 --shuffle ``` ``` -------------------------------- ### CLI: Process Multiple PDFs with Nougat Source: https://context7.com/facebookresearch/nougat/llms.txt Illustrates how to use the Nougat CLI to process multiple PDF files. This can be done by specifying a directory containing PDFs, a text file listing PDF paths, or by setting a custom batch size for inference. ```bash # Process entire directory of PDFs nougat path/to/pdf_directory -o output_directory # Process from a file containing paths (one per line) nougat path/to/pdf_list.txt -o output_directory # Set custom batch size nougat path/to/pdf_directory -o output_directory -b 4 # Recompute already processed files nougat path/to/pdf_directory -o output_directory --recompute ``` -------------------------------- ### Generate Training Dataset Source: https://context7.com/facebookresearch/nougat/llms.txt Steps to process raw PDFs and HTML files into a structured dataset format for training. ```bash python -m nougat.dataset.split_htmls_to_pages --html path/html/root --pdfs path/pdf/root --out path/paired/output python -m nougat.dataset.create_index --dir path/paired/output --out train.jsonl python -m nougat.dataset.gen_seek train.jsonl ``` -------------------------------- ### CLI Usage - Process multiple PDFs Source: https://context7.com/facebookresearch/nougat/llms.txt Shows how to process multiple PDF files or an entire directory using the Nougat CLI. ```APIDOC ## CLI Usage - Process multiple PDFs ```bash # Process entire directory of PDFs nougat path/to/pdf_directory -o output_directory # Process from a file containing paths (one per line) nougat path/to/pdf_list.txt -o output_directory # Set custom batch size nougat path/to/pdf_directory -o output_directory -b 4 # Recompute already processed files nougat path/to/pdf_directory -o output_directory --recompute ``` ``` -------------------------------- ### CLI Usage - Process a single PDF file Source: https://context7.com/facebookresearch/nougat/llms.txt Demonstrates how to process a single PDF file using the Nougat CLI with various options. ```APIDOC ## CLI Usage - Process a single PDF file ```bash # Basic usage - process PDF and output to directory nougat path/to/document.pdf -o output_directory # Use base model instead of default small model nougat path/to/document.pdf -o output_directory -m 0.1.0-base # Process specific pages (1-4 and page 7) nougat path/to/document.pdf -o output_directory -p "1-4,7" # Process with full precision (useful for CPU) nougat path/to/document.pdf -o output_directory --full-precision # Disable failure detection heuristic nougat path/to/document.pdf -o output_directory --no-skipping # Skip markdown postprocessing nougat path/to/document.pdf -o output_directory --no-markdown ``` ``` -------------------------------- ### Generate Dataset: Create Index Source: https://github.com/facebookresearch/nougat/blob/main/README.md This command creates a jsonl file containing image paths, markdown text, and meta information from the paired output directory. This index file is crucial for data loading. ```python python -m nougat.dataset.create_index --dir path/paired/output --out index.jsonl ``` -------------------------------- ### NougatConfig - Model Configuration Source: https://context7.com/facebookresearch/nougat/llms.txt Details on how to configure the Nougat model architecture using `NougatConfig`. ```APIDOC ## NougatConfig - Model Configuration ### Description `NougatConfig` defines the architecture parameters for the Nougat model. ### Python Code ```python from nougat import NougatModel, NougatConfig # Create custom configuration config = NougatConfig( input_size=[896, 672], # Image input size (height, width) align_long_axis=False, # Rotate if height > width window_size=7, # Swin Transformer window size encoder_layer=[2, 2, 14, 2], # Encoder layer depths decoder_layer=10, # Number of decoder layers max_length=4096, # Maximum sequence length max_position_embeddings=4096, patch_size=4, # Patch size for encoder embed_dim=128, # Embedding dimension num_heads=[4, 8, 16, 32], # Attention heads per layer hidden_dimension=1024 # Decoder hidden dimension ) # Initialize model with custom config model = NougatModel(config) # Load pretrained and modify max_length model = NougatModel.from_pretrained( "path/to/checkpoint", max_length=8192 # Extend sequence length ) ``` ``` -------------------------------- ### Python: Core NougatModel for Image to Text Source: https://context7.com/facebookresearch/nougat/llms.txt This Python snippet shows how to use the `NougatModel` class for direct image-to-text conversion. It covers loading pre-trained checkpoints, processing a single image, and handling pre-prepared image tensors. The output includes predictions, sequences, and repetition detection. ```python from nougat import NougatModel from nougat.utils.checkpoint import get_checkpoint from nougat.utils.device import move_to_device from PIL import Image # Load pretrained model (auto-downloads if needed) checkpoint = get_checkpoint(model_tag="0.1.0-small") model = NougatModel.from_pretrained(checkpoint) model = move_to_device(model, cuda=True) model.eval() # Process a single image image = Image.open("page.png") output = model.inference(image=image, early_stopping=True) print(output["predictions"][0]) # Markdown text output # Process with pre-prepared tensor image_tensor = model.encoder.prepare_input(image) output = model.inference( image_tensors=image_tensor.unsqueeze(0), return_attentions=False, early_stopping=True ) # Output structure # { # "predictions": ["# Title\n\nAbstract text..."], # "sequences": tensor([[...]]), # "repeats": [None], # None if no repetition detected # "repetitions": ["..."] # } ``` -------------------------------- ### Train Nougat Model Source: https://context7.com/facebookresearch/nougat/llms.txt Configuration and execution commands for training the Nougat model, including parameter overrides and debug modes. ```yaml resume_from_checkpoint_path: null result_path: "result" dataset_paths: ["path/to/train.jsonl"] max_epochs: 30 ``` ```bash python train.py --config config/train_nougat.yaml python train.py --config config/train_nougat.yaml --debug ``` -------------------------------- ### Interact with Nougat API Source: https://github.com/facebookresearch/nougat/blob/main/docker/README.md Commands to test the API server connectivity and perform PDF-to-markdown conversion using POST requests. ```shell curl -X 'GET' 'http://127.0.0.1:/' curl -X 'POST' 'http://127.0.0.1:/predict/' -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -F 'file=@;type=application/pdf' ``` -------------------------------- ### Checkpoint Management Source: https://context7.com/facebookresearch/nougat/llms.txt Explains how to automatically download and manage model checkpoints using the `checkpoint` module. ```APIDOC ## Checkpoint Management ### Description The `checkpoint` module handles automatic downloading and management of model checkpoints. ### Python Code ```python from nougat.utils.checkpoint import get_checkpoint, download_checkpoint from pathlib import Path # Auto-download and get checkpoint path checkpoint_path = get_checkpoint(model_tag="0.1.0-small") print(f"Checkpoint at: {checkpoint_path}") # Use base model instead checkpoint_path = get_checkpoint(model_tag="0.1.0-base") # Use custom checkpoint location checkpoint_path = get_checkpoint( checkpoint_path="/custom/path/to/checkpoint", download=True # Download if not exists ) # Or set via environment variable import os os.environ["NOUGAT_CHECKPOINT"] = "/path/to/checkpoint" checkpoint_path = get_checkpoint() # Manual download to specific location download_checkpoint( checkpoint=Path("/custom/path"), model_tag="0.1.0-small" ) ``` ``` -------------------------------- ### POST /predict/ Source: https://github.com/facebookresearch/nougat/blob/main/README.md Uploads a PDF file to the Nougat server to perform document parsing and text extraction. ```APIDOC ## POST /predict/ ### Description Uploads a PDF file for processing. The API returns the extracted content in a lightweight Markdown (.mmd) format. ### Method POST ### Endpoint http://127.0.0.1:8503/predict/ ### Parameters #### Query Parameters - **start** (integer) - Optional - The starting page number for processing. - **stop** (integer) - Optional - The ending page number for processing. #### Request Body - **file** (binary) - Required - The PDF file to be parsed (multipart/form-data). ### Request Example curl -X 'POST' \ 'http://127.0.0.1:8503/predict/?start=1&stop=5' \ -H 'accept: application/json' \ -H 'Content-Type: multipart/form-data' \ -F 'file=@document.pdf;type=application/pdf' ### Response #### Success Response (200) - **body** (string) - The parsed document content in Markdown format. #### Response Example "# Title of Document\n\nThis is the extracted text from the PDF..." ``` -------------------------------- ### Generate Dataset: Split HTMLs to Pages Source: https://github.com/facebookresearch/nougat/blob/main/README.md This command splits HTML files into individual pages for dataset generation. It requires directories for HTML, PDFs, and specifies an output directory and the path to pdffigures2. Additional arguments control recomputation, markdown output, worker processes, DPI, timeout, and Tesseract OCR usage. ```python python -m nougat.dataset.split_htmls_to_pages --html path/html/root --pdfs path/pdf/root --out path/paired/output --figure path/pdffigures/outputs ``` -------------------------------- ### Interact with Nougat API Endpoints Source: https://context7.com/facebookresearch/nougat/llms.txt Demonstrates how to perform health checks and convert PDF documents to Markdown using curl or a Python client. ```bash curl -X 'GET' 'http://127.0.0.1:8503/' curl -X 'POST' \ 'http://127.0.0.1:8503/predict/' \ -H 'accept: application/json' \ -H 'Content-Type: multipart/form-data' \ -F 'file=@document.pdf;type=application/pdf' ``` ```python import requests def convert_pdf_to_markdown(pdf_path: str, start: int = None, stop: int = None) -> str: url = "http://127.0.0.1:8503/predict/" params = {} if start is not None: params["start"] = start if stop is not None: params["stop"] = stop with open(pdf_path, "rb") as f: files = {"file": (pdf_path, f, "application/pdf")} response = requests.post(url, files=files, params=params) if response.status_code == 200: return response.text else: raise Exception(f"Error: {response.status_code}") ``` -------------------------------- ### NougatModel - Core Model Class Source: https://context7.com/facebookresearch/nougat/llms.txt Python code demonstrating how to use the `NougatModel` class for processing single images. ```APIDOC ## NougatModel - Core Model Class The `NougatModel` class is the main model for converting document images to text, combining a SwinTransformer encoder and BART decoder. ```python from nougat import NougatModel from nougat.utils.checkpoint import get_checkpoint from nougat.utils.device import move_to_device from PIL import Image # Load pretrained model (auto-downloads if needed) checkpoint = get_checkpoint(model_tag="0.1.0-small") model = NougatModel.from_pretrained(checkpoint) model = move_to_device(model, cuda=True) model.eval() # Process a single image image = Image.open("page.png") output = model.inference(image=image, early_stopping=True) print(output["predictions"][0]) # Markdown text output # Process with pre-prepared tensor image_tensor = model.encoder.prepare_input(image) output = model.inference( image_tensors=image_tensor.unsqueeze(0), return_attentions=False, early_stopping=True ) # Output structure # { # "predictions": ["# Title\n\nAbstract text..."], # "sequences": tensor([[...]]), # "repeats": [None], # None if no repetition detected # "repetitions": ["..."] # } ``` ``` -------------------------------- ### Nougat Model Configuration in Python Source: https://context7.com/facebookresearch/nougat/llms.txt Configure the Nougat model architecture using `NougatConfig`. This allows customization of parameters such as input size, layer depths, attention heads, and embedding dimensions. Models can be initialized with custom configurations or loaded from pretrained checkpoints with modifications. ```python from nougat import NougatModel, NougatConfig # Create custom configuration config = NougatConfig( input_size=[896, 672], # Image input size (height, width) align_long_axis=False, # Rotate if height > width window_size=7, # Swin Transformer window size encoder_layer=[2, 2, 14, 2], # Encoder layer depths decoder_layer=10, # Number of decoder layers max_length=4096, # Maximum sequence length max_position_embeddings=4096, patch_size=4, # Patch size for encoder embed_dim=128, # Embedding dimension num_heads=[4, 8, 16, 32], # Attention heads per layer hidden_dimension=1024 # Decoder hidden dimension ) # Initialize model with custom config model = NougatModel(config) ``` ```python # Load pretrained and modify max_length model = NougatModel.from_pretrained( "path/to/checkpoint", max_length=8192 # Extend sequence length ) ``` -------------------------------- ### Run Nougat Evaluation Script Source: https://context7.com/facebookresearch/nougat/llms.txt Execute the main evaluation script for the Nougat model. This script requires a checkpoint, a dataset, and a path to save the results. Custom parameters like split, batch size, and number of samples can also be specified. ```bash python test.py \ --checkpoint path/to/checkpoint \ --dataset path/to/test.jsonl \ --save_path results/output.json ``` ```bash python test.py \ --checkpoint path/to/checkpoint \ --dataset path/to/test.jsonl \ --save_path results/output.json \ --split test \ --batch_size 10 \ --num_samples 1000 \ --shuffle ``` -------------------------------- ### Evaluation: Calculate Metrics Source: https://github.com/facebookresearch/nougat/blob/main/README.md This command calculates and displays the results for different text modalities based on the evaluation output file. It takes the results file generated during the testing phase as input. ```python python -m nougat.metrics path/to/results.json ``` -------------------------------- ### Evaluation: Test Model Source: https://github.com/facebookresearch/nougat/blob/main/README.md This command runs the evaluation of a trained Nougat model on a specified test dataset. It requires a model checkpoint, the test dataset path, and an output path for saving the results. ```python test.py --checkpoint path/to/checkpoint --dataset path/to/test.jsonl --save_path path/to/results.json ``` -------------------------------- ### Generate Dataset: Create Seek Map Source: https://github.com/facebookresearch/nougat/blob/main/README.md For each generated jsonl file, this command creates a seek map, which is essential for faster data loading during training or evaluation. ```python python -m nougat.dataset.gen_seek file.jsonl ``` -------------------------------- ### Nougat Checkpoint Management in Python Source: https://context7.com/facebookresearch/nougat/llms.txt Manage Nougat model checkpoints using the `checkpoint` module. This includes automatically downloading checkpoints, specifying custom download locations, and setting the checkpoint path via environment variables. ```python from nougat.utils.checkpoint import get_checkpoint, download_checkpoint from pathlib import Path # Auto-download and get checkpoint path checkpoint_path = get_checkpoint(model_tag="0.1.0-small") print(f"Checkpoint at: {checkpoint_path}") # Use base model instead checkpoint_path = get_checkpoint(model_tag="0.1.0-base") # Use custom checkpoint location checkpoint_path = get_checkpoint( checkpoint_path="/custom/path/to/checkpoint", download=True # Download if not exists ) # Or set via environment variable import os os.environ["NOUGAT_CHECKPOINT"] = "/path/to/checkpoint" checkpoint_path = get_checkpoint() # Manual download to specific location download_checkpoint( checkpoint=Path("/custom/path"), model_tag="0.1.0-small" ) ``` -------------------------------- ### Run Nougat PDF Prediction via CLI Source: https://github.com/facebookresearch/nougat/blob/main/README.md Executes Nougat to predict the content of a PDF file and save the output to a specified directory. Supports processing single files, multiple files, or all PDFs within a directory. Options allow for specifying batch size, checkpoint, model, output directory, and recomputation. ```bash $ nougat path/to/file.pdf -o output_directory ``` ```bash $ nougat path/to/directory -o output_directory ``` ```bash usage: nougat [-h] [--batchsize BATCHSIZE] [--checkpoint CHECKPOINT] [--model MODEL] [--out OUT] [--recompute] [--markdown] [--no-skipping] pdf [pdf ...] positional arguments: pdf PDF(s) to process. options: -h, --help show this help message and exit --batchsize BATCHSIZE, -b BATCHSIZE Batch size to use. --checkpoint CHECKPOINT, -c CHECKPOINT Path to checkpoint directory. --model MODEL_TAG, -m MODEL_TAG Model tag to use. --out OUT, -o OUT Output directory. --recompute Recompute already computed PDF, discarding previous predictions. --full-precision Use float32 instead of bfloat16. Can speed up CPU conversion for some setups. --no-markdown Do not add postprocessing step for markdown compatibility. --markdown Add postprocessing step for markdown compatibility (default). --no-skipping Don't apply failure detection heuristic. --pages PAGES, -p PAGES Provide page numbers like '1-4,7' for pages 1 through 4 and page 7. Only works for single PDFs. ``` ```bash $ nougat path/to/file.pdf -o output_directory -m 0.1.0-base ``` -------------------------------- ### Compute Metrics from Nougat Results Source: https://context7.com/facebookresearch/nougat/llms.txt Calculate evaluation metrics from the output JSON file generated by the Nougat model. Metrics can be computed for all modalities or limited to a specific number of samples. ```bash python -m nougat.metrics results/output.json ``` ```bash python -m nougat.metrics results/output.json -N 100 ``` -------------------------------- ### Nougat Metrics Computation in Python Source: https://context7.com/facebookresearch/nougat/llms.txt Utilize the Nougat Python library to compute evaluation metrics. This includes computing metrics for individual prediction-ground truth pairs, splitting text into modalities (text, math, tables), and using multiprocessing for efficient metric calculation. ```python from nougat.metrics import compute_metrics, get_metrics, split_text # Compute metrics for single prediction-ground truth pair pred = "# Introduction\n\nThis paper presents..." gt = "# Introduction\n\nThis paper presents..." metrics = compute_metrics(pred, gt) # Returns: { # "edit_dist": 0.05, # Normalized edit distance # "bleu": 0.95, # BLEU score # "meteor": 0.92, # METEOR score # "precision": 0.98, # Token precision # "recall": 0.97, # Token recall # "f_measure": 0.975 # F1 score # } ``` ```python # Split text into modalities for separate evaluation predictions = ["# Title\n\[E=mc^2\]\n\begin{tabular}...\end{tabular}"] ground_truths = ["# Title\n\[E=mc^2\]\n\begin{tabular}...\end{tabular}"] pred_text, pred_math, pred_tables = split_text(predictions) gt_text, gt_math, gt_tables = split_text(ground_truths) # Get metrics with multiprocessing text_metrics = get_metrics(gt_text, pred_text, pool=True) math_metrics = get_metrics(gt_math, pred_math, pool=True) table_metrics = get_metrics(gt_tables, pred_tables, pool=True) # Average metrics for name, m in [("Text", text_metrics), ("Math", math_metrics), ("Tables", table_metrics)]: print(f"{name}: {sum(m['bleu'])/len(m['bleu']):.4f} BLEU") ``` -------------------------------- ### POST /predict/ Source: https://github.com/facebookresearch/nougat/blob/main/docker/README.md Processes a PDF file and returns the extracted markdown text. ```APIDOC ## POST /predict/ ### Description Uploads a PDF file to be processed by the Nougat model. Returns the extracted content in markdown format. ### Method POST ### Endpoint /predict/ ### Parameters #### Query Parameters - **start** (integer) - Optional - The starting page number to process. - **stop** (integer) - Optional - The ending page number to process. #### Request Body - **file** (binary) - Required - The PDF file to be processed (multipart/form-data). ### Request Example curl -X 'POST' 'http://127.0.0.1:/predict/' -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -F 'file=@;type=application/pdf' ### Response #### Success Response (200) - **markdown** (string) - The extracted text content in markdown format. ``` -------------------------------- ### Postprocess Model Output Source: https://context7.com/facebookresearch/nougat/llms.txt Utilities for cleaning and formatting raw model output, including Markdown compatibility, repetition truncation, and closing LaTeX environments. ```python from nougat.postprocessing import markdown_compatible, postprocess, truncate_repetitions, close_envs clean_output = markdown_compatible(raw_output) processed = postprocess(raw_predictions, markdown_fix=True) cleaned = truncate_repetitions(text_with_repetition, min_len=30) fixed = close_envs(incomplete) ``` -------------------------------- ### LazyDataset - PDF Processing Dataset Source: https://context7.com/facebookresearch/nougat/llms.txt Python code illustrating the use of `LazyDataset` for efficient batch processing of PDF documents. ```APIDOC ## LazyDataset - PDF Processing Dataset `LazyDataset` provides lazy loading of PDF documents for efficient batch processing with automatic page rasterization. ```python from nougat import NougatModel from nougat.utils.dataset import LazyDataset from nougat.utils.checkpoint import get_checkpoint from nougat.utils.device import move_to_device from functools import partial import torch # Setup model checkpoint = get_checkpoint(model_tag="0.1.0-small") model = NougatModel.from_pretrained(checkpoint) model = move_to_device(model, cuda=True) model.eval() # Create dataset from PDF pdf_path = "paper.pdf" dataset = LazyDataset( pdf=pdf_path, prepare=partial(model.encoder.prepare_input, random_padding=False), pages=None # Process all pages, or pass list like [0, 1, 2] ) print(f"Processing {dataset.name} with {dataset.size} pages") # Create dataloader for batch processing dataloader = torch.utils.data.DataLoader( dataset, batch_size=4, shuffle=False, collate_fn=LazyDataset.ignore_none_collate ) # Process batches predictions = [] for sample, is_last_page in dataloader: if sample is None: continue output = model.inference(image_tensors=sample, early_stopping=True) for j, pred in enumerate(output["predictions"]): predictions.append(pred) if is_last_page[j]: # End of document reached full_text = "".join(predictions) print(full_text) predictions = [] ``` ``` -------------------------------- ### Health Check Endpoint Source: https://context7.com/facebookresearch/nougat/llms.txt Checks the health status of the Nougat API server. ```APIDOC ## GET / ### Description Checks the health status of the Nougat API server. ### Method GET ### Endpoint / ### Response #### Success Response (200) - **status-code** (integer) - HTTP status code. - **data** (object) - Empty object, indicates success. ### Response Example ```json { "status-code": 200, "data": {} } ``` ``` -------------------------------- ### Convert PDF to Markdown Source: https://context7.com/facebookresearch/nougat/llms.txt Converts a PDF document to Markdown format. Supports processing the entire PDF or specific page ranges. ```APIDOC ## POST /predict/ ### Description Converts a PDF document to Markdown format. You can specify a range of pages to process using `start` and `stop` query parameters. ### Method POST ### Endpoint /predict/ ### Parameters #### Query Parameters - **start** (integer) - Optional - The starting page number (1-based index). - **stop** (integer) - Optional - The ending page number (1-based index). #### Request Body - **file** (file) - Required - The PDF file to convert. The content type should be `application/pdf`. ### Request Example (Entire PDF) ```bash curl -X 'POST' \ 'http://127.0.0.1:8503/predict/' \ -H 'accept: application/json' \ -H 'Content-Type: multipart/form-data' \ -F 'file=@document.pdf;type=application/pdf' ``` ### Request Example (Specific Pages) ```bash curl -X 'POST' \ 'http://127.0.0.1:8503/predict/?start=1&stop=5' \ -H 'accept: application/json' \ -H 'Content-Type: multipart/form-data' \ -F 'file=@document.pdf;type=application/pdf' ``` ### Response #### Success Response (200) - **Markdown text** (string) - The converted Markdown content of the PDF. ### Response Example ```text # Title of the Document This is the content of the document in Markdown format... ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.