Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 fix example scripts #265

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions examples/auto_invoice_splitter_extraction.py

This file was deleted.

43 changes: 43 additions & 0 deletions examples/auto_invoice_splitter_extraction_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from mindee import Client
from mindee.extraction.pdf_extractor import PdfExtractor
from mindee.input import PathInput
from mindee.product import InvoiceSplitterV1, InvoiceV4

mindee_client = Client(api_key="my-api-key")
# mindee_client = Client() # Optionally, set from env.


def parse_invoice(file_path):
input_source = PathInput(file_path)

if input_source.is_pdf() and input_source.count_doc_pages() > 1:
parse_multi_page(input_source)
else:
parse_single_page(input_source)


def parse_single_page(input_source):
invoice_result = mindee_client.parse(InvoiceV4, input_source)
print(invoice_result.document)


def parse_multi_page(input_source):
pdf_extractor = PdfExtractor(input_source)
invoice_splitter_response = mindee_client.enqueue_and_parse(
InvoiceSplitterV1, input_source, close_file=False
)
page_groups = (
invoice_splitter_response.document.inference.prediction.invoice_page_groups
)
extracted_pdfs = pdf_extractor.extract_invoices(page_groups, strict=False)

for extracted_pdf in extracted_pdfs:
# Optional: Save the files locally
# extracted_pdf.write_to_file("output/path")

invoice_result = mindee_client.parse(InvoiceV4, extracted_pdf.as_input_source())
print(invoice_result.document)


if __name__ == "__main__":
parse_invoice("path/to/my/file.ext")
33 changes: 33 additions & 0 deletions examples/auto_multi_receipts_extraction_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os

from mindee import Client, product
from mindee.extraction.multi_receipts_extractor.multi_receipts_extractor import (
extract_receipts,
)


def parse_receipts(input_path):
mindee_client = Client(api_key="my-api-key-here")
# mindee_client = Client() # Optionally, set from env.
input_doc = mindee_client.source_from_path(input_path)

result_split = mindee_client.parse(
product.MultiReceiptsDetectorV1, input_doc, close_file=False
)

extracted_receipts = extract_receipts(input_doc, result_split.document.inference)

for idx, receipt in enumerate(extracted_receipts, 1):
result_receipt = mindee_client.parse(product.ReceiptV5, receipt.as_source())
print(f"Receipt {idx}:")
print(result_receipt.document)
print("-" * 40)

# Uncomment to save each extracted receipt
# save_path = f"./receipt_{idx}.pdf"
# receipt.save_to_file(save_path)


if __name__ == "__main__":
input_file = "path/to/my/file.ext"
parse_receipts(input_file)
20 changes: 0 additions & 20 deletions examples/multi_receipts_tutorial.py

This file was deleted.