Skip to content

markremmey/ai-doc-processor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Document Processor - Enterprise Edition

AI Document Processor Version Azure FastAPI

🎯 Overview

The AI Document Processor is an enterprise-grade, cloud-native document processing platform that combines Azure's AI services with intelligent caching, analytics, and a modern web interface. It transforms documents into structured data using Azure Document Intelligence and OpenAI GPT models.

✨ Key Features

  • πŸš€ Live & Batch Processing - Real-time and queue-based document processing
  • 🧠 AI-Powered Analysis - Azure Document Intelligence + OpenAI GPT integration
  • πŸ“Š Enterprise Analytics - Comprehensive tracking, metrics, and cost analysis
  • πŸ”„ Smart Caching - Multi-tier caching system reducing costs by up to 80%
  • 🎨 Modern Web Interface - Advanced HTML5 interface with real-time monitoring
  • πŸ” Enterprise Security - Managed identity, IP restrictions, and secure authentication
  • πŸ“ˆ Dynamic Prompts - JSON-based prompt configuration system
  • πŸ—οΈ Azure Native - Fully integrated with Azure Container Apps and services

πŸ—οΈ Architecture

graph TB
    subgraph "Frontend Layer"
        UI[Modern Web Interface]
        API_CLIENT[API Client]
    end
    
    subgraph "API Layer"
        FASTAPI[FastAPI Server]
        ROUTES[API Routes]
        MIDDLEWARE[Security Middleware]
    end
    
    subgraph "Processing Layer"
        DOC_PROC[Document Processor]
        PROMPT_MGR[Prompt Manager]
        CACHE[Smart Cache]
        ANALYTICS[Analytics Manager]
    end
    
    subgraph "Azure Services"
        CONTAINER_APP[Container Apps]
        BLOB[Blob Storage]
        DI[Document Intelligence]
        OPENAI[Azure OpenAI]
        ACR[Container Registry]
    end
    
    subgraph "Storage Containers"
        BRONZE[Bronze - Raw Files]
        SILVER[Silver - Processed]
        GOLD[Gold - Archive]
        CACHE_STORE[Cache Storage]
        ANALYTICS_STORE[Analytics Data]
    end
    
    UI --> API_CLIENT
    API_CLIENT --> FASTAPI
    FASTAPI --> ROUTES
    ROUTES --> MIDDLEWARE
    MIDDLEWARE --> DOC_PROC
    DOC_PROC --> PROMPT_MGR
    DOC_PROC --> CACHE
    DOC_PROC --> ANALYTICS
    
    CONTAINER_APP --> FASTAPI
    DOC_PROC --> DI
    DOC_PROC --> OPENAI
    DOC_PROC --> BLOB
    
    BLOB --> BRONZE
    BLOB --> SILVER
    BLOB --> GOLD
    CACHE --> CACHE_STORE
    ANALYTICS --> ANALYTICS_STORE
Loading

πŸš€ Quick Start

Prerequisites

  • Azure Subscription with appropriate permissions
  • Azure CLI installed and configured
  • PowerShell 5.1+ (for deployment scripts)
  • Git for repository management

1. Clone Repository

git clone <your-repository-url>
cd ai-document-processor-personal-main

2. Configure Environment

# Copy environment template
Copy-Item env.example .env

# Edit .env with your Azure details
# Key variables:
# - RESOURCE_GROUP
# - AZURE_OPENAI_NAME
# - AZURE_DOCUMENT_INTELLIGENCE_NAME
# - AZURE_STORAGE_ACCOUNT

3. Deploy Infrastructure (Phase 1)

# Full infrastructure deployment - creates all Azure resources
.\infrastructure\provision-env-modular.ps1 -ResourceGroupName "your-rg-name"

# Or deploy step by step if needed
.\infrastructure\provision-env-modular.ps1 -ResourceGroupName "your-rg-name" -StartFromStep 1

4. Deploy Application (Phase 2)

# Build and deploy the actual application container
.\deploy-container-app-final.ps1 -ResourceGroupName "your-rg-name"

