Skip to main content

A tool to extract PDF files to markdown, or any other format using AI

Project description

AIPDF: Minimalistic PDF to Markdown (and others), with GPT-like Multimodal Models

AIPDF is a stand-alone, minimalistic, yet powerful pure Python library that leverages multi-modal gen AI models (OpenAI, llama3 or compatible alternatives) to extract data from PDFs and convert it Markdown.

Installation

pip install aipdf

Quick Start

from aipdf import ocr

# Your API key
# This can also be via the environment variable AIPDF_API_KEY
api_key = 'your_api_key'

file = open('somepdf.pdf', 'rb')
markdown_pages = ocr(file, api_key)

By default, AIPDF attempts to determine which pages to send to the LLM based on their content and whether they can be processed using traditional text parsing. This is done to improve performance, and the behavior can be overridden by setting the use_llm_for_all parameter to True:

markdown_pages = ocr(file, api_key, use_llm_for_all=True)

Every call to the LLM is made in parallel, so the processing time is significantly reduced. The above function will make these parallel calls using threading, however, it is also possible to make asynchronous calls instead by using the ocr_async function:

from aipdf import ocr_async
import asyncio

# Your API key
# This can also be via the environment variable AIPDF_API_KEY
api_key = 'your_api_key'

file = open('somepdf.pdf', 'rb')

async def main():
    markdown_pages = await ocr_async(file, api_key)
    return markdown_pages

markdown_pages = asyncio.run(main())

The maximum number of concurrent requests made to the LLM can also be controlled via the AIPDF_MAX_CONCURRENT_REQUESTS environment variable. By default, there is no limit set.

Ollama

You can use with any ollama multi-modal models

ocr(pdf_file, api_key='ollama', model="llama3.2", base_url= 'http://localhost:11434/v1', prompt=...)

Any file system

We chose that you pass a file object, because that way it is flexible for you to use this with any type of file system, s3, localfiles, urls etc

From url

pdf_file = io.BytesIO(requests.get('https://arxiv.org/pdf/2410.02467').content)

# extract
pages = ocr(pdf_file, api_key, prompt="extract tables, return each table in json")

From S3

s3 = boto3.client('s3', config=Config(signature_version='s3v4'),
                  aws_access_key_id=access_token,
                  aws_secret_access_key='', # Not needed for token-based auth
                  aws_session_token=access_token)


pdf_file = io.BytesIO(s3.get_object(Bucket=bucket_name, Key=object_key)['Body'].read())
# extract 
pages = ocr(pdf_file, api_key, prompt="extract charts data, turn it into tables that represent the variables in the chart")

Why AIPDF?

  1. Simplicity: AIPDF provides a straightforward function, it requires minimal setup, dependencies and configuration.
  2. Power of AI: Leverages state-of-the-art multi modal models (gpt, llama, ..).
  3. Customizable: Tailor the extraction process to your specific needs with custom prompts.
  4. Efficient: Utilizes parallel processing for faster extraction of multi-page PDFs.

Requirements

  • Python 3.7+

We will keep this super clean, only 2 required libraries:

  • openai library to talk to completion endpoints
  • PyMuPDF library for traditional text parsing and image conversion

License

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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any problems or have any questions, please open an issue on the GitHub repository.


AIPDF makes PDF data extraction simple, flexible, and powerful. Try it out and simplify your PDF processing workflow today!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aipdf-0.0.7.1.tar.gz (6.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aipdf-0.0.7.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file aipdf-0.0.7.1.tar.gz.

File metadata

  • Download URL: aipdf-0.0.7.1.tar.gz
  • Upload date:
  • Size: 6.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for aipdf-0.0.7.1.tar.gz
Algorithm Hash digest
SHA256 d01d002e2c3098fafea6e5f0b778c6fc8481da9d58a060bcde44b66e2fdf6fdd
MD5 6339ab3ca67ea477ac40d41886266d5c
BLAKE2b-256 5d140849c05c96a0ee16349b0e3b8251cca728086057b90288d54472800f6d38

See more details on using hashes here.

File details

Details for the file aipdf-0.0.7.1-py3-none-any.whl.

File metadata

  • Download URL: aipdf-0.0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for aipdf-0.0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 31a9cea0cc318b278333168133b11ce06b4417676e640d06942f0c28ecd5edc2
MD5 2a8848cbde63524a5af759f5ef5ee30f
BLAKE2b-256 730c47cafbe4b96fcfe4b548c4b3c964e3ddb7a69565003b92b1b1dcc040a1d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page