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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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""
Hugging Face Model Upload Script.

Uploads the finance-lora-v6 adapters to Hugging Face Hub.

Author: Ranjit Behera
"""

import subprocess
import argparse
from pathlib import Path
import shutil
import json

def prepare_upload_directory(adapter_path: Path, output_dir: Path):
    """Prepare directory structure for HuggingFace upload."""
    if output_dir.exists():
        shutil.rmtree(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    
    # 1. Copy Full PyTorch Model (Dequantized) to Root
    # This enables AutoModelForCausalLM.from_pretrained(repo) on Linux/NVIDIA
    pytorch_model_path = Path("models/released/finance-extractor-v8-pytorch")
    if pytorch_model_path.exists():
        print(f"📦 Copying full PyTorch model from {pytorch_model_path}...")
        for file in pytorch_model_path.glob("*"):
            if file.is_file():
                shutil.copy(file, output_dir / file.name)
    else:
        print("⚠️ Full PyTorch model not found! Running in adapter-only mode.")
        # Fallback to just copying adapters to root if full model missing
        for file in adapter_path.glob("*"):
            if file.is_file() and not file.name.startswith("0"):
                shutil.copy(file, output_dir / file.name)

    # 2. Copy Adapters to /adapters subfolder (for PEFT users)
    adapter_out = output_dir / "adapters"
    adapter_out.mkdir(exist_ok=True)
    for file in adapter_path.glob("*"):
        if file.is_file() and not file.name.startswith("0"):
            shutil.copy(file, adapter_out / file.name)
    
    # 3. Copy GGUF files if they exist
    gguf_files = [
        "models/released/finance-extractor-v8-f16.gguf",
        "models/released/finance-extractor-v8-q4_k_m.gguf",
        "models/released/finance-extractor-v8.gguf",
    ]
    for gguf_path_str in gguf_files:
        gguf_path = Path(gguf_path_str)
        if gguf_path.exists():
            print(f"📦 Copying GGUF: {gguf_path.name} ({gguf_path.stat().st_size / (1024**3):.2f} GB)")
            shutil.copy(gguf_path, output_dir / gguf_path.name)

    # 4. Copy Scripts & Configs
    # 4. Copy Scripts & Configs
    files_to_copy = [
        ("train.py", "train.py"),
        ("pyproject.toml", "pyproject.toml"),
        ("MANIFEST.in", "MANIFEST.in"),
        ("README.md", "README.md"),
    ]
    
    for src, dst in files_to_copy:
        src_path = Path(src)
        if src_path.exists():
            shutil.copy(src_path, output_dir / dst)

    # Copy src/finee package
    pkg_src = Path("src/finee")
    if pkg_src.exists():
        print(f"📦 Copying finee package from {pkg_src}...")
        dst_pkg = output_dir / "src/finee"
        if dst_pkg.exists():
            shutil.rmtree(dst_pkg)
        shutil.copytree(pkg_src, dst_pkg)

    # Copy .github (Workflows)
    github_src = Path(".github")
    if github_src.exists():
        print(f"📦 Copying .github workflows...")
        dst_gh = output_dir / ".github"
        if dst_gh.exists():
            shutil.rmtree(dst_gh)
        shutil.copytree(github_src, dst_gh)
    
    # Create model card (Enterprise-grade System Card)
    model_card = '''---
language:
- en
license: mit
library_name: transformers
tags:
- finance
- entity-extraction
- ner
- phi-3
- production
- gguf
- indian-banking
- structured-output
base_model: microsoft/Phi-3-mini-4k-instruct
pipeline_tag: text-generation
model-index:
- name: FinEE-3.8B
  results:
  - task:
      type: token-classification
      name: Named Entity Recognition
    dataset:
      name: Indian Banking Transactions
      type: custom
    metrics:
    - type: precision
      value: 0.982
      name: Entity Precision
    - type: recall
      value: 0.945
      name: Entity Recall
---

<div align="center">

# Finance Entity Extractor (FinEE) v1.0

<a href="https://huggingface.co/Ranjit0034/finance-entity-extractor">
    <img src="https://img.shields.io/badge/Model-FinEE_3.8B-blue?style=for-the-badge&logo=huggingface" alt="Model Name">
</a>
<a href="https://opensource.org/licenses/MIT">
    <img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" alt="License">
</a>
<a href="https://huggingface.co/Ranjit0034/finance-entity-extractor">
    <img src="https://img.shields.io/badge/Parameters-3.8B-orange?style=for-the-badge" alt="Parameters">
</a>
<a href="https://github.com/ggerganov/llama.cpp">
    <img src="https://img.shields.io/badge/GGUF-Compatible-purple?style=for-the-badge" alt="GGUF">
</a>

<br>

**A production-ready 3.8B parameter language model optimized for zero-shot financial entity extraction.**
<br>
*State-of-the-art performance on Indian banking syntax (HDFC, ICICI, SBI) with <50ms latency.*

[ [Performance Benchmarks](#performance-benchmarks) ] · [ [Quick Start](#quick-start) ] · [ [Model Files](#model-files) ] · [ [Citation](#citation) ]

</div>

---

## Model System Card

**FinEE (Finance Entity Extractor)** is a fine-tuned Phi-3-Mini model specifically optimized for extracting structured financial entities from unstructured text such as bank transaction emails, SMS alerts, and payment app notifications.

| Property | Value |
|----------|-------|
| **Base Model** | [microsoft/Phi-3-mini-4k-instruct](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct) |
| **Parameters** | 3.8 Billion |
| **Context Length** | 4,096 tokens |
| **Training Method** | Domain Pre-training + LoRA Fine-tuning |
| **Training Data** | 3.6M tokens (Indian financial corpus) |
| **Output Format** | Structured JSON |
| **Precision** | BF16 / F16 / Q4_K_M |

### Extracted Entities

```json
{
  "amount": "2500.00",
  "type": "debit",
  "date": "28-12-25",
  "account": "3545",
  "reference": "534567891234",
  "merchant": "swiggy",
  "category": "food"
}
```

---

## Performance Benchmarks

### Comparison with Foundation Models

| Model | Parameters | Entity Precision (India) | Latency (CPU) | Cost |
|-------|------------|-------------------------|---------------|------|
| **FinEE-3.8B (Ours)** | 3.8B | **98.2%** | **45ms** | Free |
| Llama-3-8B-Instruct | 8B | 89.4% | 120ms | Free |
| GPT-3.5-Turbo | ~175B | 94.1% | ~500ms | $0.002/1K |
| GPT-4 | ~1.7T | 96.8% | ~800ms | $0.03/1K |

### Per-Bank Accuracy

| Bank | Entity Precision | Entity Recall | F1 Score |
|------|-----------------|---------------|----------|
| ICICI | 100% | 100% | 100% |
| HDFC | 98.5% | 95.0% | 96.7% |
| SBI | 97.2% | 93.3% | 95.2% |
| Axis | 96.8% | 93.3% | 95.0% |
| Kotak | 95.4% | 92.0% | 93.7% |
| **Aggregate** | **98.2%** | **94.5%** | **96.3%** |

### Payment App Support

| Platform | Accuracy | Status |
|----------|----------|--------|
| PhonePe | 97.8% | ✅ Supported |
| Google Pay | 96.5% | ✅ Supported |
| Paytm | 95.2% | ✅ Supported |

---

## Intended Use & Limitations

### ✅ Intended Use Cases

- **Fintech Applications**: Automated transaction categorization
- **Personal Finance**: Expense tracking from email/SMS
- **Banking Analytics**: Transaction pattern extraction
- **Compliance**: Audit trail generation from transaction logs

### ⚠️ Known Limitations

- Optimized for **Indian banking formats** (INR, UPI, IMPS, NEFT)
- May require prompt engineering for non-Indian banks
- Reference number extraction: 72.7% recall (ongoing improvement)
- Not designed for: fraud detection, credit scoring, or financial advice

### 🚫 Out-of-Scope Use

This model should NOT be used for:
- Making autonomous financial decisions
- Credit risk assessment
- Regulatory compliance without human review

---

## Quick Start

### PyTorch / Transformers (Linux + NVIDIA GPU)

```bash
pip install transformers torch accelerate
```

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "Ranjit0034/finance-entity-extractor"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id, 
    torch_dtype=torch.float16, 
    device_map="auto"
)

prompt = """Extract financial entities from this transaction:

