ethanbradley commited on
Commit
b92ba3f
1 Parent(s): 2f5ed09

Include minimum working example

Browse files
Files changed (1) hide show
  1. README.md +65 -6
README.md CHANGED
@@ -16,14 +16,73 @@ A model for financial table question-answering using the [LayoutLM](https://hugg
16
 
17
  ## Quick start
18
 
19
- To get started with FinTabQA, load it, and the tokenizer, like you would any other Hugging Face Transformer model.
20
 
21
  ```python3
22
- from transformers import LayoutLMForQuestionAnswering, LayoutLMTokenizer
23
-
24
- model = LayoutLMForQuestionAnswering.from_pretrained("ethanbradley/fintabqa")
25
- tokenizer = LayoutLMTokenizer.from_pretrained(
26
- "microsoft/layoutlm-base-uncased")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ```
28
 
29
  ## Citation
 
16
 
17
  ## Quick start
18
 
19
+ To get started with FinTabQA, load it, and a fast tokenizer, like you would any other Hugging Face Transformer model and tokenizer. Below is a minimum working example using the [SynFinTabs](https://huggingface.co/datasets/ethanbradley/synfintabs) dataset.
20
 
21
  ```python3
22
+ >>> from typing import List, Tuple
23
+ >>> from datasets import load_dataset
24
+ >>> from transformers import LayoutLMForQuestionAnswering, LayoutLMTokenizerFast
25
+ >>> import torch
26
+ >>>
27
+ >>> synfintabs_dataset = load_dataset("ethanbradley/synfintabs")
28
+ >>> model = LayoutLMForQuestionAnswering.from_pretrained("ethanbradley/fintabqa")
29
+ >>> tokenizer = LayoutLMTokenizerFast.from_pretrained(
30
+ ... "microsoft/layoutlm-base-uncased")
31
+ >>>
32
+ >>> def normalise_boxes(
33
+ ... boxes: List[List[int]],
34
+ ... old_image_size: Tuple[int, int],
35
+ ... new_image_size: Tuple[int, int]) -> List[List[int]]:
36
+ ... old_im_w, old_im_h = old_image_size
37
+ ... new_im_w, new_im_h = new_image_size
38
+ ...
39
+ ... return [[
40
+ ... max(min(int(x1 / old_im_w * new_im_w), new_im_w), 0),
41
+ ... max(min(int(y1 / old_im_h * new_im_h), new_im_h), 0),
42
+ ... max(min(int(x2 / old_im_w * new_im_w), new_im_w), 0),
43
+ ... max(min(int(y2 / old_im_h * new_im_h), new_im_h), 0)
44
+ ... ] for (x1, y1, x2, y2) in boxes]
45
+ >>>
46
+ >>> item = synfintabs_dataset['test'][0]
47
+ >>> question_dict = next(question for question in item['questions']
48
+ ... if question['id'] == item['question_id'])
49
+ >>> encoding = tokenizer(
50
+ ... question_dict['question'].split(),
51
+ ... item['ocr_results']['words'],
52
+ ... max_length=512,
53
+ ... padding="max_length",
54
+ ... truncation="only_second",
55
+ ... is_split_into_words=True,
56
+ ... return_token_type_ids=True,
57
+ ... return_tensors="pt")
58
+ >>>
59
+ >>> word_boxes = normalise_boxes(
60
+ ... item['ocr_results']['bboxes'],
61
+ ... item['image'].crop(item['bbox']).size,
62
+ ... (1000, 1000))
63
+ >>> token_boxes = []
64
+ >>>
65
+ >>> for i, s, w in zip(
66
+ ... encoding['input_ids'][0],
67
+ ... encoding.sequence_ids(0),
68
+ ... encoding.word_ids(0)):
69
+ ... if s == 1:
70
+ ... token_boxes.append(word_boxes[w])
71
+ ... elif i == tokenizer.sep_token_id:
72
+ ... token_boxes.append([1000] * 4)
73
+ ... else:
74
+ ... token_boxes.append([0] * 4)
75
+ >>>
76
+ >>> encoding['bbox'] = torch.tensor([token_boxes])
77
+ >>> outputs = model(**encoding)
78
+ >>> start = encoding.word_ids(0)[outputs['start_logits'].argmax(-1)]
79
+ >>> end = encoding.word_ids(0)[outputs['end_logits'].argmax(-1)]
80
+ >>>
81
+ >>> print(f"Target: {question_dict['answer']}")
82
+ Target: 6,980
83
+ >>>
84
+ >>> print(f"Prediction: {' '.join(item['ocr_results']['words'][start : end])}")
85
+ Prediction: 6,980
86
  ```
87
 
88
  ## Citation