File size: 6,868 Bytes
4f0d177
 
5190a45
4f0d177
5190a45
 
 
 
 
 
 
 
 
ec2824d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5190a45
 
ec2824d
 
 
 
 
 
 
 
 
 
 
 
5190a45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cbd8c7
5190a45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---
license: apache-2.0
language: zh
---


# uie-micro

## 介绍
* **[PaddlePaddle/uie-micro](https://huggingface.co/PaddlePaddle/uie-medium)** 的 Pytorch 实现
## 代码调用

### forward
#### *Parameters*
* `input_ids: Optional[torch.Tensor] = None`
* `token_type_ids: Optional[torch.Tensor] = None`
* `position_ids: Optional[torch.Tensor] = None`
* `attention_mask: Optional[torch.Tensor] = None`
* `head_mask: Optional[torch.Tensor] = None`
* `inputs_embeds: Optional[torch.Tensor] = None`
* `start_positions: Optional[torch.Tensor] = None`
* `end_positions: Optional[torch.Tensor] = None`
* `output_attentions: Optional[bool] = None`
* `output_hidden_states: Optional[bool] = None`
* `return_dict: Optional[bool] = None`

#### *Returns*  

* `UIEModelOutput or tuple(torch.FloatTensor)`

### predict
#### *Parameters*
* `schema: Union[Dict, List[str], str]`
* `input_texts: Union[List[str], str]`
* `tokenizer: PreTrainedTokenizerFast`
* `max_length: int = 512`
* `batch_size: int = 32`
* `position_prob: int = 0.5`
* `progress_hook=None`

#### *Returns*  
* `List[Dict]`


```python
from tqdm import tqdm
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained('Casually/uie-micro', trust_remote_code=True)
model.eval().to('cuda')
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
hook = tqdm()
schema = {'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}
model.predict(schema=schema,
              input_texts='中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24.34度,东经99.98度)发生3.5级地震,震源深度10千米。',
              tokenizer=tokenizer,
              progress_hook=hook
              )
```
```ipython
100%|██████████| 5/5 [00:00<00:00, 10.35it/s]
[{'地震触发词': [{'end': 58,
             'probability': 0.9953331562546808,
             'relations': {'地震强度': [{'end': 56,
                                     'probability': 0.9719095188981299,
                                     'start': 52,
                                     'text': '3.5级'}],
                           '时间': [{'end': 22,
                                   'probability': 0.9653931540843175,
                                   'start': 11,
                                   'text': '5月16日06时08分'}],
                           '震中位置': [{'end': 50,
                                     'probability': 0.6063880101553423,
                                     'start': 23,
                                     'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
                           '震源深度': [{'end': 67,
                                     'probability': 0.989598549365315,
                                     'start': 63,
                                     'text': '10千米'}]},
             'start': 56,
             'text': '地震'}]}]
```

## 应用示例

### 实体抽取

```python
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained('Casually/uie-micro', trust_remote_code=True)
model.eval().to('cuda')
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
schema = ['时间', '选手', '赛事名称']
res = model.predict(schema=schema,
                    input_texts="2月8日上午北京冬奥会自由式滑雪女子大跳台决赛中中国选手谷爱凌以188.25分获得金牌!",
                    tokenizer=tokenizer,
                    )
```

```ipython
>>> from pprint import pprint
>>> pprint(res)
[{'时间': [{'end': 6,
          'probability': 0.906374348561485,
          'start': 0,
          'text': '2月8日上午'}],
  '选手': [{'end': 31,
          'probability': 0.8768158783169611,
          'start': 28,
          'text': '谷爱凌'}]}]
```



### 关系抽取
```python
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained('Casually/uie-micro', trust_remote_code=True)
model.eval().to('cuda')
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
schema = {'竞赛名称': ['主办方', '承办方', '已举办次数']}
res = model.predict(schema=schema,
                    input_texts='2022语言与智能技术竞赛由中国中文信息学会和中国计算机学会联合主办,百度公司、中国中文信息学会评测工作委员会和中国计算机学会自然语言处理专委会承办,已连续举办4届,成为全球最热门的中文NLP赛事之一。',
                    tokenizer=tokenizer,
                    )
```

```ipython
>>> from pprint import pprint
>>> pprint(res)
[{'竞赛名称': [{'end': 13,
            'probability': 0.6710958346658344,
            'relations': {'已举办次数': [{'end': 82,
                                     'probability': 0.7727802061877256,
                                     'start': 80,
                                     'text': '4届'}]},
            'start': 0,
            'text': '2022语言与智能技术竞赛'}]}]
```

### 事件抽取

```python
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained('Casually/uie-micro', trust_remote_code=True)
model.eval().to('cuda')
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
schema = {'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}
res = model.predict(schema=schema,
                    input_texts='中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24.34度,东经99.98度)发生3.5级地震,震源深度10千米。',
                    tokenizer=tokenizer,
                    )
```

```ipython
>>> from pprint import pprint
>>> pprint(res)
[{'地震触发词': [{'end': 58,
             'probability': 0.9953331562546808,
             'relations': {'地震强度': [{'end': 56,
                                     'probability': 0.9719095188981299,
                                     'start': 52,
                                     'text': '3.5级'}],
                           '时间': [{'end': 22,
                                   'probability': 0.9653931540843175,
                                   'start': 11,
                                   'text': '5月16日06时08分'}],
                           '震中位置': [{'end': 50,
                                     'probability': 0.6063880101553423,
                                     'start': 23,
                                     'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
                           '震源深度': [{'end': 67,
                                     'probability': 0.989598549365315,
                                     'start': 63,
                                     'text': '10千米'}]},
             'start': 56,
             'text': '地震'}]}]
```