Skip to content

bikram73/Document_Extract_AI

Repository files navigation

πŸ“„ DocExtract AI β€” Document Data Extraction Engine


Document Extract AI Demo


DocExtract AI is a full-stack web application designed to convert unstructured business documentsβ€”such as invoices, receipts, and purchase ordersβ€”into validated, structured JSON payloads. The system couples client-side parsing (digital PDF text extraction and optical character recognition) with cognitive language models to automate data extraction.


πŸ“‘ Table of Contents

πŸ“– Description
πŸš€ Section
View the project features and capabilities. πŸ‘‰
View the technologies, frameworks, and programming languages used. πŸ‘‰
Explore the project's folder and file organization. πŸ‘‰
Follow the installation steps and local development setup. πŸ‘‰
Understand the complete AI document processing pipeline. πŸ‘‰
Learn about the AI prompting strategy and anti-hallucination techniques. πŸ‘‰
Understand how confidence scores are calculated and interpreted. πŸ‘‰
View all deliverables required for the AI challenge. πŸ‘‰
See an example of the structured JSON output generated by the AI agent. πŸ‘‰
View the available REST API endpoints and usage examples. πŸ‘‰
View an overview of the sample documents and extraction results included in this project. πŸ‘‰
Open the dedicated Sample Results page to view all sample documents, images, PDFs, and extracted JSON outputs. πŸ‘‰
Review processing speed, latency, and performance benchmarks. πŸ‘‰
Understand the current limitations and known failure cases of the AI extractor. πŸ‘‰
View the project license information. πŸ‘‰

✨ Features

  • 🌐 Cross-Document Intelligence: Purpose-built parsers and validation templates for:
    • Invoices: Extracts line items, tax details, billing terms, date chronology, and vendor records.
    • Receipts: Tailored for business travel, retail, and expense management logs.
    • Purchase Orders: Maps ordering hierarchies, delivery deadlines, unit totals, and custom terms.
  • πŸ”„ Intelligent Multi-Provider AI Fallback Engine:
    • Automatically routes document payloads through a resilient cognitive tier.
    • Primary: Google Gemini (gemini)
    • Secondary / Fallback: OpenRouter (openrouter) (Default: google/gemini-2.5-flash)
    • Emergency Fallback: Groq Engine (groq) (Default: llama-3.2-11b-vision-preview)
    • Logs execution traces, failover logs, error statuses, and exact API latencies directly into an interactive Diagnostics Terminal.
  • πŸ’Ύ Export & Portability Suite:
    • Download JSON: Instantly export the fully parsed schema in clean formatted JSON for ERP ingestion.
    • Export CSV: Standardize metadata headers and detailed table rows into a structured CSV file for accounting and ledger management.
  • πŸ“ Multi-Format Upload Hub: Supports native drag-and-drop or manual selection for PDFs, PNGs, JPGs, JPEGs, and WEBP formats up to 10MB.
  • ⚑ Hybrid Visual OCR & Parsing Pipeline:
    • Automatically identifies whether documents contain selectable digital text layers or require localized visual OCR.
    • Returns complete confidence score arrays for crucial metadata boundaries.
  • πŸ“ Interactive Structured Workbench:
    • Real-Time Overlay Canvas: Highlights OCR bounding zones interactively as you hover over structured form inputs.
    • Interactive Spreadsheet Editor: Edit, append, or delete line-item entities with immediate recalculations.
    • Synchronized JSON Terminal: Copy, inspect, and export clean JSON schemas instantly.
  • πŸ›‘οΈ Clinical Validation Engine:
    • Date Validation: Ensures logical flow (e.g., Issue Date $\le$ Due Date).
    • Arithmetic Integrity Check: Performs math assertion loops ($Subtotal + Tax = Total$) and cross-references table records.
    • Currency Uniformity: Validates currency types across all itemization blocks.
    • Duplicate Prevention: Intelligently flags duplicate line entries.
  • πŸŒ“ Adaptive Global Theme Engine:
    • Context-Powered Toggle: Global theme state manager powered by a lightweight React Context Provider (ThemeContext).
    • Dynamic Theme Synchronization: Seamlessly toggles the root interface between high-contrast light and dark UI states.
    • Smart Persistence: Detects and defaults automatically to the user's OS system preferences, with manual selections securely persisted inside localStorage.
    • Fluid Tokens: Utilizes custom Tailwind design tokens to guarantee color consistency across all views and interactive widgets under both modes.
  • πŸ”” Interactive Toast Notification System:
    • Action Acknowledgements: Displays non-blocking floating notifications when downloading JSON configurations, exporting CSV ledgers, copying payloads, or updating ledger items.
    • Custom Animation Curves: Utilizes smooth micro-motion and responsive cubic-bezier scales/slides to slide toast bubbles gracefully on screen.
    • Multi-Type States: Supports adaptive success, info, and error styles designed to blend with Light and Dark aesthetics.
  • ✨ Premium View Entrance Transitions:
    • Dynamic Entrance: Applies a slide-and-scale entrance animation to active workspaces and dashboard panels during system mounting.
    • Visual Continuity: Smoothes out screen-routing transitions using custom physics curves ([0.16, 1, 0.3, 1]) to match native desktop interfaces.

