Upload README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,171 @@
|
|
1 |
---
|
2 |
license: apache-2.0
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: apache-2.0
|
3 |
+
language: zh
|
4 |
---
|
5 |
+
|
6 |
+
|
7 |
+
# uie-nano
|
8 |
+
|
9 |
+
## 介绍
|
10 |
+
* **[PaddlePaddle/uie-nano](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-nano', trust_remote_code=True)
|
46 |
+
model.eval().to('cuda')
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-nano')
|
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, 10.35it/s]
|
58 |
+
[{'地震触发词': [{'end': 58,
|
59 |
+
'probability': 0.9719620194304142,
|
60 |
+
'relations': {'地震强度': [{'end': 56,
|
61 |
+
'probability': 0.9345790644244119,
|
62 |
+
'start': 52,
|
63 |
+
'text': '3.5级'}],
|
64 |
+
'时间': [{'end': 22,
|
65 |
+
'probability': 0.9363583660080046,
|
66 |
+
'start': 11,
|
67 |
+
'text': '5月16日06时08分'}],
|
68 |
+
'震中位置': [{'end': 50,
|
69 |
+
'probability': 0.8117188157344657,
|
70 |
+
'start': 23,
|
71 |
+
'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
|
72 |
+
'震源深度': [{'end': 67,
|
73 |
+
'probability': 0.9900854956863228,
|
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-nano', trust_remote_code=True)
|
87 |
+
model.eval().to('cuda')
|
88 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-nano')
|
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.6513584338899392,
|
101 |
+
'start': 0,
|
102 |
+
'text': '2月8日上午'}],
|
103 |
+
'赛事名称': [{'end': 23,
|
104 |
+
'probability': 0.4908133591440702,
|
105 |
+
'start': 6,
|
106 |
+
'text': '北京冬奥会自由式滑雪女子大跳台决赛'}],
|
107 |
+
'选手': [{'end': 31,
|
108 |
+
'probability': 0.9819330659468051,
|
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-nano', trust_remote_code=True)
|
119 |
+
model.eval().to('cuda')
|
120 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-nano')
|
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 |
+
[{}]
|
132 |
+
```
|
133 |
+
|
134 |
+
### 事件抽取
|
135 |
+
|
136 |
+
```python
|
137 |
+
from transformers import AutoModel, AutoTokenizer
|
138 |
+
model = AutoModel.from_pretrained('Casually/uie-nano', trust_remote_code=True)
|
139 |
+
model.eval().to('cuda')
|
140 |
+
tokenizer = AutoTokenizer.from_pretrained('Casually/uie-nano')
|
141 |
+
schema = {'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}
|
142 |
+
res = model.predict(schema=schema,
|
143 |
+
input_texts='中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24.34度,东经99.98度)发生3.5级地震,震源深度10千米。',
|
144 |
+
tokenizer=tokenizer,
|
145 |
+
)
|
146 |
+
```
|
147 |
+
|
148 |
+
```ipython
|
149 |
+
>>> from pprint import pprint
|
150 |
+
>>> pprint(res)
|
151 |
+
[{'地震触发词': [{'end': 58,
|
152 |
+
'probability': 0.9719620194304142,
|
153 |
+
'relations': {'地震强度': [{'end': 56,
|
154 |
+
'probability': 0.9345790644244119,
|
155 |
+
'start': 52,
|
156 |
+
'text': '3.5级'}],
|
157 |
+
'时间': [{'end': 22,
|
158 |
+
'probability': 0.9363583660080046,
|
159 |
+
'start': 11,
|
160 |
+
'text': '5月16日06时08分'}],
|
161 |
+
'震中位置': [{'end': 50,
|
162 |
+
'probability': 0.8117188157344657,
|
163 |
+
'start': 23,
|
164 |
+
'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
|
165 |
+
'震源深度': [{'end': 67,
|
166 |
+
'probability': 0.9900854956863228,
|
167 |
+
'start': 63,
|
168 |
+
'text': '10千米'}]},
|
169 |
+
'start': 56,
|
170 |
+
'text': '地震'}]}]
|
171 |
+
```
|