ModernBERT Document Classification
A robust document classification library leveraging ModernBERT and Docling for state-of-the-art text extraction and classification.
Features
- Advanced Extraction: Uses
doclingandlangchainto parse complex PDFs, images, and text files with OCR and layout analysis. - ModernBERT: Finetuned on your data for high-performance classification.
- End-to-End Pipeline: From raw files to trained model and inference.
- Metrics & Visualization: Automated generation of Confusion Matrices, Classification Reports (Precision, Recall, F1).
- CLI Support: Easy-to-use command-line interface.
Installation
pip install .
Note: Requires torch, docling, and tesseract-ocr/poppler-utils (for PDF/Image processing).
Usage
1. Data Preparation
Organize your data in a folder (e.g., data_root which is the default in config) where each subfolder represents a class.
data_root/
├── invoice/
│ ├── file1.pdf
│ └── file2.png
└── resume/
├── file3.txt
└── file4.pdf
2. Training (One-Step)
Run the training command. If dataset.csv does not exist, the library will automatically extract text from files in data_dir (defined in configs/train_config.yaml) before training.
python3 -m modernbert_doc_cls.cli train --config configs/train_config.yaml
Note: You can still run modernbert-cls extract manually if preferred.
Create a configuration file (e.g., configs/train_config.yaml):
data_path: "dataset.csv"
model_id: "answerdotai/ModernBERT-base"
results_dir: "results"
training_args:
train_batch_size: 4
eval_batch_size: 4
learning_rate: 2e-5
num_train_epochs: 3
save_strategy: "epoch"
eval_strategy: "epoch"
logging_strategy: "steps"
logging_steps: 10
save_total_limit: 2
metric_for_best_model: "f1"
Run training:
python3 -m modernbert_doc_cls.cli train --config configs/train_config.yaml
The model and metrics will be saved in results/.
4. Inference
CLI Usage
Predict the class of a new document.
python3 -m modernbert_doc_cls.cli predict \
--model_path results/model \
--encoder_path results/label_encoder.pkl \
--file path/to/document.pdf
Python API Usage
You can also use the library directly in your Python scripts:
from modernbert_doc_cls.inference import DocumentClassifier
# Initialize the classifier
classifier = DocumentClassifier(
model_path="results/model",
encoder_path="results/label_encoder.pkl"
)
# Predict
label, confidence = classifier.predict("data_root/class2/invoice.pdf")
print(f"Predicted: {label} ({confidence:.4f})")
License
MIT License
Metadata
Release files for modernbert-doc-cls 0.1.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 | Actions |
|---|---|---|---|---|
| modernbert_doc_cls-0.1.0.tar.gz | 10.6 kB | Without provenance | Details for modernbert_doc_cls-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| modernbert_doc_cls-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for modernbert_doc_cls-0.1.0-py3-none-any.whl |
Total release size: 21.6 kB
Release files / modernbert_doc_cls-0.1.0.tar.gz
| Download URL | modernbert_doc_cls-0.1.0.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1abc8ab84d51c99f52379af40349f4ed27ca3e19f799280ee2411979b40ebb89
|
|
BLAKE2b-256 checksum How to use checksums |
45b60a9828ba39eb695f4eeab8e4518912ad51d8e8ba2ab6e3587af068631518
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / modernbert_doc_cls-0.1.0-py3-none-any.whl
| Download URL | modernbert_doc_cls-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dca2df53246d50d470e81b20184f9cef459f107896a05c204a80bd383348fb44
|
|
BLAKE2b-256 checksum How to use checksums |
6eaaee2bf190ee5c8c222ba1a274dc2d104308a2a5438a304497f5cbbac4e383
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for modernbert-doc-cls 0.1.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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.