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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
+
language: zh
|
| 4 |
---
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# uie-medium
|
| 8 |
+
|
| 9 |
+
## 介绍
|
| 10 |
+
* **[PaddlePaddle/uie-medium](https://huggingface.co/PaddlePaddle/uie-medium)** 的 Pytorch 实现
|
| 11 |
+
|
| 12 |
+
## 代码调用
|
| 13 |
+
|
| 14 |
+
### forward
|
| 15 |
+
> **Parameters**
|
| 16 |
+
> * `input_ids: Optional[torch.Tensor] = None`
|
| 17 |
+
> * `token_type_ids: Optional[torch.Tensor] = None`
|
| 18 |
+
> * `position_ids: Optional[torch.Tensor] = None`
|
| 19 |
+
> * `attention_mask: Optional[torch.Tensor] = None`
|
| 20 |
+
> * `head_mask: Optional[torch.Tensor] = None`
|
| 21 |
+
> * `inputs_embeds: Optional[torch.Tensor] = None`
|
| 22 |
+
> * `start_positions: Optional[torch.Tensor] = None`
|
| 23 |
+
> * `end_positions: Optional[torch.Tensor] = None`
|
| 24 |
+
> * `output_attentions: Optional[bool] = None`
|
| 25 |
+
> * `output_hidden_states: Optional[bool] = None`
|
| 26 |
+
> * `return_dict: Optional[bool] = None`
|
| 27 |
+
>
|
| 28 |
+
> **Returns** *UIEModelOutput or tuple(torch.FloatTensor)*
|
| 29 |
+
|
| 30 |
+
### predict
|
| 31 |
+
> **Parameters**
|
| 32 |
+
> * `schema: Union[Dict, List[str], str]`
|
| 33 |
+
> * `input_texts: Union[List[str], str]`
|
| 34 |
+
> * `tokenizer: PreTrainedTokenizerFast`
|
| 35 |
+
> * `max_length: int = 512`
|
| 36 |
+
> * `batch_size: int = 32`
|
| 37 |
+
> * `position_prob: int = 0.5`
|
| 38 |
+
> * `progress_hook=None`
|
| 39 |
+
>
|
| 40 |
+
> **Returns** * List[Dict]*
|
| 41 |
+
|
| 42 |
+
```python
|
| 43 |
+
from tqdm import tqdm
|
| 44 |
+
from transformers import AutoModel, AutoTokenizer
|
| 45 |
+
model = AutoModel.from_pretrained('Casually/uie-medium', trust_remote_code=True)
|
| 46 |
+
model.eval().to('cuda')
|
| 47 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-medium')
|
| 48 |
+
hook = tqdm()
|
| 49 |
+
schema = {'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}
|
| 50 |
+
model.predict(schema=schema,
|
| 51 |
+
input_texts='中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24.34度,东经99.98度)发生3.5级地震,震源深度10千米。',
|
| 52 |
+
tokenizer=tokenizer,
|
| 53 |
+
progress_hook=hook
|
| 54 |
+
)
|
| 55 |
+
```
|
| 56 |
+
```ipython
|
| 57 |
+
100%|█████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 5.92it/s]
|
| 58 |
+
[{'地震触发词': [{'end': 58,
|
| 59 |
+
'probability': 0.9760800067224977,
|
| 60 |
+
'relations': {'地震强度': [{'end': 56,
|
| 61 |
+
'probability': 0.9914701318735943,
|
| 62 |
+
'start': 52,
|
| 63 |
+
'text': '3.5级'}],
|
| 64 |
+
'时间': [{'end': 22,
|
| 65 |
+
'probability': 0.9744665638249757,
|
| 66 |
+
'start': 11,
|
| 67 |
+
'text': '5月16日06时08分'}],
|
| 68 |
+
'震中位置': [{'end': 50,
|
| 69 |
+
'probability': 0.8756744991726784,
|
| 70 |
+
'start': 23,
|
| 71 |
+
'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
|
| 72 |
+
'震源深度': [{'end': 67,
|
| 73 |
+
'probability': 0.9953776312703866,
|
| 74 |
+
'start': 63,
|
| 75 |
+
'text': '10千米'}]},
|
| 76 |
+
'start': 56,
|
| 77 |
+
'text': '地震'}]}]
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
## 应用示例
|
| 81 |
+
|
| 82 |
+
### 实体抽取
|
| 83 |
+
|
| 84 |
+
```python
|
| 85 |
+
from transformers import AutoModel, AutoTokenizer
|
| 86 |
+
model = AutoModel.from_pretrained('Casually/uie-medium', trust_remote_code=True)
|
| 87 |
+
model.eval().to('cuda')
|
| 88 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-medium')
|
| 89 |
+
schema = ['时间', '选手', '赛事名称']
|
| 90 |
+
res = model.predict(schema=schema,
|
| 91 |
+
input_texts="2月8日上午北京冬奥会自由式滑雪女子大跳台决赛中中国选手谷爱凌以188.25分获得金牌!",
|
| 92 |
+
tokenizer=tokenizer,
|
| 93 |
+
)
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
```ipython
|
| 97 |
+
>>> from pprint import pprint
|
| 98 |
+
>>> pprint(res)
|
| 99 |
+
[{'时间': [{'end': 6,
|
| 100 |
+
'probability': 0.9492842181233527,
|
| 101 |
+
'start': 0,
|
| 102 |
+
'text': '2月8日上午'}],
|
| 103 |
+
'赛事名称': [{'end': 23,
|
| 104 |
+
'probability': 0.8751025829007837,
|
| 105 |
+
'start': 6,
|
| 106 |
+
'text': '北京冬奥会自由式滑雪女子大跳台决赛'}],
|
| 107 |
+
'选手': [{'end': 31,
|
| 108 |
+
'probability': 0.727718602068137,
|
| 109 |
+
'start': 28,
|
| 110 |
+
'text': '谷爱凌'}]}]
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
### 关系抽取
|
| 116 |
+
```python
|
| 117 |
+
from transformers import AutoModel, AutoTokenizer
|
| 118 |
+
model = AutoModel.from_pretrained('Casually/uie-medium', trust_remote_code=True)
|
| 119 |
+
model.eval().to('cuda')
|
| 120 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-medium')
|
| 121 |
+
schema = {'竞赛名称': ['主办方', '承办方', '已举办次数']}
|
| 122 |
+
res = model.predict(schema=schema,
|
| 123 |
+
input_texts='2022语言与智能技术竞赛由中国中文信息学会和中国计算机学会联合主办,百度公司、中国中文信息学会评测工作委员会和中国计算机学会自然语言处理专委会承办,已连续举办4届,成为全球最热门的中文NLP赛事之一。',
|
| 124 |
+
tokenizer=tokenizer,
|
| 125 |
+
)
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
```ipython
|
| 129 |
+
>>> from pprint import pprint
|
| 130 |
+
>>> pprint(res)
|
| 131 |
+
[{'竞赛名称': [{'end': 13,
|
| 132 |
+
'probability': 0.9181450958544701,
|
| 133 |
+
'relations': {'主办方': [{'end': 22,
|
| 134 |
+
'probability': 0.7343272429010028,
|
| 135 |
+
'start': 14,
|
| 136 |
+
'text': '中国中文信息学会'},
|
| 137 |
+
{'end': 30,
|
| 138 |
+
'probability': 0.6493546533916614,
|
| 139 |
+
'start': 23,
|
| 140 |
+
'text': '中国计算机学会'}],
|
| 141 |
+
'已举办次数': [{'end': 82,
|
| 142 |
+
'probability': 0.3304018101023303,
|
| 143 |
+
'start': 80,
|
| 144 |
+
'text': '4届'}],
|
| 145 |
+
'承办方': [{'end': 55,
|
| 146 |
+
'probability': 0.566899022081806,
|
| 147 |
+
'start': 40,
|
| 148 |
+
'text': '中国中文信息学会评测工作委员会'},
|
| 149 |
+
{'end': 39,
|
| 150 |
+
'probability': 0.7439131999045969,
|
| 151 |
+
'start': 35,
|
| 152 |
+
'text': '百度公司'},
|
| 153 |
+
{'end': 72,
|
| 154 |
+
'probability': 0.6428244378844958,
|
| 155 |
+
'start': 56,
|
| 156 |
+
'text': '中国计算机学会自然语言处理专委会'}]},
|
| 157 |
+
'start': 0,
|
| 158 |
+
'text': '2022语言与智能技术竞赛'}]}]
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
### 事件抽取
|
| 162 |
+
|
| 163 |
+
```python
|
| 164 |
+
from transformers import AutoModel, AutoTokenizer
|
| 165 |
+
model = AutoModel.from_pretrained('Casually/uie-medium', trust_remote_code=True)
|
| 166 |
+
model.eval().to('cuda')
|
| 167 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-medium')
|
| 168 |
+
schema = {'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}
|
| 169 |
+
res = model.predict(schema=schema,
|
| 170 |
+
input_texts='中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24.34度,东经99.98度)发生3.5级地震,震源深度10千米。',
|
| 171 |
+
tokenizer=tokenizer,
|
| 172 |
+
)
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
```ipython
|
| 176 |
+
>>> from pprint import pprint
|
| 177 |
+
>>> pprint(res)
|
| 178 |
+
[{'地震触发词': [{'end': 58,
|
| 179 |
+
'probability': 0.9760800067224977,
|
| 180 |
+
'relations': {'地震强度': [{'end': 56,
|
| 181 |
+
'probability': 0.9914701318735943,
|
| 182 |
+
'start': 52,
|
| 183 |
+
'text': '3.5级'}],
|
| 184 |
+
'时间': [{'end': 22,
|
| 185 |
+
'probability': 0.9744665638249757,
|
| 186 |
+
'start': 11,
|
| 187 |
+
'text': '5月16日06时08分'}],
|
| 188 |
+
'震中位置': [{'end': 50,
|
| 189 |
+
'probability': 0.8756744991726784,
|
| 190 |
+
'start': 23,
|
| 191 |
+
'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
|
| 192 |
+
'震源深度': [{'end': 67,
|
| 193 |
+
'probability': 0.9953776312703866,
|
| 194 |
+
'start': 63,
|
| 195 |
+
'text': '10千米'}]},
|
| 196 |
+
'start': 56,
|
| 197 |
+
'text': '地震'}]}]
|
| 198 |
+
```
|