Image-to-Text
Transformers
Joblib
Persian
English
document-ai
ocr
invoice
persian
enterprise
aria-ai
Instructions to use alirezaaminzadeh/docflow-invoice-parser-fa with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use alirezaaminzadeh/docflow-invoice-parser-fa with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # 'pip install "transformers<5.0.0' from transformers import pipeline pipe = pipeline("image-to-text", model="alirezaaminzadeh/docflow-invoice-parser-fa")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("alirezaaminzadeh/docflow-invoice-parser-fa", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 2,369 Bytes
af24ae8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | """Export extracted invoices to JSON and Excel formats."""
from __future__ import annotations
import json
from io import BytesIO
from pathlib import Path
from typing import Any
import pandas as pd
from docflow.models import ExtractedInvoice, ValidationReport
def to_json(invoice: ExtractedInvoice, validation: ValidationReport | None = None) -> str:
payload: dict[str, Any] = {
"invoice": invoice.to_export_dict(),
"validation": validation.model_dump() if validation else None,
}
return json.dumps(payload, ensure_ascii=False, indent=2)
def to_excel_bytes(invoice: ExtractedInvoice, validation: ValidationReport | None = None) -> bytes:
buffer = BytesIO()
summary = pd.DataFrame(
[
{"Field": "Vendor", "Value": invoice.vendor_name},
{"Field": "Tax ID", "Value": invoice.vendor_tax_id},
{"Field": "Invoice #", "Value": invoice.invoice_number},
{"Field": "Date (Jalali)", "Value": invoice.invoice_date_jalali},
{"Field": "Buyer", "Value": invoice.buyer_name},
{"Field": "Subtotal", "Value": invoice.subtotal},
{"Field": "Tax", "Value": invoice.tax_amount},
{"Field": "Total", "Value": invoice.total_amount},
{"Field": "Currency", "Value": invoice.currency},
{"Field": "Confidence", "Value": invoice.confidence},
]
)
items = pd.DataFrame([item.model_dump() for item in invoice.line_items])
if items.empty:
items = pd.DataFrame(columns=["description", "quantity", "unit_price", "total", "confidence"])
validation_df = pd.DataFrame()
if validation and validation.issues:
validation_df = pd.DataFrame([i.model_dump() for i in validation.issues])
with pd.ExcelWriter(buffer, engine="openpyxl") as writer:
summary.to_excel(writer, sheet_name="Summary", index=False)
items.to_excel(writer, sheet_name="Line Items", index=False)
if not validation_df.empty:
validation_df.to_excel(writer, sheet_name="Validation", index=False)
buffer.seek(0)
return buffer.read()
def save_json(path: str | Path, invoice: ExtractedInvoice, validation: ValidationReport | None = None) -> None:
Path(path).write_text(to_json(invoice, validation), encoding="utf-8")
|