File size: 1,203 Bytes
c211d20
 
b3f2e54
 
 
c107161
b1b8035
c211d20
b3f2e54
eb0e008
b3f2e54
 
 
 
 
f3db17e
b3f2e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: mit
language:
- en
library_name: transformers
inference: false
pipeline_tag: document-question-answering
---

LiLT Model [Read Here](https://arxiv.org/pdf/2202.13669v1.pdf). This model being fine-tuned on English DocVQA

```python
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
from datasets import load_dataset

model_checkpoint = "TusharGoel/LiLT-Document-QA"
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, add_prefix_space=True)
model_predict = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint)

model_predict.eval()
dataset = load_dataset("nielsr/funsd", split="train")
example = dataset[0]
print(example)

question = "What is the Licensee Number?"
print(question)

words = example["words"]
boxes = example["bboxes"]

encoding = tokenizer(question, words, boxes = boxes, return_token_type_ids=True, return_tensors="pt")

word_ids = encoding.word_ids(0)
outputs = model_predict(**encoding)

loss = outputs.loss
start_scores = outputs.start_logits
end_scores = outputs.end_logits

start, end = word_ids[start_scores.argmax(-1).item()], word_ids[end_scores.argmax(-1).item()]
# print(start, end)
print(" ".join(words[start : end + 1]))
```