mineru 2.5.4__py3-none-any.whl → 2.6.1__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.
Files changed (103) hide show
  1. mineru/backend/pipeline/model_init.py +25 -3
  2. mineru/backend/pipeline/model_json_to_middle_json.py +2 -2
  3. mineru/backend/pipeline/model_list.py +0 -1
  4. mineru/backend/utils.py +24 -0
  5. mineru/backend/vlm/model_output_to_middle_json.py +2 -2
  6. mineru/backend/vlm/{custom_logits_processors.py → utils.py} +36 -2
  7. mineru/backend/vlm/vlm_analyze.py +45 -50
  8. mineru/backend/vlm/vlm_magic_model.py +155 -1
  9. mineru/cli/common.py +25 -22
  10. mineru/cli/fast_api.py +2 -8
  11. mineru/cli/gradio_app.py +96 -9
  12. mineru/cli/models_download.py +1 -0
  13. mineru/model/mfr/pp_formulanet_plus_m/predict_formula.py +152 -0
  14. mineru/model/mfr/pp_formulanet_plus_m/processors.py +657 -0
  15. mineru/model/mfr/unimernet/unimernet_hf/modeling_unimernet.py +1 -326
  16. mineru/model/mfr/utils.py +338 -0
  17. mineru/model/ocr/paddleocr2pytorch/pytorch_paddle.py +103 -16
  18. mineru/model/table/rec/unet_table/main.py +1 -1
  19. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/data/imaug/operators.py +5 -5
  20. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/__init__.py +2 -1
  21. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/rec_lcnetv3.py +7 -7
  22. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/rec_pphgnetv2.py +2 -2
  23. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/heads/__init__.py +2 -0
  24. mineru/model/utils/pytorchocr/modeling/heads/rec_ppformulanet_head.py +1383 -0
  25. mineru/model/utils/pytorchocr/modeling/heads/rec_unimernet_head.py +2631 -0
  26. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/postprocess/rec_postprocess.py +25 -28
  27. mineru/model/utils/pytorchocr/utils/__init__.py +0 -0
  28. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/arch_config.yaml +130 -0
  29. mineru/model/utils/pytorchocr/utils/resources/dict/ppocrv5_arabic_dict.txt +747 -0
  30. mineru/model/utils/pytorchocr/utils/resources/dict/ppocrv5_cyrillic_dict.txt +850 -0
  31. mineru/model/utils/pytorchocr/utils/resources/dict/ppocrv5_devanagari_dict.txt +568 -0
  32. mineru/model/utils/pytorchocr/utils/resources/dict/ppocrv5_ta_dict.txt +513 -0
  33. mineru/model/utils/pytorchocr/utils/resources/dict/ppocrv5_te_dict.txt +540 -0
  34. mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/models_config.yml +15 -15
  35. mineru/model/utils/pytorchocr/utils/resources/pp_formulanet_arch_config.yaml +24 -0
  36. mineru/model/utils/tools/infer/__init__.py +1 -0
  37. mineru/model/{ocr/paddleocr2pytorch → utils}/tools/infer/predict_det.py +6 -3
  38. mineru/model/{ocr/paddleocr2pytorch → utils}/tools/infer/predict_rec.py +16 -25
  39. mineru/model/vlm_vllm_model/server.py +7 -2
  40. mineru/resources/header.html +2 -2
  41. mineru/utils/enum_class.py +1 -0
  42. mineru/utils/llm_aided.py +4 -2
  43. mineru/utils/ocr_utils.py +16 -0
  44. mineru/utils/table_merge.py +102 -13
  45. mineru/version.py +1 -1
  46. {mineru-2.5.4.dist-info → mineru-2.6.1.dist-info}/METADATA +32 -8
  47. mineru-2.6.1.dist-info/RECORD +195 -0
  48. mineru-2.5.4.dist-info/RECORD +0 -181
  49. /mineru/model/{ocr/paddleocr2pytorch/pytorchocr → mfr/pp_formulanet_plus_m}/__init__.py +0 -0
  50. /mineru/model/{ocr/paddleocr2pytorch/tools/infer → utils}/__init__.py +0 -0
  51. /mineru/model/{ocr/paddleocr2pytorch/pytorchocr/modeling → utils/pytorchocr}/__init__.py +0 -0
  52. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/base_ocr_v20.py +0 -0
  53. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/data/__init__.py +0 -0
  54. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/data/imaug/__init__.py +0 -0
  55. /mineru/model/{ocr/paddleocr2pytorch/pytorchocr/utils → utils/pytorchocr/modeling}/__init__.py +0 -0
  56. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/architectures/__init__.py +0 -0
  57. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/architectures/base_model.py +0 -0
  58. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/det_mobilenet_v3.py +0 -0
  59. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/rec_donut_swin.py +0 -0
  60. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/rec_hgnet.py +0 -0
  61. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/rec_mobilenet_v3.py +0 -0
  62. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/rec_mv1_enhance.py +0 -0
  63. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/backbones/rec_svtrnet.py +0 -0
  64. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/common.py +0 -0
  65. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/heads/cls_head.py +0 -0
  66. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/heads/det_db_head.py +0 -0
  67. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/heads/rec_ctc_head.py +0 -0
  68. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/heads/rec_multi_head.py +0 -0
  69. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/necks/__init__.py +0 -0
  70. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/necks/db_fpn.py +0 -0
  71. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/necks/intracl.py +0 -0
  72. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/modeling/necks/rnn.py +0 -0
  73. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/postprocess/__init__.py +0 -0
  74. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/postprocess/cls_postprocess.py +0 -0
  75. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/postprocess/db_postprocess.py +0 -0
  76. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/arabic_dict.txt +0 -0
  77. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/chinese_cht_dict.txt +0 -0
  78. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/cyrillic_dict.txt +0 -0
  79. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/devanagari_dict.txt +0 -0
  80. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/en_dict.txt +0 -0
  81. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/japan_dict.txt +0 -0
  82. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ka_dict.txt +0 -0
  83. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/korean_dict.txt +0 -0
  84. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/latin_dict.txt +0 -0
  85. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocr_keys_v1.txt +0 -0
  86. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv4_doc_dict.txt +0 -0
  87. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv5_dict.txt +0 -0
  88. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv5_el_dict.txt +0 -0
  89. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv5_en_dict.txt +0 -0
  90. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv5_eslav_dict.txt +0 -0
  91. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv5_korean_dict.txt +0 -0
  92. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv5_latin_dict.txt +0 -0
  93. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ppocrv5_th_dict.txt +0 -0
  94. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/ta_dict.txt +0 -0
  95. /mineru/model/{ocr/paddleocr2pytorch → utils}/pytorchocr/utils/resources/dict/te_dict.txt +0 -0
  96. /mineru/model/{ocr/paddleocr2pytorch → utils}/tools/__init__.py +0 -0
  97. /mineru/model/{ocr/paddleocr2pytorch → utils}/tools/infer/predict_cls.py +0 -0
  98. /mineru/model/{ocr/paddleocr2pytorch → utils}/tools/infer/predict_system.py +0 -0
  99. /mineru/model/{ocr/paddleocr2pytorch → utils}/tools/infer/pytorchocr_utility.py +0 -0
  100. {mineru-2.5.4.dist-info → mineru-2.6.1.dist-info}/WHEEL +0 -0
  101. {mineru-2.5.4.dist-info → mineru-2.6.1.dist-info}/entry_points.txt +0 -0
  102. {mineru-2.5.4.dist-info → mineru-2.6.1.dist-info}/licenses/LICENSE.md +0 -0
  103. {mineru-2.5.4.dist-info → mineru-2.6.1.dist-info}/top_level.txt +0 -0
