Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0928e7e

Browse files
committed
Update Taskflow README.md
1 parent 810be72 commit 0928e7e

4 files changed

Lines changed: 160 additions & 5 deletions

File tree

docs/model_zoo/taskflow.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ PaddleNLP提供**开箱即用**的产业级NLP预置任务能力,无需训练
3232
| [词性标注](#词性标注) | `Taskflow("pos_tagging")` |||||| 基于百度前沿词法分析工具LAC |
3333
| [命名实体识别](#命名实体识别) | `Taskflow("ner")` |||||| 覆盖最全中文实体标签 |
3434
| [依存句法分析](#依存句法分析) | `Taskflow("dependency_parsing")` |||| || 基于最大规模中文依存句法树库研发的DDParser |
35+
| [信息抽取](#信息抽取) | `Taskflow("information_extraction")` ||||| | 适配多场景的开放域通用信息抽取工具 |
3536
| [『解语』-知识标注](#解语知识标注) | `Taskflow("knowledge_mining")` |||||| 覆盖所有中文词汇的知识标注工具 |
3637
| [文本纠错](#文本纠错) | `Taskflow("text_correction")` |||||| 融合拼音特征的端到端文本纠错模型ERNIE-CSC |
3738
| [文本相似度](#文本相似度) | `Taskflow("text_similarity")` |||| | | 基于百度知道2200万对相似句组训练 |
@@ -394,6 +395,129 @@ from paddlenlp import Taskflow
394395
* `task_path`:自定义任务路径,默认为None。
395396
</div></details>
396397

398+
### 信息抽取
399+
<details><summary>&emsp; 适配多场景的开放域通用信息抽取工具 </summary><div>
400+
401+
#### 支持多场景信息抽取任务
402+
403+
- 命名实体识别
404+
405+
命名实体识别(Named Entity Recognition,简称NER),是指识别文本中具有特定意义的实体,主要包括人名、地名、机构名、专有名词等。UIE可以抽取的实体类型包括但不限于人名、地名等类型。
406+
407+
例如抽取的目标实体类型是"出租方"和"承租方", schema构造如下:
408+
409+
```text
410+
['出租方', '承租方']
411+
```
412+
413+
```python
414+
>>> from paddlenlp import Taskflow
415+
416+
>>> schema = ['出租方', '承租方'] # Define the schema for entity extraction
417+
>>> ie = Taskflow('information_extraction', schema=schema)
418+
>>> ie('出租方:小明 地址:筒子街12号 电话:12345678900 承租方:小红 地址:新华路8号 电话:1234500000')
419+
[{'出租方': [{'text': '小明', 'start': 4, 'end': 6, 'probability': 0.9767557939143963}], '承租方': [{'text': '小红', 'start': 36, 'end': 38, 'probability': 0.9588206726186428}]}]
420+
```
421+
422+
- 关系抽取
423+
424+
关系抽取(Relation Extraction,简称RE),是指从文本中识别实体并抽取实体之间的语义关系,即抽取三元组(entity1,关系类型,entity2)。
425+
426+
例如抽取的目标是"出租方的地址"、"出租方的电话"、"承租方的地址"和"承租方的电话", schema构造如下:
427+
428+
```text
429+
[{'出租方': ['地址', '电话'], '承租方': ['地址', '电话']}]
430+
```
431+
432+
在实体抽取中我们已经实例化了一个`Taskflow`对象,这里可以通过`set_schema`方法重置抽取目标。
433+
434+
```python
435+
>>> schema = [{'出租方': ['地址', '电话'], '承租方': ['地址', '电话']}] # Define the schema for relation extraction
436+
>>> ie.set_schema(schema) # Reset schema
437+
>>> ie('出租方:小明 地址:筒子街12号 电话:12345678900 承租方:小红 地址:新华路8号 电话:1234500000')
438+
[{'出租方': [{'text': '小明', 'start': 4, 'end': 6, 'probability': 0.9767557939143963, 'relation': {'地址': [{'text': '筒子街12号', 'start': 10, 'end': 16, 'probability': 0.9962335807051907}], '电话': [{'text': '12345678900', 'start': 20, 'end': 31, 'probability': 0.9970156522060591}]}}], '承租方': [{'text': '小红', 'start': 36, 'end': 38, 'probability': 0.9588206726186428, 'relation': {'地址': [{'text': '新华路8号', 'start': 42, 'end': 47, 'probability': 0.9726211208360169}], '电话': [{'text': '1234500000', 'start': 51, 'end': 61, 'probability': 0.9597719304216668}]}}]}]
439+
```
440+
441+
- 事件抽取
442+
443+
事件抽取 (Event Extraction, 简称EE),是指从自然语言文本中抽取事件并识别事件类型和事件论元的技术。UIE所包含的事件抽取任务,是指根据已知事件类型,抽取该事件所包含的事件论元。
444+
445+
例如抽取的目标是"地震"的"地震强度"、"时间"、"震中位置"和"震源深度",schema构造如下:
446+
447+
```text
448+
[{'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}]
449+
```
450+
451+
触发词的格式统一为`XX触发词``XX`表示具体事件类型,上例中的事件类型是`地震`,则对应触发词为`地震触发词`
452+
453+
```python
454+
>>> schema = [{'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}] # Define the schema for event extraction
455+
>>> ie.set_schema(schema) # Reset schema
456+
>>> ie('中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24.34度,东经99.98度)发生3.5级地震,震源深度10千米。')
457+
[{'地震触发词': [{'text': '地震', 'start': 56, 'end': 58, 'probability': 0.9987181623528585, 'relation': {'地震强度': [{'text': '3.5级', 'start': 52, 'end': 56, 'probability': 0.9962985320905915}], '时间': [{'text': '5月16日06时08分', 'start': 11, 'end': 22, 'probability': 0.9882578028575182}], '震中位置': [{'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)', 'start': 23, 'end': 50, 'probability': 0.8551415716584501}], '震源深度': [{'text': '10千米', 'start': 63, 'end': 67, 'probability': 0.999158304648045}]}}]}]
458+
```
459+
460+
- 评论观点抽取
461+
462+
评论观点抽取,是指抽取文本中包含的评价维度、观点词。
463+
464+
例如希望抽取文本中包含的评价维度以及对应的观点词,schema构造如下:
465+
466+
```text
467+
[{'评价维度': ['观点词']}]
468+
```
469+
470+
```python
471+
>>> schema = [{'评价维度': ['观点词']}] # Define the schema for opinion extraction
472+
>>> ie.set_schema(schema) # Reset schema
473+
>>> ie('个人觉得管理太混乱了,票价太高了')
474+
[{'评价维度': [{'text': '管理', 'start': 4, 'end': 6, 'probability': 0.8902373594544031, 'relation': {'观点词': [{'text': '混乱', 'start': 7, 'end': 9, 'probability': 0.9993566520321409}]}}, {'text': '票价', 'start': 11, 'end': 13, 'probability': 0.9856116411308662, 'relation': {'观点词': [{'text': '', 'start': 14, 'end': 15, 'probability': 0.995628420935013}]}}]}]
475+
```
476+
477+
- 情感倾向分类
478+
479+
句子级情感倾向分类,即判断句子的情感倾向是“正向”还是“负向”,schema构造如下:
480+
481+
```text
482+
['情感倾向[正向,负向]']
483+
```
484+
485+
```python
486+
>>> schema = ['情感倾向[正向,负向]'] # Define the schema for sentence-level sentiment classification
487+
>>> ie.set_schema(schema) # Reset schema
488+
>>> ie('这个产品用起来真的很流畅,我非常喜欢')
489+
[{'情感倾向[正向,负向]': [{'text': '正向', 'probability': 0.9990110458312529}]}]
490+
```
491+
492+
#### 多模型选择,满足精度、速度要求
493+
494+
使用`UIE-Medium`进行预测
495+
```python
496+
>>> from paddlenlp import Taskflow
497+
498+
>>> schema = ['出租方', '承租方']
499+
>>> ie = Taskflow('information_extraction', schema=schema, model="uie-medium")
500+
>>> ie('出租方:小明 地址:筒子街12号 电话:12345678900 承租方:小红 地址:新华路8号 电话:1234500000')
501+
[{'出租方': [{'text': '小明', 'start': 4, 'end': 6, 'probability': 0.9944859053454067}], '承租方': [{'text': '小红', 'start': 36, 'end': 38, 'probability': 0.8872193425652384}]}]
502+
```
503+
504+
使用`UIE-Large`进行预测
505+
```python
506+
>>> from paddlenlp import Taskflow
507+
508+
>>> schema = ['出租方', '承租方']
509+
>>> ie = Taskflow('information_extraction', schema=schema, model="uie-large")
510+
>>> ie('出租方:小明 地址:筒子街12号 电话:12345678900 承租方:小红 地址:新华路8号 电话:1234500000')
511+
[{'出租方': [{'text': '小明', 'start': 4, 'end': 6, 'probability': 0.9979592241500157}], '承租方': [{'text': '小红', 'start': 36, 'end': 38, 'probability': 0.7938207126153785}]}]
512+
```
513+
514+
#### 可配置参数说明
515+
* `batch_size`:批处理大小,请结合机器情况进行调整,默认为1。
516+
* `model`:选择任务使用的模型,默认为`uie-base`,可选有`uie-medium``uie-base``uie-large`
517+
* `schema`:定义任务抽取目标,可参考示例中对于不同信息抽取任务的schema配置自定义抽取目标。
518+
* `position_prob`:模型对于span的起始位置/终止位置的结果概率0~1之间,返回结果去掉小于这个阈值的结果,默认为0.5,span的最终概率输出为起始位置概率和终止位置概率的乘积。
519+
</div></details>
520+
397521
### 解语知识标注
398522
<details><summary>&emsp;覆盖所有中文词汇的知识标注工具</summary><div>
399523

paddlenlp/taskflow/information_extraction.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,33 @@ class UIETask(Task):
8484
"https://bj.bcebos.com/paddlenlp/taskflow/information_extraction/uie_base/model_state.pdparams",
8585
"004d7e53f5222349741217fabfb241ac"
8686
],
87-
}
87+
},
88+
"uie-medium": {
89+
"model_state": [
90+
"https://bj.bcebos.com/paddlenlp/taskflow/information_extraction/uie_medium/model_state.pdparams",
91+
"6248b4897fec78a61ab6da3edf9707fe"
92+
],
93+
},
94+
"uie-large": {
95+
"model_state": [
96+
"https://bj.bcebos.com/paddlenlp/taskflow/information_extraction/uie_large/model_state.pdparams",
97+
"83b6452ef2de41cc7ac44bbb18211cf7"
98+
],
99+
},
88100
}
89101

90102
def __init__(self, task, model, schema, **kwargs):
91103
super().__init__(task=task, model=model, **kwargs)
92104
self.set_schema(schema)
93-
self._encoding_model = "ernie-3.0-base"
105+
if model == "uie-base":
106+
self._encoding_model = "ernie-3.0-base"
107+
elif model == "uie-medium":
108+
self._encoding_model = "ernie-3.0-medium"
109+
elif model == "uie-large":
110+
self._encoding_model = "ernie-3.0-large"
111+
else:
112+
raise ValueError(
113+
"Model should be one of uie-base, uie-medium and uie-large")
94114
self._check_task_files()
95115
self._construct_tokenizer()
96116
self._get_inference_model()
@@ -129,7 +149,7 @@ def _construct_model(self, model):
129149
"""
130150
Construct the inference model for the predictor.
131151
"""
132-
model_instance = UIE(self._encoding_model)
152+
model_instance = UIE(self._encoding_model, self.kwargs['hidden_size'])
133153
model_path = os.path.join(self._task_path, "model_state.pdparams")
134154

135155
# Load the model parameter for the predict

paddlenlp/taskflow/models/information_extraction_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
class UIE(nn.Layer):
22-
def __init__(self, encoding_model, hidden_size=768):
22+
def __init__(self, encoding_model, hidden_size):
2323
super(UIE, self).__init__()
2424
self.encoder = AutoModel.from_pretrained(encoding_model)
2525
weight_attr_start = paddle.ParamAttr(name="weight_start")

paddlenlp/taskflow/taskflow.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,19 @@
223223
"models": {
224224
"uie-base": {
225225
"task_class": UIETask,
226+
"hidden_size": 768,
226227
"task_flag": "information_extraction-uie-base"
227228
},
229+
"uie-medium": {
230+
"task_class": UIETask,
231+
"hidden_size": 768,
232+
"task_flag": "information_extraction-uie-medium"
233+
},
234+
"uie-large": {
235+
"task_class": UIETask,
236+
"hidden_size": 1024,
237+
"task_flag": "information_extraction-uie-large"
238+
}
228239
},
229240
"default": {
230241
"model": "uie-base"
@@ -338,6 +349,6 @@ def interactive_mode(self, max_turn):
338349

339350
def set_schema(self, schema):
340351
assert self.task_instance.model in [
341-
"uie-base"
352+
"uie-base", "uie-medium", "uie-large"
342353
], 'This method can only used for the task with uie model.'
343354
self.task_instance.set_schema(schema)

0 commit comments

Comments
 (0)