πŸ› οΈ Tech Stack & Languages

  • Frontend Environment:
    • βš›οΈ React 18 (Functional hooks architecture)
    • ⚑ Vite (Next-generation lightning-fast frontend tooling)
    • 🎨 Tailwind CSS (Utility-first styling with customized fluid design tokens)
    • πŸŒ€ motion/react (Fluid micro-interactions and route-change layouts)
    • πŸ› οΈ Lucide React (Consistent, modern vector iconography)
  • Backend Environment:
    • 🟒 Node.js & Express
    • 🦾 TypeScript (Strict type definitions spanning client & server layers)
  • Cognitive Engines:
    • 🧠 Multi-Model Fallback System (Gemini, OpenRouter, Groq API interfaces)
    • πŸ‘οΈ Tesseract.js / pdf.js (Digital text extraction and visual OCR)

πŸ“‚ File Structure

Document_Extract_AI/
β”œβ”€β”€ .env.example              # Environment variables template
β”œβ”€β”€ .gitignore                # Production untracked files setup
β”œβ”€β”€ index.html                # Entry points for Vite SPA mounting
β”œβ”€β”€ package.json              # Scripts, modules, and dependencies manifests
β”œβ”€β”€ server.ts                 # Full-stack server (Express, API routers, Vite middleware)
β”œβ”€β”€ tsconfig.json             # TypeScript compiler rules
β”œβ”€β”€ vite.config.ts            # Client-side bundler configuration
β”œβ”€β”€ netlify.toml              # Netlify cloud configuration
β”‚
β”œβ”€β”€ server/                   # Backend services & orchestrators
β”‚   └── services/
β”‚       └── ai/
β”‚           β”œβ”€β”€ ProviderInterface.ts  # Interface standard for AI Services
β”‚           β”œβ”€β”€ GeminiService.ts     # Google Gemini API provider integration
β”‚           β”œβ”€β”€ OpenRouterService.ts # OpenRouter proxy service integration
β”‚           β”œβ”€β”€ GroqService.ts       # Groq high-speed vision model integration
β”‚           β”œβ”€β”€ FallbackManager.ts   # Failover scheduling & trace-logging engine
β”‚           β”œβ”€β”€ PromptBuilder.ts     # Standardized schema extraction instructions
β”‚           └── ResponseValidator.ts # Dynamic validation, sanitization, & parsing
β”‚
└── src/                      # Client React codebase
    β”œβ”€β”€ App.tsx               # Primary screen router, states, and app entry
    β”œβ”€β”€ index.css             # Tailwinds setup, base styles, and animations
    β”œβ”€β”€ main.tsx              # React mounting file
    β”œβ”€β”€ types.ts              # Global TypeScript models and structures
    β”‚
    β”œβ”€β”€ context/              # Global state contexts
    β”‚   β”œβ”€β”€ ThemeContext.tsx  # Dynamic Light/Dark Theme management context
    β”‚   └── ToastContext.tsx  # Premium Toast notification service context
    β”‚
    └── components/           # Modular visual views and cards
        β”œβ”€β”€ HomeView.tsx      # High-fidelity dashboard, landing and features page
        β”œβ”€β”€ UploadView.tsx    # File drag-and-drop and templates selector
        β”œβ”€β”€ AnalyzingView.tsx # Real-time progress visualizer and pipeline status logs
        β”œβ”€β”€ ResultsView.tsx   # Visual workbench, mock-document, fallback engine tabs, and spreadsheet
        └── AnalyticsView.tsx # Dynamic arithmetic audits, confidence score gauges, and alerts

