Metadata-Version: 2.1
Name: pdf2md_llm
Version: 0.1.3
Summary: A package to convert PDF files to Markdown using a local LLM.
Author: Leon Eversberg
License: MIT License
        
        Copyright (c) 2025 Leon Eversberg
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/leoneversberg/pdf2md_llm
Project-URL: Repository, https://github.com/leoneversberg/pdf2md_llm.git
Keywords: markdown,pdf,llm,parser,converter
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Environment :: GPU :: NVIDIA CUDA
Classifier: Topic :: File Formats
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pdf2img~=0.1.2
Requires-Dist: vllm==0.7.3
Requires-Dist: qwen-vl-utils~=0.0.10
Requires-Dist: accelerate~=1.4.0
Requires-Dist: transformers~=4.49.0
Requires-Dist: torch==2.5.1

# pdf2md_llm

`pdf2md_llm` is a Python package that converts PDF files to Markdown using a local Large Language Model (LLM). 

The package leverages the `pdf2image` library to convert PDF pages to images and a vision language model to generate Markdown text from these images.

## Features

- Convert PDF files to images.
- Generate Markdown text from images using a local LLM.
- Keep your data private. No third-party file uploads. 

## Installation

You need a CUDA compatible GPU to run local LLMs with vLLM.

You can use `pip` to install the package:

```bash
pip install pdf2md-llm
```
## Usage

### CLI

You can use the `pdf2md_llm` package via the **command line interface (CLI)**.

To convert a PDF file to Markdown, run the following command:

```bash
pdf2md_llm <pdf_file> [options]
```

#### Options

* `pdf_file`: Path to the PDF file to convert.
* `--model`: Name of the model to use (default: `Qwen/Qwen2.5-VL-3B-Instruct-AWQ`).
* `--dtype`: Data type for the model weights and activations (default: `None`).
* `--max_model_len`: Max model context length (default: `7000`).
* `--prompt`: Custom prompt for the LLM. (default: `None`).
* `--size`: Image size as a tuple (default: `(700, None)`).
* `--dpi`: DPI of the images (default: `200`).
* `--fmt`: Image format (default: `jpeg`).
* `--output_folder`: Folder to save the output Markdown file (default: `./out`).

#### Example

```bash
pdf2md_llm example.pdf --model "Qwen/Qwen2.5-VL-3B-Instruct-AWQ" --output_folder "./output"
```

##### Model Support:
Currently the following Qwen2.5-VL models are supported: 

* `Qwen/Qwen2.5-VL-3B-Instruct`
* `Qwen/Qwen2.5-VL-3B-Instruct-AWQ`
* `Qwen/Qwen2.5-VL-7B-Instruct`
* `Qwen/Qwen2.5-VL-7B-Instruct-AWQ`
* `Qwen/Qwen2.5-VL-72B-Instruct`
* `Qwen/Qwen2.5-VL-72B-Instruct-AWQ`

If you want to use a different model, feel free to add a vLLM compatible model to the factory function `llm_model()` in `llm.py`

### Python API

You can use the `pdf2md_llm` package via the **Python API**.

Basic usage:

```python
from vllm import SamplingParams

from pdf2md_llm.llm import llm_model
from pdf2md_llm.pdf2img import PdfToImg

pdf2img = PdfToImg(size=(700, None), output_folder="./out")
img_files = pdf2img.convert("example.pdf")

llm = llm_model(
    model="Qwen/Qwen2.5-VL-3B-Instruct-AWQ",  # Name of the huggingface model
    dtype="half",  # Model data type
)

sampling_params = SamplingParams(
    temperature=0.1,
    min_p=0.1,
    max_tokens=8192,
    stop_token_ids=[],
)

# Append all pages to one output Markdown file
for img_file in img_files:
    markdown_text = llm.generate(
        img_file, sampling_params=sampling_params
    )  # convert image to Markdown with LLM
    with open("example.md", "a", encoding="utf-8") as myfile:
        myfile.write(markdown_text)
```

For a full example, see [example_api.py](./pdf2md_llm/example_api.py)


## License

This project is licensed under the MIT License. See the LICENSE file for details.

## Acknowledgements

* [pdf2image](https://github.com/Belval/pdf2image) for converting PDF files to images.

* [Qwen2.5-VL](https://github.com/QwenLM/Qwen2.5-VL) LLM model

* [vLLM](https://github.com/vllm-project/vllm) for efficient LLM model inference
