A Django-based document processing system that performs OCR, document classification, and entity extraction using machine learning models and ChromaDB for vector storage.
- OCR Text Extraction - Extract text from images using Tesseract
- Document Classification - Classify documents into types (letter, invoice, form, etc.)
- Entity Extraction - Extract names, organizations, and locations from text
- Vector Storage - Store document embeddings in ChromaDB for similarity search
- REST API - Simple API for document processing
Create a .env file:
DJANGO_SECRET_KEY=your-secret-key
DEBUG=True
ALLOWED_HOSTS=*
CHROMA_DB_HOST=chromadb
CHROMA_DB_PORT=8000-
Install dependencies
pip install -r requirements.txt
-
Run migrations
python manage.py migrate
-
Start development server
python manage.py runserver 0.0.0.0:8000
Endpoint: POST /api/process-document/
Request: Upload an image file using form-data with key file
Response:
{
"document_id": "uuid-string",
"document_type": "letter",
"entities": {
"sender_organization": ["Company Name"],
"names": ["John Doe"],
"locations": ["New York, NY"]
}
}curl -X POST -F "file=@document.jpg" http://localhost:8000/api/process-document/doc_processor/
├── api/ # REST API endpoints
├── documents/ # Core processing logic
│ ├── ocr.py # OCR text extraction
│ ├── classifier.py # Document classification
│ ├── extractor.py # Entity extraction
│ └── chroma_client.py # Vector database client
├── docs-sm/ # Training data
├── tests/ # Test files
└── requirements.txt # Python dependencies
# Fast tests (lightweight only) - Recommended for development
python run_tests_fast.py
# Or using pytest directly
python -m pytest tests/test_*_lightweight.py -v --tb=short
# Full test suite (slower - includes integration tests)
python -m pytest tests/ --tb=short
# Install test dependencies first
pip install -r requirements.txt
pip install -r requirements.dev.txtThis project enforces strict typing with mypy:
# Run type checking
python run_type_check.py
# Or directly with mypy
python -m mypy api documents doc_processor
# Install type checking dependencies
pip install -r requirements.dev.txtCode linting and import sorting:
# Run all code quality checks
python run_lint.py
# Fix import sorting
python -m isort api documents doc_processor
# Run linting only
python -m flake8 api documents doc_processorSetup automatic code quality checks on commit:
# Install pre-commit hooks
python setup_precommit.py
# Or manually
pre-commit install
# Run hooks on all files
pre-commit run --all-files- Add training data to
docs-sm/new_type/ - Update
ENTITY_MAPPINGindocuments/extractor.py - Retrain the classifier model
The system follows a modular, pipeline-based architecture designed for scalability, maintainability, and extensibility. Each component has a single responsibility and can be independently tested and modified.
- REST API using Django REST Framework
- Swagger/OpenAPI documentation for interactive testing
- File upload handling with multipart form support
- Error handling with structured JSON responses
Image Upload → OCR → Classification → Entity Extraction → Vector Storage
-
OCR Module (
ocr.py): Tesseract + OpenCV for text extraction- Image preprocessing for better OCR accuracy
- Caching system to avoid reprocessing
- Error handling for invalid images
-
Classifier (
classifier.py): ML-based document type detection- TF-IDF vectorization + Logistic Regression
- Supports 15+ document types (invoice, letter, form, etc.)
- Auto-training from folder structure
-
Entity Extractor (
extractor.py): Multi-layered entity extraction- Regex patterns for document-specific entities
- HuggingFace NER (BERT-based) for general entities
- Domain mapping to convert generic entities to document-specific fields
- ChromaDB integration for semantic search
- Sentence embeddings for document similarity
- Metadata storage with extracted entities
- Scalable retrieval for large document collections
- Rapid development with built-in admin, ORM, and middleware
- REST API with automatic serialization and validation
- Extensible for future features (user auth, permissions, etc.)
- Regex: Fast, domain-specific patterns (names, dates, codes)
- BERT NER: General entity recognition with high accuracy
- Fallback system: Ensures entities are always extracted
- Vector similarity search for document retrieval
- Embedded database - no separate server required
- Automatic embeddings with sentence-transformers
- Metadata filtering combined with semantic search
- Single Responsibility: Each module has one clear purpose
- Testability: 86%+ test coverage with isolated unit tests
- Maintainability: Easy to modify individual components
- Extensibility: Add new document types or extraction methods easily
- Upload: Client sends image via REST API
- OCR: Extract text using Tesseract with preprocessing
- Classification: Predict document type using trained ML model
- Extraction: Extract entities using multi-layered approach
- Storage: Store document + entities in ChromaDB with embeddings
- Response: Return structured JSON with extracted information
- Stateless API: Easy horizontal scaling
- Caching: OCR results cached to avoid reprocessing
- Async-ready: Django structure supports async processing
- Database: ChromaDB handles large-scale vector operations
- Backend: Django, Django REST Framework
- OCR: Tesseract, OpenCV
- Vector DB: ChromaDB
- Documentation: Swagger/OpenAPI (drf-yasg)
MIT License