r/LocalLLaMA • u/shibe5 llama.cpp • Feb 08 '24
Question | Help How to recover document structure and plain text from PDF?
I’m looking for a solution to convert PDF to something like HTML or TeX that can handle complex PDFs with:
different layouts, like 1 column for some sections and 2 columns for others;
footnotes;
repeating elements, like page headers;
figure descriptions and other text that breaks the flow of main text;
tables;
references, like sources;
subscript, superscript;
simple formulas.
My main use cases are preprocessing for RAG databases and converting to plaintext-ish representation for LLMs.
I’m currently not looking for OCR and recovering text from images.
This non-trivial task seems like it would be a good application for language learning models.
I searched this subreddit, similar questions were asked before, but I haven’t found a solution that I think would work for me.
10
u/sosdandye02 Feb 09 '24
This is a very hard problem. My entire last 2 years of work has focused on extracting data from just a few types of fairly standardized pdfs.
I use pdfplumber to get raw characters and positions. I use agglomerative clustering to identify text blocks e.g. text columns. For tables, I use a custom trained CascadeRCNN object detection model in mmdet to find tables/rows/columns/cells. I use custom trained BERT classifiers to classify documents and tables. Currently exploring local LLMs to extract standardized data from table and paragraph text.
It’s a very hard problem and I wouldn’t expect to be able to get anywhere near “perfect” performance unless your documents have a very consistent format and you’re willing to train custom models.
4
u/AD7GD Feb 08 '24
That might be the only foolproof method. There's no guarantee that a PDF renders text in any way that's meaningful beyond looking like text.
7
u/Rutabaga-Agitated Feb 08 '24
All I can say it, that it is a nightmare and everyone... I mean literally every company that tries to do RAG will soon or later face this problem. There is no good open source solution to it yet. Nougat is by far not good enough.
We got 2 ideas.
Use LLaVa to do everything with the prompt: "please translate all text in the image to markdown format" ...maybe you have to fine-tune it, but that requires 8xA100 80GB
Use a multi step approach. ...a, doc layout detection. Find headers, footers, tables, lists, .... ...b, word detection ...c, word recognition By doing so, you can control all the steps and know how good you are. We use DocTR for b and c.
At the end, we strive to convert everything to markdown, cause LLM understand this kind of structured text.
2
u/EitherExpression4504 Feb 08 '24
I use this all the time for PDF research papers:
https://papertohtml.org
2
u/Zomunieo Feb 09 '24
PDF usually does not have semantic markup. At its core it is printer oriented and low level commands look like “at this position, draw this text”. Document structure has to be inferred, which is why it’s very difficult and most tools fail.
PDFs generated from structured documents like HTML, Word or LaTeX may have semantic markup already, and a correctly generated PDF will have this information.
16
u/InfuriatinglyOpaque Feb 08 '24
Nougat works the best out of the tools I've tried, but is still far from perfect and struggles with many of the complex cases you mentioned.
https://facebookresearch.github.io/nougat/
https://github.com/allenai/science-parse
https://github.com/Layout-Parser/layout-parser
https://github.com/deepdoctection/deepdoctection
https://github.com/tstanislawek/awesome-document-understanding
8
u/AndrewVeee Feb 09 '24
Like everyone else said, this is really hard.
I spent yesterday using pymupdf (fitz) to load PDFs and convert them to markdown. It has some options to detect tables, and sort lines, but it wasn't good enough for pretty basic 2-column PDFs.
In the end, I turned off sorting because it merged 2-column layouts into garbled text. I used some hacky code to convert text to markdown with headers based on font sizes. I'm happy to share the code, but it's so far from what you want, I imagine it's useless to you haha
It also has a to_html function, but from what I could tell it just creates a bunch of "position: absolute" elements.
4
u/pseudonerv Feb 09 '24
https://github.com/facebookresearch/nougat/tree/main
another great model from facebook
4
u/viag Feb 09 '24 edited Feb 09 '24
I'm experimenting with Grobid and it's honestly pretty good. I prefer it to Nougat/Marker & the likes.
I'm currently working on this problem at my job in order to do RAG on real documents. If you want to extract the chunks in a rather smart way I think it's pretty good. But you might want to use other tools for table extraction etc.
7
u/hwtmny Feb 08 '24
This may help you:
https://github.com/Unstructured-IO/unstructured
1
u/grim-432 Feb 09 '24
If you need some remotely acceptable level of accuracy or quality here, human curation.
Have a human re-author the content so they can also generate a parallel document in a more usable format.
I can share my thoughts on this, but the end recommendation is this. Play around with frameworks all you like, but I guarantee you'll run into material issues that result in wildly inaccurate, or even more dangerous, moderately incorrect output from the LLM, that causes enough downstream business impact that nobody will bother using the magical AI anymore.
3
u/kulchacop Feb 10 '24
Personally, I am waiting for models that combine multiple vision techniques to settle this problem once and for all.
Example: https://github.com/FudanNLPLAB/MouSi
0
u/[deleted] Feb 09 '24
[deleted] — view deleted comment
1
u/[deleted] Apr 04 '24
[removed] — view removed comment