11import json
2+ import os
23import shutil
34from collections .abc import AsyncGenerator , Callable , Iterable
45from functools import partial
1920 Result ,
2021 Status ,
2122)
23+ from objects import Device
2224
2325from .constants import ARTIFACTS , DEFAULT_MD_PAGE_SEP
24- from .utils import path_to_artifacts_dirname
26+ from .utils import path_to_artifacts_dirname , reset_env
2527
2628_MINER_U_CONVERSION_ERRORS = tuple ()
2729MDMakeFunction = Callable [[list , str , str ], str | None ]
2830
2931
3032@Pipeline .register (PipelineType .MINER_U )
3133class MinerUPipeline (Pipeline ):
32- def __init__ (self , config : MinerUConfig , language : str ):
34+ def __init__ (
35+ self , config : MinerUConfig , language : str , * , device : Device = Device .CPU
36+ ):
37+ super ().__init__ (device )
3338 self ._config = config
3439 self ._language = language
3540 self ._md_make_fn = _parse_md_make_fn (config .backend )
@@ -39,36 +44,43 @@ async def extract_content(
3944 ) -> AsyncGenerator [Result , None ]:
4045 from mineru .cli .common import aio_do_parse # noqa: PLC0415
4146
42- docs = list (docs )
43- # TODO: exclude files which are not pdf and return an error
44- pdfs_bytes = [d .path .read_bytes () for d in docs ]
45- pdfs_names = [d .path .name for d in docs ]
46- p_lang_list = [self ._language for _ in pdfs_names ]
47- # TODO: we should only process valid PDFs
48- with TemporaryDirectory (prefix = "mineru-" ) as workdir :
49- workdir = Path (workdir ) # noqa: PLW2901
50- await aio_do_parse (
51- output_dir = workdir ,
52- pdf_file_names = pdfs_names ,
53- pdf_bytes_list = pdfs_bytes ,
54- p_lang_list = p_lang_list ,
55- ** self ._config .as_parse_kwargs (),
56- )
57- res_paths = [
58- _revert_mineru_output (workdir , pdf_filename = p ) for p in pdfs_names
59- ]
60- for doc , res_path in zip (docs , res_paths , strict = True ):
61- yield _process_doc (
62- doc ,
63- md_make_fn = self ._md_make_fn ,
64- res_path = res_path ,
65- output_format = output_format ,
66- output_path = output_path ,
47+ with reset_env ():
48+ os .environ ["MINERU_DEVICE_MODE" ] = self ._device
49+ docs = list (docs )
50+ # TODO: exclude files which are not pdf and return an error
51+ pdfs_bytes = [d .path .read_bytes () for d in docs ]
52+ pdfs_names = [d .path .name for d in docs ]
53+ p_lang_list = [self ._language for _ in pdfs_names ]
54+ # TODO: we should only process valid PDFs
55+ with TemporaryDirectory (prefix = "mineru-" ) as workdir :
56+ workdir = Path (workdir ) # noqa: PLW2901
57+ await aio_do_parse (
58+ output_dir = workdir ,
59+ pdf_file_names = pdfs_names ,
60+ pdf_bytes_list = pdfs_bytes ,
61+ p_lang_list = p_lang_list ,
62+ ** self ._config .as_parse_kwargs (),
6763 )
64+ res_paths = [
65+ _revert_mineru_output (workdir , pdf_filename = p ) for p in pdfs_names
66+ ]
67+ for doc , res_path in zip (docs , res_paths , strict = True ):
68+ yield _process_doc (
69+ doc ,
70+ md_make_fn = self ._md_make_fn ,
71+ res_path = res_path ,
72+ output_format = output_format ,
73+ output_path = output_path ,
74+ )
6875
6976 @classmethod
70- def _from_config (cls , config : MinerUPipelineConfig ) -> Self :
71- return cls (config .config , language = config .language )
77+ def _from_config (
78+ cls ,
79+ config : MinerUPipelineConfig ,
80+ * ,
81+ device : Device = Device .CPU ,
82+ ) -> Self :
83+ return cls (config .config , language = config .language , device = device )
7284
7385
7486def _revert_mineru_output (output_dir : Path , * , pdf_filename : str ) -> Path :
0 commit comments