Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Dataset derived from FUNSD for training GLiNER-based multi-modal models.
|
2 |
+
|
3 |
+
Processing script:
|
4 |
+
```python
|
5 |
+
from datasets import load_dataset
|
6 |
+
import shutil
|
7 |
+
import json
|
8 |
+
import os
|
9 |
+
|
10 |
+
dataset = load_dataset("nielsr/funsd")
|
11 |
+
raw_labels = dataset['train'].features['ner_tags'].feature.names
|
12 |
+
|
13 |
+
gliner_pdf_dataset = []
|
14 |
+
output_dir = "gliner_funsd"
|
15 |
+
os.makedirs(output_dir, exist_ok=True)
|
16 |
+
os.makedirs(os.path.join(output_dir, 'images'), exist_ok=True)
|
17 |
+
|
18 |
+
def process_dataset(example):
|
19 |
+
tokens = example['words']
|
20 |
+
bboxes = example['bboxes']
|
21 |
+
img_src = example['image_path']
|
22 |
+
|
23 |
+
ner_spans = []
|
24 |
+
prev_label = 'O'
|
25 |
+
prev_idx = None
|
26 |
+
|
27 |
+
for idx, tag_id in enumerate(example['ner_tags']):
|
28 |
+
lbl = raw_labels[tag_id]
|
29 |
+
if lbl == 'O':
|
30 |
+
if prev_label != 'O':
|
31 |
+
ner_spans.append([prev_idx, idx - 1, prev_label])
|
32 |
+
prev_label = 'O'
|
33 |
+
continue
|
34 |
+
|
35 |
+
_, label = lbl.split('-', 1)
|
36 |
+
if prev_label != label:
|
37 |
+
if prev_label != 'O':
|
38 |
+
ner_spans.append([prev_idx, idx - 1, prev_label])
|
39 |
+
prev_idx = idx
|
40 |
+
prev_label = label
|
41 |
+
|
42 |
+
if prev_label != 'O':
|
43 |
+
ner_spans.append([prev_idx, len(example['ner_tags']) - 1, prev_label])
|
44 |
+
|
45 |
+
fname = os.path.basename(img_src)
|
46 |
+
img_dst = os.path.join(output_dir, 'images', fname)
|
47 |
+
shutil.copy(img_src, img_dst)
|
48 |
+
|
49 |
+
gliner_pdf_dataset.append({
|
50 |
+
"tokenized_text": tokens,
|
51 |
+
"bboxes": bboxes,
|
52 |
+
"ner": ner_spans,
|
53 |
+
"image_path": img_dst
|
54 |
+
})
|
55 |
+
|
56 |
+
for ex in dataset['train']:
|
57 |
+
process_dataset(ex)
|
58 |
+
|
59 |
+
with open(os.path.join(output_dir, 'data.json'), 'w') as f:
|
60 |
+
json.dump(gliner_pdf_dataset, f)
|
61 |
+
```
|