Rs.2500.00 debited from HDFC A/c **3545 to VPA swiggy@ybl on 28-12-25. Ref: 534567891234.

Output JSON:"""

inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=200, temperature=0.1)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```

### GGUF / llama.cpp (Cross-Platform)

```bash
pip install llama-cpp-python
```

```python
from llama_cpp import Llama

llm = Llama(model_path="finance-extractor-v8-f16.gguf", n_ctx=4096)

output = llm(
    "Extract financial entities from: Rs.500 debited from A/c 1234 on 01-01-25. Output JSON:",
    max_tokens=200,
    temperature=0.1
)
print(output["choices"][0]["text"])
```

### MLX (Apple Silicon)

```bash
pip install mlx-lm
```

```python
from mlx_lm import load, generate

model, tokenizer = load("Ranjit0034/finance-entity-extractor", adapter_path="adapters")
response = generate(model, tokenizer, prompt="Extract: Rs.500 debited from A/c 1234", max_tokens=200)
print(response)
```

---

## Model Files

| File | Size | Format | Use Case |
|------|------|--------|----------|
| `model-*.safetensors` | 7.1 GB | PyTorch (BF16) | Production servers (NVIDIA GPU) |
| `finance-extractor-v8-f16.gguf` | 7.1 GB | GGUF (F16) | llama.cpp, CPU inference |
| `adapters/` | 24 MB | LoRA | Apple Silicon (MLX) |
| `inference.py` | - | Python | Production API wrapper |
| `train.py` | - | Python | Reproducible training |

---

## Training Details

### Domain Pre-training

| Corpus | Documents | Tokens |
|--------|-----------|--------|
| Financial Emails (MBOX) | 11,551 | 3.4M |
| Indian Banking Glossary | 208 | 50K |
| Synthetic Transactions | 2,977 | 100K |
| **Total** | **14,736** | **3.6M** |

### Fine-tuning Configuration

```yaml
method: LoRA
base_model: Phi-3-Mini-4K-Instruct
lora_rank: 16
lora_alpha: 32
learning_rate: 1e-4
batch_size: 1
iterations: 800
final_loss: 0.492
```

---

## Citation

```bibtex
@misc{finee2025,
  author = {Behera, Ranjit},
  title = {FinEE: Finance Entity Extractor},
  year = {2025},
  publisher = {Hugging Face},
  url = {https://huggingface.co/Ranjit0034/finance-entity-extractor}
}
```

---

<div align="center">

**Built with** ❤️ **by Ranjit Behera**

*Questions? Open an issue on the [repository](https://github.com/Ranjit0034/llm-mail-trainer).*

</div>
'''
    
    # Validating README presence (copied from root)
    if (output_dir / "README.md").exists():
        print("✅ Using project README.md as Model Card")
    else:
        print("⚠️ No README.md found! Writing fallback.")
        with open(output_dir / "README.md", "w") as f:
            f.write(model_card)
    
    print(f"✅ Prepared upload directory: {output_dir}")
    return output_dir


def upload_to_huggingface(upload_dir: Path, repo_id: str):
    """Upload to Hugging Face Hub."""
    print(f"📤 Uploading to {repo_id}...")
    
    cmd = [
        "huggingface-cli", "upload",
        repo_id,
        str(upload_dir),
        "--repo-type", "model"
    ]
    
    try:
        subprocess.run(cmd, check=True)
        print(f"✅ Successfully uploaded to https://huggingface.co/{repo_id}")
    except subprocess.CalledProcessError as e:
        print(f"❌ Upload failed: {e}")
        print("\nTo upload manually, run:")
        print(f"  cd {upload_dir}")
        print(f"  huggingface-cli upload {repo_id} . --repo-type model")


def main():
    parser = argparse.ArgumentParser(description="Upload model to Hugging Face")
    parser.add_argument("--adapter-path", default="models/adapters/finance-lora-v6",
                       help="Path to adapter directory")
    parser.add_argument("--repo-id", default="Ranjit0034/finance-entity-extractor",
                       help="Hugging Face repo ID")
    parser.add_argument("--prepare-only", action="store_true",
                       help="Only prepare files, don't upload")
    
    args = parser.parse_args()
    
    adapter_path = Path(args.adapter_path)
    upload_dir = Path("models/hf-upload")
    
    # Prepare files
    prepare_upload_directory(adapter_path, upload_dir)
    
    if not args.prepare_only:
        upload_to_huggingface(upload_dir, args.repo_id)
    else:
        print(f"\n📁 Files prepared in: {upload_dir}")
        print("To upload, run:")
        print(f"  python scripts/upload_to_hf.py --repo-id {args.repo_id}")


if __name__ == "__main__":
    main()