File size: 10,361 Bytes
a67de9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
---

license: mit
base_model: microsoft/Phi-3-mini-4k-instruct
tags:
- phi-3
- lora
- payments
- finance
- information-extraction
- structured-data-extraction
- text-to-data
- finetuned
datasets:
- custom
language:
- en
pipeline_tag: text-generation
library_name: transformers
---


# Phi-3 Mini Reverse Fine-tuned for Payments Domain

This is a **reverse** fine-tuned version of [Microsoft's Phi-3-Mini-4k-Instruct](microsoft/Phi-3-mini-4k-instruct) model, adapted for extracting structured payment metadata from natural language descriptions using LoRA (Low-Rank Adaptation).

## Model Description

This model converts natural language payment descriptions into structured, machine-readable metadata. It performs the **opposite** task of the forward model - instead of generating human-friendly text, it extracts structured data that can be processed by payment APIs and applications.

### Related Models

**Forward Model (Companion):** [aamanlamba/phi3-payments-finetune](https://huggingface.co/aamanlamba/phi3-payments-finetune)
- Converts structured metadata → natural language
- Use together for round-trip validation

### Training Data

The model was trained on a dataset of 500+ synthetic payment transactions where:
- **Input**: Natural language payment descriptions
- **Output**: Structured metadata in `action(field[value], ...)` format

Transaction types covered:
- Standard payments (ACH, wire transfer, credit/debit card)
- Refunds (full and partial)
- Chargebacks and disputes
- Failed/declined transactions
- International transfers with currency conversion
- Transaction fees
- Recurring payments/subscriptions

### Example Usage

```python

from transformers import AutoModelForCausalLM, AutoTokenizer

from peft import PeftModel

import torch



# Load base model

base_model = "microsoft/Phi-3-mini-4k-instruct"

model = AutoModelForCausalLM.from_pretrained(

    base_model,

    torch_dtype=torch.float16,

    device_map="auto"

)



# Load LoRA adapters (reverse model)

model = PeftModel.from_pretrained(model, "aamanlamba/phi3-payments-reverse-finetune")

tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)



# Extract structured data

prompt = """<|system|>

You are a financial data extraction assistant that converts natural language payment descriptions into structured metadata that can be processed by payment applications.<|end|>

<|user|>

Extract structured payment information from the following description:



Your payment of USD 1,500.00 to Global Supplies Inc via wire transfer was successfully completed on 2024-10-27.<|end|>

<|assistant|>

"""



inputs = tokenizer(prompt, return_tensors="pt").to(model.device)



with torch.no_grad():

    outputs = model.generate(

        **inputs,

        max_new_tokens=200,

        temperature=0.3,  # Lower temperature for more deterministic extraction

        top_p=0.9,

        do_sample=True

    )



response = tokenizer.decode(outputs[0], skip_special_tokens=True)

structured_data = response.split("<|assistant|>")[-1].strip()

print(structured_data)

```

**Expected output:**
```

inform(transaction_type[payment], amount[1500.00], currency[USD], receiver[Global Supplies Inc], status[completed], method[wire_transfer], date[2024-10-27])

```

### Parsing the Output

```python

import re



def parse_structured_data(structured_str: str) -> dict:

    """Parse structured payment data into a dictionary"""

    action_match = re.match(r'(\w+)\((.*)\)', structured_str)

    if not action_match:

        return None



    action_type = action_match.group(1)

    fields_str = action_match.group(2)



    fields = {}

    field_pattern = r'(\w+)\[(.*?)\]'

    for match in re.finditer(field_pattern, fields_str):

        field_name = match.group(1)

        field_value = match.group(2)



        # Convert numeric values

        if field_name in ['amount', 'refund_amount', 'fee_amount', 'exchange_rate']:

            try:

                field_value = float(field_value)

            except ValueError:

                pass



        fields[field_name] = field_value



    return {

        'action_type': action_type,

        'fields': fields

    }



# Use it

parsed = parse_structured_data(structured_data)

print(parsed)

# Output: {'action_type': 'inform', 'fields': {'transaction_type': 'payment', 'amount': 1500.0, ...}}

```

## Training Details

### Training Configuration

- **Base Model**: microsoft/Phi-3-mini-4k-instruct
- **Fine-tuning Method**: LoRA (Low-Rank Adaptation)
- **Task Direction**: Natural Language → Structured Data (Reverse)
- **LoRA Rank**: 16
- **LoRA Alpha**: 32
- **Target Modules**: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj

- **Quantization**: 8-bit (training), float16 (inference)

- **Training Epochs**: 3

- **Learning Rate**: 2e-4

- **Batch Size**: 1 (with 8 gradient accumulation steps)

- **Hardware**: NVIDIA RTX 3060 (12GB VRAM)

- **Training Time**: ~35-45 minutes



### Training Loss



- Initial Loss: ~3.5-4.0

- Final Loss: ~0.8-1.2

- Validation Loss: ~1.0-1.3

- Extraction Accuracy: ~90-95% on validation set



## Model Size



- **LoRA Adapter Size**: ~15MB (only the adapter weights, not the full model)

- **Full Model Size**: ~7GB (when combined with base model)



## Supported Transaction Types



1. **Payments**: Standard payment transactions with various methods

2. **Refunds**: Full and partial refunds

3. **Chargebacks**: Dispute and chargeback processing

4. **Failed Payments**: Declined or failed transactions with reasons

5. **International Transfers**: Cross-border payments with currency conversion

6. **Fees**: Transaction and processing fees

7. **Recurring Payments**: Subscriptions and scheduled payments

8. **Reversals**: Payment reversals and adjustments



## Output Format



The model extracts data in this structured format:

```

action_type(field1[value1], field2[value2], ...)
```



**Action Types:**

- `inform`: Informational transactions (payments, refunds, transfers)

- `alert`: Alerts and notifications (failures, chargebacks)



**Common Fields:**

- `transaction_type`: Type of transaction

- `amount`: Transaction amount (numeric)

- `currency`: Currency code (USD, EUR, GBP, etc.)

- `sender`/`receiver`/`merchant`: Party names

- `status`: Transaction status (completed, pending, failed, etc.)

- `method`: Payment method (credit_card, ACH, wire_transfer, etc.)

- `date`: Transaction date (YYYY-MM-DD)

- `reason`: Failure/chargeback reason (for alerts)



## Use Cases



### 1. Conversational Payment Interfaces

Extract payment details from user messages:

```
User: "I want to send $500 to John via PayPal"
Extracted: inform(transaction_type[payment], amount[500], currency[USD], receiver[John], method[PayPal])

```



### 2. Email Parsing

Extract transaction data from payment notification emails automatically.



### 3. Voice Payment Systems

Convert spoken payment descriptions into structured API calls.



### 4. Payment API Integration

Transform natural language payment requests into API-ready parameters.



## Limitations



- Trained on synthetic data - may require additional fine-tuning for production use

- Optimized for English language only

- Best performance on transaction patterns similar to training data

- Output format is custom - requires parsing (see example above)

- Not suitable for handling real financial transactions without validation

- Lower temperature (0.3) recommended for consistent extraction



## Ethical Considerations



- This model was trained on synthetic, anonymized data only

- Does not contain any real customer PII or transaction data

- Should be validated for accuracy before production deployment

- Implement validation and error handling for extracted data

- Consider regulatory compliance (PCI-DSS, GDPR, etc.) in your jurisdiction

- Always verify extracted financial data before processing



## Intended Use



**Primary Use Cases:**

- Extracting transaction data from natural language descriptions

- Building conversational payment bots

- Parsing payment notifications and emails

- Converting user requests to API parameters

- Training and demonstration purposes

- Research in financial NLP and information extraction



**Out of Scope:**

- Direct transaction processing without validation

- Real-time financial systems without error handling

- Compliance-critical data extraction

- Medical or legal payment processing



## Performance Notes



- **Inference Speed**: ~2-3 seconds per extraction on RTX 3060

- **Temperature**: Use 0.1-0.3 for deterministic extraction

- **Validation**: Always validate output format and field values

- **Error Handling**: Implement fallbacks for malformed outputs



## How to Cite



If you use this model in your research or application, please cite:



```bibtex

@misc{phi3-payments-reverse-finetuned,

  author = {aamanlamba},

  title = {Phi-3 Mini Reverse Fine-tuned for Payments Domain},

  year = {2024},

  publisher = {HuggingFace},

  howpublished = {\url{https://huggingface.co/aamanlamba/phi3-payments-reverse-finetune}}

}

```



## Training Code



The complete training code and dataset generation scripts are available on GitHub:

- **Repository**: [github.com/aamanlamba/phi3-tune-payments](https://github.com/aamanlamba/phi3-tune-payments)

- **Branch**: `reverse-structured-extraction` (this model)

- **Includes**: Reverse dataset generator, training scripts, testing utilities, parsing examples



## Acknowledgements



- Base model: [Microsoft Phi-3-Mini-4k-Instruct](microsoft/Phi-3-mini-4k-instruct)

- Fine-tuning method: [LoRA: Low-Rank Adaptation of Large Language Models](https://arxiv.org/abs/2106.09685)

- Training framework: HuggingFace Transformers + PEFT

- Inspired by: [NVIDIA AI Workbench Phi-3 Fine-tuning Example](https://github.com/NVIDIA/workbench-example-phi3-finetune)



## License



This model is released under the MIT license, compatible with the base Phi-3 model license.



## Contact



For questions or issues, please open an issue on the GitHub repository or contact the author.



---



**Note**: This is a **reverse** model for structured data extraction. For generating natural language from structured data, see the companion forward model.