AI-powered document extraction system for Indian documents with support for multiple document types including Aadhaar, PAN, Voter ID, Driving License, Passport, and Invoices.
- Multi-Document Support: Aadhaar, PAN, Voter ID, Driving License, Passport, Invoice, eKYC
- Hybrid Extraction: Gemini AI + OCR fallback for maximum accuracy
- Async Processing: Concurrent document processing with worker pool
- Smart Caching: File hash-based caching to avoid reprocessing
- Type-Safe Schemas: Pydantic models with validation and review flags
- REST API: FastAPI-based API with async task management
- Web Interface: Simple frontend for document upload and results
┌─────────────┐
│ Frontend │
└──────┬──────┘
│
┌──────▼──────────────────────────────────────┐
│ FastAPI Server │
│ ┌────────────┐ ┌──────────────┐ │
│ │ Upload │ │ Task Queue │ │
│ │ Router │ │ │ │
│ └─────┬──────┘ └──────┬───────┘ │
│ │ │ │
│ ┌─────▼────────────────▼─────┐ │
│ │ Worker Pool │ │
│ │ (Concurrent Processing) │ │
│ └─────┬────────────────────────┘ │
│ │ │
│ ┌─────▼──────────┐ ┌──────────────┐ │
│ │ LLM Service │ │ OCR Service │ │
│ │ (Gemini/Groq) │ │ (Tesseract) │ │
│ └────────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │Cache Manager │ │ Normalizer │ │
│ └──────────────┘ └──────────────┘ │
└──────────────────────────────────────────────┘
- Python 3.11+
- Tesseract OCR
- Google Gemini API key or Groq API key
- Clone the repository
git clone <repository-url>
cd project-text-extraction- Install dependencies
pip install -r requirements.txt- Install Tesseract OCR
- Windows: Download from https://github.com/UB-Mannheim/tesseract/wiki
- Linux:
sudo apt-get install tesseract-ocr - Mac:
brew install tesseract
- Configure environment variables
cp .env.example .env
# Edit .env with your API keysRequired environment variables:
GEMINI_API_KEY=your_gemini_api_key
GROQ_API_KEY=your_groq_api_key (optional)
AI_PROVIDER=gemini
OCR_ENGINE=tesseract
python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000- Web Interface: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
POST /api/upload
Content-Type: multipart/form-data
Response:
{
"task_id": "abc123",
"status": "pending",
"message": "Task created successfully"
}GET /api/tasks/status/{task_id}
Response:
{
"task_id": "abc123",
"filename": "document.jpg",
"status": "completed",
"progress": 100.0
}GET /api/tasks/result/{task_id}
Response:
{
"task_id": "abc123",
"filename": "document.jpg",
"status": "completed",
"extracted_data": {
"document_type": "aadhaar",
"name": "John Doe",
"aadhaar_number_masked": "XXXX XXXX 1234",
...
}
}-
Aadhaar Card
- Name, DOB, Gender, Address
- Masked Aadhaar number
- Front/Back side detection
-
PAN Card
- Name, Father's name, DOB
- PAN number
-
Voter ID
- Name, Father's name, DOB, Gender
- Voter ID number, Address
- Polling station details
-
Driving License
- Name, DOB, Address
- License number, Issue/Expiry dates
- Vehicle classes
-
Passport
- Name, DOB, Gender, Nationality
- Passport number, Issue/Expiry dates
- MRZ parsing
-
Invoice
- Vendor/Buyer details
- Invoice number, date
- Line items, amounts
| Variable | Default | Description |
|---|---|---|
AI_PROVIDER |
gemini |
AI provider (gemini/groq) |
GEMINI_API_KEY |
- | Google Gemini API key |
GROQ_API_KEY |
- | Groq API key (optional) |
OCR_ENGINE |
tesseract |
OCR engine |
OCR_LANG |
eng+hin |
OCR languages |
UPLOAD_DIR |
uploads |
Upload directory |
OUTPUT_DIR |
outputs |
Output directory |
CACHE_DIR |
cache |
Cache directory |
Configure concurrent processing:
- Default: 8 workers
- Adjust based on CPU cores and memory
- File hash-based caching
- Automatic cache invalidation
- Persistent storage in JSON format
project/
├── main.py # FastAPI application
├── requirements.txt # Python dependencies
├── .env # Environment variables
├── models/ # Pydantic schemas
│ ├── schemas.py # Document schemas
│ └── task_schemas.py # Task schemas
├── routes/ # API routes
│ ├── upload.py # Upload endpoint
│ ├── tasks.py # Task management
│ └── batch.py # Batch processing
├── services/ # Business logic
│ ├── document_service.py # Main processing
│ ├── llm_service.py # AI extraction
│ ├── ocr_service.py # OCR processing
│ ├── cache_manager.py # Caching
│ ├── task_queue.py # Task management
│ └── worker_pool.py # Concurrent processing
├── frontend/ # Web interface
│ └── index.html # UI
├── tests/ # Test suite
└── logs/ # Application logs
python -m pytest tests/- Processing Time: 5-10 seconds per document
- Cache Hit Rate: >50% for repeated documents
- Concurrent Processing: Up to 8 documents simultaneously
- Accuracy: >90% for clear images
- Automatic Aadhaar number masking
- No storage of sensitive data in logs
- File hash-based deduplication
- Input validation and sanitization
MIT License
For issues and questions, please open an issue on the repository.