Skip to content

Commit 3345743

Browse files
dt-yyactions-userquyuan
authored
add new prompt (#241)
* 📚 Auto-update metrics documentation * add OCR prompt * 📚 Auto-update metrics documentation * fix pylint * Update document_parsing_quality_ocr_train.py * add new ocr prompt --------- Co-authored-by: GitHub Action <[email protected]> Co-authored-by: quyuan <[email protected]>
1 parent 86502d1 commit 3345743

File tree

5 files changed

+192
-35
lines changed

5 files changed

+192
-35
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import base64
2+
import json
3+
import re
4+
from typing import List
5+
6+
from dingo.io import Data
7+
from dingo.model import Model
8+
from dingo.model.llm.base_openai import BaseOpenAI
9+
from dingo.model.modelres import ModelRes
10+
from dingo.model.prompt.prompt_mineru_recognize_train import PromptMinerURecognizeTrainQuality
11+
from dingo.model.response.response_class import ResponseScoreReason
12+
from dingo.utils import log
13+
from dingo.utils.exception import ConvertJsonError
14+
15+
16+
@Model.llm_register("PromptMinerURecognizeTrainQuality")
17+
class LLMMinerURecognizeTrainQuality(BaseOpenAI):
18+
"""
19+
LLM for document parsing quality ocr
20+
"""
21+
prompt = PromptMinerURecognizeTrainQuality
22+
23+
@classmethod
24+
def build_messages(cls, input_data: Data) -> List:
25+
if isinstance(input_data.image[0], str):
26+
with open(input_data.image[0], "rb") as image_file:
27+
base64_image = base64.b64encode(image_file.read()).decode('utf-8')
28+
else:
29+
base64_image = input_data.image[0]
30+
31+
messages = [
32+
{
33+
"role": "user",
34+
"content": [
35+
{"type": "text", "text": cls.prompt.content},
36+
{"type": "image_url", "image_url": {"url": base64_image}},
37+
{"type": "text", "text": f"Markdown:\n{input_data.content}"}
38+
]
39+
}
40+
]
41+
return messages
42+
43+
@classmethod
44+
def process_response(cls, response: str) -> ModelRes:
45+
log.info(response)
46+
json_match = re.search(r'\{[\s\S]*"errors"[\s\S]*\}', response)
47+
types = []
48+
names = []
49+
50+
if json_match:
51+
try:
52+
json_str = json_match.group()
53+
result_data = json.loads(json_str)
54+
errors = result_data.get("errors", [])
55+
56+
for error in errors:
57+
error_category = error.get("error_category", "")
58+
error_label = error.get("error_label", "")
59+
# 只提取 error_category 和 error_label
60+
if error_category and error_label:
61+
types.append(error_category)
62+
names.append(error_label)
63+
except json.JSONDecodeError as e:
64+
log.error(f"JSON解析错误: {e}")
65+
else:
66+
log.error("未找到JSON内容")
67+
68+
result = ModelRes()
69+
result.error_status = False
70+
result.type = types
71+
result.name = names
72+
result.reason = [json_str] if 'json_str' in locals() else [response]
73+
74+
return result
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from dingo.model.model import Model
2+
from dingo.model.prompt.base import BasePrompt
3+
4+
5+
@Model.prompt_register("PromptMinerURecognizeTrainQuality", [], ["PromptDocumentParsingQuality"])
6+
class PromptMinerURecognizeTrainQuality(BasePrompt):
7+
"""
8+
Metadata for documentation generation
9+
"""
10+
_metric_info = {
11+
"category": "OCR Eval Metric",
12+
"metric_name": "MinerURecognizeTrainQuality",
13+
"description": "Evaluate the quality of mineru recognize",
14+
"evaluation_results": "error_category and error_label",
15+
}
16+
content = r"""
17+
你是一位熟悉文档解析领域的质量专家,你的核心任务是根据带bbox的图"原图",以及对应OCR工具预测结果"Pred的内容",获取工具预测结果的错误类型。
18+
*错误类别和标签*
19+
以下是你可以使用的错误类别和对应的标签。在输出的JSON中,"error_category"字段应填写问题大类(如:公式识别相关问题),"error_label"字段应填写问题子类(如:公式中字符识别错误)。
20+
**1.公式识别相关问题**
21+
- 公式字符识别错误:公式渲染正确,但识别错误
22+
- 公式内容模型输出重复
23+
**2.表格识别相关问题**
24+
- 表格输出格式错误:输出otsl格式有误导致转换失败
25+
- 表格结构错误:结构造成的内容丢失也算在里面
26+
- 表格内容错误:结构是对的,仅文本错
27+
- 表格内容模型输出重复
28+
**3. 分行分段相关问题**
29+
- 非跨栏内容段落粘连: 原本不同段落的文本,在OCR结果中被错误地合并成一个段落。
30+
- 段落异常拆分: 原本完整的一个段落,在OCR结果中被错误地分割成了多个段落的文本。
31+
**4.列表相关问题**
32+
-列表项异常合并/粘连: 原图中文档中的独立的列表项(有序列表和无序列表,或者(1)、(2)...样式的列表)、参考文献被合并成一行。可能是多个项合并成一项,或列表项与前后文本合并。
33+
**5.标题相关问题**
34+
-标题格式丢失: 原文件中的标题,在OCR结果中被识别为普通文本,丢失了标题应有的Markdown格式(如#)。
35+
-标题分级错误: 原图中的标题被识别,但其层级(如H1, H2)与原图不符,包括层级识别错误(如一级标题识别为二级)。
36+
**5.OCR识别问题**
37+
- 字符识别错误:文本、标题、列表类型等文本内容识别错误。
38+
**6.其他**
39+
-其他问题: 此分类用于标记不属于上述任何具体类别的其他OCR质量问题。经过仔细判断后确认无法归入其他既有标签的OCR质量问题。
40+
41+
*输出格式*
42+
请严格按照以下JSON结构组织你的发现:
43+
```json
44+
{
45+
"errors": [
46+
{
47+
"bbox_id": "1", //原图中的bbox序号
48+
"bbox_type": "equation", //图中的bbox类型
49+
"error_category": "公式识别相关问题", // 错误的大类
50+
"error_label": "公式中字符识别错误", // 从上面的《错误类别和标签》列表中选取的一个具体的二级标签
51+
},
52+
{
53+
"bbox_id": "2",
54+
"bbox_type": "table", //图中的bbox类型
55+
"error_category": "表格识别相关问题",
56+
"error_label": "表格输出格式错误"
57+
},
58+
{
59+
"bbox_id": "3",
60+
// ... 更多按 error_label 汇总的错误
61+
}
62+
]
63+
}
64+
```
65+
*工作流程:*
66+
1. 接收并理解 **原图** 和 **Pred的内容**。
67+
2. 仔细比对两者,识别所有内容和格式上的差异。
68+
3. 根据 **错误类别和标签** 对每个差异进行分类。
69+
4. 记录每个错误的信息(错误类别、错误标签)。如果同一位置存在多个独立的错误,请在 errors 列表内分别列出,不要再堆叠。
70+
5. 按照指定的 **输出格式** 生成 JSON 报告
71+
```
72+
*输入:*
73+
* **原图:**
74+
* **Pred的内容:**
75+
*输出:*
76+
```json
77+
[请在此处提供你的JSON分析结果, 注意仅输出json,不要输出任何解释]
78+
```
79+
"""

docs/metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ This document provides comprehensive information about all quality metrics used
9292
|------|--------|-------------|--------------|-------------------|
9393
| `PromptDocumentParsingQuality` | PromptDocumentParsingQuality | Evaluate the quality of general document parsing | Internal Implementation | N/A |
9494
| `PromptMinerURecognizeQuality` | MinerURecognizeQuality | Evaluate the quality of mineru recognize | Internal Implementation | [📊 See Results](error_category and error_label) |
95+
| `PromptMinerURecognizeTrainQuality` | MinerURecognizeTrainQuality | Evaluate the quality of mineru recognize | Internal Implementation | [📊 See Results](error_category and error_label) |
9596

9697
### Resume Quality Assessment Metrics
9798

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from dingo.config import InputArgs
2+
from dingo.exec import Executor
3+
4+
if __name__ == '__main__':
5+
input_data = {
6+
"input_path": "test/data/test_document_OCR_recognize.jsonl",
7+
"dataset": {
8+
"source": "local",
9+
"format": "jsonl",
10+
"field": {
11+
"id": "id",
12+
"content": "pred_content",
13+
"image": "pred_bbox_image",
14+
}
15+
},
16+
"executor": {
17+
"prompt_list": ["PromptMinerURecognizeTrainQuality"],
18+
"result_save": {
19+
"bad": True,
20+
"good": True
21+
}
22+
},
23+
"evaluator": {
24+
"llm_config": {
25+
"LLMMinerURecognizeQuality": {
26+
"model": "gemini-2.5-pro",
27+
"key": "",
28+
"api_url": ""
29+
}
30+
}
31+
}
32+
}
33+
input_args = InputArgs(**input_data)
34+
executor = Executor.exec_map["local"](input_args)
35+
result = executor.execute()
36+
print(result)

0 commit comments

Comments
 (0)