Skip to content

Commit

Permalink
docs and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
j2whiting committed Sep 6, 2024
1 parent 177a0a6 commit a950fdc
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
98 changes: 98 additions & 0 deletions document_intelligence/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
# NougatEquation Task

This project implements a FastAPI application to process PDF files, extract images, and recognize LaTeX equations from the images using Hugging Face's VisionEncoderDecoderModel. The application is designed to handle PDF uploads, extract images from the pages, and run them through a model to extract LaTeX mathematical notations.

## Features

- Accepts PDF files via a FastAPI endpoint.
- Extracts images from the PDF.
- Recognizes LaTeX equations in images.
- Processes image batches to ensure efficient resource usage.
- Returns the extracted LaTeX equations in a JSON response.

## Requirements

Before running the project, ensure you have the following dependencies installed:

- Python 3.8+
- `torch` (PyTorch)
- `transformers` (Hugging Face Transformers)
- `Pillow` (Python Imaging Library for image processing)
- `PyMuPDF` (for PDF processing)
- `fastapi` (web framework for the API)
- `uvicorn` (ASGI server)

### Installation

1. **Clone the repository**:

```bash
git clone https://github.com/yourusername/nougat-equation-task.git
cd nougat-equation-task
```

2. **Create a virtual environment**:

```bash
python3 -m venv nougat_env
source nougat_env/bin/activate
```

3. **Install the dependencies**:

First, create a `requirements.txt` file:

```txt
torch
transformers
Pillow
PyMuPDF
fastapi
uvicorn
```

Then install the dependencies using `pip`:

```bash
pip install -r requirements.txt
```

### Usage

1. **Start the FastAPI server**:

You can run the FastAPI server using `uvicorn`:

```bash
uvicorn main:app --reload --host 0.0.0.0 --port 8000
```

This will start the server at `http://0.0.0.0:8000`.

2. **API Endpoint**:

- **POST /process_pdf**: Accepts a PDF file and extracts LaTeX equations from the images in the file.

Example usage with `curl`:

```bash
curl -X 'POST' \
'http://127.0.0.1:8000/process_pdf' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F '[email protected]'
```

The response will be a JSON object with the extracted LaTeX equations.

### Example

Suppose you upload a PDF that contains images of mathematical equations. The API will process the PDF, extract the images, and return LaTeX code for the detected equations in JSON format.

Example response:

```json
{
"0": ["\\( x^2 + y^2 = z^2 \\)"],
"1": ["\\[ e = mc^2 \\]"]
}
1 change: 1 addition & 0 deletions document_intelligence/nougat/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self, images: List[Image.Image], batch_size: int):
self.index = 0

def next_batch(self) -> Tuple[List[Image.Image], int]:
logger.info(f"Processing batch {self.index} to {self.index + self.batch_size}")
if self.index >= len(self.images):
return None, self.index

Expand Down

0 comments on commit a950fdc

Please sign in to comment.