English · हिंदी · Hinglish — fine-tuned with QLoRA for ₹0 on free GPUs, built to run 100% offline on Android.
⚠️ Not financial advice. BankSathi can be wrong, and RBI rules change (facts verified 2026-11-14). Always confirm with your bank branch. In the app, a rule-based scam filter runs before the model and a deterministic fallback handles any reply that fails JSON parsing. Do not deploy the model without both.
Millions of first-time bank users in rural India deal with Bank Mitras (BC points), DBT credits, Kisan Credit Cards and a flood of OTP scams, often with patchy internet and in a mix of Hindi and English. BankSathi answers on the phone itself: ask in any language mix, even with speech-to-text typos, and it replies in clean English and Devanagari Hindi, ready for text-to-speech, in a strict JSON format the app can trust.
🗣️ Banking assistant (Hinglish, as spoken):
haan 5400 rs nikalna hai bankmitra se ho jayega kya
{
"replyEn": "Rs 5,400 is within the Rs 10,000 Bank Mitra limit, so the BC point is fine. Give your Aadhaar thumb scan, pay no extra fee, and collect the printed receipt.",
"replyHi": "₹5,400 बैंक मित्र की ₹10,000 सीमा के अंदर है, इसलिए बीसी पॉइंट ठीक है। आधार अंगूठा लगाएँ, कोई अतिरिक्त फीस न दें और छपी रसीद लें।",
"task": "WITHDRAW",
"suggestedAmountInr": 5400,
"isScamAlert": false,
"abstained": false
}
🛡️ ScamGuard (suspicious call or SMS):
caller bol raha hai Central Bank se hoon otp batao warna card band
{
"riskLevel": "HIGH_RISK",
"explanationEn": "The caller is asking for OTP, PIN or CVV. No bank or government officer ever asks for these.",
"explanationHi": "कॉल करने वाला ओटीपी, पिन या सीवीवी माँग रहा है। कोई बैंक या सरकारी अधिकारी ये कभी नहीं माँगता।",
"actionTipEn": "Do not reply, click or share OTP. Call 1930 now and report to your bank within 3 working days for zero liability.",
"actionTipHi": "जवाब न दें, लिंक न खोलें, ओटीपी न बताएँ। तुरंत 1930 पर कॉल करें और 3 कार्य दिवस में बैंक को बताएँ।"
}
Both examples are gold rows from the training set, shown to illustrate the output contract.
| 2.6 B parameters | 83 MB LoRA adapter | 1.67 GB on-device model | ₹0 compute bill |
| 1,500 dataset rows | 3 languages / registers | 70 min training on a free T4 | 8.0 GB peak VRAM |
| 100 % scam recall | 100 % safe abstention | 98.5 % intent accuracy | 0 false scam alarms |
| Path | What it is |
|---|---|
adapter_model.safetensors, adapter_config.json |
LoRA adapter (r=16, α=32, 7 projection modules, 83 MB) |
mediapipe/gemma2-2b-banksathi-cpu-int4.bin |
Merged model for Android: MediaPipe LLM Inference, CPU, int4 FFN / int8 attention (1.67 GB) |
gguf/banksathi-gemma2-2b-Q8_0-0000N-of-00006.gguf |
Merged model as Q8_0 GGUF in 6 shards (2.78 GB) for llama.cpp, Ollama, LM Studio and the browser demo |
training/ |
Full pipeline: dataset generator, 5-KPI eval gate, QLoRA trainer, converter, Colab notebooks |
data/ |
The 1,500-row synthetic dataset (1,274 train / 226 eval) and its audit |
reports/ |
Hold-out KPI report and training metrics |
On the phone, the model never runs alone:
Task A: banking assistant. Input is a user question, optionally with verified RBI context snippets.
{"replyEn": "...", "replyHi": "...", "task": "WITHDRAW|TRANSFER|PASSBOOK_DBT|LOAN_KCC|NONE",
"suggestedAmountInr": 5000, "isScamAlert": false, "abstained": false}
Task B: SMS scam check. Input is an SMS or call description.
{"riskLevel": "SAFE|CAUTION|HIGH_RISK", "explanationEn": "...", "explanationHi": "...",
"actionTipEn": "...", "actionTipHi": "..."}
Prompts must be built exactly as in training/banksathi_common.py (build_task_a_prompt,
build_task_b_prompt, then the Gemma-2 chat wrapper). Use greedy decoding.
Every row is synthetic and generated by code (seed 2026), so nothing private was ever collected and the exact
dataset can be rebuilt anywhere.
?, aadhar, pasbuk, rupees, fillers like
haan / accha, and stutters, just like Whisper-tiny output. The targets stay clean.[SOURCE_ID | Verified 2026-11-14]).
30% of those also carry a distractor snippet.| Gate | Result |
|---|---|
| Every target is valid JSON | ✅ 100% |
| Hindi replies are really Devanagari (≥ 85% of letters) | ✅ 100% |
| Replies ≤ 45 words, so text-to-speech stays short | ✅ 100% |
| Train / eval leakage (SHA-256 of normalised queries) | ✅ 0 |
| Duplicate queries | ✅ 0 |
| Withdraw share ≤ 25% (stops "everything is WITHDRAW") | ✅ 22% |
| No bucket > 16.5% | ✅ 16% |
| Setting | Value |
|---|---|
| Base | google/gemma-2-2b-it |
| Quantization | 4-bit NF4 + double quant (QLoRA) |
| LoRA | r = 16, α = 32, dropout 0.05, all 7 projection modules |
| Optimiser | lr 2e-4, cosine, 8% warmup, effective batch 16 (2 × 8 accumulation) |
| Length / epochs | 512 tokens · 4 epochs |
| Loss | completion-only (prompt tokens masked) |
| Hardware | Google Colab free T4 · 4,182 s · 7.99 GB peak VRAM |
| Final loss | train 0.073 · eval 0.0016 |
| KPI | Target | Result | |
|---|---|---|---|
| Valid JSON | ≥ 99% | 98.67% | ⚠️ |
| Intent accuracy | ≥ 96% | 98.52% | ✅ |
| ScamGuard recall | 100% | 100% | ✅ |
| Safe abstention | ≥ 95% | 100% | ✅ |
| Bilingual TTS (Devanagari parity) | ≥ 98% | 98.67% | ✅ |
| False scam alarms on safe SMS | — | 0% | ✅ |
| Over-abstention | — | 0% | ✅ |
9 of 10 buckets scored 100%. The only misses were 3 Hinglish KCC questions where the model wrote
"task":"LOAN" instead of "LOAN_KCC", so the fix is known (more Hinglish KCC templates, or a 5th epoch).
The on-device .bin has not yet been validated with the phone smoke tests.
Training was the easy part. Squeezing it onto a phone took four attempts:
🛠️ Every wall we hit and how we got past it| Wall | Fix |
|---|---|
| Colab gave a CPU runtime even with T4 selected | Disconnect and delete the runtime, then check nvidia-smi |
transformers 5.x removed warmup_ratio |
Compute warmup_steps and filter unknown TrainingArguments |
Colab's preinstalled torchao broke the PEFT merge |
Uninstall it before merging |
ai_edge_torch was renamed to litert_torch |
Import either name at runtime |
mediapipe 1.x dropped the .task bundler |
Pin mediapipe==0.10.35 |
litert-torch 0.10 needs kv_cache_max_len, export_config and mask_cache_size |
Detect signatures and pass them |
| Gemma gated on Hugging Face inside Kaggle | Use Kaggle Models (google/gemma-2) as the base, no token |
| 83 MB adapter vs a 10 MB browser upload limit | Split into 10 chunks, cat them back, verify SHA-256 |
| 31 GB still not enough for litert-torch | Switch to the MediaPipe GenAI converter (GEMMA2_2B, CPU, int4 FFN / int8 attention) |
Accept the Gemma licence first, then log in with your token.
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
from banksathi_common import build_task_a_prompt, wrap_prompt # from training/
tok = AutoTokenizer.from_pretrained("google/gemma-2-2b-it")
base = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b-it", attn_implementation="eager")
model = PeftModel.from_pretrained(base, "0xr4j/banksathi-gemma2-2b").merge_and_unload()
prompt = wrap_prompt(build_task_a_prompt("Bhaiya 5000 rupaye nikalne hain", []))
ids = tok(prompt, return_tensors="pt", add_special_tokens=False)
out = model.generate(**ids, max_new_tokens=200, do_sample=False)
print(tok.decode(out[0, ids["input_ids"].shape[1]:], skip_special_tokens=True))
📱 Android (MediaPipe LLM Inference, offline)mediapipe/gemma2-2b-banksathi-cpu-int4.bin (1.67 GB, no login needed).adb push gemma2-2b-banksathi-cpu-int4.bin /data/local/tmp/llm/gemma2-2b-it-cpu-int4.binLlmInference (CPU backend). The app must wrap prompts as
<start_of_turn>user\n{prompt}<end_of_turn>\n<start_of_turn>model\n.| Ask | Expect |
|---|---|
Bhaiya 5000 rupaye nikalne hain |
WITHDRAW · ₹5000 · Bank Mitra · no fee |
25000 rupees nikalne hain |
WITHDRAW · ₹25000 · Branch / ATM |
150000 KCC loan documents? |
LOAN_KCC · ₹2 L collateral-free · Khatauni + 2 copies + 3 photos |
Caller asked OTP & AnyDesk |
Rule engine fires first · 1930 · report in 3 days |
Exact balance in SBI acct 30491? |
abstained = true · task NONE · branch / 14448 |
| A PM-KISAN credit SMS | riskLevel SAFE |
Download all six gguf/ shards into one folder and point the tool at the first one; llama.cpp loads the rest
automatically.
hf download 0xr4j/banksathi-gemma2-2b --include "gguf/*" --local-dir banksathi
llama-cli -m banksathi/gguf/banksathi-gemma2-2b-Q8_0-00001-of-00006.gguf --temp 0 -n 256 -cnv
Send the same prompts as the Python example (build_task_a_prompt / build_task_b_prompt); the GGUF carries the
Gemma chat template, so chat-mode tools wrap them correctly.
cd training
pip install -q -r requirements.txt && huggingface-cli login
python3 dataset_generator.py --out-dir data --target-rows 1500
python3 eval_gemma_banksathi.py --train-file data/train.jsonl --eval-file data/eval.jsonl # must PASS
python3 train_gemma2b_qlora.py --model-id google/gemma-2-2b-it \
--train-file data/train.jsonl --eval-file data/eval.jsonl --epochs 4 --output-dir outputs/gemma2-2b-banksathi
python3 eval_gemma_banksathi.py --model-dir outputs/gemma2-2b-banksathi/merged_fp16 --eval-file data/eval.jsonl
pip install -r requirements-convert.txt
python3 convert_to_mediapipe.py --merged-dir outputs/gemma2-2b-banksathi/merged_fp16 \
--output-task outputs/gemma2-2b-banksathi-cpu-int4.task # writes the .bin next to it
LOAN → LOAN_KCC) and hit 5 / 5This is a Gemma derivative, provided under and subject to the Gemma Terms of Use. Use must comply with the Gemma Prohibited Use Policy.
gguf/banksathi-gemma2-2b-Q8_0-00001-of-00006.gguf 603 MiBgguf/banksathi-gemma2-2b-Q8_0-00002-of-00006.gguf 474 MiBgguf/banksathi-gemma2-2b-Q8_0-00003-of-00006.gguf 474 MiBgguf/banksathi-gemma2-2b-Q8_0-00004-of-00006.gguf 474 MiBgguf/banksathi-gemma2-2b-Q8_0-00005-of-00006.gguf 474 MiBgguf/banksathi-gemma2-2b-Q8_0-00006-of-00006.gguf 158 MiBmediapipe/gemma2-2b-banksathi-cpu-int4.bin 1.7 GiBadapter_model.safetensors 79 MiB