# Or start from specific step if needed
.\deploy-container-app-final.ps1 -ResourceGroupName "your-rg-name" -StartFromStep 3

5. Access Application

Your application will be available at:

https://app-<your-resource-name>.agreeablerock-<hash>.eastus2.azurecontainerapps.io

πŸ“‹ Features Deep Dive

πŸ”„ Processing Workflows

Live Processing

  • Real-time document processing
  • Immediate results via REST API
  • Job tracking with unique job IDs
  • Progress monitoring through web interface
# Example: Process document via API
curl -X POST "https://your-app-url/process-optimized" \
  -H "Content-Type: multipart/form-data" \
  -F "file=@document.pdf" \
  -F "prompt=Extract key information from this document"

Batch Processing

  • Queue-based processing for multiple documents
  • Background processing with status tracking
  • Bulk operations with progress monitoring
  • Result aggregation and reporting

🧠 AI Integration

Azure Document Intelligence

  • Multi-format support (PDF, DOC, DOCX, images)
  • Text extraction with layout preservation
  • Table and form recognition
  • Custom model support

Azure OpenAI Integration

  • GPT-4 and GPT-3.5-turbo support
  • Custom prompts with dynamic configuration
  • Token management and cost optimization
  • Structured output generation

πŸ“Š Enterprise Analytics

Real-time Metrics

  • Processing statistics (documents, tokens, costs)
  • Cache performance (hit rates, savings)
  • System health monitoring
  • Active job tracking

Historical Analytics

  • Cost analysis over time
  • Performance trends and optimization insights
  • Usage patterns and capacity planning
  • Compliance reporting

πŸ”„ Smart Caching System

Multi-tier Architecture

  • In-memory cache (fastest access)
  • Blob storage cache (persistent, low-cost)
  • LRU eviction policy
  • TTL-based expiration

Cost Optimization

  • Up to 80% reduction in API calls
  • Automatic cache warming
  • Intelligent cache invalidation
  • Performance metrics tracking

🎨 Modern Web Interface

Dashboard Features

  • Live Processing tab with drag-drop upload
  • Batch Processing queue management
  • Monitoring with real-time metrics
  • Settings and configuration management
  • API Integration testing tools

User Experience

  • Responsive design for all devices
  • Real-time updates via WebSocket-like polling
  • Progress indicators and status tracking
  • Error handling with user-friendly messages

πŸ”§ API Reference

Core Endpoints

Document Processing

POST /process-optimized
Content-Type: multipart/form-data

Parameters:
- file: Document file (PDF, DOC, DOCX, TXT, images)
- prompt: Processing instructions (optional)
- workflow: "live" or "batch" (default: "live")

Batch Operations

POST /batch/queue-document
GET /batch/queue-summary
GET /batch/job-status/{job_id}
DELETE /batch/cancel-job/{job_id}

Analytics

GET /analytics/real-time
GET /analytics/cache-performance  
GET /analytics/historical
GET /analytics/cost-analysis

System

GET /health
GET /cache/stats
GET /cache/clear

Response Format

{
  "success": true,
  "job_id": "uuid-string",
  "processing_time_ms": 1500,
  "extracted_text": "Document content...",
  "analysis": "AI-generated analysis...",
  "metadata": {
    "file_size": 1024000,
    "pages": 5,
    "tokens_used": 150,
    "cache_hit": false,
    "cost_estimate": 0.0023
  },
  "storage_info": {
    "bronze_container": "bronze/uuid/filename.pdf",
    "silver_container": "silver/uuid/analysis.json",
    "gold_container": "gold/uuid/filename.pdf"
  }
}

πŸš€ Two-Phase Deployment Architecture

The deployment process is split into two distinct phases for better modularity and troubleshooting:

Phase 1: Infrastructure Provisioning (provision-env-modular.ps1)

Purpose: Creates all Azure resources and configures the foundation Location: infrastructure/provision-env-modular.ps1

