MinerU web api部署
MinerU 能夠將包含圖片、公式、表格等元素的多模態 PDF、PPT、DOCX 等文檔轉化為易于分析的 Markdown 格式。
-
克隆 MinerU 的倉庫
git clone https://github.com/opendatalab/MinerU.git
-
cd 到 projects/web-api
cd projects/web-api
-
在可以科學上網的情況下可以直接運行
docker build -t mineru-api .
-
如果不能科學上網,使用 modelscope
修改改 requirement.txt,添加
modelscope==1.25.0
?修改 download_models.py,刪掉所有舊代碼,改成這個
#!/usr/bin/env python import os from modelscope.hub.snapshot_download import snapshot_download# 配置模型下載路徑(Windows路徑示例:r'C:\MinerU') MAIN_MODEL_DIR = "/opt/" os.makedirs(MAIN_MODEL_DIR, exist_ok=True)if __name__ == "__main__":# PDF-Extract-Kit的模型下載(需替換為ModelScope對應ID)pdf_extract_patterns = ["models/Layout/YOLO/**","models/MFD/YOLO/**","models/MFR/unimernet_hf_small_2503/**","models/OCR/paddleocr_torch/**",]pdf_model_path = snapshot_download('opendatalab/PDF-Extract-Kit-1.0', # ModelScope模型IDallow_patterns=pdf_extract_patterns,cache_dir=os.path.join(MAIN_MODEL_DIR, "models"),revision='master' # 指定模型版本)# LayoutReader模型下載(需替換為ModelScope對應模型)layout_model_path = snapshot_download('ppaanngggg/layoutreader', # 替換為實際ModelScope IDcache_dir=os.path.join(MAIN_MODEL_DIR, "layoutreader"),allow_patterns=["*.json", "*.safetensors"])print(f"主模型目錄:{pdf_model_path}")print(f"布局模型目錄:{layout_model_path}")
再運行
docker build -t mineru-api .
? -
編寫 docker-compose.yml
services:mineru-api:image: mineru-apirestart: alwaysports:- "8920:8920"stdin_open: true # 對應 -i 參數tty: true # 對應 -t 參數runtime: nvidia # 使用 NVIDIA 容器運行時environment:- NVIDIA_VISIBLE_DEVICES=all # 允許訪問所有 GPU
-
如果沒有 nvidia 的 runtime,先備份自己的 daemon.json 文件 (/etc/docker/),通過
yum install -y nvidia-docker2
? 安裝,這個命令會生成一個新的 daemon.json 文件,覆蓋掉原來舊的,安裝后之后比較一下新舊兩個 daemon 文件,合并在一起。 -
啟動
systemctl daemon-reload systemctl restart docker docker compose up -d
-
訪問 localhost:8000/docs
?
-
進入mineru容器目錄,編寫app.py,保證images字段的正常輸出
async def file_parse(file: UploadFile = None,file_path: str = Form(None),parse_method: str = Form("auto"),is_json_md_dump: bool = Form(False),output_dir: str = Form("output"),return_layout: bool = Form(False),return_info: bool = Form(False),return_content_list: bool = Form(False),# 此處修改為 Truereturn_images: bool = Form(False), ):
-
重啟mineRu容器
使用MineRu插件
-
Dify配置
-
配置http://api:5001,端口地址暴露給宿主機
docker-compose.yaml:文本搜索services:
services:# API serviceapi:image: langgenius/dify-api:1.4.1restart: always# 添加以下端口映射 ↓ports:- "5001:5001" # 將API 5001 端口映射到宿主機的 5001 端口environment:# Use the shared environment variables.<<: *shared-api-worker-env# Startup mode, 'api' starts the API server.MODE: apiSENTRY_DSN: ${API_SENTRY_DSN:-}SENTRY_TRACES_SAMPLE_RATE: ${API_SENTRY_TRACES_SAMPLE_RATE:-1.0}SENTRY_PROFILES_SAMPLE_RATE: ${API_SENTRY_PROFILES_SAMPLE_RATE:-1.0}PLUGIN_REMOTE_INSTALL_HOST: ${EXPOSE_PLUGIN_DEBUGGING_HOST:-localhost}PLUGIN_REMOTE_INSTALL_PORT: ${EXPOSE_PLUGIN_DEBUGGING_PORT:-5003}PLUGIN_MAX_PACKAGE_SIZE: ${PLUGIN_MAX_PACKAGE_SIZE:-52428800}INNER_API_KEY_FOR_PLUGIN: ${PLUGIN_DIFY_INNER_API_KEY:-QaHbTe77CtuXmsfyhR7+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1}depends_on:db:condition: service_healthyredis:condition: service_started
其他部署方式:FILES_URL 設置為 ‘http://Dify宿主機IP:5001’
(如 http://192.168.1.100:5001,這里的 IP 通常是運行 Dify 的機器的 IP,即前文提到的 “本地 IP” 端口。5001 是 Dify API 服務的默認端口)。 -
設置Dify文件預覽/下載地址:Files_URL
.env:
# File preview or download Url prefix. # used to display File preview or download Url to the front-end or as Multi-model inputs; # Url is signed and has expiration time. # Setting FILES_URL is required for file processing plugins. # - For https://example.com, use FILES_URL=https://example.com # - For http://example.com, use FILES_URL=http://example.com # Recommendation: use a dedicated domain (e.g., https://upload.example.com). # Alternatively, use http://<your-ip>:5001 or http://api:5001, # ensuring port 5001 is externally accessible (see docker-compose.yaml). FILES_URL=http://api:5001
-
重啟Dify
- 進入 Dify 源碼 docker目錄
- 地址欄輸入cmd,打開命令行
- 停止舊容器:
docker-compose down
? - 重新構建(僅當 Dockerfile 或 build 參數變更時):
docker-compose build
? - 啟動服務:
docker-compose up -d
?
-
-
Dify插件市場安裝MineRu插件,并打開插件目錄
-
點擊該插件詳情,出現授權界面
?Base URL配置為http://host.docker.internal:8920
令牌為空,服務類型選擇本地部署,點擊保存,顯示已授權即可
?
編輯解析測試工作流
-
創建單輪工作流應用,設置基本信息
-
配置PDF文件下載地址的HTTP請求節點
?
-
配置MineRu插件的解析節點
注意:以上只有解析變量及其解析方法可以使用,其他都是官方API使用參數
輸出變量:text : 解析后的 Markdown 文本
files: 額外的導出格式文件(html,docx,latex)
json:解析后的內容列表
full_zip_url: 僅限官方 API,完整解析結果的 zip URL
images: 從 PDF 中提取的圖片
-
獲取PDF文件第一個大標題作為文件名的代碼執行節點
import re def main(mdText: str) -> dict:match = re.search(r'^#\s+(.*)$', mdText, re.MULTILINE)if match:return {'result': match.group(1)}else:return {'result': ""}
-
插件市場安裝Markdown轉換器插件,并使用
? -
結束節點:結果存在文件下載地址
??
?注意:dify保存文件存在過期時間,可能會導致md文件圖片鏈接失效,建議,給予LLM模型解析使用即可,如需本地,保存圖片本地使用