doc-page-extractor 0.2.1__py3-none-any.whl → 0.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of doc-page-extractor might be problematic. Click here for more details.

@@ -1,76 +0,0 @@
1
- import torch
2
-
3
- from torch import nn
4
- from transformers import AutoModelForVision2Seq, AutoProcessor
5
-
6
-
7
- class Pix2Struct(nn.Module):
8
- def __init__(
9
- self,
10
- model_path='U4R/StructTable-base',
11
- max_new_tokens=1024,
12
- max_time=30,
13
- cache_dir=None,
14
- local_files_only=None,
15
- **kwargs,
16
- ):
17
- super().__init__()
18
- self.model_path = model_path
19
- self.max_new_tokens = max_new_tokens
20
- self.max_generate_time = max_time
21
- self.cache_dir = cache_dir
22
- self.local_files_only = local_files_only
23
-
24
- # init model and image processor from ckpt path
25
- self.init_image_processor(model_path)
26
- self.init_model(model_path)
27
-
28
- self.special_str_list = ['\\midrule', '\\hline']
29
- self.supported_output_format = ['latex']
30
-
31
- def postprocess_latex_code(self, code):
32
- for special_str in self.special_str_list:
33
- code = code.replace(special_str, special_str + ' ')
34
- return code
35
-
36
- def init_model(self, model_path):
37
- self.model = AutoModelForVision2Seq.from_pretrained(
38
- pretrained_model_name_or_path=model_path,
39
- cache_dir=self.cache_dir,
40
- local_files_only=self.local_files_only,
41
- )
42
- self.model.eval()
43
-
44
- def init_image_processor(self, image_processor_path):
45
- self.data_processor = AutoProcessor.from_pretrained(
46
- pretrained_model_name_or_path=image_processor_path,
47
- cache_dir=self.cache_dir,
48
- local_files_only=self.local_files_only,
49
- )
50
-
51
- def forward(self, image, **kwargs):
52
- # process image to tokens
53
- image_tokens = self.data_processor.image_processor(
54
- images=image,
55
- return_tensors='pt',
56
- )
57
-
58
- device = next(self.parameters()).device
59
- for k, v in image_tokens.items():
60
- image_tokens[k] = v.to(device)
61
-
62
- # generate text from image tokens
63
- model_output = self.model.generate(
64
- flattened_patches=image_tokens['flattened_patches'],
65
- attention_mask=image_tokens['attention_mask'],
66
- max_new_tokens=self.max_new_tokens,
67
- max_time=self.max_generate_time,
68
- no_repeat_ngram_size=20,
69
- )
70
-
71
- latex_codes = self.data_processor.batch_decode(model_output, skip_special_tokens=True)
72
- # postprocess
73
- for i, code in enumerate(latex_codes):
74
- latex_codes[i] = self.postprocess_latex_code(code)
75
-
76
- return latex_codes