PatchFinder
Based on the paper "PatchFinder: Leveraging Visual Language Models for Accurate Information Retrieval using Model Uncertainty"
PatchFinder is a Python library for accurate document text extraction using Vision Language Models (VLMs). It works by splitting images into overlapping patches, processing each patch independently, and combining results based on model confidence.
Features
- Efficient patch-based document processing
- Support for custom VLM models and prompts
- Confidence-based result aggregation
- GPU acceleration support
- Batch processing capabilities
- Comprehensive CLI interface
Installation
pip install patchfinder
Quick Start
Process a single document:
from patchfinder import PatchFinder
from transformers import AutoProcessor, AutoModelForCausalLM
# Initialize model and processor
model_name = "microsoft/phi-3-vision-128k-instruct"
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
# Create PatchFinder instance
finder = PatchFinder(model=model, processor=processor)
# Process image
result = finder.extract(
image_path="document.jpg",
prompt="Extract all text from this document"
)
print(f"Extracted text: {result['text']}")
print(f"Confidence: {result['confidence']}")
Integration with Existing Models
If you're already using transformers or other vision models, PatchFinder can be easily integrated:
import torch
from transformers import AutoProcessor, AutoModelForCausalLM
from patchfinder import PatchFinder
class DocumentProcessor:
def __init__(self, model_name="microsoft/phi-3-vision-128k-instruct"):
# Initialize your existing model pipeline
self.processor = AutoProcessor.from_pretrained(
model_name,
trust_remote_code=True
)
self.model = AutoModelForCausalLM.from_pretrained(
model_name,
trust_remote_code=True,
torch_dtype=torch.float16,
device_map="auto" if torch.cuda.is_available() else None
)
# Add PatchFinder on top
self.patchfinder = PatchFinder(
model=self.model,
processor=self.processor,
patch_size=256,
overlap=0.25
)
def process_document(self, image_path: str, custom_prompt: str = None) -> dict:
# Use your existing preprocessing if needed
prompt = custom_prompt or "Extract all text from this document"
# Let PatchFinder handle the patch-based processing
result = self.patchfinder.extract(
image_path=image_path,
prompt=prompt,
timeout=30
)
# Post-process or format results as needed
return {
"text": result["text"],
"confidence": result["confidence"],
"processed_patches": result["processed_patches"]
}
# Usage example
processor = DocumentProcessor()
result = processor.process_document(
"document.jpg",
custom_prompt="Extract and structure all text from this document"
)
Command Line Interface
PatchFinder provides a powerful CLI for both single-file and batch processing:
Process a single image:
python -m patchfinder.cli process image.jpg --prompt="Extract text" --verbose
Process all images in a directory:
python -m patchfinder.cli batch_process ./images/ --output_file=results.json
CLI Options:
model_name: Vision language model to use (default: microsoft/phi-3-vision-128k-instruct)patch_size: Size of image patches (default: 256)overlap: Overlap ratio between patches (default: 0.25)device: Processing device (default: auto-detect GPU/CPU)timeout: Processing timeout in seconds (default: 30)verbose: Enable detailed logging
Advanced Usage
Custom model configuration:
finder = PatchFinder(
model=model,
processor=processor,
patch_size=512, # Larger patches
overlap=0.5, # More overlap
max_workers=2 # Parallel processing
)
Batch processing with custom settings:
from pathlib import Path
image_dir = Path("./documents")
for image_path in image_dir.glob("*.jpg"):
result = finder.extract(
image_path=str(image_path),
prompt="Extract and format all text",
timeout=60
)
print(f"Processed {image_path.name}: {result['confidence']:.2f} confidence")
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for patchfinder 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| patchfinder-0.2.0.tar.gz | 13.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| patchfinder-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.2 kB
Release files / patchfinder-0.2.0.tar.gz
| Download URL | patchfinder-0.2.0.tar.gz |
|---|---|
| Size | 13.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
93bb193338861214ad73e4be58f8c110f355365a76936534b09d3ee523e867b3
|
|
BLAKE2b-256 checksum How to use checksums |
84e2d1340f5aaedefc95a228527ede55373bad93a794886cf98cecd309427487
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.18
|
Release files / patchfinder-0.2.0-py3-none-any.whl
| Download URL | patchfinder-0.2.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3bc79b2b130db9098c3ceeb816c4d22762031c09feec95a4bfc94c0f5db76eb5
|
|
BLAKE2b-256 checksum How to use checksums |
761f8f42723c0800706c4d27897beab3d95a7cbc3120d6169c9490fa2af91811
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.18
|
Security for patchfinder 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.