Docs /tutorials/ Invoice Extraction
tutorials / use-cases

Invoice Extraction

Extract structured invoice data from images and PDFs using GLM-OCR and Sinapsis Agents.

ocrdocumentsinvoicesstructured-outputgpuDocumentation latest

Source: Invoice Extraction — GitHub

This demo extracts invoice information from an uploaded image or PDF and presents the result as JSON. It uses Sinapsis to configure the document-loading and inference steps, with a Gradio interface for selecting an input and downloading the output.

The executable implementation is in sinapsis-demos/invoice-extraction. All YAML configurations, OCR prompts, sample invoices, screenshots, and Docker assets remain in that repository.

What it demonstrates

  • Ingestion of PDF and image invoices using dedicated Templates.

  • GPU-backed invoice understanding with the zai-org/GLM-OCR model.

  • Structured extraction of fields including invoice identifiers, parties, line items, payment information, taxes and totals.

  • A PDF path that additionally uses a batch OCR Template and a structured-output LLM stage.

  • Export of results as JSON through a Gradio application.

Application flow

The UI chooses one of two Agent configurations according to the uploaded file type. They are separate declarative Agents invoked by the application, not an implicit branch within one Agent.

◇ DiagramDrag nodes · Scroll to zoom · Drag canvas to pan
flowchart TD
    UPLOAD([Upload invoice]) --> SELECT{PDF or image?}
    SELECT -->|Image| IMG[Image Agent<br/>FolderImageDatasetCV2]
    IMG --> IMG_OCR[GLMOCRInference]
    SELECT -->|PDF| PDF[PDF Agent<br/>PDFToImage]
    PDF --> BATCH[GLMOCRBatchInference]
    BATCH --> CONV[TextToLLMConversation]
    CONV --> LLM[LLaMACPPTextCompletion]
    IMG_OCR --> JSON[Parse structured result as JSON]
    LLM --> JSON
    JSON --> OUT([Display and download JSON])

Green-bordered nodes reference Sinapsis templates. Hover or focus them to fetch documentation.

View Mermaid source
mermaid
flowchart TD
    UPLOAD([Upload invoice]) --> SELECT{PDF or image?}
    SELECT -->|Image| IMG[Image Agent<br/>FolderImageDatasetCV2]
    IMG --> IMG_OCR[GLMOCRInference]
    SELECT -->|PDF| PDF[PDF Agent<br/>PDFToImage]
    PDF --> BATCH[GLMOCRBatchInference]
    BATCH --> CONV[TextToLLMConversation]
    CONV --> LLM[LLaMACPPTextCompletion]
    IMG_OCR --> JSON[Parse structured result as JSON]
    LLM --> JSON
    JSON --> OUT([Display and download JSON])

webapp.py selects configs/inference_invoice_image.yaml for image files and configs/inference_batch_invoice.yaml for PDFs. Another config, configs/inference_invoice_pdf.yaml, provides a simpler PDF-to-GLMOCRInference path but is not the PDF config selected by this web application. The diagram follows the application's actual configuration choices.

Source files

FilePurpose
README.mdOverview, prerequisites and UI instructions.
webapp.pyFile handling, Agent selection and JSON display/download.
configs/inference_invoice_image.yamlImage ingestion and GLM-OCR.
configs/inference_batch_invoice.yamlPDF batch OCR with a later LLM extraction stage.
configs/inference_invoice_pdf.yamlAlternative standalone PDF OCR configuration.
artifacts/invoice_example.pngExample image input.
artifacts/invoice_sample.pdfExample PDF input.
docker/DockerfileGPU runtime dependencies.
docker/compose_test.yamlDevelopment deployment.
instructions/webapp.pngScreenshot of the web interface.

The source examples contain extraction prompts and schemas. Instead of duplicating those large schemas here, follow the linked YAML for the authoritative set of requested JSON fields.

Requirements and running the demo

The source README calls for Docker/Compose with BuildKit, an NVIDIA GPU/NVIDIA Container Toolkit, the sinapsis-nvidia:base image, and access to the model and build dependencies. It gives the following commands:

Terminalbash
cd invoice-extraction/docker
DOCKER_BUILDKIT=1 docker compose -f compose_test.yaml build --no-cache --ssh default
docker compose -f compose_test.yaml up

Open http://localhost:7860 on the Docker host. In Gradio, upload a PDF, PNG or JPG invoice and select Run Agent. The application displays JSON and makes an output file available for download.

This is the source-provided setup, not a tested public quickstart. Building requires access to some SSH-installed repositories, GPU dependencies and model weights.

See Templates and Agent Model for the computation model behind these flows.

Published from Git revision 7b7ba25d627b