πŸš€ Installation & Local Setup

πŸ“‹ Prerequisites

Ensure you have the following installed on your machine:

  • Node.js (v18 or higher recommended)
  • npm (Node package manager)

βš™οΈ Step-by-Step Setup

  1. Clone the Project:

    git clone https://github.com/bikram73/Document_Extract_AI
    cd Document_Extract_AI
  2. Install Dependencies:

    npm install
  3. Configure Environment Variables: Create a .env file in the root directory by copying the example template:

    cp .env.example .env

    Open the newly created .env file and input your secure API Credentials:

    GEMINI_API_KEY=your_gemini_api_key_here
    
    # Optional Fallbacks
    OPENROUTER_API_KEY=your_openrouter_key
    OPENROUTER_MODEL=google/gemini-2.5-flash
    GROQ_API_KEY=your_groq_key
    GROQ_MODEL=llama-3.2-11b-vision-preview
    AI_PROVIDER_PRIORITY=gemini,openrouter,groq
  4. Launch the Development Server:

    npm run dev

    The local server will boot. Open your browser and navigate to: πŸ‘‰ http://localhost:3000

  5. Build for Production: Compiles client-side bundles and compiles server TypeScript into an optimized, standalone CommonJS module inside dist/:

    npm run build
  6. Start Production Service:

    npm run start

πŸ—οΈ Architecture Pipeline

Below is a visual diagram of the document processing and extraction pipeline:

+------------------------------------------------------------+
|                        User Upload                         |
|         Supports PDF, PNG, JPG, JPEG, WEBP (< 10MB)        |
+------------------------------+-----------------------------+
                               |
                               v
+------------------------------------------------------------+
|                  Client-Side Pre-processing                |
|  - Digital PDF: Extracts raw text layers via pdf.js        |
|  - Images/Scans: Extracts localized text via Tesseract.js  |
+------------------------------+-----------------------------+
                               |
                               v
+------------------------------------------------------------+
|            Node.js / Express Backend Proxy                 |
|            - Endpoints exposed at /api/extract             |
|            - Handles environment-safe API credentials      |
+------------------------------+-----------------------------+
                               |
                               v
+------------------------------------------------------------+
|              Intelligent AI Provider Fallback              |
|        Attempts connection in priority sequence:            |
|        1. Gemini Service (Primary)                         |
|        2. OpenRouter Service (Secondary)                   |
|        3. Groq Vision Service (Tertiary / Emergency)       |
+------------------------------+-----------------------------+
                               |
                               v
+------------------------------------------------------------+
|               Post-Extraction Validation Layer             |
| - Strips formatting/markdown wrappers                      |
| - Verifies schema compliance (vendor, financials, totals)  |
| - Normalizes numerical records & line-item schemas         |
+------------------------------+-----------------------------+
                               |
                               v
+------------------------------------------------------------+
|                  Interactive UI Workbench                  |
| - Real-time editable spreadsheet with custom recalculations|
| - Integrity checks panel & copyable JSON output terminal   |
+------------------------------------------------------------+

🧠 AI Prompt Strategy & Anti-Hallucination Guardrails

