File size: 4,303 Bytes
553240d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
language:
- en
- hi
- pa
tags:
- text-classification
- bert
- finance
- multilingual
- transaction-classification
license: mit
---

# BERT Transaction Classifier — SecureWealth Twin (M2)

Fine-tuned `bert-base-multilingual-cased` for automatic transaction categorisation across English, Hindi (Devanagari), and Punjabi (Gurmukhi).

Part of the **SecureWealth Twin** AI system — a bank-grade fraud detection and financial intelligence platform.

---

## Model Details

| | |
|---|---|
| Base model | `bert-base-multilingual-cased` |
| Task | 7-class text classification |
| Languages | English · Hindi · Punjabi |
| Max sequence length | 64 |
| Training epochs | 5 |
| Learning rate | 2e-5 |
| Batch size | 32 |
| Train/val/test split | 70 / 15 / 15 (stratified) |

---

## Categories

| Label | ID |
|-------|----|
| Food | 0 |
| Transport | 1 |
| EMIs | 2 |
| Entertainment | 3 |
| Utilities | 4 |
| Investments | 5 |
| Other | 6 |

---

## Architecture

```
bert-base-multilingual-cased
  → [CLS] token (768-d)
  → Dropout(0.3)
  → Linear(768 → 7)
```

---

## Usage

### Load and run inference

```python
import torch
import torch.nn as nn
from transformers import BertTokenizer, BertModel
from huggingface_hub import hf_hub_download

# Model class (must match training definition)
class BERTTxnClassifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.bert       = BertModel.from_pretrained("bert-base-multilingual-cased")
        self.drop       = nn.Dropout(0.3)
        self.classifier = nn.Linear(768, 7)

    def forward(self, input_ids, attention_mask):
        cls = self.bert(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state[:, 0, :]
        return self.classifier(self.drop(cls))

CATEGORIES = ["Food", "Transport", "EMIs", "Entertainment", "Utilities", "Investments", "Other"]

# Download model + tokenizer
model_path = hf_hub_download(repo_id="NanG01/bert-txn-classifier", filename="bert_classifier.pt")

device    = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer = BertTokenizer.from_pretrained("NanG01/bert-txn-classifier")
model     = BERTTxnClassifier()
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device).eval()

# Inference
def predict(text: str) -> dict:
    enc = tokenizer(text, max_length=64, padding="max_length",
                    truncation=True, return_tensors="pt")
    with torch.no_grad():
        probs = torch.softmax(
            model(enc["input_ids"].to(device), enc["attention_mask"].to(device)), dim=-1
        ).squeeze(0)
    pred = probs.argmax().item()
    return {"category": CATEGORIES[pred], "confidence": round(probs[pred].item(), 4)}
```

### Examples

```python
predict("SWIGGY ORDER PAYMENT")
# → {"category": "Food", "confidence": 0.9821}

predict("HDFC BANK PERSONAL LOAN EMI")
# → {"category": "EMIs", "confidence": 0.9743}

predict("OLA RIDE PAYMENT")
# → {"category": "Transport", "confidence": 0.9512}

predict("ZERODHA MUTUAL FUND")
# → {"category": "Investments", "confidence": 0.9301}

predict("बिजली बिल भुगतान")
# → {"category": "Utilities", "confidence": 0.9104}

predict("ਖਾਣੇ ਦਾ ਭੁਗਤਾਨ")
# → {"category": "Food", "confidence": 0.8932}
```

---

## Files

| File | Description |
|------|-------------|
| `bert_classifier.pt` | Full model state dict |
| `tokenizer/tokenizer.json` | Tokenizer vocab + merges |
| `tokenizer/tokenizer_config.json` | Tokenizer config |

---

## Training Data

~1,300 transaction descriptions across 3 languages:
- ~500 English transactions
- ~400 Hindi transactions
- ~400 Punjabi transactions

Dataset: `SecureWealthTwin_DL_Datasets_v2.xlsx` (private)

---

## Part of SecureWealth Twin

This model is M2 in a 6-model AI system:

| # | Model | Task |
|---|-------|------|
| M1 | BehaviorDNA | Behavioural anomaly detection |
| **M2** | **BERT Txn Classifier** | **Transaction categorisation** |
| M3 | NLP Coercion Detector | Coercion language detection |
| M4 | Coercion Risk Scorer | Composite risk scoring |
| M5 | Monte Carlo Simulator | Wealth projection |
| M6 | Predictive Early Warning | Financial distress prediction |

**GitHub:** [BlackBox-Wealth/AI_Models_2](https://github.com/BlackBox-Wealth/AI_Models_2)