Skip to main content

PDF to markdown using Azure OpenAI batch processing

Project description

Parallex

What it does

  • Converts PDF into images
  • Makes requests to Azure OpenAI to convert the images to markdown using Batch API
  • Polls for batch completion and then converts AI responses in structured output based on the page of the corresponding PDF
  • Post batch processing to do what you wish with the resulting markdown

Requirements

Parallex uses graphicsmagick for the conversion of PDF to images.

brew install graphicsmagick

Installation

pip install parallex

Example usage

import os
from parallex.models.parallex_callable_output import ParallexCallableOutput
from parallex.parallex import parallex

os.environ["AZURE_API_KEY"] = "key"
os.environ["AZURE_API_BASE"] = "your-endpoint.com"
os.environ["AZURE_API_VERSION"] = "deployment_version"
os.environ["AZURE_API_DEPLOYMENT"] = "deployment_name"

model = "gpt-4o"

async def some_operation(file_url: str) -> None:
  response_data: ParallexCallableOutput = await parallex(
    model=model,
    pdf_source_url=file_url,
    post_process_callable=example_post_process, # Optional
    concurrency=2, # Optional
    prompt_text="Turn images into markdown", # Optional
    log_level="ERROR" # Optional
  )
  pages = response_data.pages

def example_post_process(output: ParallexCallableOutput) -> None:
    file_name = output.file_name
    pages = output.pages
    for page in pages:
        markdown_for_page = page.output_content
        pdf_page_number = page.page_number
        

Responses have the following structure;

class ParallexCallableOutput(BaseModel):
    file_name: str = Field(description="Name of file that is processed")
    pdf_source_url: str = Field(description="Given URL of the source of output")
    trace_id: UUID = Field(description="Unique trace for each file")
    pages: list[PageResponse] = Field(description="List of PageResponse objects")

class PageResponse(BaseModel):
    output_content: str = Field(description="Markdown generated for the page")
    page_number: int = Field(description="Page number of the associated PDF")

Default prompt is

"""
    Convert the following PDF page to markdown.
    Return only the markdown with no explanation text.
    Leave out any page numbers and redundant headers or footers.
    Do not include any code blocks (e.g. "```markdown" or "```") in the response.
    If unable to parse, return an empty string.
"""

Batch processing for list of prompts

If you do not need to process images, but just want to process prompts using the Batch API, you can call;

response_data: ParallexPromptsCallableOutput = await parallex_simple_prompts(
    model=model,
    prompts=["Some prompt", "Some other prompt"],
    post_process_callable=example_post_process
)
responses = response_data.responses

This will create a batch that includes all the prompts in prompts and responses can be tied back to the prompt by index.

Responses have the following structure;

class ParallexPromptsCallableOutput(BaseModel):
    original_prompts: list[str] = Field(description="List of given prompts")
    trace_id: UUID = Field(description="Unique trace for each file")
    responses: list[PromptResponse] = Field(description="List of PromptResponse objects")

class PromptResponse(BaseModel):
    output_content: str = Field(description="Response from the model")
    prompt_index: int = Field(description="Index corresponding to the given prompts")

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

parallex-0.2.1.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

parallex-0.2.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file parallex-0.2.1.tar.gz.

File metadata

  • Download URL: parallex-0.2.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.6 Darwin/21.6.0

File hashes

Hashes for parallex-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1e759adebf8afec2eb2ba9a841c0e29cad703532eea7a4e175afb6da336537fb
MD5 1b84eaf2847cdc6733c20fad7557a495
BLAKE2b-256 2c1d9ce5adee873b965df720877b02534dc34fefb46f9a9b4364eee4b2e7ed20

See more details on using hashes here.

File details

Details for the file parallex-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: parallex-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.6 Darwin/21.6.0

File hashes

Hashes for parallex-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 612208eded13d9164e85f0ecaafa3fe0b76126108331e74f95ed88710a8e5090
MD5 6cc4af9cf9f19ddd1650afc65b6d500e
BLAKE2b-256 5fe0d61cb2ba8b3029a255178b2fe407316347356ef3d533d0e8f44b23a3fb43

See more details on using hashes here.

Supported by

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