To ensure high-fidelity extraction and mitigate hallucination tendencies of large language models, the prompting framework is engineered as follows:

  1. Strict Output Constraints (No Preambles): The model is instructed to output a raw JSON payload directly. By explicitly stating: "Return ONLY a valid JSON object. Do not include any markdown formatting, backticks, or wrapper blocks", we prevent parsing failures on the backend and bypass conversational preambles.
  2. Explicit Null Coercion: Instead of allowing the model to make logical guesses about missing metadata fields (such as vendorTaxId or dueDate), the prompt instructions state: "If a field is not present in the document, set its value to null. Do not invent, extrapolate, or estimate values."
  3. Closed-Loop Arithmetic Auditing: Rather than blindly transcribing numerical strings, the prompt instructs the model to double-check calculation logic (e.g., confirming whether subtotal + tax = total) and reflect the mathematical validity in an integrityCheck block.
  4. Post-Extraction Sanitizer: The backend implements a robust verification utility (ResponseValidator.ts) that runs immediately after the LLM output is received. It catches partial/broken JSON, enforces correct numeric conversions, handles array fallbacks for missing line items, and establishes sensible defaults to prevent interface exceptions.

πŸ“Š Confidence Scores Explanation

Each document extraction evaluates and assigns a set of distinct confidence ratings:

  • Overall Confidence: An aggregate assessment of the document's structure, completeness, and legibility as assessed by the language model contextually.
  • Vendor Confidence: Determined by analyzing the clarity and prominence of the vendor name, registered tax ID, and branding fields.
  • Date Confidence: Generated by analyzing standard ISO representations, matching syntax markers, and checking logical chronology.
  • Line Items Confidence: Evaluates the consistency of tables, itemizations, description boundaries, and math calculations across individual entries.
  • Currency Confidence: Evaluates the consistency of currency identifiers throughout the document.

πŸ“„ Challenge Deliverables

The core requirements outlined in the challenge guidelines are mapped to the following modules:

  1. Sample Documents Included: Pre-configured sample files for Invoices, Receipts, and Purchase Orders are integrated directly into the Upload Hub. Reviewers can test the extraction features immediately with one click.
  2. Structured JSON Output: A copyable, syntax-highlighted JSON code block is updated in real-time in the output terminal of the Workbench as edits are made.
  3. Validation Notes & Audits: The interactive workspace highlights integrity warnings regarding:
    • Chronology: Verifying that the due date occurs on or after the issue date.
    • Arithmetic: Ensuring line items sum precisely to the subtotal, and subtotal + tax sums precisely to the total.
    • Currency Uniformity: Flagging mismatched currency codes within the same document context.
  4. Transparent Codebase: Schema properties are isolated cleanly within TypeScript type definitions to ensure complete compliance.

πŸ–¨οΈ Sample JSON Payload

Below is an example of the validated extraction output schema generated by the system:

{
  "documentType": "Invoice",
  "vendorName": "Stripe Payments UK",
  "vendorTaxId": "GB 123 456 789",
  "invoiceNumber": "INV-2024-88412",
  "issueDate": "Oct 24, 2024",
  "dueDate": "Nov 23, 2024",
  "currency": "USD ($)",
  "paymentTerms": "Net 30",
  "financials": {
    "subtotal": 1995.00,
    "tax": 399.00,
    "total": 2394.00
  },
  "lineItems": [
    {
      "description": "AI Document Processing (Enterprise)",
      "qty": 1,
      "unitPrice": 1200.00,
      "amount": 1200.00
    },
    {
      "description": "Custom API Integration Support",
      "qty": 5,
      "unitPrice": 150.00,
      "amount": 750.00
    },
    {
      "description": "Monthly Cloud Storage Surcharge",
      "qty": 1,
      "unitPrice": 45.00,
      "amount": 45.00
    }
  ],
  "confidence": {
    "overall": 95,
    "vendor": 99,
    "date": 97,
    "lineItems": 84,
    "currency": 100
  },
  "insights": "This document appears to be a Service Invoice from Stripe Payments UK dated October 24, 2024. The billing cycle matches your previous subscriptions.",
  "integrityCheck": {
    "dateValidation": "PASSED",
    "arithmeticTotal": "PASSED",
    "currencyConsistency": "PASSED"
  },
  "alerts": [
    {
      "title": "Address Mismatch",
      "message": "The vendor address on invoice differs from the Master Data record. Recommended: Update CRM record."
    }
  ]
}

