Skip to content

Commit 924d977

Browse files
authored
支持mineru npu处理 (#174)
* feature: unstructured支持简单pdf处理 * feature: update values.yaml to enhance ray-cluster configuration with security context, environment variables, and resource limits * feature: update deploy.yaml and process.py for mineru server configuration and PDF processing enhancements * feature: update deploy.yaml and process.py for mineru server configuration and PDF processing enhancements * feature: improve PDF processing logic and update dependencies in process.py and pyproject.toml * feature: improve PDF processing logic and update dependencies in process.py and pyproject.toml * feature: update Dockerfile for improved package source mirrors and add mineru-npu to build targets
1 parent 3b4f848 commit 924d977

File tree

8 files changed

+110
-35
lines changed

8 files changed

+110
-35
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ endef
155155
# ========== Build Targets ==========
156156

157157
# Valid build targets
158-
VALID_BUILD_TARGETS := backend database frontend runtime backend-python deer-flow mineru
158+
VALID_BUILD_TARGETS := backend database frontend runtime backend-python deer-flow mineru mineru-npu
159159

160160
# Generic docker build target with service name as parameter
161161
# Automatically prefixes image names with "datamate-" unless it's deer-flow

deployment/docker/datamate/docker-compose.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,28 @@ services:
9999
restart: on-failure
100100
environment:
101101
MINERU_MODEL_SOURCE: local
102-
MINERU_DEVICE_MODE: cpu # cpu|cuda|npu|mps
103-
MINERU_BACKEND_MODE: pipeline
102+
MINERU_DEVICE_MODE: npu # cpu|cuda|npu|mps
103+
VLLM_WORKER_MULTIPROC_METHOD: spawn
104104
privileged: true
105105
command:
106-
- python
107-
- /opt/runtime/datamate/mineru/mineru_api.py
108-
- --port
109-
- "9001"
106+
- mineru-openai-server
107+
- --engine vllm
108+
- --host 0.0.0.0
109+
- --port "8000"
110110
volumes:
111111
- dataset_volume:/dataset
112112
- mineru_log_volume:/var/log/datamate/mineru
113+
- /var/log/npu/:/usr/slog
114+
- /usr/local/dcmi:/usr/local/dcmi
115+
- /usr/local/bin/npu-smi:/usr/local/bin/npu-smi
116+
- /usr/local/Ascend/driver:/usr/local/Ascend/driver
113117
networks: [ datamate ]
114118
profiles: [ mineru ]
119+
devices:
120+
- /dev/davinci0
121+
- /dev/davinci_manager
122+
- /dev/devmm_svm
123+
- /dev/hisi_hdc
115124

116125
# 5) redis
117126
datamate-redis:

deployment/helm/datamate/values.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ ray-cluster:
175175
enabled: true
176176
head:
177177
rayStartParams:
178-
object-store-memory: '78643200'
179178
num-cpus: '0'
180179
containerEnv:
181180
- name: RAY_DEDUP_LOGS
@@ -194,8 +193,8 @@ ray-cluster:
194193
value: "datamate"
195194
resources:
196195
limits:
197-
cpu: "2"
198-
memory: "8G"
196+
cpu: "4"
197+
memory: "16G"
199198
requests:
200199
cpu: "1"
201200
memory: "2G"

deployment/kubernetes/mineru/deploy.yaml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,32 @@ spec:
2222
image: datamate-mineru
2323
imagePullPolicy: IfNotPresent
2424
command:
25-
- python
26-
- /opt/runtime/datamate/mineru/mineru_api.py
25+
- mineru-openai-server
26+
args:
27+
- --engine
28+
- vllm
29+
- --host
30+
- 0.0.0.0
2731
- --port
28-
- "9001"
32+
- "8000"
2933
env:
3034
- name: MINERU_MODEL_SOURCE
3135
value: local
3236
- name: MINERU_DEVICE_MODE
33-
value: cpu
34-
- name: MINERU_BACKEND_MODE
35-
value: pipeline
37+
value: npu
38+
- name: VLLM_WORKER_MULTIPROC_METHOD
39+
value: spawn
3640
ports:
37-
- containerPort: 9001
41+
- containerPort: 8000
3842
resources:
3943
limits:
40-
cpu: 16
44+
cpu: 8
4145
memory: 32Gi
46+
huawei.com/Ascend910: 1
4247
requests:
4348
cpu: 100m
4449
memory: 100Mi
50+
huawei.com/Ascend910: 1
4551
volumeMounts:
4652
- name: dataset-volume
4753
mountPath: /dataset
@@ -67,8 +73,8 @@ metadata:
6773
spec:
6874
type: ClusterIP
6975
ports:
70-
- port: 9001
71-
targetPort: 9001
76+
- port: 8000
77+
targetPort: 8000
7278
protocol: TCP
7379
selector:
7480
app: datamate

runtime/ops/formatter/mineru_formatter/process.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,58 @@
55
Description: MinerU PDF文本抽取
66
Create: 2025/10/29 17:24
77
"""
8-
import json
8+
import os
9+
import shutil
910
import time
10-
from loguru import logger
1111
from typing import Dict, Any
1212

13-
from datamate.core.base_op import Mapper
1413
from datamate.common.utils.rest_client import http_request
14+
from datamate.core.base_op import Mapper
15+
from loguru import logger
16+
from mineru.cli.common import do_parse, read_fn
17+
from mineru.cli.fast_api import get_infer_result
18+
from pypdf import PdfReader
1519

1620

1721
class MineruFormatter(Mapper):
1822
"""基于外部API,抽取PDF中的文本"""
1923

2024
def __init__(self, *args, **kwargs):
2125
super(MineruFormatter, self).__init__(*args, **kwargs)
22-
self.base_url = "http://datamate-mineru:9001"
23-
self.pdf_extract_url = f"{self.base_url}/api/pdf-extract"
26+
self.server_url = "http://datamate-mineru:8000"
27+
self.backend = "vlm-http-client"
28+
self.output_dir = "/dataset/outputs"
2429

2530
def execute(self, sample: Dict[str, Any]) -> Dict[str, Any]:
2631
start = time.time()
2732
filename = sample[self.filename_key]
28-
if not filename.lower().endswith(".pdf"):
33+
filename_without_ext = os.path.splitext(filename)[0]
34+
if not filename.lower().endswith((".png", ".jpeg", ".jpg", ".webp", ".gif", ".pdf")):
2935
return sample
3036
try:
31-
data = {"source_path": sample[self.filepath_key], "export_path": sample[self.export_path_key]}
32-
response = http_request(method="POST", url=self.pdf_extract_url, data=data)
33-
sample[self.text_key] = json.loads(response.text).get("result")
37+
filepath = sample[self.filepath_key]
38+
parse_dir = os.path.join(self.output_dir, filename_without_ext, "vlm")
39+
pdf_bytes = read_fn(filepath)
40+
total_page = len(PdfReader(filepath).pages)
41+
content = ""
42+
for page in range(0, total_page, 10):
43+
do_parse(
44+
output_dir=self.output_dir,
45+
pdf_file_names=[filename_without_ext],
46+
pdf_bytes_list=[pdf_bytes],
47+
p_lang_list=["ch"],
48+
backend=self.backend,
49+
server_url=self.server_url,
50+
start_page_id=page,
51+
end_page_id=min(page + 9, total_page - 1),
52+
)
53+
if os.path.exists(parse_dir):
54+
content += get_infer_result(".md", filename_without_ext, parse_dir)
55+
shutil.rmtree(parse_dir)
56+
sample[self.text_key] = content
3457
logger.info(
3558
f"fileName: {filename}, method: MineruFormatter costs {(time.time() - start):6f} s")
36-
except UnicodeDecodeError as err:
37-
logger.exception(f"fileName: {filename}, method: MineruFormatter causes decode error: {err}")
59+
except Exception as e:
60+
logger.exception(f"fileName: {filename}, method: MineruFormatter causes error: {e}")
3861
raise
3962
return sample

runtime/ops/pyproject.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ dependencies = [
1111
"emoji>=2.15.0",
1212
"jieba>=0.42.1",
1313
"loguru>=0.7.3",
14-
"numpy==1.23.3",
14+
"mineru>=2.6.5",
15+
"numpy==1.24.3",
16+
"python-multipart>=0.0.20",
1517
"opencv-contrib-python-headless==4.7.0.72",
1618
"opencv-python-headless==4.7.0.72",
1719
"openslide-python>=1.4.3",
@@ -29,4 +31,4 @@ dependencies = [
2931
"sqlalchemy>=2.0.44",
3032
"xmltodict>=1.0.2",
3133
"zhconv>=1.4.3",
32-
]
34+
]

scripts/images/backend/Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
FROM maven:3-eclipse-temurin-8 AS datax-builder
22

3-
RUN apt-get update && \
3+
RUN sed -i "s@http://.*archive.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list && \
4+
sed -i "s@http://.*security.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list && \
5+
apt-get update && \
46
apt-get install -y git && \
57
git clone https://github.com/alibaba/DataX.git
68

@@ -21,7 +23,9 @@ RUN cd /opt/backend && \
2123

2224
FROM eclipse-temurin:21-jdk
2325

24-
RUN apt-get update && \
26+
RUN sed -i "s@http://.*archive.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list && \
27+
sed -i "s@http://.*security.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list && \
28+
apt-get update && \
2529
apt-get install -y vim wget curl nfs-common rsync python3 python3-pip python-is-python3 dos2unix && \
2630
apt-get clean && \
2731
rm -rf /var/lib/apt/lists/*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 基础镜像配置 vLLM 或 LMDeploy ,请根据实际需要选择其中一个,要求 ARM(AArch64) CPU + Ascend NPU。
2+
# Base image containing the vLLM inference environment, requiring ARM(AArch64) CPU + Ascend NPU.
3+
FROM quay.io/ascend/vllm-ascend:v0.11.0rc2
4+
# Base image containing the LMDeploy inference environment, requiring ARM(AArch64) CPU + Ascend NPU.
5+
# FROM crpi-4crprmm5baj1v8iv.cn-hangzhou.personal.cr.aliyuncs.com/lmdeploy_dlinfer/ascend:mineru-a2
6+
7+
8+
# Install libgl for opencv support & Noto fonts for Chinese characters
9+
RUN apt-get update && \
10+
apt-get install -y \
11+
fonts-noto-core \
12+
fonts-noto-cjk \
13+
fontconfig \
14+
libgl1 \
15+
libglib2.0-0 && \
16+
fc-cache -fv && \
17+
apt-get clean && \
18+
rm -rf /var/lib/apt/lists/*
19+
20+
# Install mineru latest
21+
RUN python3 -m pip install -U pip -i https://mirrors.aliyun.com/pypi/simple && \
22+
python3 -m pip install 'mineru[core]>=2.6.5' \
23+
numpy==1.26.4 \
24+
opencv-python==4.11.0.86 \
25+
-i https://mirrors.aliyun.com/pypi/simple && \
26+
python3 -m pip cache purge
27+
28+
# Download models and update the configuration file
29+
RUN TORCH_DEVICE_BACKEND_AUTOLOAD=0 /bin/bash -c "mineru-models-download -s modelscope -m all"
30+
31+
# Set the entry point to activate the virtual environment and run the command line tool
32+
ENTRYPOINT ["/bin/bash", "-c", "export MINERU_MODEL_SOURCE=local && exec \"$@\"", "--"]

0 commit comments

Comments
 (0)