mineru/cli/gradio_app.py CHANGED
@@ -134,20 +134,107 @@ with open(header_path, 'r') as header_file:
134
134
 
135
135
 
136
136
  latin_lang = [
137
- 'af', 'az', 'bs', 'cs', 'cy', 'da', 'de', 'es', 'et', 'fr', 'ga', 'hr', # noqa: E126
138
- 'hu', 'id', 'is', 'it', 'ku', 'la', 'lt', 'lv', 'mi', 'ms', 'mt', 'nl',
139
- 'no', 'oc', 'pi', 'pl', 'pt', 'ro', 'rs_latin', 'sk', 'sl', 'sq', 'sv',
140
- 'sw', 'tl', 'tr', 'uz', 'vi', 'french', 'german'
137
+ "af",
138
+ "az",
139
+ "bs",
140
+ "cs",
141
+ "cy",
142
+ "da",
143
+ "de",
144
+ "es",
145
+ "et",
146
+ "fr",
147
+ "ga",
148
+ "hr",
149
+ "hu",
150
+ "id",
151
+ "is",
152
+ "it",
153
+ "ku",
154
+ "la",
155
+ "lt",
156
+ "lv",
157
+ "mi",
158
+ "ms",
159
+ "mt",
160
+ "nl",
161
+ "no",
162
+ "oc",
163
+ "pi",
164
+ "pl",
165
+ "pt",
166
+ "ro",
167
+ "rs_latin",
168
+ "sk",
169
+ "sl",
170
+ "sq",
171
+ "sv",
172
+ "sw",
173
+ "tl",
174
+ "tr",
175
+ "uz",
176
+ "vi",
177
+ "french",
178
+ "german",
179
+ "fi",
180
+ "eu",
181
+ "gl",
182
+ "lb",
183
+ "rm",
184
+ "ca",
185
+ "qu",
141
186
  ]