🌐 API Documentation

The Express server exposes the following API endpoints to handle document operations securely:

1. Document Extraction

  • Endpoint: POST /api/extract
  • Content-Type: application/json
  • Request Body:
    {
      "base64Data": "data:image/png;base64,iVBORw0KG...",
      "mimeType": "image/png",
      "fileName": "invoice_october.png"
    }
  • Success Response (200 OK):
    {
      "success": true,
      "data": { ... }, // ExtractedData structured JSON schema
      "providerUsed": "gemini",
      "providerReason": "Primary Provider",
      "providerLogs": [
        {
          "provider": "gemini",
          "event": "STARTED",
          "message": "Attempting document extraction using GEMINI...",
          "timestamp": "2026-07-08T05:31:15.000Z"
        },
        {
          "provider": "gemini",
          "event": "SUCCESS",
          "message": "Successfully extracted structured data in 1435ms.",
          "timestamp": "2026-07-08T05:31:16.435Z",
          "latencyMs": 1435
        }
      ]
    }
  • Error Response (500 Internal Server Error):
    {
      "success": false,
      "error": "Intelligent AI Provider Fallback Engine failed. All active providers failed to extract data."
    }

2. Health Check

  • Endpoint: GET /api/health
  • Success Response (200 OK):
    {
      "status": "ok",
      "timestamp": "2026-07-08T05:31:15.000Z"
    }

πŸ“„ Sample Documents & Extraction Results

To help reviewers quickly evaluate the AI Document Data Extractor, a complete set of sample documents and their corresponding structured JSON outputs are included in this repository.

Included Samples

  • πŸ“„ Invoice β†’ PDF + Extracted JSON
  • 🧾 Receipt β†’ Image + Extracted JSON
  • 🧾 Receipt 2 β†’ Image + Extracted JSON
  • πŸ“¦ Purchase Order β†’ Image + Extracted JSON

πŸ‘‰ View all sample documents and outputs here:

➑️ Sample Documents & Extraction Results ⬅️

This page includes:

  • Original input documents
  • Generated JSON outputs
  • Validation summary
  • OCR and AI extraction results
  • Confidence scores
  • AI provider information

⚑ Performance Notes

  • Processing Latency: Standard processing cycles average between 1.5 to 4 seconds depending on document length and network round-trip speeds.
  • Payload Max Limits: API routes enforce a maximum request limit of 50MB to support high-resolution scans, while client-side validations warn of files exceeding 10MB to optimize client performance.
  • OCR Processing Time: Tesseract.js client OCR processes document text in 1 to 2 seconds locally before transporting data to the server, ensuring quick frontend feedback.

⚠️ Known Limitations

  1. Extremely Low Resolution / Blurry Images: Hand-captured documents with high radial blur, glare, or extremely low lighting might degrade OCR accuracy, resulting in lower confidence scores.
  2. Complex Multi-Page Tables: Documents with tables spanning across multiple pages without consistent header records can occasionally experience row-alignment drift.
  3. Handwritten Fields: While the cognitive model is capable of reading legible cursive handwriting, highly stylized or messy handwriting can occasionally result in transcription gaps.
  4. Foreign Language Currencies: Standard processing maps USD, EUR, GBP, and major currency formats accurately. Highly exotic or regional currency symbols might fallback to default representation string formats.

πŸ“„ License

This project is licensed under the MIT License.

About

πŸ“„ DocExtract AI - An AI-powered document data extraction platform that converts invoices, receipts, and purchase orders into structured JSON using OCR and intelligent multi-model AI processing. It features automatic provider fallback, validation, confidence scoring, editable results, and export options for seamless business automation.

Topics

Resources

License

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages