# ck-rajput/invoice_information_application architecture

> Architecture diagram of the GitHub repository ck-rajput/invoice_information_application, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ck-rajput/invoice_information_application
- Repository: https://github.com/ck-rajput/invoice_information_application

## Overview

##### Project Type and Purpose
- This is a full-stack, single-page Streamlit application (“app.py”) providing an end-to‐end invoice extraction pipeline: from image upload and OCR through post-processing, ML-based field extraction, and visualization. It also supports model training/evaluation and writes results to a JSON store.

##### File Structure
- Top-Level
- app.py – Streamlit UI entrypoint
- config.yaml + .env (not checked in) – configuration for API keys, thresholds
- invoice_results.json – local storage for extracted results
- requirements.txt / setup.py – dependencies & packaging
- Source Modules (src/)
- data_processing/
– download_datasets.py – fetch or update raw invoice image sets
– preprocess.py – image cleaning, resizing, normalization
- extraction/
– ocr_engine.py – plugin‐style wrapper over Gemini API, Tesseract, EasyOCR, PaddleOCR
– extract.py – post-OCR parsing, regex or ML model for field extraction, confidence scoring
- model/
– train.py – training simulation, model evaluation / analytics pipeline
- utils/
– config_loader.py – reads config.yaml/.env, supplies API keys & thresholds
– logger.py – unified logging for traceability

##### README for Architecture Insights
- Frontend: Streamlit
- OCR Engines: multiple external services & libraries
- ML: simulated training in model/train.py
- Storage: local JSON output + in-app dashboards
- No external database: invoice_results.json acts as simple persistence

a. Main Components
1. User Interface Layer (Streamlit app)
2. Application Service Layer (Controller in app.py invoking utility modules)
3. Data Processing Module
4. OCR & Extraction Module
5. ML Training Module
6. Configuration & Logging (cross‐cutting)
7. Data Store (invoice_results.json)
8. External Services (Gemini API, Tesseract, EasyOCR, PaddleOCR)

b. Relationships & Data Flow
- UI → Config Loader (load API keys, thresholds)
- UI → Data Processing → Preprocessed Image Data
- Preprocessed Data → OCR Engine → Raw Text
- Raw Text → Extraction → Structured Fields + Confidence Scores
- Structured Output → JSON Store → UI Dashboard / Download
- “Train Model” UI action → Model Module → (optionally) update analytics dashboard
- All modules → Logger → Centralized logs

c. Architectural Patterns & Design Principles
- Layered Architecture: UI / Service / Processing / Storage
- Plugin Pattern: ocr_engine.py dynamically selects OCR backend
- Single Responsibility & Modularity: each script fulfills one clear role
- Configuration‐Driven: config_loader abstracts environment vs YAML

d. Key Technologies & Frameworks
- Streamlit (UI)
- Python packages: Pillow, OpenCV, Pandas
- OCR: Gemini API, Tesseract, EasyOCR, PaddleOCR
- Logging: custom logger in utils/logger.py
- Data storage: JSON file

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Interaction Zone
    subgraph "User Interaction Zone"
        UI["Streamlit UI<br/>(app.py)"]:::ui
    end

    %% Backend Processing Zone
    subgraph "Backend Processing Zone"
        %% Utilities
        UTILCFG["Config Loader"]:::util
        UTILLG["Logger"]:::util

        %% Inference Pipeline
        subgraph "Inference Pipeline"
            DP["Dataset Downloading"]:::processing
            PP["Image Preprocessing"]:::processing
            OCRE["OCR Engine"]:::processing
            EXTR["Field Extraction"]:::processing
        end

        %% Training Pipeline
        subgraph "Training Pipeline"
            TRAIN["Model Training & Evaluation"]:::processing
        end

        %% Data Store
        STORE["Invoice Results<br/>JSON Store"]:::storage

        %% External OCR Services
        subgraph "External OCR Services"
            GEM["Gemini API"]:::external
            TESS["Tesseract"]:::external
            EASY["EasyOCR"]:::external
            PADD["PaddleOCR"]:::external
        end
    end

    %% Data Flow & Dependencies
    UI -->|"loads config"| UTILCFG
    UTILCFG -->|provides settings| DP
    UTILCFG -->|provides settings| PP
    UTILCFG -->|provides settings| OCRE
    UTILCFG -->|provides settings| EXTR
    UTILCFG -->|provides settings| TRAIN

    UI -->|"upload invoice / invoke pipeline"| DP
    DP -->|"raw images"| PP
    PP -->|"cleaned images"| OCRE
    OCRE -->|"select engine"| GEM
    OCRE -->|"select engine"| TESS
    OCRE -->|"select engine"| EASY
    OCRE -->|"select engine"| PADD
    OCRE -->|"raw text"| EXTR
    EXTR -->|"structured data"| STORE
    STORE -->|"display results"| UI

    UI -->|"train model action"| TRAIN
    TRAIN -->|"evaluation metrics"| UI
    TRAIN -->|"update store"| STORE

    %% Logging
    DP -->|logs| UTILLG
    PP -->|logs| UTILLG
    OCRE -->|logs| UTILLG
    EXTR -->|logs| UTILLG
    TRAIN -->|logs| UTILLG

    %% Click Events
    click UI "https://github.com/ck-rajput/invoice_information_application/blob/main/app.py"
    click UTILCFG "https://github.com/ck-rajput/invoice_information_application/blob/main/src/utils/config_loader.py"
    click UTILLG "https://github.com/ck-rajput/invoice_information_application/blob/main/src/utils/logger.py"
    click DP "https://github.com/ck-rajput/invoice_information_application/blob/main/src/data_processing/download_datasets.py"
    click PP "https://github.com/ck-rajput/invoice_information_application/blob/main/src/data_processing/preprocess.py"
    click OCRE "https://github.com/ck-rajput/invoice_information_application/blob/main/src/extraction/ocr_engine.py"
    click EXTR "https://github.com/ck-rajput/invoice_information_application/blob/main/src/extraction/extract.py"
    click TRAIN "https://github.com/ck-rajput/invoice_information_application/blob/main/src/model/train.py"
    click STORE "https://github.com/ck-rajput/invoice_information_application/blob/main/invoice_results.json"
    click UTILCFG "https://github.com/ck-rajput/invoice_information_application/blob/main/config.yaml"
    click UI "https://github.com/ck-rajput/invoice_information_application/blob/main/requirements.txt"
    click UI "https://github.com/ck-rajput/invoice_information_application/blob/main/setup.py"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#333,stroke-width:1px;
    classDef processing fill:#d4edda,stroke:#333,stroke-width:1px;
    classDef external fill:#fff3cd,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5;
    classDef storage fill:#d6d8db,stroke:#333,stroke-width:1px,stroke-dasharray: 2 2;
    classDef util fill:#e2e3e5,stroke:#333,stroke-width:1px,stroke-dasharray: 3 3;
```
