w3robotics commited on
Commit
2baa8f1
·
verified ·
1 Parent(s): 1b79f2a

Create Parse.py

Browse files
Files changed (1) hide show
  1. Parse.py +77 -0
Parse.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ from transformers import DonutProcessor, VisionEncoderDecoderModel
4
+ from datasets import load_dataset
5
+ import torch
6
+
7
+ processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
8
+ model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model.to(device)
12
+ # load document image
13
+ dataset = load_dataset("hf-internal-testing/example-documents", split="test")
14
+ image = dataset[2]["image"]
15
+
16
+ # prepare decoder inputs
17
+ task_prompt = "<s_cord-v2>"
18
+ decoder_input_ids = processor.tokenizer(task_prompt, add_special_tokens=False, return_tensors="pt").input_ids
19
+
20
+ pixel_values = processor(image, return_tensors="pt").pixel_values
21
+
22
+ outputs = model.generate(
23
+ pixel_values.to(device),
24
+ decoder_input_ids=decoder_input_ids.to(device),
25
+ max_length=model.decoder.config.max_position_embeddings,
26
+ pad_token_id=processor.tokenizer.pad_token_id,
27
+ eos_token_id=processor.tokenizer.eos_token_id,
28
+ use_cache=True,
29
+ bad_words_ids=[[processor.tokenizer.unk_token_id]],
30
+ return_dict_in_generate=True,
31
+ )
32
+
33
+ sequence = processor.batch_decode(outputs.sequences)[0]
34
+ sequence = sequence.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
35
+ sequence = re.sub(r"<.*?>", "", sequence, count=1).strip() # remove first task start token
36
+ print(processor.token2json(sequence))
37
+ {'menu': {'nm': 'CINNAMON SUGAR', 'unitprice': '17,000', 'cnt': '1 x', 'price': '17,000'}, 'sub_total': {'subtotal_price': '17,000'}, 'total': {'total_price': '17,000', 'cashprice': '20,000', 'changeprice': '3,000'}}
38
+ Step-by-step Document Visual Question Answering (DocVQA)
39
+ Copied
40
+ import re
41
+
42
+ from transformers import DonutProcessor, VisionEncoderDecoderModel
43
+ from datasets import load_dataset
44
+ import torch
45
+
46
+ processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
47
+ model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
48
+
49
+ device = "cuda" if torch.cuda.is_available() else "cpu"
50
+ model.to(device)
51
+ # load document image from the DocVQA dataset
52
+ dataset = load_dataset("hf-internal-testing/example-documents", split="test")
53
+ image = dataset[0]["image"]
54
+
55
+ # prepare decoder inputs
56
+ task_prompt = "<s_docvqa><s_question>{user_input}</s_question><s_answer>"
57
+ question = "When is the coffee break?"
58
+ prompt = task_prompt.replace("{user_input}", question)
59
+ decoder_input_ids = processor.tokenizer(prompt, add_special_tokens=False, return_tensors="pt").input_ids
60
+
61
+ pixel_values = processor(image, return_tensors="pt").pixel_values
62
+
63
+ outputs = model.generate(
64
+ pixel_values.to(device),
65
+ decoder_input_ids=decoder_input_ids.to(device),
66
+ max_length=model.decoder.config.max_position_embeddings,
67
+ pad_token_id=processor.tokenizer.pad_token_id,
68
+ eos_token_id=processor.tokenizer.eos_token_id,
69
+ use_cache=True,
70
+ bad_words_ids=[[processor.tokenizer.unk_token_id]],
71
+ return_dict_in_generate=True,
72
+ )
73
+
74
+ sequence = processor.batch_decode(outputs.sequences)[0]
75
+ sequence = sequence.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
76
+ sequence = re.sub(r"<.*?>", "", sequence, count=1).strip() # remove first task start token
77
+ print(processor.token2json(sequence))