What it does:

  • βœ… Creates Resource Group
  • βœ… Deploys Storage Account with containers (bronze, silver, gold)
  • βœ… Creates Cognitive Services (OpenAI + Document Intelligence)
  • βœ… Deploys GPT-4.1-mini model
  • βœ… Configures custom subdomains for managed identity
  • βœ… Sets up Key Vault with secure API key storage
  • βœ… Creates Container Registry (ACR)
  • βœ… Creates Container App Environment with placeholder app

Phase 2: Application Deployment (deploy-container-app-final.ps1)

Purpose: Builds and deploys your actual application code Location: Root directory deploy-container-app-final.ps1

What it does:

  • βœ… Builds Docker image using ACR build service (no Docker Desktop needed)
  • βœ… Updates Container App with your application image
  • βœ… Configures managed identity authentication
  • βœ… Sets up role assignments for Azure services
  • βœ… Creates required blob storage containers
  • βœ… Verifies deployment health and provides URLs

Why Two Scripts?

Aspect Phase 1 (Infrastructure) Phase 2 (Application)
Frequency Once per environment Every code update
Duration 10-15 minutes 3-5 minutes
Purpose Foundation setup Code deployment
Failures Resource conflicts, permissions Build issues, config errors
Restart Modular (any step 1-8) Modular (any step 1-6)

πŸ—οΈ Infrastructure Components

Azure Resources Created (Phase 1)

Resource Purpose Configuration
Container App Environment Hosting platform Auto-scaling, HTTPS
Container App Application runtime 0-3 replicas, managed identity
Container Registry Image storage Basic tier, admin enabled
Storage Account Document & cache storage 5 containers, lifecycle policies
Azure OpenAI LLM processing GPT-4.1-mini, custom subdomain
Document Intelligence Text extraction Custom subdomain
Key Vault Secure secret storage API keys, access policies
Managed Identity Secure authentication Cognitive Services & Storage access

Storage Container Structure

Storage Account
β”œβ”€β”€ bronze/          # Raw uploaded documents
β”œβ”€β”€ silver/          # Processed results (JSON)
β”œβ”€β”€ gold/            # Final archived documents
β”œβ”€β”€ cache/           # Smart cache storage
└── analytics/       # Analytics and metrics data

πŸ” Security Features

Authentication & Authorization

  • Managed Identity for Azure service access
  • IP-based restrictions for enhanced security
  • API key authentication for external access
  • Azure AD integration ready

Data Protection

  • Secure file upload with validation
  • Encrypted storage in Azure Blob
  • SAS token based access control
  • No sensitive data in logs

Network Security

  • Container App with HTTPS only
  • Private networking options available
  • CORS configuration for web security
  • Input sanitization and validation

πŸš€ Deployment Guide

Detailed Deployment Guide

Phase 1: Infrastructure Provisioning (provision-env-modular.ps1)

Run once per environment - Sets up all Azure resources:

# Full infrastructure deployment
.\infrastructure\provision-env-modular.ps1 -ResourceGroupName "RG-DOC-Process-WB-P"

# Or step-by-step deployment
.\infrastructure\provision-env-modular.ps1 -ResourceGroupName "RG-DOC-Process-WB-P" -StartFromStep 1

Steps performed:

  1. Resource Group - Creates or validates resource group
  2. Storage Account - Creates storage with bronze/silver/gold containers
  3. Cognitive Services - Deploys OpenAI and Document Intelligence
  4. GPT Model - Deploys GPT-4.1-mini model
  5. Service Endpoints - Retrieves service endpoints securely
  6. Key Vault - Creates Key Vault with API keys (secure storage)
  7. Container Registry - Creates ACR for Docker images
  8. Container App - Creates Container App Environment with placeholder

Phase 2: Application Deployment (deploy-container-app-final.ps1)

Run for every code update - Builds and deploys your application:

# Full application deployment
.\deploy-container-app-final.ps1 -ResourceGroupName "RG-DOC-Process-WB-P"

# Skip Docker build if unchanged
.\deploy-container-app-final.ps1 -ResourceGroupName "RG-DOC-Process-WB-P" -SkipImageBuild

# Start from specific step
.\deploy-container-app-final.ps1 -ResourceGroupName "RG-DOC-Process-WB-P" -StartFromStep 3

Steps performed:

  1. Container Environment - Validates Container App Environment exists
  2. Docker Build - Builds image using ACR build service (no Docker Desktop)
  3. ACR Credentials - Retrieves registry credentials
  4. Container App - Updates app with new image and environment variables
  5. Storage Containers - Creates required blob containers (bronze, silver, gold, prompts)
  6. Health Check - Verifies deployment and provides URLs

πŸ”„ Deployment Workflows

First-Time Deployment

# Step 1: Infrastructure (run once)
.\infrastructure\provision-env-modular.ps1 -ResourceGroupName "RG-DOC-Process-WB-P"

# Step 2: Application (initial deployment)
.\deploy-container-app-final.ps1 -ResourceGroupName "RG-DOC-Process-WB-P"

Code Updates (Daily Development)

# Only run Phase 2 for code changes
.\deploy-container-app-final.ps1 -ResourceGroupName "RG-DOC-Process-WB-P"

# Skip Docker build if no code changes
.\deploy-container-app-final.ps1 -ResourceGroupName "RG-DOC-Process-WB-P" -SkipImageBuild

Troubleshooting Deployments

# Phase 1: Restart from specific infrastructure step
.\infrastructure\provision-env-modular.ps1 -ResourceGroupName "RG-DOC-Process-WB-P" -StartFromStep 5

# Phase 2: Restart from specific application step  
.\deploy-container-app-final.ps1 -ResourceGroupName "RG-DOC-Process-WB-P" -StartFromStep 4

When to Use Each Script

Scenario Script to Use Reason
New Environment Phase 1 β†’ Phase 2 Need all Azure resources
Code Changes Phase 2 only Infrastructure already exists
Config Changes Phase 2 only Environment variables update
Resource Issues Phase 1 (specific step) Fix infrastructure problems
App Not Starting Phase 2 (specific step) Fix deployment issues
New Features Phase 2 only Code deployment sufficient

Troubleshooting Deployment

Common Issues

  1. Resource Group Not Found

    # Create resource group first
    az group create --name "your-rg" --location "eastus2"
  2. Managed Identity Permissions

    # Check role assignments
    az role assignment list --assignee <managed-identity-id>
  3. Container App Not Starting

    # Check logs
    az containerapp logs show --name "your-app" --resource-group "your-rg"
  4. Custom Subdomain Issues

    # Update cognitive services
    az cognitiveservices account update --name "your-openai" --resource-group "your-rg" --custom-domain "your-openai"

πŸ“Š Monitoring & Operations

Health Monitoring

  • Health check endpoint (/health)
  • System metrics tracking
  • Performance monitoring built-in
  • Error logging with structured format

Analytics Dashboard

  • Real-time processing metrics
  • Cost tracking and optimization
  • Cache performance analysis
  • Historical trends and reporting

Maintenance

  • Automatic scaling based on demand
  • Cache cleanup and optimization
  • Log rotation and archival
  • Performance tuning recommendations

πŸ”§ Configuration

Environment Variables

# Azure Services
AZURE_OPENAI_ENDPOINT=https://your-openai.openai.azure.com/
AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT=https://your-di.cognitiveservices.azure.com/
AZURE_STORAGE_ACCOUNT=yourstorageaccount

# Application Settings
API_DEBUG=false
LOG_LEVEL=INFO
MAX_FILE_SIZE_MB=50
CACHE_TTL_SECONDS=3600

# Resource Names (for deployment)
RESOURCE_GROUP=ai-doc-proc-rg
AZURE_CONTAINER_APP=app-ai-doc-proc-202510
AZURE_CONTAINER_REGISTRY=acraidocproc202510

Prompt Configuration

The system uses a dynamic JSON-based prompt configuration system:

