Views
No views yet
Intelligent document processing system that extracts structured information from invoices, forms, and scanned documents using fine-tuned DistilBERT and transfer learning.
1# Clone the repository
2git clone https://github.com/sanjanb/small-language-model.git
3cd small-language-model
4
5# Install dependencies
6pip install -r requirements.txt
7
8# Install Tesseract OCR (Windows)
9# Download from: https://github.com/UB-Mannheim/tesseract/wiki
10# Add to PATH or set TESSERACT_PATH environment variable
11
12# Install Tesseract OCR (Ubuntu/Debian)
13sudo apt install tesseract-ocr
14
15# Install Tesseract OCR (macOS)
16brew install tesseract1# Run the interactive demo
2python demo.py
3
4# Option 1: Complete demo with training and inference
5# Option 2: Train model only
6# Option 3: Test specific text1# Start the web API server
2python api/app.py
3
4# Open your browser to http://localhost:8000
5# Upload documents or enter text for extraction1graph TD
2 A[Document Input] --> B[OCR Processing]
3 B --> C[Text Cleaning]
4 C --> D[Tokenization]
5 D --> E[DistilBERT NER Model]
6 E --> F[Entity Extraction]
7 F --> G[Post-processing]
8 G --> H[Structured JSON Output]
9
10 I[Training Data] --> J[Auto-labeling]
11 J --> K[Model Training]
12 K --> Esmall-language-model/
├── src/ # Core source code
│ ├── data_preparation.py # OCR & dataset creation
│ ├── model.py # DistilBERT NER model
│ ├── training_pipeline.py # Training orchestration
│ └── inference.py # Document processing
├── api/ # Web API service
│ └── app.py # FastAPI application
├── config/ # Configuration files
│ └── settings.py # Project settings
├── data/ # Data directories
│ ├── raw/ # Input documents
│ └── processed/ # Processed datasets
├── models/ # Trained models
├── results/ # Training results
│ ├── plots/ # Training visualizations
│ └── metrics/ # Evaluation metrics
├── tests/ # Unit tests
├── demo.py # Interactive demo
├── requirements.txt # Dependencies
└── README.md # This file1from src.inference import DocumentInference
2
3# Load trained model
4inference = DocumentInference("models/document_ner_model")
5
6# Process a document
7result = inference.process_document("path/to/invoice.pdf")
8print(result['structured_data'])
9# Output: {'Name': 'John Doe', 'Date': '01/15/2025', 'Amount': '$1,500.00'}
10
11# Process text directly
12result = inference.process_text_directly(
13 "Invoice sent to Alice Smith on 03/20/2025 Amount: $2,300.50"
14)
15print(result['structured_data'])1# Upload and process a file
2curl -X POST "http://localhost:8000/extract-from-file" \
3 -H "accept: application/json" \
4 -H "Content-Type: multipart/form-data" \
5 -F "file=@invoice.pdf"
6
7# Process text directly
8curl -X POST "http://localhost:8000/extract-from-text" \
9 -H "Content-Type: application/json" \
10 -d '{"text": "Invoice INV-001 for John Doe $1000"}'
http://localhost:80001from src.model import ModelConfig
2
3config = ModelConfig(
4 model_name="distilbert-base-uncased",
5 max_length=512,
6 batch_size=16,
7 learning_rate=2e-5,
8 num_epochs=3,
9 entity_labels=['O', 'B-NAME', 'I-NAME', 'B-DATE', 'I-DATE', ...]
10)1# Optional: Custom Tesseract path
2export TESSERACT_PATH="/usr/bin/tesseract"
3
4# Optional: CUDA for GPU acceleration
5export CUDA_VISIBLE_DEVICES=01# Run all tests
2python -m pytest tests/
3
4# Run specific test module
5python tests/test_extraction.py
6
7# Test with coverage
8python -m pytest tests/ --cov=src --cov-report=html| Entity Type | Precision | Recall | F1-Score |
|---|---|---|---|
| NAME | 0.95 | 0.92 | 0.94 |
| DATE | 0.98 | 0.96 | 0.97 |
| AMOUNT | 0.93 | 0.91 | 0.92 |
| INVOICE_NO | 0.89 | 0.87 | 0.88 |
| 0.97 | 0.94 | 0.95 | |
| PHONE | 0.91 | 0.89 | 0.90 |
1# Place your documents in data/raw/
2mkdir -p data/raw
3cp your_invoices/*.pdf data/raw/1from src.training_pipeline import TrainingPipeline, create_custom_config
2
3# Create custom configuration
4config = create_custom_config()
5config.num_epochs = 5
6config.batch_size = 16
7
8# Run training
9pipeline = TrainingPipeline(config)
10model_path = pipeline.run_complete_pipeline("data/raw")results/plots/training_history.pngresults/metrics/evaluation_results.jsonmodels/document_ner_model/1FROM python:3.9-slim
2
3WORKDIR /app
4COPY requirements.txt .
5RUN pip install -r requirements.txt
6
7# Install Tesseract
8RUN apt-get update && apt-get install -y tesseract-ocr
9
10COPY . .
11EXPOSE 8000
12
13CMD ["python", "api/app.py"]git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)