Skip to content

feat(mineru): MinerU v4.1 — Cloud API + Docling Fallback + Kuaishou 374p Verified#161

Open
feiyu169 wants to merge 2 commits into
noho:mainfrom
feiyu169:feat/mineru-v4.1
Open

feat(mineru): MinerU v4.1 — Cloud API + Docling Fallback + Kuaishou 374p Verified#161
feiyu169 wants to merge 2 commits into
noho:mainfrom
feiyu169:feat/mineru-v4.1

Conversation

@feiyu169

@feiyu169 feiyu169 commented May 5, 2026

Copy link
Copy Markdown

概述

MinerU 云 API v4 集成到 dayu-agent,作为 PDF 解析后端。当 Docling 解析失败(港股复杂 PDF 超时/崩溃)时,自动 fallback 到 MinerU 云 API。

核心收益:快手 2025 年报(374 页 / 9.9MB 港股 PDF)解析耗时从 Docling CPU >10min(未完成)降低到 MinerU 云 API 78.7s。

新增文件(5 个模块 + 2 个测试 + 1 个文档)

文件 行数 职责
dayu/document_protocol.py 362 统一中间格式 ConvertedDocument(frozen dataclass)+ bbox 版本探测
dayu/mineru_runtime.py 899 MinerU 云 API v4:COS 上传 → 并发提交 → 并发轮询 → zip 下载解析 → 五层回退链
dayu/quota_tracker.py 182 配额跟踪器:本地计数器 + 文件持久化 + 每日 5000 页上限
dayu/cos_helper.py 167 腾讯云 COS 上传/删除/URL 提取辅助
dayu/config/pdf_backend.py 112 MinerU 配置模块(环境变量 → 配置值)
tests/test_mineru_basic.py 306 30 个基础测试
tests/test_mineru_runtime.py 458 26 个运行时测试
docs/mineru-v4.1-changelog.md 完整变更说明

修改文件

文件 改动 说明
dayu/fins/docling_export.py +86 新增 convert_pdf_bytes_with_fallback:Docling 优先,失败 fallback 到 MinerU
dayu/fins/pipelines/docling_upload_service.py ±22 接入 fallback 函数,存 _mineru.json
pyproject.toml +2 新增依赖:pikepdf, cos-python-sdk-v5
9 个已有测试文件 +70 -34 适配新接口签名

五层回退链

parse_pdf_bytes_with_mineru(pdf_bytes, filename)
  ├─ 层1: MinerU 云 API 单次(≤200 页)
  ├─ 层2: MinerU 云 API 分批(>200 页,page_ranges 服务端分页)
  ├─ 层3: MinerU 本地 CLI(TODO)
  ├─ 层4: MinerU 本地 Python API(TODO)
  └─ 层5: Docling(终极兜底)

v4.1 关键修复

  1. 配额检查时机:check_and_consume 移到 COS 上传之后(避免白扣)
  2. _KNOWN_BLOCK_TYPES:补全 paragraph/image
  3. COS filename:消除 hardcoded filename,参数化透传
  4. Fallback 存储:只存 _mineru.json(不存冗余 .md

环境变量

DAYU_MINERU_TOKEN=xxx           # MinerU API Token
DAYU_MINERU_API_BASE=https://mineru.net
DAYU_COS_SECRET_ID=xxx          # 腾讯云 SecretId
DAYU_COS_SECRET_KEY=xxx         # 腾讯云 SecretKey
DAYU_COS_BUCKET=xxx             # COS Bucket
DAYU_COS_REGION=ap-chengdu      # COS Region

测试结果

  • 4281 测试通过(1 失败:serper 网络超时)
  • pyright 0 errors, 0 warnings
  • 快手 2025 年报: 78.7s, 2398 sections, 202 tables, 112 images, 341KB markdown

…74p Verified

Core (5 new modules, +1719 lines):
- dayu/document_protocol.py: ConvertedDocument unified format + bbox version detection
- dayu/mineru_runtime.py: MinerU cloud API v4 (COS + concurrent polling + zip + 5-layer fallback)
- dayu/quota_tracker.py: Persistent quota tracker (5000 pages/day)
- dayu/cos_helper.py: Tencent COS upload/delete helper
- dayu/config/pdf_backend.py: MinerU config module

Pipeline (2 modified):
- dayu/fins/docling_export.py: +convert_pdf_bytes_with_fallback (Docling-first, MinerU fallback)
- dayu/fins/pipelines/docling_upload_service.py: Fallback integration, stores _mineru.json

Tests (2 new, 9 adapted):
- tests/test_mineru_basic.py: 30 tests
- tests/test_mineru_runtime.py: 26 tests
- 9 existing test files adapted for new signatures

v4.1 key fixes:
- Quota check moved after COS upload (avoid false deduction)
- _KNOWN_BLOCK_TYPES: added paragraph/image
- COS filename parameterized (was hardcoded kuaishou2025.pdf)
- Fallback stores _mineru.json only (no redundant .md)

E2E verified: kuaishou 2025 annual report 374 pages → 78.7s
2398 sections, 202 tables, 112 images, 341KB markdown
4281 tests passed, pyright 0 errors

GBrain: mineru-integration-plan-v4.1
Docs: docs/mineru-v4.1-changelog.md
@feiyu169

Copy link
Copy Markdown
Author

添加了MinerU的支持,对中文的财报适配更加优秀

层4 Python API (magic_pdf.tools.common.do_parse):
- try/except Exception 包裹,模型下载异常不穿透
- tempfile 临时文件 + try/finally 清理

层3 CLI (magic-pdf subprocess):
- 修复 shutil.which("mineru") → "magic-pdf"
- subprocess.run shell=False,timeout=300
- try/except Exception 包裹

代码重构:
- 抽取 _flatten_content_list + _build_document_from_blocks 消除 4 处重复
- 抽取 _cleanup_temp_files 统一层3/层4 清理逻辑
- 提取 4 个命名常量消除魔法数字

测试:
- 层3 CLI: 9 个 mock 测试 + 1 个 skip-if-not-installed 烟雾测试
- 层4 Python API: 8 个 mock 测试 + 1 个 skip-if-not-installed 烟雾测试
- MineruProcessor: 31 个单元测试覆盖全部 7 个接口
- 总计: 66 passed, 0 failed, pyright 0 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant