Intelligent Contract Analysis Platform - Automatically extract, analyze, and score critical financial and legal terms from PDF contracts with advanced OCR, regex-based extraction, and optional LLM enhancement.
Contract Intelligence Parser is a production-ready document processing system designed for accounts receivable platforms and legal teams. The system automatically processes PDF contracts, extracting critical financial and operational data with confidence scoring and gap analysis to accelerate contract review workflows.
Key Value Propositions:
- Automated Extraction: Extract 40+ critical contract fields including parties, dates, commercial terms, and legal clauses
- Confidence Scoring: Advanced multi-signal scoring system with evidence-based validation
- Gap Detection: Intelligent identification of missing critical information with severity assessment
- OCR Support: Seamless processing of both digital and scanned contracts
- Scalable Architecture: Dockerized microservices supporting concurrent document processing
Main dashboard showing contract processing pipeline and analytics
contract_intelligence_parser/
β
βββ π backend/ # FastAPI backend service
β βββ app/ # Main application code
β β βββ api/ # API layer
β β β βββ endpoints/ # API route definitions
β β β β βββ contracts.py # Contract CRUD operations
β β β β βββ health.py # Health check endpoint
β β β β βββ __init__.py
β β β βββ middleware/ # Custom middleware (CORS, auth, etc.)
β β β βββ __init__.py
β β βββ core/ # Core configuration
β β β βββ config.py # Settings management
β β β βββ database.py # MongoDB connection setup
β β β βββ __init__.py
β β βββ models/ # Data models
β β β βββ contract.py # Contract document schema
β β β βββ __init__.py
β β βββ services/ # Business logic
β β β βββ background_processor.py # Async processing
β β β βββ contract_extractor.py # Field extraction logic
β β β βββ extraction_patterns.py # Regex patterns library
β β β βββ pdf_processor.py # PDF/OCR processing
β β β βββ __init__.py
β β βββ utils/ # Utility functions
β β βββ main.py # FastAPI application entry
β β βββ __init__.py
β βββ tests/ # Backend test suite
β β βββ test_api_endpoints.py # API route tests
β β βββ test_contract_extractor.py # Extraction logic tests
β β βββ test_extraction_patterns.py # Pattern validation
β β βββ __init__.py
β βββ uploads/ # Temporary file storage
β βββ Dockerfile # Backend container definition
β βββ requirements.txt # Python dependencies
β βββ pytest.ini # Test configuration
β
βββ π frontend/ # Next.js frontend application
β βββ src/ # Source code
β β βββ app/ # Next.js app directory
β β β βββ contracts/ # Contract pages
β β β β βββ [id]/ # Dynamic contract detail
β β β β β βββ page.tsx # Contract detail view
β β β β βββ page.tsx # Contract list view
β β β βββ layout.tsx # Root layout
β β β βββ page.tsx # Home/upload page
β β βββ components/ # React components
β β β βββ ui/ # Reusable UI components
β β β β βββ badge.tsx # Status/confidence badges
β β β β βββ button.tsx # Button component
β β β β βββ card.tsx # Card layouts
β β β β βββ progress.tsx # Progress indicators
β β β βββ file-upload.tsx # File upload component
β β β βββ providers.tsx # Context providers
β β βββ hooks/ # Custom React hooks
β β βββ lib/ # Utility libraries
β β β βββ api.ts # API client
β β β βββ utils.ts # Helper functions
β β βββ types/ # TypeScript definitions
β β βββ index.ts # Type exports
β βββ public/ # Static assets
β βββ Dockerfile # Frontend container definition
β βββ next.config.js # Next.js configuration
β βββ package.json # Node.js dependencies
β βββ postcss.config.js # PostCSS configuration
β βββ tailwind.config.js # Tailwind CSS configuration
β βββ tsconfig.json # TypeScript configuration
β
βββ π docs/ # Documentation assets
β βββ images/ # Screenshots and diagrams
β
βββ π³ Docker Configuration
β βββ docker-compose.yml # Main orchestration file
β βββ docker-compose.override.yml # Development overrides
β βββ .env.example # Environment template
β
βββ π Documentation
β βββ README.md # This file
β βββ TROUBLESHOOTING.md # Common issues & solutions
β
βββ π§ͺ Testing & Samples
βββ test_sample_contract.py # Integration test script
βββ sample_contract.pdf # Example PDF for testing
app/api/: REST API endpoints for contract operations (upload, retrieve, update, delete)app/core/: Core application settings, database connections, and configurationapp/models/: Pydantic models and MongoDB document schemasapp/services/: Business logic including PDF processing, OCR, field extraction, and confidence scoringapp/utils/: Shared utilities and helper functionstests/: Comprehensive test suite with unit and integration testsuploads/: Temporary storage for uploaded PDFs during processing
src/app/: Next.js 14 app router pages and layoutssrc/components/: Reusable React components and UI librarysrc/hooks/: Custom React hooks for state managementsrc/lib/: API client, utilities, and shared logicsrc/types/: TypeScript type definitions and interfaces
- Docker files: Container definitions and orchestration for all services
- Configuration: Environment variables, database settings, and service configs
- Documentation: Comprehensive guides for setup, deployment, and troubleshooting
- Multi-format Support: Digital PDFs and scanned documents via OCR (Tesseract)
- Intelligent Text Extraction: PyMuPDF with coordinate preservation and page mapping
- Robust Parsing: 40+ field extraction with sophisticated regex patterns
- Confidence Scoring: Multi-signal confidence calculation (0.0-1.0) with evidence tracking
- Weighted Scoring System: Financial completeness (30%), Party identification (25%), Payment terms (20%), SLAs (15%), Contacts (10%)
- Gap Analysis: Automatic identification of missing critical fields with severity levels
- Evidence Tracking: Page-level source attribution for all extracted values
- Processing Metrics: Performance monitoring with duration and success tracking
- Parties: Company names, roles, signatories, contact information
- Financial Terms: Contract values, currencies, billing frequencies, payment terms
- Dates: Effective dates, termination dates, renewal terms, notice periods
- Legal Clauses: Governing law, liability caps, indemnification, IP ownership
- Service Terms: SLAs, performance metrics, support requirements
- RESTful API: Comprehensive FastAPI backend with OpenAPI documentation
- Real-time Processing: Asynchronous processing with status tracking
- Data Persistence: MongoDB with structured document storage
- File Management: Secure upload handling with hash-based deduplication
- Error Recovery: Graceful degradation and retry mechanisms
- FastAPI 0.109.0 - High-performance Python web framework
- Pydantic 2.5.3 - Data validation and serialization
- Beanie/Motor - Async MongoDB ODM with Motor driver
- PyMuPDF 1.23.21 - PDF text extraction and analysis
- OpenCV + Tesseract - OCR processing for scanned documents
- Celery + Redis - Background task processing
- Next.js 14.1.0 - React framework with TypeScript
- Tailwind CSS - Utility-first styling framework
- shadcn/ui - Modern React component library
- React Query - Server state management and caching
- React Hook Form - Form handling and validation
- Lucide React - Icon library
- Docker + docker-compose - Containerized deployment
- MongoDB 4.0+ - Document database
- Redis - Caching and task queue
- Nginx (Production) - Reverse proxy and static serving
Contract analysis results with confidence indicators and gap detection
- Docker and Docker Compose
- Git
-
Clone the Repository first
-
Environment Setup
# Copy environment template cp .env.example .env # Edit environment variables as needed nano .env
-
Start the Application
# Build and start all services docker-compose up --build # Or run in detached mode docker-compose up -d --build
-
Verify Installation
# Check service status docker-compose ps # View logs docker-compose logs -f
- Frontend Application: http://localhost:3000
- API Documentation: http://localhost:8000/docs
- MongoDB: localhost:27017 (internal)
- Redis: localhost:6379 (internal)
Detailed contract view with extracted fields, confidence scores, and evidence
curl -X POST "http://localhost:8000/api/contracts" \
-H "Content-Type: multipart/form-data" \
-F "file=@sample_contract.pdf"Response:
{
"id": "66f2a1b8e4b0c1234567890a",
"filename": "sample_contract.pdf",
"status": "processing",
"message": "Contract uploaded successfully"
}curl "http://localhost:8000/api/contracts/66f2a1b8e4b0c1234567890a"curl "http://localhost:8000/api/contracts/66f2a1b8e4b0c1234567890a/fields"- Upload: Drag and drop PDF files or click to browse
- Monitor: Real-time processing status with progress indicators
- Review: Examine extracted fields with confidence indicators
- Validate: Review gaps and low-confidence extractions
- Edit: Manual corrections with evidence preservation
- Export: Download processed data in multiple formats
FastAPI automatic documentation with interactive endpoint testing
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Frontend β β Backend β β Database β
β (Next.js) βββββΊβ (FastAPI) βββββΊβ (MongoDB) β
β β β β β β
β β’ Upload UI β β β’ PDF Processing β β β’ Document β
β β’ Results View β β β’ Text Extract β β Storage β
β β’ Analytics β β β’ Field Parse β β β’ Metadata β
βββββββββββββββββββ β β’ Confidence β β β’ Search Index β
β Scoring β βββββββββββββββββββ
βββββββββββββββββββ β β’ Gap Detection β βββββββββββββββββββ
β Redis β β β’ API Endpoints β β File Storage β
β (Caching) βββββΊβ βββββΊβ (Local/S3) β
β β ββββββββββββββββββββ β β
β β’ Task Queue β ββββββββββββββββββββ β β’ PDF Files β
β β’ Session β β OCR Engine β β β’ Thumbnails β
β β’ Cache β β (Tesseract) β β β’ Temp Files β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
- Ingestion: PDF upload with validation and hash generation
- Text Extraction: PyMuPDF for digital text, OCR fallback for scanned docs
- Field Extraction: Regex-based pattern matching with 40+ field definitions
- Confidence Scoring: Multi-signal algorithm with evidence weighting
- Gap Detection: Missing field identification with severity assessment
- Storage: MongoDB persistence with full-text search indexing
- Response: Structured JSON with confidence metrics and evidence
-
Backend Development
cd backend python -m venv venv source venv/bin/activate #venv\Scripts\activate pip install -r requirements.txt uvicorn app.main:app --reload --port 8000
-
Frontend Development
cd frontend npm install npm run dev -
Database Setup
# Start MongoDB docker-compose up mongodb -d # Initialize database (optional) python backend/scripts/init_db.py
# Backend tests
cd backend
pytest --cov=app tests/
# Frontend tests
cd frontend
npm test
# Integration tests
python test_sample_contract.py# Python linting
flake8 backend/
black backend/
mypy backend/
# TypeScript checking
cd frontend
npm run lint
npm run type-checkFor some issues I faced; to read, refer to Troubleshooting Guide.
Container Issues:
# Reset Docker environment
docker-compose down -v
docker-compose up --buildDatabase Connection:
# Check MongoDB status
docker-compose logs mongodb
# Reset database
docker-compose down -v mongodb
docker-compose up mongodb -dBuilt with β€οΈ by Himangshu Shekhar