File size: 4,584 Bytes
1a7ee60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""

Quick Start Demo Mode for IDP System

Works without trained models using rule-based extraction

Allows immediate testing while models train

"""

import re
from typing import Dict, List
import numpy as np


class DemoClassifier:
    """Simple rule-based classifier for demo"""
    
    def predict(self, text: str) -> Dict:
        text_lower = text.lower()
        
        # Simple heuristics
        if 'invoice' in text_lower:
            return {
                'predicted_class': 'INVOICE',
                'confidence': 0.85,
                'probabilities': {'INVOICE': 0.85, 'RECEIPT': 0.10, 'FORM': 0.03, 'OTHER': 0.02}
            }
        elif any(k in text_lower for k in ['bank statement', 'account statement', 'statement of account']):
             return {
                'predicted_class': 'BANK_STATEMENT',
                'confidence': 0.90,
                'probabilities': {'INVOICE': 0.05, 'RECEIPT': 0.05, 'FORM': 0.05, 'BANK_STATEMENT': 0.85}
            }
        elif 'receipt' in text_lower:
            return {
                'predicted_class': 'RECEIPT',
                'confidence': 0.80,
                'probabilities': {'INVOICE': 0.15, 'RECEIPT': 0.80, 'FORM': 0.03, 'OTHER': 0.02}
            }
        elif 'form' in text_lower or 'application' in text_lower:
            return {
                'predicted_class': 'FORM',
                'confidence': 0.75,
                'probabilities': {'INVOICE': 0.05, 'RECEIPT': 0.10, 'FORM': 0.75, 'OTHER': 0.10}
            }
        else:
            # Fallback for things with prices/totals that aren't explicitly receipts
            if 'total' in text_lower and '$' in text_lower:
                 return {
                    'predicted_class': 'RECEIPT',
                    'confidence': 0.60,
                    'probabilities': {'INVOICE': 0.20, 'RECEIPT': 0.60, 'FORM': 0.10, 'OTHER': 0.10}
                }
            
            return {
                'predicted_class': 'OTHER',
                'confidence': 0.70,
                'probabilities': {'INVOICE': 0.10, 'RECEIPT': 0.10, 'FORM': 0.10, 'OTHER': 0.70}
            }


class DemoNER:
    """Simple rule-based NER for demo"""
    
    def __init__(self):
        self.patterns = {
            'INVOICE_NUMBER': [
                r'INV[-\s]?\d+',
                r'Invoice\s*#?\s*:?\s*([A-Z0-9\-]+)',
                r'\b\d{6,}\b',
            ],
            'DATE': [
                r'\d{1,2}[/-]\d{1,2}[/-]\d{2,4}',
                r'\d{4}[/-]\d{1,2}[/-]\d{1,2}',
            ],
            'TOTAL_AMOUNT': [
                r'Total\s*:?\s*[₹$€£]\s*[\d,]+\.?\d*',
                r'[₹$€£]\s*[\d,]+\.?\d*',
            ],
            'TAX_AMOUNT': [
                r'Tax\s*:?\s*[₹$€£]\s*[\d,]+\.?\d*',
                r'GST\s*:?\s*[₹$€£]\s*[\d,]+\.?\d*',
            ],
        }
    
    def predict(self, text: str) -> Dict:
        entities = []
        
        for entity_type, patterns in self.patterns.items():
            for pattern in patterns:
                matches = re.finditer(pattern, text, re.IGNORECASE)
                for match in matches:
                    entities.append({
                        'entity': entity_type,
                        'text': match.group(0).strip(),
                        'confidence': 0.75,
                        'start': match.start(),
                        'end': match.end()
                    })
        
        return {'entities': entities, 'text': text}


def create_demo_models_directory():
    """Create models directory with demo flag"""
    import os
    
    os.makedirs('models/classifier', exist_ok=True)
    os.makedirs('models/ner', exist_ok=True)
    
    # Create demo marker
    with open('models/DEMO_MODE.txt', 'w') as f:
        f.write('Using demo mode - rule-based extraction\n')
        f.write('Train models with: python train_classifier.py && python train_ner.py\n')
    
    print("✓ Demo mode initialized")
    print("  Using rule-based extraction until models are trained")


if __name__ == "__main__":
    create_demo_models_directory()
    
    # Test demo
    demo_text = """

    INVOICE #INV-12345

    Date: 28/11/2025

    Total: $1,250.00

    Tax: $125.00

    """
    
    classifier = DemoClassifier()
    ner = DemoNER()
    
    print("\nDemo Classifier:")
    print(classifier.predict(demo_text))
    
    print("\nDemo NER:")
    print(ner.predict(demo_text))