Skip to content

Commit eae9cfc

Browse files
committed
feature: 修改算子开发指南
1 parent c22683d commit eae9cfc

File tree

4 files changed

+56
-149
lines changed

4 files changed

+56
-149
lines changed

runtime/ops/README.md

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,83 @@
77
每个自定义算子都需要包含一个 `metadata.yml` 文件:
88

99
```yaml
10-
name: '落盘算子'
11-
name_en: 'save file operator'
12-
description: '将文件内容保存为文件。'
13-
description_en: 'Save the file data as a file.'
14-
language: 'Python'
15-
vendor: 'Huawei'
16-
raw_id: 'FileExporter'
10+
name: '测试算子'
11+
description: '这是一个测试算子。'
12+
language: 'python'
13+
vendor: 'huawei'
14+
raw_id: 'TestMapper'
1715
version: '1.0.0'
18-
types:
19-
- 'collect'
20-
modal: 'others'
21-
effect:
22-
before: ''
23-
after: ''
24-
inputs: 'all'
25-
outputs: 'all'
16+
modal: 'text'
17+
inputs: 'text'
18+
outputs: 'text'
2619
```
2720
2821
### 算子实现
2922
30-
创建 `process.py` 文件:
23+
#### process.py
3124
3225
```python
3326
# -*- coding: utf-8 -*-
3427

35-
"""
36-
Description: Json文本抽取
37-
Create: 2024/06/06 15:43
38-
"""
39-
import time
40-
from loguru import logger
41-
from typing import Dict, Any
42-
28+
# 导入所需数据结构,可以通过以下方式直接导入使用
29+
# 提供两种算子类:
30+
# Mapper用于映射和转换数据,使用时直接修改数据内容
4331
from datamate.core.base_op import Mapper
4432

33+
class TestMapper(Mapper):
34+
def execute(self, sample):
35+
sample[self.text_key] += "\n新增的数据"
36+
return sample
37+
38+
39+
# Filter用于过滤和选择性保留数据,使用时将需要过滤的数据的text或data置为空值
40+
from datamate.core.base_op import Filter
4541

46-
class TextFormatter(Mapper):
47-
"""把输入的json文件流抽取为txt"""
48-
49-
def __init__(self, *args, **kwargs):
50-
super(TextFormatter, self).__init__(*args, **kwargs)
51-
52-
@staticmethod
53-
def _extract_json(byte_io):
54-
"""将默认使用utf-8编码的Json文件流解码,抽取为txt"""
55-
# 用utf-8-sig的格式进行抽取,可以避免uft-8 BOM编码格式的文件在抽取后产生隐藏字符作为前缀。
56-
return byte_io.decode("utf-8-sig").replace("\r\n", "\n")
57-
58-
def byte_read(self, sample: Dict[str, Any]):
59-
filepath = sample[self.filepath_key]
60-
with open(filepath, "rb") as file:
61-
byte_data = file.read()
62-
sample[self.data_key] = byte_data
63-
64-
def execute(self, sample: Dict[str, Any]) -> Dict[str, Any]:
65-
start = time.time()
66-
try:
67-
self.byte_read(sample)
68-
sample[self.text_key] = self._extract_json(sample[self.data_key])
69-
sample[self.data_key] = b"" # 将sample[self.data_key]置空
70-
logger.info(
71-
f"fileName: {sample[self.filename_key]}, method: TextFormatter costs {(time.time() - start):6f} s")
72-
except UnicodeDecodeError as err:
73-
logger.exception(f"fileName: {sample[self.filename_key]}, method: TextFormatter causes decode error: {err}")
74-
raise
42+
class TestFilter(Filter):
43+
def execute(self, sample):
44+
if len(sample[self.text_key]) > 100:
45+
sample[self.text_key] += ""
7546
return sample
7647

7748
```
7849

79-
创建 `__init__.py` 文件:
50+
其中,sample的数据结构如下所示:
51+
```json lines
52+
// 数据结构
53+
{
54+
"text": "数据文件的文本内容",
55+
"data": "多模态文件的内容",
56+
"fileName": "文件名称",
57+
"fileType": "文件类型",
58+
"filePath": "文件路径",
59+
"fileSize": "文件大小",
60+
"export_path": "保存的文件路径",
61+
"extraFileType": "导出的文件类型"
62+
}
63+
64+
// 数据示例
65+
{
66+
"text": "text",
67+
"data": "data",
68+
"fileName": "test",
69+
"fileType": "pdf",
70+
"filePath": "/dataset/test.pdf",
71+
"fileSize": "100B",
72+
"export_path": "/dataset/test.txt",
73+
"extraFileType": "txt"
74+
}
75+
```
76+
77+
#### \_\_init__.py
8078

8179
```python
8280
# -*- coding: utf-8 -*-
8381

82+
# 导入OPERATORS用于进行模块注册,可以通过以下方式直接导入使用
8483
from datamate.core.base_op import OPERATORS
8584

86-
OPERATORS.register_module(module_name='TextFormatter',
87-
module_path="ops.formatter.text_formatter.process")
85+
# module_name必须填写算子类名称;module_path中须替换模块的算子压缩包名称:python_operator.user.压缩包名.process
86+
OPERATORS.register_module(module_name='TestMapper',
87+
module_path="ops.user.test_operator.process")
8888

8989
```

runtime/ops/examples/test_operator/metadata.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ vendor: 'huawei'
55
raw_id: 'TestMapper'
66
version: '1.0.0'
77
modal: 'text'
8-
effect:
9-
before: '使用方式很简单,只需要将代码放入Markdown文本中即可,富文本格式可直接复制表情😀使用。'
10-
after: '使用方式很简单,只需要将代码放入Markdown文本中即可,富文本格式可直接复制表情使用。'
118
inputs: 'text'
129
outputs: 'text'
1310
settings:

runtime/python-executor/README.md

Lines changed: 0 additions & 89 deletions
This file was deleted.

runtime/python-executor/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ description = "Data Processing for and with Foundation Models."
55
authors = [
66
{ name = "Huawei datamate team" }
77
]
8-
readme = "README.md"
98
license = { text = "Apache-2.0" }
109
requires-python = ">=3.10"
1110
urls = { repository = "https://github.com/ModelEngine-Group/datamate" }

0 commit comments

Comments
 (0)