{
  "version": "1.0.0",
  "workflow_types": {
    "live": {
      "name": "Live Processing",
      "max_tokens": 2000
    },
    "batch": {
      "name": "Batch Processing",
      "max_tokens": 8000
    }
  },
  "document_prompts": {
    "invoice": {
      "live": {
        "minimal": "Extract key invoice data...",
        "general": "Extract invoice details...",
        "detailed": "Extract comprehensive invoice information..."
      }
    }
  }
}

πŸ§ͺ Testing

API Testing

# Health check
curl https://your-app-url/health

# Process document
curl -X POST "https://your-app-url/process-optimized" \
  -F "file=@test-document.pdf" \
  -F "prompt=Analyze this document"

# Get analytics
curl https://your-app-url/analytics/real-time

Web Interface Testing

  1. Navigate to your Container App URL
  2. Use the Live Processing tab for immediate testing
  3. Try Batch Processing for multiple documents
  4. Monitor results in the Monitoring tab

Load Testing

# Use the provided test scripts
python test_api_comprehensive.py
python test_batch_processing.py

πŸ“ˆ Performance & Scaling

Auto-scaling Configuration

  • Minimum replicas: 0 (cost-effective)
  • Maximum replicas: 3
  • Scale trigger: HTTP concurrency > 10
  • Scale down: Automatic when idle

Performance Optimization

  • Smart caching reduces API calls by 80%
  • Async processing for better throughput
  • Connection pooling for Azure services
  • Efficient memory management

Cost Optimization

  • Pay-per-use scaling (scales to zero)
  • Cache-first strategy for repeated requests
  • Token optimization for LLM calls
  • Storage lifecycle policies

πŸ”„ CI/CD Pipeline

GitHub Actions (Optional)

name: Deploy AI Document Processor
on:
  push:
    branches: [main]
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Deploy to Azure
        run: |
          az acr build --registry $ACR_NAME --image ai-document-processor:$GITHUB_SHA .
          az containerapp update --name $APP_NAME --image $ACR_NAME.azurecr.io/ai-document-processor:$GITHUB_SHA

Manual Deployment

# Standard deployment process
.\deploy-container-app-final.ps1 -ResourceGroupName "your-rg"

🀝 Contributing

Development Setup

  1. Fork the repository
  2. Create feature branch
  3. Install dependencies: pip install -r requirements.txt
  4. Run locally: python -m api.server
  5. Test changes thoroughly
  6. Submit pull request

Code Standards

  • Python 3.9+ compatibility
  • Type hints for all functions
  • Async/await for I/O operations
  • Comprehensive error handling
  • Unit tests for new features

πŸ“š Documentation

πŸ†˜ Support & Troubleshooting

Common Issues

1. Container App Not Accessible

  • Check if custom domain is configured
  • Verify managed identity permissions
  • Review container logs for errors

2. Processing Errors

  • Validate input file formats
  • Check Azure service quotas
  • Monitor token usage limits

3. Cache Issues

  • Clear cache via /cache/clear endpoint
  • Check blob storage permissions
  • Review cache configuration

Getting Help

  1. Check logs in Azure Portal
  2. Review troubleshooting section above
  3. Test with provided sample documents
  4. Create issue with detailed error information

Performance Monitoring

# Monitor system health
curl https://your-app-url/health

# Check cache performance
curl https://your-app-url/cache/stats

# View analytics
curl https://your-app-url/analytics/real-time

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ† Enterprise Features Summary

βœ… Production Ready - Full enterprise deployment with monitoring
βœ… Cost Optimized - Smart caching and auto-scaling
βœ… Secure - Managed identity and IP restrictions
βœ… Scalable - Container Apps with automatic scaling
βœ… Observable - Comprehensive analytics and monitoring
βœ… Maintainable - Modular architecture and documentation
βœ… User Friendly - Modern web interface with real-time updates
βœ… API First - Complete REST API with OpenAPI documentation


πŸš€ Ready for Enterprise Deployment!

This AI Document Processor represents a complete, production-ready solution for enterprise document processing with Azure's most advanced AI services.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published