Casually commited on
Commit
5190a45
1 Parent(s): 4f0d177

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +171 -0
README.md CHANGED
@@ -1,3 +1,174 @@
1
  ---
2
  license: apache-2.0
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ language: zh
4
  ---
5
+
6
+
7
+ # uie-micro
8
+
9
+ ## 介绍
10
+ * **[PaddlePaddle/uie-micro](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-micro', trust_remote_code=True)
46
+ model.eval().to('cuda')
47
+ tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
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.9953331562546808,
60
+ 'relations': {'地震强度': [{'end': 56,
61
+ 'probability': 0.9719095188981299,
62
+ 'start': 52,
63
+ 'text': '3.5级'}],
64
+ '时间': [{'end': 22,
65
+ 'probability': 0.9653931540843175,
66
+ 'start': 11,
67
+ 'text': '5月16日06时08分'}],
68
+ '震中位置': [{'end': 50,
69
+ 'probability': 0.6063880101553423,
70
+ 'start': 23,
71
+ 'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
72
+ '震源深度': [{'end': 67,
73
+ 'probability': 0.989598549365315,
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-micro', trust_remote_code=True)
87
+ model.eval().to('cuda')
88
+ tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
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.906374348561485,
101
+ 'start': 0,
102
+ 'text': '2月8日上午'}],
103
+ '选手': [{'end': 31,
104
+ 'probability': 0.8768158783169611,
105
+ 'start': 28,
106
+ 'text': '谷爱凌'}]}]
107
+ ```
108
+
109
+
110
+
111
+ ### 关系抽取
112
+ ```python
113
+ from transformers import AutoModel, AutoTokenizer
114
+ model = AutoModel.from_pretrained('Casually/uie-micro', trust_remote_code=True)
115
+ model.eval().to('cuda')
116
+ tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
117
+ schema = {'竞赛名称': ['主办方', '承办方', '已举办次数']}
118
+ res = model.predict(schema=schema,
119
+ input_texts='2022语言与智能技术竞赛由中国中文信息学会和中国计算机学会联合主办,百度公司、中国中文信息学会评测工作委员会和中国计算机学会自然语言处理专委会承办,已连续举办4届,成为全球最热门的中文NLP赛事之一。',
120
+ tokenizer=tokenizer,
121
+ )
122
+ ```
123
+
124
+ ```ipython
125
+ >>> from pprint import pprint
126
+ >>> pprint(res)
127
+ [{'竞赛名称': [{'end': 13,
128
+ 'probability': 0.6710958346658344,
129
+ 'relations': {'已举办次数': [{'end': 82,
130
+ 'probability': 0.7727802061877256,
131
+ 'start': 80,
132
+ 'text': '4届'}]},
133
+ 'start': 0,
134
+ 'text': '2022语言与智能技术竞赛'}]}]
135
+ ```
136
+
137
+ ### 事件抽取
138
+
139
+ ```python
140
+ from transformers import AutoModel, AutoTokenizer
141
+ model = AutoModel.from_pretrained('Casually/uie-micro', trust_remote_code=True)
142
+ model.eval().to('cuda')
143
+ tokenizer = AutoTokenizer.from_pretrained('Casually/uie-micro')
144
+ schema = {'地震触发词': ['地震强度', '时间', '震中位置', '震源深度']}
145
+ res = model.predict(schema=schema,
146
+ input_texts='中国地震台网正式测定:5月16日06时08分在云南临沧市凤庆县(北纬24.34度,东经99.98度)发生3.5级地震,震源深度10千米。',
147
+ tokenizer=tokenizer,
148
+ )
149
+ ```
150
+
151
+ ```ipython
152
+ >>> from pprint import pprint
153
+ >>> pprint(res)
154
+ [{'地震触发词': [{'end': 58,
155
+ 'probability': 0.9953331562546808,
156
+ 'relations': {'地震强度': [{'end': 56,
157
+ 'probability': 0.9719095188981299,
158
+ 'start': 52,
159
+ 'text': '3.5级'}],
160
+ '时间': [{'end': 22,
161
+ 'probability': 0.9653931540843175,
162
+ 'start': 11,
163
+ 'text': '5月16日06时08分'}],
164
+ '震中位置': [{'end': 50,
165
+ 'probability': 0.6063880101553423,
166
+ 'start': 23,
167
+ 'text': '云南临沧市凤庆县(北纬24.34度,东经99.98度)'}],
168
+ '震源深度': [{'end': 67,
169
+ 'probability': 0.989598549365315,
170
+ 'start': 63,
171
+ 'text': '10千米'}]},
172
+ 'start': 56,
173
+ 'text': '地震'}]}]
174
+ ```