Document Question Answering
Transformers
Safetensors
English
Inference Endpoints
File size: 5,715 Bytes
62239c6
 
98829b2
 
a469fb3
98829b2
 
 
62239c6
 
98829b2
62239c6
cacf325
62239c6
1161b0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62239c6
 
 
 
98829b2
62239c6
 
 
98829b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62239c6
 
 
 
 
98829b2
62239c6
 
 
d271c8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62239c6
 
 
98829b2
 
 
 
 
 
 
 
 
62239c6
 
 
98829b2
 
 
 
 
 
 
 
 
 
 
 
 
62239c6
 
 
 
 
 
 
 
 
 
4ef23e3
62239c6
 
 
1161b0b
62239c6
 
 
a469fb3
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
---
library_name: transformers
license: mit
datasets:
- chenghao/sec-material-contracts-qa-splitted
language:
- en
pipeline_tag: document-question-answering
---

# Idefices2-EDGAR

Idefices2 8B fine-tuned on 800+ multi-page documents for Visual DocQA. Make sure you have the latest peft and transformers before loading the model. GPU is required for it to work properly.

Compared to the base model, it has a lower edit distance (53% improvement on micro average) on the test set.

|    | Category                    |   Idefics2-8B |   Idefics2-8B-EDGAR | Δ(↑)   |
|---:|:----------------------------|--------------:|--------------------:|:-------|
|  0 | agreement_date              |      0.878489 |           0.0999479 | 88.62% |
|  1 | agreement_term              |      0.907067 |           0.438816  | 51.62% |
|  2 | auto_renewal                |      0.634946 |           0.0516129 | 91.87% |
|  3 | contract_value              |      0.474438 |           0.418815  | 11.72% |
|  4 | counterparty_address        |      0.771387 |           0.59835   | 22.43% |
|  5 | counterparty_name           |      0.825491 |           0.633359  | 23.27% |
|  6 | counterparty_signer_name    |      0.842091 |           0.480444  | 42.95% |
|  7 | counterparty_signer_title   |      0.61746  |           0.496041  | 19.66% |
|  8 | effective_date              |      0.903268 |           0.125641  | 86.09% |
|  9 | expiration_date             |      0.88673  |           0.235197  | 73.48% |
| 10 | governing_law               |      0.881037 |           0.308771  | 64.95% |
| 11 | opt_out_length              |      0.431548 |           0.047619  | 88.97% |
| 12 | party_address               |      0.730897 |           0.608301  | 16.77% |
| 13 | party_name                  |      0.726411 |           0.490194  | 32.52% |
| 14 | payment_frequency           |      0.686123 |           0.373724  | 45.53% |
| 15 | payment_term                |      0.854552 |           0.593333  | 30.57% |
| 16 | renewal_term                |      0.92829  |           0.0595238 | 93.59% |
| 17 | termination_for_cause       |      0.436    |           0.048     | 88.99% |
| 18 | termination_for_convenience |      0.628261 |           0.156522  | 75.09% |
| 19 | termination_notice_period   |      0.329748 |           0.178394  | 45.90% |
| 20 | venue                       |      0.781417 |           0.61403   | 21.42% |



![image/png](https://cdn-uploads.huggingface.co/production/uploads/607a5b44489fc71534e91c0e/3Jc7I1Fj2J3rabos2HLyY.png)

## Model Details

### Model Description

Finetuned form [Idefics2](https://huggingface.co/docs/transformers/main/en/model_doc/idefics2).

## Uses

```python
import torch
from transformers import AutoProcessor, Idefics2ForConditionalGeneration, BitsAndBytesConfig
from datasets import load_from_disk

base_model = "HuggingFaceM4/idefics2-8b"
peft_model_id = "chenghao/idefics2-edgar"
quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
    bnb_4bit_compute_dtype=torch.float16
)
model = Idefics2ForConditionalGeneration.from_pretrained(
    peft_model_id,
    torch_dtype=torch.float16,
    quantization_config=quantization_config,
)

model.eval()
processor = AutoProcessor.from_pretrained(base_model, do_image_splitting=True,
                                          size={"longest_edge": 490, "shortest_edge": 350})
dataset = load_from_disk("local-dataset")
test_example = dataset["test"][30]
images, question, answer = test_example["images"], test_example["question"], test_example["answer"]

messages = [
    {
        "role": "user",
        "content": [{"type": "image"} for _ in range(len(images))] + [{"type": "text", "text": question}],
    },
]
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=prompt, images=images, return_tensors="pt").to("cuda")
with torch.no_grad():
    generated_ids = model.generate(**inputs, max_new_tokens=1024)
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
preds = [t.split("Assistant:", 1)[-1].strip() for t in generated_texts]
print(f"""
Question: {question}
Answer: {answer}
Prediction: {preds or 'N/A'}
""")
```

## Training Details

### Training Data

[SEC Contract QA](https://huggingface.co/datasets/chenghao/sec-material-contracts-qa)

### Training Procedure

10 epochs with QLoRA. Trained with A100-80GB for about 10 hours.

```
MAX_LENGTH = 1024
USE_LORA = False
USE_QLORA = True
MAX_PAGE = 5

config = {
    "max_epochs": 10,
    # "val_check_interval": 0.2,
    "check_val_every_n_epoch": 1,
    "gradient_clip_val": 1.0,
    "accumulate_grad_batches": 12,
    "lr": 1e-4,
    "batch_size": 2,
    "precision": "16-mixed",
    "seed": 42,
    "warmup_steps": 50,
    "result_path": "./result",
    "verbose": True,
}
```

#### Preprocessing [optional]

No image splitting due to memory limit.

```python
processor = AutoProcessor.from_pretrained(
    "HuggingFaceM4/idefics2-8b",
    do_image_splitting=False,
    size={"longest_edge": 490, "shortest_edge": 350}
)
```

#### Training Hyperparameters

```python
quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
    bnb_4bit_compute_dtype=torch.float16
)
model = Idefics2ForConditionalGeneration.from_pretrained(
    "HuggingFaceM4/idefics2-8b",
    torch_dtype=torch.float16,
    quantization_config=quantization_config,
)
```

#### Speeds, Sizes, Times [optional]


## Evaluation

### Testing Data, Factors & Metrics

#### Testing Data

20% percent of the dataset.

#### Metrics

Edit Distance (nltk).

### Results

See above.