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
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
---
license: mit
tags:
- finance
- llm
- lora
- sentiment-analysis
- named-entity-recognition
- xbrl
pipeline_tag: text-generation
---

# FinLoRA: Financial Large Language Models with LoRA Adaptation

[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
[![PyTorch](https://img.shields.io/badge/PyTorch-2.0+-red.svg)](https://pytorch.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Overview

FinLoRA is a comprehensive framework for fine-tuning large language models on financial tasks using Low-Rank Adaptation (LoRA). This repository contains trained LoRA adapters for various financial NLP tasks including sentiment analysis, named entity recognition, headline classification, XBRL processing, and **RAG-enhanced models** for CFA knowledge and FinTagging tasks.

## Model Architecture

- **Base Model**: Meta-Llama-3.1-8B-Instruct
- **Adaptation Method**: LoRA (Low-Rank Adaptation)
- **Quantization**: 8-bit and 4-bit quantization support
- **Tasks**: Financial sentiment analysis, NER, classification, XBRL processing, CFA knowledge, FinTagging

## Available Models

### 8-bit Quantized Models (Recommended)
- `sentiment_llama_3_1_8b_8bits_r8` - Financial sentiment analysis
- `ner_llama_3_1_8b_8bits_r8` - Named entity recognition
- `headline_llama_3_1_8b_8bits_r8` - Financial headline classification
- `xbrl_extract_llama_3_1_8b_8bits_r8` - XBRL tag extraction
- `xbrl_term_llama_3_1_8b_8bits_r8` - XBRL terminology processing
- `financebench_llama_3_1_8b_8bits_r8` - Comprehensive financial benchmark
- `finer_llama_3_1_8b_8bits_r8` - Financial NER
- `formula_llama_3_1_8b_8bits_r8` - Financial formula processing

### RAG-Enhanced Models (New!)
- `cfa_rag_llama_3_1_8b_8bits_r8` - CFA knowledge-enhanced model with RAG
- `fintagging_combined_rag_llama_3_1_8b_8bits_r8` - Combined FinTagging RAG model
- `fintagging_fincl_rag_llama_3_1_8b_8bits_r8` - FinCL RAG-enhanced model
- `fintagging_finni_rag_llama_3_1_8b_8bits_r8` - FinNI RAG-enhanced model

### 4-bit Quantized Models (Memory Efficient)
- `sentiment_llama_3_1_8b_4bits_r4` - Financial sentiment analysis
- `ner_llama_3_1_8b_4bits_r4` - Named entity recognition
- `headline_llama_3_1_8b_4bits_r4` - Financial headline classification
- `xbrl_extract_llama_3_1_8b_4bits_r4` - XBRL tag extraction
- `xbrl_term_llama_3_1_8b_4bits_r4` - XBRL terminology processing
- `financebench_llama_3_1_8b_4bits_r4` - Comprehensive financial benchmark
- `finer_llama_3_1_8b_4bits_r4` - Financial NER
- `formula_llama_3_1_8b_4bits_r4` - Financial formula processing

## Quick Start

### 1. Installation

```bash
# Install dependencies
pip install -r requirements.txt
```

### 2. Basic Usage

```python
from inference import FinLoRAPredictor

# Initialize predictor with 8-bit model (recommended)
predictor = FinLoRAPredictor(
    model_name="sentiment_llama_3_1_8b_8bits_r8",
    use_4bit=False
)

# Financial sentiment analysis
sentiment = predictor.classify_sentiment(
    "The company's quarterly earnings exceeded expectations by 20%."
)
print(f"Sentiment: {sentiment}")

# Entity extraction
entities = predictor.extract_entities(
    "Apple Inc. reported revenue of $394.3 billion in 2022."
)
print(f"Entities: {entities}")

# Use 4-bit model for memory efficiency (if you have limited GPU memory)
predictor_4bit = FinLoRAPredictor(
    model_name="sentiment_llama_3_1_8b_4bits_r4",
    use_4bit=True
)

# CPU-only mode (if no GPU available)
predictor_cpu = FinLoRAPredictor(
    model_name="sentiment_llama_3_1_8b_8bits_r8",
    use_4bit=False
)
# The script will automatically detect CPU and adjust accordingly
```

### 3. Run Complete Test

```bash
# Test all models (this will download the base Llama model if not present)
python inference.py

# Test specific model
python -c "
from inference import FinLoRAPredictor
predictor = FinLoRAPredictor('sentiment_llama_3_1_8b_8bits_r8')
print('Model loaded successfully!')
"
```

## Usage Examples

### Financial Sentiment Analysis

```python
predictor = FinLoRAPredictor("sentiment_llama_3_1_8b_8bits_r8")

# Test cases
test_texts = [
    "Stock prices are soaring to new heights.",
    "Revenue declined by 15% this quarter.",
    "The company maintained stable performance."
]

for text in test_texts:
    sentiment = predictor.classify_sentiment(text)
    print(f"Text: {text}")
    print(f"Sentiment: {sentiment}\n")
```

### Named Entity Recognition

```python
predictor = FinLoRAPredictor("ner_llama_3_1_8b_8bits_r8")

text = "Apple Inc. reported revenue of $394.3 billion in 2022."
entities = predictor.extract_entities(text)
print(f"Entities: {entities}")
```

### XBRL Processing

```python
predictor = FinLoRAPredictor("xbrl_extract_llama_3_1_8b_8bits_r8")

text = "Total assets: $1,234,567,890. Current assets: $456,789,123."
xbrl_tags = predictor.extract_xbrl_tags(text)
print(f"XBRL Tags: {xbrl_tags}")
```

### RAG-Enhanced Models

```python
# CFA RAG-enhanced model for financial knowledge
predictor = FinLoRAPredictor("cfa_rag_llama_3_1_8b_8bits_r8")

# Enhanced financial analysis with CFA knowledge
response = predictor.generate_response(
    "Explain the concept of discounted cash flow valuation"
)
print(f"CFA Response: {response}")

# FinTagging RAG models for financial information extraction
fintagging_predictor = FinLoRAPredictor("fintagging_combined_rag_llama_3_1_8b_8bits_r8")

# Extract financial information with enhanced context
entities = fintagging_predictor.extract_entities(
    "Apple Inc. reported revenue of $394.3 billion in 2022."
)
print(f"Enhanced Entities: {entities}")
```

### Memory-Efficient 4-bit Models

```python
# For users with limited GPU memory
predictor = FinLoRAPredictor(
    model_name="sentiment_llama_3_1_8b_4bits_r4",
    use_4bit=True
)

# Same API as 8-bit models
sentiment = predictor.classify_sentiment("The market is performing well.")
```

## Evaluation

### For Competition Organizers

This section provides guidance for evaluating the submitted models:

#### 1. Quick Model Test
```bash
# Test if all models can be loaded successfully
python test_submission.py
```

#### 2. Comprehensive Evaluation
```bash
# Run full evaluation on all models and datasets
python comprehensive_evaluation.py

# Check results
cat comprehensive_evaluation_results.json
```

#### 3. Incremental Evaluation
```bash
# Run evaluation on missing tasks
python incremental_evaluation.py

# Check results
cat incremental_evaluation_results.json
```

#### 4. Evaluation Results
The evaluation results are provided in:
- `comprehensive_evaluation_results.json` - Complete evaluation results
- `incremental_evaluation_results.json` - Missing task evaluation results

#### 5. Model Performance Summary
All models have been evaluated on multiple financial datasets. See the Performance Results section below for detailed metrics.

### For Researchers

Run comprehensive evaluation on financial datasets:

```bash
# Run full evaluation
python comprehensive_evaluation.py

# Run incremental evaluation
python incremental_evaluation.py

# Run robust evaluation
python robust_incremental.py
```

## Performance Results

The models have been evaluated on multiple financial datasets:

| Task | Dataset | F1 Score | Accuracy |
|------|---------|----------|----------|
| Sentiment Analysis | Financial Phrasebank | 0.333 | 0.500 |
| NER | Financial NER | 0.889 | 0.800 |
| Classification | Headline Classification | 0.697 | 0.700 |
| XBRL Processing | XBRL Tag Extraction | - | 0.200 |
| Sentiment Analysis | FIQA SA | 0.727 | 0.700 |

## Project Structure

```
finlora_hf_submission/
β”œβ”€β”€ models/                          # 8-bit LoRA model adapters (13 models)
β”‚   β”œβ”€β”€ sentiment_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ ner_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ headline_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ xbrl_extract_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ xbrl_term_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ financebench_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ finer_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ formula_llama_3_1_8b_8bits_r8/
β”‚   β”œβ”€β”€ cfa_rag_llama_3_1_8b_8bits_r8/          # NEW: CFA RAG model
β”‚   β”œβ”€β”€ fintagging_combined_rag_llama_3_1_8b_8bits_r8/  # NEW: Combined RAG
β”‚   β”œβ”€β”€ fintagging_fincl_rag_llama_3_1_8b_8bits_r8/     # NEW: FinCL RAG
β”‚   β”œβ”€β”€ fintagging_finni_rag_llama_3_1_8b_8bits_r8/     # NEW: FinNI RAG
β”‚   └── xbrl_train.jsonl-meta-llama-Llama-3.1-8B-Instruct-8bits_r8/
β”œβ”€β”€ models_4bit/                     # 4-bit LoRA model adapters (8 models)
β”‚   β”œβ”€β”€ sentiment_llama_3_1_8b_4bits_r4/
β”‚   β”œβ”€β”€ ner_llama_3_1_8b_4bits_r4/
β”‚   β”œβ”€β”€ headline_llama_3_1_8b_4bits_r4/
β”‚   β”œβ”€β”€ xbrl_extract_llama_3_1_8b_4bits_r4/
β”‚   β”œβ”€β”€ xbrl_term_llama_3_1_8b_4bits_r4/
β”‚   β”œβ”€β”€ financebench_llama_3_1_8b_4bits_r4/
β”‚   β”œβ”€β”€ finer_llama_3_1_8b_4bits_r4/
β”‚   └── formula_llama_3_1_8b_4bits_r4/
β”œβ”€β”€ testdata/                        # Evaluation datasets
β”‚   β”œβ”€β”€ FinCL-eval-subset.csv
β”‚   └── FinNI-eval-subset.csv
β”œβ”€β”€ rag_system/                      # RAG system components
β”œβ”€β”€ inference.py                     # Main inference script
β”œβ”€β”€ comprehensive_evaluation.py      # Full evaluation script
β”œβ”€β”€ incremental_evaluation.py        # Incremental evaluation
β”œβ”€β”€ robust_incremental.py           # Robust evaluation
β”œβ”€β”€ missing_tests.py                # Missing test detection
β”œβ”€β”€ requirements.txt                 # Python dependencies
└── README.md                       # This file
```

## Environment Requirements

### Minimum Requirements (CPU Mode)
- Python 3.8+
- PyTorch 2.0+
- 8GB RAM
- No GPU required

### Recommended Requirements (GPU Mode)
- Python 3.9+
- PyTorch 2.1+
- CUDA 11.8+ (for NVIDIA GPUs)
- 16GB+ GPU memory
- 32GB+ RAM

### Installation Instructions

```bash
# 1. Clone or download this repository
# 2. Install dependencies
pip install -r requirements.txt

# 3. For GPU support (optional but recommended)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

# 4. Verify installation
python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}')"
```

### Troubleshooting

**If you encounter memory issues:**
- Use 4-bit models instead of 8-bit models
- Reduce batch size in inference
- Use CPU mode if GPU memory is insufficient

**If models fail to load:**
- Ensure all model files are present in the correct directories
- Check that the base model (Llama-3.1-8B-Instruct) can be downloaded from HuggingFace
- Verify internet connection for model downloads

**Important Notes for Competition Organizers:**
- The base model (Llama-3.1-8B-Instruct) will be automatically downloaded from HuggingFace on first use (~15GB)
- All LoRA adapters are included in this submission and do not require additional downloads
- Models work in both CPU and GPU modes, with automatic device detection
- RAG-enhanced models require the same base model as regular models

## Model Details

### Training Configuration
- **LoRA Rank**: 8
- **LoRA Alpha**: 16
- **Learning Rate**: 1e-4
- **Batch Size**: 4
- **Epochs**: 3-5
- **Quantization**: 8-bit (BitsAndBytes) / 4-bit (NF4)

### Training Data
- Financial Phrasebank
- FinGPT datasets (NER, Headline, XBRL)
- BloombergGPT financial datasets
- Custom financial text datasets

## Citation

If you use this work in your research, please cite:

```bibtex
@article{finlora2024,
  title={FinLoRA: Financial Large Language Models with LoRA Adaptation},
  author={Your Name},
  journal={Financial AI Conference},
  year={2024}
}
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## Contact

For questions and support, please open an issue or contact [your-email@example.com](mailto:your-email@example.com).

## Submission Summary

### What's Included
- **21 Total Models**: 13 8-bit models (9 original + 4 RAG-enhanced) + 8 4-bit models
- **Complete Evaluation Results**: Comprehensive and incremental evaluation results
- **RAG-Enhanced Models**: CFA and FinTagging models with enhanced knowledge
- **Cross-Platform Support**: Works on CPU, GPU, and various memory configurations
- **Ready-to-Use**: All dependencies specified, automatic device detection

### Quick Start for Competition Organizers
1. Install dependencies: `pip install -r requirements.txt`
2. Test submission: `python test_submission.py`
3. Run evaluation: `python comprehensive_evaluation.py`
4. Check results: `cat comprehensive_evaluation_results.json`

### Model Categories
- **Financial NLP**: Sentiment, NER, Classification, XBRL processing
- **RAG-Enhanced**: CFA knowledge and FinTagging with retrieval augmentation
- **Memory Options**: Both 8-bit and 4-bit quantized versions available

## Acknowledgments

- Meta for the Llama-3.1-8B-Instruct base model
- Hugging Face for the transformers and PEFT libraries
- The financial NLP community for datasets and benchmarks