-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update: change pdf text parser to pymupdf4llm #139
base: main
Are you sure you want to change the base?
Conversation
@tungsten106 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
maybe this can patch #142 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to let the user choose the engine rather than replacing it
I agree. There are pros and cons to each. The main thing is to allow a common interface. Can you propose an interface for this? One option is to just call register_page_converter externally, and the precedence logic would give precedence to whichever converter is registered later. (see here for example: https://github.com/microsoft/markitdown/blob/925c4499f72757abcf6cb521ee10e4844967af3d/src/markitdown/_markitdown.py#L1269C1-L1287C1) Another option would be to see which dependencies are installed (though this is more opaque) |
…or better user instruction. Add examples for PdfConverter.convert() calling.
I have added a parameter source = "https://arxiv.org/pdf/2308.08155v2.pdf"
markitdown.convert(source, pdf_engine="pymupdf4llm") # use pymupdf4llm
markitdown.convert(source, pdf_engine="pdfminer") # use pdfminer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add test for pymupdf4llm
?
src/markitdown/_markitdown.py
Outdated
else: | ||
return None # unknown method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part doesn't return anything. Could you update it? Maybe add a warning message?
""" | ||
|
||
def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]: | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think pdf_engine
should be a named parameter in the function instead of being accessed via kwargs
. This provides more clarity and ensures better handling of default values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for you suggestion. I have added it to a named parameters ( I was meant to align with other converters' parameter definition), and added the exception case when pdf_engine
is not valid. The new test cases could be seen on my new commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is optional, but adding one more named parameter could make this more customizable, like:
def convert(self, local_path, engine: Literal['pdfminer', 'pymupdf4llm']='pdfminer', engine_kwargs=None, **kwargs) -> Union[None, DocumentConverterResult]:
… cases for pdf. Raised exceptions when pdf_engine is not valid.
import sys | ||
sys.path.insert(0, "/home/yxl/Projects/markitdown/src") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think don't need this. Let me know if you need help setting up the test!
elif pdf_engine == "pymupdf4llm": | ||
text_content = pymupdf4llm.to_markdown(local_path, show_progress=False) | ||
else: | ||
# return None # unknown method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this
""" | ||
|
||
def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]: | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is optional, but adding one more named parameter could make this more customizable, like:
def convert(self, local_path, engine: Literal['pdfminer', 'pymupdf4llm']='pdfminer', engine_kwargs=None, **kwargs) -> Union[None, DocumentConverterResult]:
# return None # unknown method | ||
raise FileConversionException("'pdf_engine' not valid. Please choose between ['pdfminer', 'pymupdf4llm'].") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest checking the engine first. You could use _engines
to define the allowed engines
_engines: Mapping[str, Any] = {
"pdfminer": pdfminer,
"pymupdf4llm": pymupdf4llm,
}
###
if engine is not None and engine not in self._engines:
raise
Using
pymupdf4llm
instead ofpdfminer
to parse pdf contents into markdown formats, as suggested by #131.Pros and Cons:
pdfminer
extract texts only, generated files have no heading, titles, etc.pymupdf4llm
, however, could perform a nice markdown featrues including different levels of heading, code blocks and images (could be saved to specific path, but not included in this commit)pymupdf4llm
may easily create lines of digits which belongs to plots, and create non-existing tables. This is a common problem to most PDF parsers, except those using ocr models (such as markers, MinerU).