Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
fix: resolve inaccuracy of drawing layout box caused by paragraphs combination #384 (#574)
Kaiwen Liu58a003177c95
diff --git a/README.md b/README.mdindex 414ebffc..ae009c01 100644--- a/README.md+++ b/README.md@@ -13,6 +13,12 @@[](https://badge.fury.io/py/magic-pdf)[](https://pepy.tech/project/magic-pdf)[](https://pepy.tech/project/magic-pdf)++[](https://huggingface.co/spaces/opendatalab/MinerU)+[](https://www.modelscope.cn/studios/OpenDataLab/MinerU)+[](https://colab.research.google.com/gist/papayalove/b5f4913389e7ff9883c6b687de156e78/mineru_demo.ipynb)+[](#)+<a href="https://trendshift.io/repositories/11174" target="_blank"><img src="https://trendshift.io/api/badge/repositories/11174" alt="opendatalab%2FMinerU | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a><!-- language -->@@ -232,10 +238,16 @@ If your device supports CUDA and meets the GPU requirements of the mainline envi- [Windows 10/11 + GPU](docs/README_Windows_CUDA_Acceleration_en_US.md)- Quick Deployment with Docker> Docker requires a GPU with at least 16GB of VRAM, and all acceleration features are enabled by default.+ >+ > Before running this Docker, you can use the following command to check if your device supports CUDA acceleration on Docker.+ >+ > ```bash+ > docker run --rm --gpus=all nvidia/cuda:12.1.0-base-ubuntu22.04 nvidia-smi+ > ``````bashwget https://github.com/opendatalab/MinerU/raw/master/Dockerfile- docker build -t mineru:0.7.0b1 .- docker run --rm -it --gpus=all mineru:0.7.0b1 /bin/bash+ docker build -t mineru:latest .+ docker run --rm -it --gpus=all mineru:latest /bin/bashmagic-pdf --help```diff --git a/README_zh-CN.md b/README_zh-CN.mdindex 40ae8747..d2005466 100644--- a/README_zh-CN.md+++ b/README_zh-CN.md@@ -13,6 +13,12 @@[](https://badge.fury.io/py/magic-pdf)[](https://pepy.tech/project/magic-pdf)[](https://pepy.tech/project/magic-pdf)++[](https://huggingface.co/spaces/opendatalab/MinerU)+[](https://www.modelscope.cn/studios/OpenDataLab/MinerU)+[](https://colab.research.google.com/gist/papayalove/b5f4913389e7ff9883c6b687de156e78/mineru_demo.ipynb)+[](#)+<a href="https://trendshift.io/repositories/11174" target="_blank"><img src="https://trendshift.io/api/badge/repositories/11174" alt="opendatalab%2FMinerU | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a><!-- language -->@@ -235,10 +241,16 @@ cp magic-pdf.template.json ~/magic-pdf.json- [Windows10/11 + GPU](docs/README_Windows_CUDA_Acceleration_zh_CN.md)- 使用Docker快速部署> Docker 需设备gpu显存大于等于16GB,默认开启所有加速功能+ >+ > 运行本docker前可以通过以下命令检测自己的设备是否支持在docker上使用CUDA加速+ >+ > ```bash+ > docker run --rm --gpus=all nvidia/cuda:12.1.0-base-ubuntu22.04 nvidia-smi+ > ``````bashwget https://github.com/opendatalab/MinerU/raw/master/Dockerfile- docker build -t mineru:0.7.0b1 .- docker run --rm -it --gpus=all mineru:0.7.0b1 /bin/bash+ docker build -t mineru:latest .+ docker run --rm -it --gpus=all mineru:latest /bin/bashmagic-pdf --help```diff --git a/magic_pdf/para/para_split_v2.py b/magic_pdf/para/para_split_v2.pyindex 5c9fa636..0e9ab0cd 100644--- a/magic_pdf/para/para_split_v2.py+++ b/magic_pdf/para/para_split_v2.py@@ -202,7 +202,9 @@ def __valign_lines(blocks, layout_bboxes):min_distance = 3min_sample = 2new_layout_bboxes = []-+ # add bbox_fs for para split calculation+ for block in blocks:+ block["bbox_fs"] = copy.deepcopy(block["bbox"])for layout_box in layout_bboxes:blocks_in_layoutbox = [b for b in blocks ifb["type"] == BlockType.Text and is_in_layout(b['bbox'], layout_box['layout_bbox'])]@@ -251,8 +253,6 @@ def __valign_lines(blocks, layout_bboxes):min([line['bbox'][1] for line in block['lines']]),max([line['bbox'][2] for line in block['lines']]),max([line['bbox'][3] for line in block['lines']])]- else:- block['bbox_fs'] = copy.deepcopy(block['bbox'])"""新计算layout的bbox,因为block的bbox变了。"""layout_x0 = min([block['bbox_fs'][0] for block in blocks_in_layoutbox])layout_y0 = min([block['bbox_fs'][1] for block in blocks_in_layoutbox])diff --git a/projects/README.md b/projects/README.mdindex 39964de6..13d72ce2 100644--- a/projects/README.md+++ b/projects/README.md@@ -1,7 +1,6 @@-# 欢迎来到 MinerU 项目列表+# Welcome to the MinerU Project List-## 项目列表+## Project List-- [llama_index_rag](./llama_index_rag/README.md): 基于 llama_index 构建轻量级 RAG 系统+- [llama_index_rag](./llama_index_rag/README.md): Build a lightweight RAG system based on llama_index-- [web_api](./web_api/README.md): PDF解析的restful api服务diff --git a/projects/README_zh-CN.md b/projects/README_zh-CN.mdnew file mode 100644index 00000000..6e9dc7a3--- /dev/null+++ b/projects/README_zh-CN.md@@ -0,0 +1,5 @@+# 欢迎来到 MinerU 项目列表++## 项目列表++- [llama_index_rag](./llama_index_rag/README_zh-CN.md): 基于 llama_index 构建轻量级 RAG 系统diff --git a/projects/llama_index_rag/README.md b/projects/llama_index_rag/README.mdindex 84c1fc2b..57cd9aeb 100644--- a/projects/llama_index_rag/README.md+++ b/projects/llama_index_rag/README.md@@ -1,4 +1,4 @@-## 安装+## InstallationMinerU@@ -11,7 +11,7 @@ conda activate MinerUpip install .[full] --extra-index-url https://wheels.myhloli.com```-第三方软件+Third-party software```bash# install@@ -26,7 +26,7 @@ pip install accelerate==0.33.0pip uninstall transformer-engine```-## 环境配置+## Environment Configuration```export DASHSCOPE_API_KEY={some_key}@@ -34,12 +34,11 @@ export ES_USER={some_es_user}export ES_PASSWORD={some_es_password}export ES_URL=http://{es_url}:9200```+For instructions on obtaining a DASHSCOPE_API_KEY, refer to [documentation](https://help.aliyun.com/zh/dashscope/opening-service)-DASHSCOPE_API_KEY 开通参考[文档](https://help.aliyun.com/zh/dashscope/opening-service)+## Usage-## 使用--### 导入数据+### Data Ingestion```bashpython data_ingestion.py -p some.pdf # load data from pdf@@ -49,16 +48,16 @@ python data_ingestion.py -p some.pdf # load data from pdfpython data_ingestion.py -p /opt/data/some_pdf_directory/ # load data from multiples pdf which under the directory of {some_pdf_directory}```-### 查询+### Query```bashpython query.py --question '{the_question_you_want_to_ask}'```-## 示例+## Example````bash-# 启动 es 服务+# Start the es servicedocker compose up -dor@@ -66,44 +65,45 @@ ordocker-compose up -d-# 配置环境变量+# Set environment variablesexport ES_USER=elasticexport ES_PASSWORD=llama_indexexport ES_URL=http://127.0.0.1:9200export DASHSCOPE_API_KEY={some_key}-# 导入数据+# Ingest datapython data_ingestion.py example/data/declaration_of_the_rights_of_man_1789.pdf-# 查询问题+# Ask a questionpython query.py -q 'how about the rights of men'## outputs-请基于```内的内容回答问题。"+Please answer the question based on the content within ```:```I. Men are born, and always continue, free and equal in respect of their rights. Civil distinctions, therefore, can be founded only on public utility.```- 我的问题是:how about the rights of men。+ My question is:how about the rights of men。question: how about the rights of menanswer: The statement implies that men are born free and equal in terms of their rights. Civil distinctions should only be based on public utility. However, it does not specify what those rights are. It is up to society and individual countries to determine and protect the specific rights of their citizens.````-## 开发+## Development++`MinerU` provides a `RAG` integration interface, allowing users to specify a single input `pdf` file or a directory. `MinerU` will automatically parse the input files and return an iterable interface for retrieving the data.-`MinerU` 提供了 `RAG` 集成接口,用户可以通过指定输入单个 `pdf` 文件或者某个目录。`MinerU` 会自动解析输入文件并返回可以迭代的接口用于获取数据-### API 接口+### API Interface```pythonfrom magic_pdf.integrations.rag.type import Nodeclass RagPageReader:def get_rel_map(self) -> list[ElementRelation]:- # 获取节点间的关系+ # Retrieve the relationships between nodespass...@@ -115,41 +115,43 @@ class DataReader:passdef get_documents_count(self) -> int:- """获取 pdf 文档数量"""+ """Get the number of pdf documents"""passdef get_document_result(self, idx: int) -> RagDocumentReader | None:- """获取某个 pdf 的解析内容"""+ """Retrieve the parsed content of a specific pdf"""passdef get_document_filename(self, idx: int) -> Path:- """获取某个 pdf 的具体路径"""+ """Retrieve the path of a specific pdf"""pass```-类型定义+Type Definitions```python+class Node(BaseModel):- category_type: CategoryType = Field(description='类别') # 类别- text: str | None = Field(description='文本内容',- default=None)- image_path: str | None = Field(description='图或者表格(表可能用图片形式存储)的存储路径',- default=None)- anno_id: int = Field(description='unique id', default=-1)- latex: str | None = Field(description='公式或表格 latex 解析结果', default=None)- html: str | None = Field(description='表格的 html 解析结果', default=None)+ category_type: CategoryType = Field(description='Category') # Category+ text: str | None = Field(description='Text content', default=None)+ image_path: str | None = Field(description='Path to image or table (table may be stored as an image)', default=None)+ anno_id: int = Field(description='Unique ID', default=-1)+ latex: str | None = Field(description='LaTeX output for equations or tables', default=None)+ html: str | None = Field(description='HTML output for tables', default=None)++```-表格存储形式可能会是 图片、latex、html 三种形式之一。-anno_id 是该 Node 的在全局唯一ID。后续可以用于匹配该 Node 和其他 Node 的关系。节点的关系可以通过方法 `get_rel_map` 获取。用户可以用 `anno_id` 匹配节点之间的关系,并用于构建具备节点的关系的 rag index。+Tables can be stored in one of three formats: image, LaTeX, or HTML.+`anno_id` is a globally unique ID for each Node. It can be used later to match this Node with other Nodes. The relationships between nodes can be retrieved using the `get_rel_map` method. Users can use `anno_id` to link nodes and construct a RAG index that includes node relationships.+-### 节点类型关系矩阵+### Node Relationship Matrix| | image_body | table_body || -------------- | ---------- | ---------- |diff --git a/projects/llama_index_rag/README_zh-CN.md b/projects/llama_index_rag/README_zh-CN.mdnew file mode 100644index 00000000..84c1fc2b--- /dev/null+++ b/projects/llama_index_rag/README_zh-CN.md@@ -0,0 +1,158 @@+## 安装++MinerU++```bash+git clone https://github.com/opendatalab/MinerU.git+cd MinerU++conda create -n MinerU python=3.10+conda activate MinerU+pip install .[full] --extra-index-url https://wheels.myhloli.com+```++第三方软件++```bash+# install+pip install llama-index-vector-stores-elasticsearch==0.2.0+pip install llama-index-embeddings-dashscope==0.2.0+pip install llama-index-core==0.10.68+pip install einops==0.7.0+pip install transformers-stream-generator==0.0.5+pip install accelerate==0.33.0++# uninstall+pip uninstall transformer-engine+```++## 环境配置++```+export DASHSCOPE_API_KEY={some_key}+export ES_USER={some_es_user}+export ES_PASSWORD={some_es_password}+export ES_URL=http://{es_url}:9200+```++DASHSCOPE_API_KEY 开通参考[文档](https://help.aliyun.com/zh/dashscope/opening-service)++## 使用++### 导入数据++```bash+python data_ingestion.py -p some.pdf # load data from pdf++ or++python data_ingestion.py -p /opt/data/some_pdf_directory/ # load data from multiples pdf which under the directory of {some_pdf_directory}+```++### 查询++```bash+python query.py --question '{the_question_you_want_to_ask}'+```++## 示例++````bash+# 启动 es 服务+docker compose up -d++or++docker-compose up -d+++# 配置环境变量+export ES_USER=elastic+export ES_PASSWORD=llama_index+export ES_URL=http://127.0.0.1:9200+export DASHSCOPE_API_KEY={some_key}+++# 导入数据+python data_ingestion.py example/data/declaration_of_the_rights_of_man_1789.pdf+++# 查询问题+python query.py -q 'how about the rights of men'++## outputs+请基于```内的内容回答问题。"+ ```+ I. Men are born, and always continue, free and equal in respect of their rights. Civil distinctions, therefore, can be founded only on public utility.+ ```+ 我的问题是:how about the rights of men。++question: how about the rights of men+answer: The statement implies that men are born free and equal in terms of their rights. Civil distinctions should only be based on public utility. However, it does not specify what those rights are. It is up to society and individual countries to determine and protect the specific rights of their citizens.++````++## 开发++`MinerU` 提供了 `RAG` 集成接口,用户可以通过指定输入单个 `pdf` 文件或者某个目录。`MinerU` 会自动解析输入文件并返回可以迭代的接口用于获取数据++### API 接口++```python+from magic_pdf.integrations.rag.type import Node++class RagPageReader:+ def get_rel_map(self) -> list[ElementRelation]:+ # 获取节点间的关系+ pass+ ...++class RagDocumentReader:+ ...++class DataReader:+ def __init__(self, path_or_directory: str, method: str, output_dir: str):+ pass++ def get_documents_count(self) -> int:+ """获取 pdf 文档数量"""+ pass++ def get_document_result(self, idx: int) -> RagDocumentReader | None:+ """获取某个 pdf 的解析内容"""+ pass+++ def get_document_filename(self, idx: int) -> Path:+ """获取某个 pdf 的具体路径"""+ pass+++```++类型定义++```python++class Node(BaseModel):+ category_type: CategoryType = Field(description='类别') # 类别+ text: str | None = Field(description='文本内容',+ default=None)+ image_path: str | None = Field(description='图或者表格(表可能用图片形式存储)的存储路径',+ default=None)+ anno_id: int = Field(description='unique id', default=-1)+ latex: str | None = Field(description='公式或表格 latex 解析结果', default=None)+ html: str | None = Field(description='表格的 html 解析结果', default=None)++```++表格存储形式可能会是 图片、latex、html 三种形式之一。+anno_id 是该 Node 的在全局唯一ID。后续可以用于匹配该 Node 和其他 Node 的关系。节点的关系可以通过方法 `get_rel_map` 获取。用户可以用 `anno_id` 匹配节点之间的关系,并用于构建具备节点的关系的 rag index。++### 节点类型关系矩阵++| | image_body | table_body |+| -------------- | ---------- | ---------- |+| image_caption | sibling | |+| table_caption | | sibling |+| table_footnote | | sibling |