← Back to list

Nougat OCR — Extracting information from Academic Documents has never been easier— How to get…

In this article I want to guide you how to start with Nougat, I will use my usual very distilled and dry way of explaining things, so bear…

Francesco C. in Stackademic · 2024-08-06 22:01 · 220 claps · 1.2 min read
#nougat #llamaindex #meta #ai #scientific-papers
Open on Medium ↗
Wiki topics: LLM · Large Language Models AI · AI · General 🔬 · Science · General

Nougat OCR — Extracting information from Academic Documents has never been easier— How to get started — Simple guide.

[embed]

In this article I want to guide you on how to start with Nougat, I will use my usual very distilled and dry way of explaining things, so bear with me.

Nougat is part of transformers library, in order to do so you need to install Nougat library.

pip install git+https://github.com/facebookresearch/nougat
pip install fastapi
pip install requests
pip install python-multipart
pip install uvicorn
> nougat_api

And you will receive this error:

.env\Lib\site-packages\pydantic\main.py", line 193, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for InitSchema
alpha_affine
  Input should be None [type=none_required, input_value=1.2, input_type=float]
    For further information visit https://errors.pydantic.dev/2.8/v/none_required

ERROR:root:An error occurred while processing the PDF: Nougat OCR command failed.

To solve this error:

Go to :

.env\Lib\site-packages\nougat\transforms.py

Line 150 and modify alpha_affine=120 * 0.01 to None

            alb.Compose(
                [
                    alb.Affine(
                        translate_px=(0, 5), always_apply=True, cval=(255, 255, 255)
                    ),
                    alb.ElasticTransform(
                        p=1,
                        alpha=50,
                        sigma=120 * 0.1,
                        alpha_affine=None,
                        border_mode=0,
                        value=(255, 255, 255),
                    ),
                ],
                p=0.04,
            )

After you need to fix the same error as before, next you have another error

TypeError: BARTDecoder.prepare_inputs_for_inference() got an unexpected keyword argument 'cache_position'

To solve this issue you need to downgrade transformers (this update to 2024 August)

pip install transformers==4.38.2  

After that, you can run this command again

nougat_api

and use this small script to extract PDF

import requests
import json

def send_pdf(file_path, start=None, stop=None):
    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(file_path, 'rb') as f:
        files = {'file': (file_path, f, 'application/pdf')}
        headers = {'accept': 'application/json'}

        response = requests.post(url, headers=headers, files=files, params=params)
    print(response.json())
    return response.json()

Stackademic 🎓

Thank you for reading until the end. Before you go:


메타데이터
post_id
d1ffcbe2f62d
slug
nougat-ocr-extracting-information-from-academic-documents-has-never-been-easier-how-to-get-d1ffcbe2f62d
url
https://blog.stackademic.com/nougat-ocr-extracting-information-from-academic-documents-has-never-been-easier-how-to-get-d1ffcbe2f62d
canonical_url
https://blog.stackademic.com/nougat-ocr-extracting-information-from-academic-documents-has-never-been-easier-how-to-get-d1ffcbe2f62d
author_url
https://medium.com/@francesco.cozzolino
status
ok
fetched_at
2026-07-23 05:11:44