142
- arabic_lang = ['ar', 'fa', 'ug', 'ur']
187
+ arabic_lang = ["ar", "fa", "ug", "ur", "ps", "ku", "sd", "bal"]
143
188
  cyrillic_lang = [
144
- 'rs_cyrillic', 'bg', 'mn', 'abq', 'ady', 'kbd', 'ava', # noqa: E126
145
- 'dar', 'inh', 'che', 'lbe', 'lez', 'tab'
189
+ "ru",
190
+ "rs_cyrillic",
191
+ "be",
192
+ "bg",
193
+ "uk",
194
+ "mn",
195
+ "abq",
196
+ "ady",
197
+ "kbd",
198
+ "ava",
199
+ "dar",
200
+ "inh",
201
+ "che",
202
+ "lbe",
203
+ "lez",
204
+ "tab",
205
+ "kk",
206
+ "ky",
207
+ "tg",
208
+ "mk",
209
+ "tt",
210
+ "cv",
211
+ "ba",
212
+ "mhr",
213
+ "mo",
214
+ "udm",
215
+ "kv",
216
+ "os",
217
+ "bua",
218
+ "xal",
219
+ "tyv",
220
+ "sah",
221
+ "kaa",
146
222
  ]
147
223
  east_slavic_lang = ["ru", "be", "uk"]
148
224
  devanagari_lang = [
149
- 'hi', 'mr', 'ne', 'bh', 'mai', 'ang', 'bho', 'mah', 'sck', 'new', 'gom', # noqa: E126
150
- 'sa', 'bgc'
225
+ "hi",
226
+ "mr",
227
+ "ne",
228
+ "bh",
229
+ "mai",
230
+ "ang",
231
+ "bho",
232
+ "mah",
233
+ "sck",
234
+ "new",
235
+ "gom",
236
+ "sa",
237
+ "bgc",
151
238
  ]
152
239
  other_lang = ['ch', 'ch_lite', 'ch_server', 'en', 'korean', 'japan', 'chinese_cht', 'ta', 'te', 'ka', "el", "th"]
153
240
  add_lang = ['latin', 'arabic', 'east_slavic', 'cyrillic', 'devanagari']
@@ -70,6 +70,7 @@ def download_pipeline_models():
70
70
  ModelPath.unet_structure,
71
71
  ModelPath.paddle_table_cls,
72
72
  ModelPath.paddle_orientation_classification,
73
+ ModelPath.pp_formulanet_plus_m,
73
74
  ]
74
75
  download_finish_path = ""
75
76
  for model_path in model_paths:
