# nanonets/docaiagent architecture

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

- Interactive diagram: https://gitdiagram.com/nanonets/docaiagent
- Repository: https://github.com/nanonets/docaiagent

## Overview

##### Project Type and Purpose
- This is a Python-based, modular Document AI Agent featuring a Gradio web UI for end-users, a core “agent” orchestration layer, and several specialized processing modules. Its goal is to ingest scanned or photographed documents, perform OCR, classify document types, extract structured fields via LLMs, review the results, and present/download final JSON data.

##### File Structure
- Top-level scripts:
- `gradio_app.py` – the front-end server and orchestrator for user interactions
- `document_ai_agent.py` – an autonomous agent built atop the qwen-agent framework that drives extraction and review tools
- `ocr_module.py`, `classifier.py`, `extractor.py`, `reviewer.py` – four core processing stages
- `llm_module.py` – unified interface to call external LLM endpoints
- `config.py` & `.env.example` – LLM endpoint, API key configurations
- `utils/` – shared helper functions for each module
- Support files: `requirements.txt`, Jupyter walkthrough notebook, environment template

##### Extract Insights from README
- Workflow stages: Upload → OCR → Classification → Field Confirmation → Extraction → (Optional) Review → Display & Download
- Multiple OCR backends (Tesseract, Google Vision, Surya)
- Multiple LLM backends (Qwen2.5, Qwen2.5-VL, Qwen3-8B, etc.), configurable in `config.py` and `.env`
- Agentic orchestration vs. explicit function calls

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface"
        UI["Client / Browser"]:::external
        GR["Gradio Web Server"]:::ui
    end

    %% Processing Service Layer
    subgraph "Processing Service"
        AGENT["Agent Orchestrator"]:::core
        OCR["OCR Engine(s)"]:::core
        CLASS["Classifier Module"]:::core
        EXTR["Extractor Module"]:::core
        REV["Reviewer Module"]:::core
        LLM["LLM Interface"]:::external
    end

    %% Supporting Components
    CONFIG["Configuration Store"]:::config
    DATA["Data Store (JSON)"]:::datastore
    DOCS["Jupyter Notebook"]:::docs
    REQ["Dependencies / requirements.txt"]:::config

    %% Data Flow
    UI -->|"Upload Image"| GR
    GR -->|"Start Orchestration"| AGENT
    AGENT -->|"Invoke OCR"| OCR
    OCR -->|"Text + BBoxes"| CLASS
    CLASS -->|"Display Field Suggestions"| GR
    GR -->|"User Confirms Fields"| EXTR
    EXTR -->|"Extracted Key-Value Pairs"| REV
    REV -->|"Review Results"| GR
    GR -->|"Download JSON"| DATA

    %% Module to LLM calls
    OCR -->|"calls LLM"| LLM
    CLASS -->|"calls LLM"| LLM
    EXTR -->|"calls LLM"| LLM
    REV -->|"calls LLM"| LLM

    %% Configuration Dependencies
    CONFIG --> LLM
    CONFIG --> OCR
    CONFIG --> CLASS
    CONFIG --> EXTR
    CONFIG --> REV

    %% Documentation and Dependencies
    DOCS
    REQ

    %% Click Events
    click GR "https://github.com/nanonets/docaiagent/blob/main/gradio_app.py"
    click AGENT "https://github.com/nanonets/docaiagent/blob/main/document_ai_agent.py"
    click OCR "https://github.com/nanonets/docaiagent/blob/main/ocr_module.py"
    click OCR "https://github.com/nanonets/docaiagent/blob/main/utils/ocr_engines.py"
    click CLASS "https://github.com/nanonets/docaiagent/blob/main/classifier.py"
    click CLASS "https://github.com/nanonets/docaiagent/blob/main/utils/classifier_utils.py"
    click EXTR "https://github.com/nanonets/docaiagent/blob/main/extractor.py"
    click EXTR "https://github.com/nanonets/docaiagent/blob/main/utils/extractor_utils.py"
    click REV "https://github.com/nanonets/docaiagent/blob/main/reviewer.py"
    click REV "https://github.com/nanonets/docaiagent/blob/main/utils/reviewer_utils.py"
    click LLM "https://github.com/nanonets/docaiagent/blob/main/llm_module.py"
    click CONFIG "https://github.com/nanonets/docaiagent/blob/main/config.py"
    click CONFIG "https://github.com/nanonets/docaiagent/blob/main/.env.example"
    click DATA "https://github.com/nanonets/docaiagent/tree/main/_temp_doc_ai_files/"
    click DOCS "https://github.com/nanonets/docaiagent/blob/main/Workshop_Walkthrough_Componenets.ipynb"
    click REQ "https://github.com/nanonets/docaiagent/blob/main/requirements.txt"

    %% Styles
    classDef ui fill:#C3D9FF,stroke:#0275d8,color:#000
    classDef core fill:#D4EFDF,stroke:#27AE60,color:#000
    classDef external fill:#FDEBD0,stroke:#D35400,color:#000
    classDef config fill:#E8E8E8,stroke:#888,color:#000
    classDef datastore fill:#FCF3CF,stroke:#B7950B,color:#000
    classDef docs fill:#F5EEF8,stroke:#8E44AD,color:#000
```
