Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pdf-miner
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Qin Kaijie
pdf-miner
Commits
872cd73f
Commit
872cd73f
authored
Mar 28, 2024
by
赵小蒙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pipeline重构
parent
7fcbae01
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
188 additions
and
177 deletions
+188
-177
pdf_parse_by_ocr.py
magic_pdf/pdf_parse_by_ocr.py
+2
-3
pipeline.py
magic_pdf/pipeline.py
+4
-174
pipeline_ocr.py
magic_pdf/pipeline_ocr.py
+182
-0
No files found.
magic_pdf/pdf_parse_by_ocr.py
View file @
872cd73f
...
@@ -36,8 +36,7 @@ from magic_pdf.pre_proc.remove_bbox_overlap import remove_overlap_between_bbox
...
@@ -36,8 +36,7 @@ from magic_pdf.pre_proc.remove_bbox_overlap import remove_overlap_between_bbox
def
parse_pdf_by_ocr
(
def
parse_pdf_by_ocr
(
pdf_path
,
pdf_bytes
,
s3_pdf_profile
,
pdf_model_output
,
pdf_model_output
,
save_path
,
save_path
,
book_name
,
book_name
,
...
@@ -47,7 +46,7 @@ def parse_pdf_by_ocr(
...
@@ -47,7 +46,7 @@ def parse_pdf_by_ocr(
end_page_id
=
None
,
end_page_id
=
None
,
debug_mode
=
False
,
debug_mode
=
False
,
):
):
pdf_bytes
=
read_file
(
pdf_path
,
s3_pdf_profile
)
save_tmp_path
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"../.."
,
"tmp"
,
"unittest"
)
save_tmp_path
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"../.."
,
"tmp"
,
"unittest"
)
book_name
=
sanitize_filename
(
book_name
)
book_name
=
sanitize_filename
(
book_name
)
md_bookname_save_path
=
""
md_bookname_save_path
=
""
...
...
magic_pdf/pipeline.py
View file @
872cd73f
...
@@ -490,8 +490,11 @@ def ocr_parse_pdf(jso: dict, start_page_id=0, debug_mode=False) -> dict:
...
@@ -490,8 +490,11 @@ def ocr_parse_pdf(jso: dict, start_page_id=0, debug_mode=False) -> dict:
def
ocr_parse_pdf_core
(
jso
:
dict
,
start_page_id
=
0
,
debug_mode
=
False
)
->
dict
:
def
ocr_parse_pdf_core
(
jso
:
dict
,
start_page_id
=
0
,
debug_mode
=
False
)
->
dict
:
s3_pdf_path
=
jso
.
get
(
"file_location"
)
s3_pdf_path
=
jso
.
get
(
"file_location"
)
s3_config
=
get_s3_config
(
s3_pdf_path
)
s3_config
=
get_s3_config
(
s3_pdf_path
)
pdf_bytes
=
read_file
(
s3_pdf_path
,
s3_config
)
model_output_json_list
=
jso
.
get
(
"doc_layout_result"
)
model_output_json_list
=
jso
.
get
(
"doc_layout_result"
)
data_source
=
get_data_source
(
jso
)
data_source
=
get_data_source
(
jso
)
file_id
=
jso
.
get
(
"file_id"
)
file_id
=
jso
.
get
(
"file_id"
)
...
@@ -506,8 +509,7 @@ def ocr_parse_pdf_core(jso: dict, start_page_id=0, debug_mode=False) -> dict:
...
@@ -506,8 +509,7 @@ def ocr_parse_pdf_core(jso: dict, start_page_id=0, debug_mode=False) -> dict:
file
=
sys
.
stderr
,
file
=
sys
.
stderr
,
)
)
pdf_info_dict
=
parse_pdf_by_ocr
(
pdf_info_dict
=
parse_pdf_by_ocr
(
s3_pdf_path
,
pdf_bytes
,
s3_config
,
model_output_json_list
,
model_output_json_list
,
save_path
,
save_path
,
book_name
,
book_name
,
...
@@ -531,177 +533,5 @@ def ocr_parse_pdf_core(jso: dict, start_page_id=0, debug_mode=False) -> dict:
...
@@ -531,177 +533,5 @@ def ocr_parse_pdf_core(jso: dict, start_page_id=0, debug_mode=False) -> dict:
return
jso
return
jso
def
ocr_pdf_intermediate_dict_to_markdown
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
markdown_content
=
ocr_mk_mm_markdown
(
pdf_intermediate_dict
)
jso
[
"content"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_markdown_with_para
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
# markdown_content = ocr_mk_mm_markdown_with_para(pdf_intermediate_dict)
markdown_content
=
ocr_mk_nlp_markdown_with_para
(
pdf_intermediate_dict
)
jso
[
"content"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_markdown_with_para_and_pagination
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
markdown_content
=
ocr_mk_mm_markdown_with_para_and_pagination
(
pdf_intermediate_dict
)
jso
[
"content"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
# jso["doc_layout_result"] = ""
jso
[
"pdf_intermediate_dict"
]
=
""
# jso["pdf_meta"] = ""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_markdown_with_para_for_qa
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
markdown_content
=
ocr_mk_mm_markdown_with_para
(
pdf_intermediate_dict
)
jso
[
"content_ocr"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"mid_json_ocr"
]
=
pdf_intermediate_dict
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_standard_format
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
standard_format
=
ocr_mk_mm_standard_format
(
pdf_intermediate_dict
)
jso
[
"content_list"
]
=
standard_format
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},content_list length is {len(standard_format)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_standard_format_with_para
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
standard_format
=
make_standard_format_with_para
(
pdf_intermediate_dict
)
jso
[
"content_list"
]
=
standard_format
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},content_list length is {len(standard_format)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
pass
pass
magic_pdf/pipeline_ocr.py
0 → 100644
View file @
872cd73f
import
sys
from
loguru
import
logger
from
magic_pdf.dict2md.ocr_mkcontent
import
ocr_mk_mm_markdown
,
ocr_mk_nlp_markdown_with_para
,
\
ocr_mk_mm_markdown_with_para_and_pagination
,
ocr_mk_mm_markdown_with_para
,
ocr_mk_mm_standard_format
,
\
make_standard_format_with_para
from
magic_pdf.libs.commons
import
join_path
from
magic_pdf.libs.json_compressor
import
JsonCompressor
from
magic_pdf.spark.base
import
get_data_source
,
exception_handler
def
ocr_pdf_intermediate_dict_to_markdown
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
markdown_content
=
ocr_mk_mm_markdown
(
pdf_intermediate_dict
)
jso
[
"content"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_markdown_with_para
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
# markdown_content = ocr_mk_mm_markdown_with_para(pdf_intermediate_dict)
markdown_content
=
ocr_mk_nlp_markdown_with_para
(
pdf_intermediate_dict
)
jso
[
"content"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_markdown_with_para_and_pagination
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
markdown_content
=
ocr_mk_mm_markdown_with_para_and_pagination
(
pdf_intermediate_dict
)
jso
[
"content"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
# jso["doc_layout_result"] = ""
jso
[
"pdf_intermediate_dict"
]
=
""
# jso["pdf_meta"] = ""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_markdown_with_para_for_qa
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
markdown_content
=
ocr_mk_mm_markdown_with_para
(
pdf_intermediate_dict
)
jso
[
"content_ocr"
]
=
markdown_content
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},markdown content length is {len(markdown_content)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"mid_json_ocr"
]
=
pdf_intermediate_dict
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_standard_format
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
standard_format
=
ocr_mk_mm_standard_format
(
pdf_intermediate_dict
)
jso
[
"content_list"
]
=
standard_format
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},content_list length is {len(standard_format)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
def
ocr_pdf_intermediate_dict_to_standard_format_with_para
(
jso
:
dict
,
debug_mode
=
False
)
->
dict
:
if
debug_mode
:
pass
else
:
# 如果debug没开,则检测是否有needdrop字段
if
jso
.
get
(
"need_drop"
,
False
):
book_name
=
join_path
(
get_data_source
(
jso
),
jso
[
"file_id"
])
logger
.
info
(
f
"book_name is:{book_name} need drop"
,
file
=
sys
.
stderr
)
jso
[
"dropped"
]
=
True
return
jso
try
:
pdf_intermediate_dict
=
jso
[
"pdf_intermediate_dict"
]
# 将 pdf_intermediate_dict 解压
pdf_intermediate_dict
=
JsonCompressor
.
decompress_json
(
pdf_intermediate_dict
)
standard_format
=
make_standard_format_with_para
(
pdf_intermediate_dict
)
jso
[
"content_list"
]
=
standard_format
logger
.
info
(
f
"book_name is:{get_data_source(jso)}/{jso['file_id']},content_list length is {len(standard_format)}"
,
file
=
sys
.
stderr
,
)
# 把无用的信息清空
jso
[
"doc_layout_result"
]
=
""
jso
[
"pdf_intermediate_dict"
]
=
""
jso
[
"pdf_meta"
]
=
""
except
Exception
as
e
:
jso
=
exception_handler
(
jso
,
e
)
return
jso
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment