kevin-pek commited on
Commit
8ccadc6
1 Parent(s): 13dd7c2

add custom handler.py and upload files

Browse files
README.md ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - vision
5
+ - nougat
6
+ pipeline_tag: image-to-text
7
+ ---
8
+
9
+ # Nougat model, base-sized version
10
+
11
+ Nougat model trained on PDF-to-markdown. It was introduced in the paper [Nougat: Neural Optical Understanding for Academic Documents](https://arxiv.org/abs/2308.13418) by Blecher et al. and first released in [this repository](https://github.com/facebookresearch/nougat/tree/main).
12
+
13
+ Disclaimer: The team releasing Nougat did not write a model card for this model so this model card has been written by the Hugging Face team.
14
+
15
+ Note: this model corresponds to the "0.1.0-base" version of the original repository.
16
+
17
+ ## Model description
18
+
19
+ Nougat is a [Donut](https://huggingface.co/docs/transformers/model_doc/donut) model trained to transcribe scientific PDFs into an easy-to-use markdown format. The model consists of a Swin Transformer as vision encoder, and an mBART model as text decoder.
20
+
21
+ The model is trained to autoregressively predict the markdown given only the pixels of the PDF image as input.
22
+
23
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/nougat_architecture.jpg"
24
+ alt="drawing" width="600"/>
25
+
26
+ <small> Nougat high-level overview. Taken from the <a href="https://arxiv.org/abs/2308.13418">original paper</a>. </small>
27
+
28
+ ## Intended uses & limitations
29
+
30
+ You can use the raw model for transcribing a PDF into Markdown. See the [model hub](https://huggingface.co/models?search=nougat) to look for other
31
+ fine-tuned versions that may interest you.
32
+
33
+ ### How to use
34
+
35
+ We refer to the [docs](https://huggingface.co/docs/transformers/main/en/model_doc/nougat).
36
+
37
+ ### BibTeX entry and citation info
38
+
39
+ ```bibtex
40
+ @misc{blecher2023nougat,
41
+ title={Nougat: Neural Optical Understanding for Academic Documents},
42
+ author={Lukas Blecher and Guillem Cucurull and Thomas Scialom and Robert Stojnic},
43
+ year={2023},
44
+ eprint={2308.13418},
45
+ archivePrefix={arXiv},
46
+ primaryClass={cs.LG}
47
+ }
48
+ ```
config.json ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "VisionEncoderDecoderModel"
4
+ ],
5
+ "decoder": {
6
+ "_name_or_path": "",
7
+ "activation_dropout": 0.0,
8
+ "activation_function": "gelu",
9
+ "add_cross_attention": true,
10
+ "add_final_layer_norm": true,
11
+ "architectures": null,
12
+ "attention_dropout": 0.0,
13
+ "bad_words_ids": null,
14
+ "begin_suppress_tokens": null,
15
+ "bos_token_id": 0,
16
+ "chunk_size_feed_forward": 0,
17
+ "classifier_dropout": 0.0,
18
+ "cross_attention_hidden_size": null,
19
+ "d_model": 1024,
20
+ "decoder_attention_heads": 16,
21
+ "decoder_ffn_dim": 4096,
22
+ "decoder_layerdrop": 0.0,
23
+ "decoder_layers": 10,
24
+ "decoder_start_token_id": null,
25
+ "diversity_penalty": 0.0,
26
+ "do_sample": false,
27
+ "dropout": 0.1,
28
+ "early_stopping": false,
29
+ "encoder_attention_heads": 16,
30
+ "encoder_ffn_dim": 4096,
31
+ "encoder_layerdrop": 0.0,
32
+ "encoder_layers": 12,
33
+ "encoder_no_repeat_ngram_size": 0,
34
+ "eos_token_id": 2,
35
+ "exponential_decay_length_penalty": null,
36
+ "finetuning_task": null,
37
+ "forced_bos_token_id": null,
38
+ "forced_eos_token_id": 2,
39
+ "id2label": {
40
+ "0": "LABEL_0",
41
+ "1": "LABEL_1"
42
+ },
43
+ "init_std": 0.02,
44
+ "is_decoder": true,
45
+ "is_encoder_decoder": false,
46
+ "label2id": {
47
+ "LABEL_0": 0,
48
+ "LABEL_1": 1
49
+ },
50
+ "length_penalty": 1.0,
51
+ "max_length": 20,
52
+ "max_position_embeddings": 4096,
53
+ "min_length": 0,
54
+ "model_type": "mbart",
55
+ "no_repeat_ngram_size": 0,
56
+ "num_beam_groups": 1,
57
+ "num_beams": 1,
58
+ "num_hidden_layers": 12,
59
+ "num_return_sequences": 1,
60
+ "output_attentions": false,
61
+ "output_hidden_states": false,
62
+ "output_scores": false,
63
+ "pad_token_id": 1,
64
+ "prefix": null,
65
+ "problem_type": null,
66
+ "pruned_heads": {},
67
+ "remove_invalid_values": false,
68
+ "repetition_penalty": 1.0,
69
+ "return_dict": true,
70
+ "return_dict_in_generate": false,
71
+ "scale_embedding": true,
72
+ "sep_token_id": null,
73
+ "suppress_tokens": null,
74
+ "task_specific_params": null,
75
+ "temperature": 1.0,
76
+ "tf_legacy_loss": false,
77
+ "tie_encoder_decoder": false,
78
+ "tie_word_embeddings": false,
79
+ "tokenizer_class": null,
80
+ "top_k": 50,
81
+ "top_p": 1.0,
82
+ "torch_dtype": null,
83
+ "torchscript": false,
84
+ "typical_p": 1.0,
85
+ "use_bfloat16": false,
86
+ "use_cache": true,
87
+ "vocab_size": 50000
88
+ },
89
+ "encoder": {
90
+ "_name_or_path": "",
91
+ "add_cross_attention": false,
92
+ "architectures": null,
93
+ "attention_probs_dropout_prob": 0.0,
94
+ "bad_words_ids": null,
95
+ "begin_suppress_tokens": null,
96
+ "bos_token_id": null,
97
+ "chunk_size_feed_forward": 0,
98
+ "cross_attention_hidden_size": null,
99
+ "decoder_start_token_id": null,
100
+ "depths": [
101
+ 2,
102
+ 2,
103
+ 14,
104
+ 2
105
+ ],
106
+ "diversity_penalty": 0.0,
107
+ "do_sample": false,
108
+ "drop_path_rate": 0.1,
109
+ "early_stopping": false,
110
+ "embed_dim": 128,
111
+ "encoder_no_repeat_ngram_size": 0,
112
+ "eos_token_id": null,
113
+ "exponential_decay_length_penalty": null,
114
+ "finetuning_task": null,
115
+ "forced_bos_token_id": null,
116
+ "forced_eos_token_id": null,
117
+ "hidden_act": "gelu",
118
+ "hidden_dropout_prob": 0.0,
119
+ "hidden_size": 1024,
120
+ "id2label": {
121
+ "0": "LABEL_0",
122
+ "1": "LABEL_1"
123
+ },
124
+ "image_size": [
125
+ 896,
126
+ 672
127
+ ],
128
+ "initializer_range": 0.02,
129
+ "is_decoder": false,
130
+ "is_encoder_decoder": false,
131
+ "label2id": {
132
+ "LABEL_0": 0,
133
+ "LABEL_1": 1
134
+ },
135
+ "layer_norm_eps": 1e-05,
136
+ "length_penalty": 1.0,
137
+ "max_length": 20,
138
+ "min_length": 0,
139
+ "mlp_ratio": 4.0,
140
+ "model_type": "donut-swin",
141
+ "no_repeat_ngram_size": 0,
142
+ "num_beam_groups": 1,
143
+ "num_beams": 1,
144
+ "num_channels": 3,
145
+ "num_heads": [
146
+ 4,
147
+ 8,
148
+ 16,
149
+ 32
150
+ ],
151
+ "num_layers": 4,
152
+ "num_return_sequences": 1,
153
+ "output_attentions": false,
154
+ "output_hidden_states": false,
155
+ "output_scores": false,
156
+ "pad_token_id": null,
157
+ "patch_size": 4,
158
+ "prefix": null,
159
+ "problem_type": null,
160
+ "pruned_heads": {},
161
+ "qkv_bias": true,
162
+ "remove_invalid_values": false,
163
+ "repetition_penalty": 1.0,
164
+ "return_dict": true,
165
+ "return_dict_in_generate": false,
166
+ "sep_token_id": null,
167
+ "suppress_tokens": null,
168
+ "task_specific_params": null,
169
+ "temperature": 1.0,
170
+ "tf_legacy_loss": false,
171
+ "tie_encoder_decoder": false,
172
+ "tie_word_embeddings": true,
173
+ "tokenizer_class": null,
174
+ "top_k": 50,
175
+ "top_p": 1.0,
176
+ "torch_dtype": null,
177
+ "torchscript": false,
178
+ "typical_p": 1.0,
179
+ "use_absolute_embeddings": false,
180
+ "use_bfloat16": false,
181
+ "window_size": 7
182
+ },
183
+ "is_encoder_decoder": true,
184
+ "model_type": "vision-encoder-decoder",
185
+ "tie_word_embeddings": false,
186
+ "torch_dtype": "float32",
187
+ "transformers_version": "4.34.0.dev0"
188
+ }
generation_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 0,
4
+ "eos_token_id": 2,
5
+ "forced_eos_token_id": 2,
6
+ "pad_token_id": 1,
7
+ "transformers_version": "4.34.0.dev0"
8
+ }
handler.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ from typing import Dict, List, Any
3
+ from transformers import NougatProcessor, VisionEncoderDecoderModel
4
+ from transformers.image_utils import base64
5
+ from PIL import Image
6
+ import torch
7
+
8
+ class EndpointHandler():
9
+ def __init__(self, path="facebook/nougat-base") -> None:
10
+ self.processor = NougatProcessor.from_pretrained(path)
11
+ self.model = VisionEncoderDecoderModel.from_pretrained(path)
12
+
13
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ self.model.to(self.device)
15
+
16
+ def __call__(self, data: Dict[str, Any]) -> str:
17
+ image = data.pop("inputs", data)
18
+ image_data = Image.open(BytesIO(base64.b64decode(image)))
19
+ pixel_values = self.processor(image_data, return_tensors="pt").pixel_values
20
+
21
+ outputs = self.model.generate(
22
+ pixel_values.to(self.device),
23
+ min_length=1,
24
+ max_new_tokens=30,
25
+ bad_words_ids=[[self.processor.tokenizer.unk_token_id]]
26
+ )
27
+
28
+ text = self.processor.batch_decode(outputs, skip_special_tokens=True)[0]
29
+ text = self.processor.post_process_generation(text, fix_markdown=False)
30
+
31
+ return text
preprocessor_config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_align_long_axis": false,
3
+ "do_crop_margin": true,
4
+ "do_normalize": true,
5
+ "do_pad": true,
6
+ "do_rescale": true,
7
+ "do_resize": true,
8
+ "do_thumbnail": true,
9
+ "image_mean": [
10
+ 0.485,
11
+ 0.456,
12
+ 0.406
13
+ ],
14
+ "image_processor_type": "NougatImageProcessor",
15
+ "image_std": [
16
+ 0.229,
17
+ 0.224,
18
+ 0.225
19
+ ],
20
+ "processor_class": "NougatProcessor",
21
+ "resample": 2,
22
+ "rescale_factor": 0.00392156862745098,
23
+ "size": {
24
+ "height": 896,
25
+ "width": 672
26
+ }
27
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1d869e03add64b8f6a803fc7873e5c9048aa263a9b899fe834203e4352ddd440
3
+ size 1395352653
special_tokens_map.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "</s>",
4
+ "pad_token": "<pad>",
5
+ "unk_token": "<unk>"
6
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<s>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<pad>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "</s>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "<unk>",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "4": {
36
+ "content": "[START_REF]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ },
43
+ "5": {
44
+ "content": "[END_REF]",
45
+ "lstrip": false,
46
+ "normalized": false,
47
+ "rstrip": false,
48
+ "single_word": false,
49
+ "special": true
50
+ },
51
+ "6": {
52
+ "content": "[IMAGE]",
53
+ "lstrip": false,
54
+ "normalized": false,
55
+ "rstrip": false,
56
+ "single_word": false,
57
+ "special": true
58
+ },
59
+ "7": {
60
+ "content": "<fragments>",
61
+ "lstrip": false,
62
+ "normalized": false,
63
+ "rstrip": false,
64
+ "single_word": false,
65
+ "special": true
66
+ },
67
+ "8": {
68
+ "content": "</fragments>",
69
+ "lstrip": false,
70
+ "normalized": false,
71
+ "rstrip": false,
72
+ "single_word": false,
73
+ "special": true
74
+ },
75
+ "9": {
76
+ "content": "<work>",
77
+ "lstrip": false,
78
+ "normalized": false,
79
+ "rstrip": false,
80
+ "single_word": false,
81
+ "special": true
82
+ },
83
+ "10": {
84
+ "content": "</work>",
85
+ "lstrip": false,
86
+ "normalized": false,
87
+ "rstrip": false,
88
+ "single_word": false,
89
+ "special": true
90
+ },
91
+ "11": {
92
+ "content": "[START_SUP]",
93
+ "lstrip": false,
94
+ "normalized": false,
95
+ "rstrip": false,
96
+ "single_word": false,
97
+ "special": true
98
+ },
99
+ "12": {
100
+ "content": "[END_SUP]",
101
+ "lstrip": false,
102
+ "normalized": false,
103
+ "rstrip": false,
104
+ "single_word": false,
105
+ "special": true
106
+ },
107
+ "13": {
108
+ "content": "[START_SUB]",
109
+ "lstrip": false,
110
+ "normalized": false,
111
+ "rstrip": false,
112
+ "single_word": false,
113
+ "special": true
114
+ },
115
+ "14": {
116
+ "content": "[END_SUB]",
117
+ "lstrip": false,
118
+ "normalized": false,
119
+ "rstrip": false,
120
+ "single_word": false,
121
+ "special": true
122
+ },
123
+ "15": {
124
+ "content": "[START_DNA]",
125
+ "lstrip": false,
126
+ "normalized": false,
127
+ "rstrip": false,
128
+ "single_word": false,
129
+ "special": true
130
+ },
131
+ "16": {
132
+ "content": "[END_DNA]",
133
+ "lstrip": false,
134
+ "normalized": false,
135
+ "rstrip": false,
136
+ "single_word": false,
137
+ "special": true
138
+ },
139
+ "17": {
140
+ "content": "[START_AMINO]",
141
+ "lstrip": false,
142
+ "normalized": false,
143
+ "rstrip": false,
144
+ "single_word": false,
145
+ "special": true
146
+ },
147
+ "18": {
148
+ "content": "[END_AMINO]",
149
+ "lstrip": false,
150
+ "normalized": false,
151
+ "rstrip": false,
152
+ "single_word": false,
153
+ "special": true
154
+ },
155
+ "19": {
156
+ "content": "[START_SMILES]",
157
+ "lstrip": false,
158
+ "normalized": false,
159
+ "rstrip": false,
160
+ "single_word": false,
161
+ "special": true
162
+ },
163
+ "20": {
164
+ "content": "[END_SMILES]",
165
+ "lstrip": false,
166
+ "normalized": false,
167
+ "rstrip": false,
168
+ "single_word": false,
169
+ "special": true
170
+ },
171
+ "21": {
172
+ "content": "[START_I_SMILES]",
173
+ "lstrip": false,
174
+ "normalized": false,
175
+ "rstrip": false,
176
+ "single_word": false,
177
+ "special": true
178
+ },
179
+ "22": {
180
+ "content": "[END_I_SMILES]",
181
+ "lstrip": false,
182
+ "normalized": false,
183
+ "rstrip": false,
184
+ "single_word": false,
185
+ "special": true
186
+ }
187
+ },
188
+ "additional_special_tokens": [],
189
+ "bos_token": "<s>",
190
+ "clean_up_tokenization_spaces": false,
191
+ "eos_token": "</s>",
192
+ "max_length": 4096,
193
+ "model_max_length": 4096,
194
+ "pad_to_multiple_of": null,
195
+ "pad_token": "<pad>",
196
+ "pad_token_type_id": 0,
197
+ "padding_side": "right",
198
+ "processor_class": "NougatProcessor",
199
+ "stride": 0,
200
+ "tokenizer_class": "NougatTokenizer",
201
+ "truncation_side": "right",
202
+ "truncation_strategy": "longest_first",
203
+ "unk_token": "<unk>",
204
+ "vocab_file": null
205
+ }