@@ -0,0 +1,152 @@
1
+ import os
2
+ import torch
3
+ import yaml
4
+ from pathlib import Path
5
+
6
+ from loguru import logger
7
+ from tqdm import tqdm
8
+ from mineru.model.utils.tools.infer import pytorchocr_utility
9
+ from mineru.model.utils.pytorchocr.base_ocr_v20 import BaseOCRV20
10
+ from .processors import (
11
+ UniMERNetImgDecode,
12
+ UniMERNetTestTransform,
13
+ LatexImageFormat,
14
+ ToBatch,
15
+ UniMERNetDecode,
16
+ )
17
+
18
+
19
+ class FormulaRecognizer(BaseOCRV20):
20
+ def __init__(
21
+ self,
22
+ weight_dir,
23
+ device="cpu",
24
+ ):
25
+ self.weights_path = os.path.join(
26
+ weight_dir,
27
+ "PP-FormulaNet_plus-M.pth",
28
+ )
29
+ self.yaml_path = os.path.join(
30
+ Path(__file__).parent.parent.parent,
31
+ "utils",
32
+ "pytorchocr",
33
+ "utils",
34
+ "resources",
35
+ "pp_formulanet_arch_config.yaml"
36
+ )
37
+ self.infer_yaml_path = os.path.join(
38
+ weight_dir,
39
+ "PP-FormulaNet_plus-M_inference.yml",
40
+ )
41
+
42
+ network_config = pytorchocr_utility.AnalysisConfig(
43
+ self.weights_path, self.yaml_path
44
+ )
45
+ weights = self.read_pytorch_weights(self.weights_path)
46
+
47
+ super(FormulaRecognizer, self).__init__(network_config)
48
+
49
+ self.load_state_dict(weights)
50
+ self.device = torch.device(device) if isinstance(device, str) else device
51
+ self.net.to(self.device)
52
+ self.net.eval()
53
+
54
+ with open(self.infer_yaml_path, "r", encoding="utf-8") as yaml_file:
55
+ data = yaml.load(yaml_file, Loader=yaml.FullLoader)
56
+
57
+ self.pre_tfs = {
58
+ "UniMERNetImgDecode": UniMERNetImgDecode(input_size=(384, 384)),
59
+ "UniMERNetTestTransform": UniMERNetTestTransform(),
60
+ "LatexImageFormat": LatexImageFormat(),
61
+ "ToBatch": ToBatch(),
62
+ }
63
+
64
+ self.post_op = UniMERNetDecode(
65
+ character_list=data["PostProcess"]["character_dict"]
66
+ )
67
+
68
+ def predict(self, img_list, batch_size: int = 64):
69
+ # Reduce batch size by 50% to avoid potential memory issues during inference.
70
+ batch_size = int(0.5 * batch_size)
71
+ batch_imgs = self.pre_tfs["UniMERNetImgDecode"](imgs=img_list)
72
+ batch_imgs = self.pre_tfs["UniMERNetTestTransform"](imgs=batch_imgs)
73
+ batch_imgs = self.pre_tfs["LatexImageFormat"](imgs=batch_imgs)
74
+ inp = self.pre_tfs["ToBatch"](imgs=batch_imgs)
75
+ inp = torch.from_numpy(inp[0])
76
+ inp = inp.to(self.device)
77
+ rec_formula = []
78
+ with torch.no_grad():
79
+ with tqdm(total=len(inp), desc="MFR Predict") as pbar:
80
+ for index in range(0, len(inp), batch_size):
81
+ batch_data = inp[index: index + batch_size]
82
+ # with torch.amp.autocast(device_type=self.device.type):
83
+ # batch_preds = [self.net(batch_data)]
84
+ batch_preds = [self.net(batch_data)]
85
+ batch_preds = [p.reshape([-1]) for p in batch_preds[0]]
86
+ batch_preds = [bp.cpu().numpy() for bp in batch_preds]
87
+ rec_formula += self.post_op(batch_preds)
88
+ pbar.update(len(batch_preds))
89
+ return rec_formula
90
+
91
+ def batch_predict(
92
+ self, images_mfd_res: list, images: list, batch_size: int = 64
93
+ ) -> list:
94
+ images_formula_list = []
95
+ mf_image_list = []
96
+ backfill_list = []
97
+ image_info = [] # Store (area, original_index, image) tuples
98
+
99
+ # Collect images with their original indices
100
+ for image_index in range(len(images_mfd_res)):
101
+ mfd_res = images_mfd_res[image_index]
102
+ image = images[image_index]
103
+ formula_list = []
104
+
105
+ for idx, (xyxy, conf, cla) in enumerate(
106
+ zip(mfd_res.boxes.xyxy, mfd_res.boxes.conf, mfd_res.boxes.cls)
107
+ ):
108
+ xmin, ymin, xmax, ymax = [int(p.item()) for p in xyxy]
109
+ new_item = {
110
+ "category_id": 13 + int(cla.item()),
111
+ "poly": [xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax],
112
+ "score": round(float(conf.item()), 2),
113
+ "latex": "",
114
+ }
115
+ formula_list.append(new_item)
116
+ bbox_img = image[ymin:ymax, xmin:xmax]
117
+ area = (xmax - xmin) * (ymax - ymin)
118
+
119
+ curr_idx = len(mf_image_list)
120
+ image_info.append((area, curr_idx, bbox_img))
121
+ mf_image_list.append(bbox_img)
122
+
123
+ images_formula_list.append(formula_list)
124
+ backfill_list += formula_list
125
+
126
+ # Stable sort by area
127
+ image_info.sort(key=lambda x: x[0]) # sort by area
128
+ sorted_indices = [x[1] for x in image_info]
129
+ sorted_images = [x[2] for x in image_info]
130
+
131
+ # Create mapping for results
132
+ index_mapping = {
133
+ new_idx: old_idx for new_idx, old_idx in enumerate(sorted_indices)
134
+ }
135
+
136
+ if len(sorted_images) > 0:
137
+ # 进行预测
138
+ batch_size = min(batch_size, max(1, 2 ** (len(sorted_images).bit_length() - 1))) if sorted_images else 1
139
+ rec_formula = self.predict(sorted_images, batch_size)
140
+ else:
141
+ rec_formula = []
142
+
143
+ # Restore original order
144
+ unsorted_results = [""] * len(rec_formula)
145
+ for new_idx, latex in enumerate(rec_formula):
146
+ original_idx = index_mapping[new_idx]
147
+ unsorted_results[original_idx] = latex
148
+
149
+ for res, latex in zip(backfill_list, unsorted_results):
150
+ res["latex"] = latex
151
+
152
+ return images_formula_list