forked from PaddlePaddle/PaddleHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.py
421 lines (349 loc) · 15.8 KB
/
module.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import ast
import io
import math
import os
import numpy as np
import six
from paddle.inference import Config
from paddle.inference import create_predictor
from .custom import Customization
from .processor import load_kv_dict
from .processor import parse_result
from .processor import word_to_ids
from paddlehub.module.module import moduleinfo
from paddlehub.module.module import runnable
from paddlehub.module.module import serving
from paddlehub.utils.parser import txt_parser
from paddlehub.utils.utils import sys_stdin_encoding
class DataFormatError(Exception):
def __init__(self, *args):
self.args = args
@moduleinfo(
name="lac",
version="2.4.0",
summary=
"Baidu's open-source lexical analysis tool for Chinese, including word segmentation, part-of-speech tagging & named entity recognition",
author="baidu-nlp",
author_email="[email protected]",
type="nlp/lexical_analysis")
class LAC:
def __init__(self, user_dict=None):
"""
initialize with the necessary elements
"""
self.default_pretrained_model_path = os.path.join(self.directory, "infer_model", "model")
self.word2id_dict = load_kv_dict(os.path.join(self.directory, "assets/word.dic"), reverse=True, value_func=int)
self.id2word_dict = load_kv_dict(os.path.join(self.directory, "assets/word.dic"))
self.label2id_dict = load_kv_dict(os.path.join(self.directory, "assets/tag.dic"), reverse=True, value_func=int)
self.id2label_dict = load_kv_dict(os.path.join(self.directory, "assets/tag.dic"))
self.word_replace_dict = load_kv_dict(os.path.join(self.directory, "assets/q2b.dic"))
self.oov_id = self.word2id_dict['OOV']
self.word_dict_len = max(map(int, self.word2id_dict.values())) + 1
self.label_dict_len = max(map(int, self.label2id_dict.values())) + 1
self.tag_file = os.path.join(self.directory, "assets/tag_file.txt")
if user_dict:
self.set_user_dict(dict_path=user_dict)
else:
self.custom = None
self._set_config()
def _set_config(self):
"""
predictor config setting
"""
model = self.default_pretrained_model_path + '.pdmodel'
params = self.default_pretrained_model_path + '.pdiparams'
cpu_config = Config(model, params)
cpu_config.disable_glog_info()
cpu_config.disable_gpu()
self.cpu_predictor = create_predictor(cpu_config)
try:
_places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0])
use_gpu = True
except:
use_gpu = False
if use_gpu:
gpu_config = Config(model, params)
gpu_config.disable_glog_info()
gpu_config.enable_use_gpu(memory_pool_init_size_mb=500, device_id=0)
self.gpu_predictor = create_predictor(gpu_config)
def set_user_dict(self, dict_path, sep=None):
"""
Set the costomized dictionary if you wanna exploit the self-defined dictionary
Args:
dict_path(str): The directory to the costomized dictionary.
sep: The seperation token in phases. Default as ' ' or '\t'.
"""
if not os.path.exists(dict_path):
raise RuntimeError("File %s is not exist." % dict_path)
self.custom = Customization()
self.custom.load_customization(dict_path, sep)
def del_user_dict(self):
"""
Delete the costomized dictionary if you don't wanna exploit the self-defined dictionary any longer
"""
if self.custom:
self.custom = None
print("Successfully delete the customized dictionary!")
def to_unicode(self, texts):
"""
Convert each element's type(str) of texts(list) to unicode in python2.7
Args:
texts(list): each element's type is str in python2.7
Returns:
texts(list): each element's type is unicode in python2.7
"""
if six.PY2:
unicode_texts = []
for text in texts:
if isinstance(text, six.string_types):
unicode_texts.append(text.decode(sys_stdin_encoding()).decode("utf8"))
else:
unicode_texts.append(text)
texts = unicode_texts
return texts
def preprocess(self, texts):
"""
Tranform the texts(list) to PaddleTensor
Args:
texts(list): texts
Returns:
np.array, list, list
"""
lod = [0]
data = []
for i, text in enumerate(texts):
text_inds = word_to_ids(text, self.word2id_dict, self.word_replace_dict, oov_id=self.oov_id)
data += text_inds
lod.append(len(text_inds) + lod[i])
return np.array(data).astype('int64'), [lod], [lod[-1], 1]
def _get_index(self, data_list, item=""):
"""
find all indexes of item in data_list
"""
res = []
for index, data in enumerate(data_list):
if data == item:
res.append(index)
return res
@serving
def cut(self, text, use_gpu=False, batch_size=1, return_tag=True):
"""
The main function that segments an entire text that contains
Chinese characters into separated words.
Args:
text(:obj:`str` or :obj:`List[str]`): The chinese texts to be segmented. This can be a string, a list of strings.
use_gpu(bool): whether use gpu to predict or not
batch_size(int): the program deals once with one batch
return_tag: Whether to get tag or not.
Returns:
results(dict or list): The word segmentation result of the input text, whose key is 'word', if text is a list.
If text is a str, the word segmentation result (list) is obtained.
"""
if use_gpu:
try:
_places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0])
except:
raise RuntimeError(
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES as cuda_device_id."
)
if isinstance(text, list) and len(text) != 0:
predicted_data = self.to_unicode(text)
# drop the empty string like "" in predicted_data
empty_str_indexes = self._get_index(predicted_data)
predicted_data = [data for data in predicted_data if data != ""]
start_idx = 0
iteration = int(math.ceil(len(predicted_data) / batch_size))
results = []
for i in range(iteration):
if i < (iteration - 1):
batch_data = predicted_data[start_idx:(start_idx + batch_size)]
else:
batch_data = predicted_data[start_idx:]
start_idx = start_idx + batch_size
data, lod, shape = self.preprocess(batch_data)
predictor = self.gpu_predictor if use_gpu else self.cpu_predictor
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
input_handle.copy_from_cpu(data)
input_handle.set_lod(lod)
input_handle.reshape(shape)
predictor.run()
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])
batch_result = parse_result(batch_data, output_handle, self.id2label_dict, interventer=self.custom)
results += batch_result
for index in empty_str_indexes:
results.insert(index, {"word": [""], "tag": [""]})
if not return_tag:
for result in results:
result = result.pop("tag")
return results
return results
elif isinstance(text, str) and text != "":
data, lod, shape = self.preprocess([text])
predictor = self.gpu_predictor if use_gpu else self.cpu_predictor
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
input_handle.copy_from_cpu(data)
input_handle.set_lod(lod)
input_handle.reshape(shape)
predictor.run()
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])
batch_result = parse_result([text], output_handle, self.id2label_dict, interventer=self.custom)
return batch_result[0]['word']
elif text == "":
return text
else:
raise TypeError("The input data is inconsistent with expectations.")
def lexical_analysis(self, texts=[], data={}, use_gpu=False, batch_size=1, return_tag=True):
"""
Get the word segmentation results with the texts as input
Args:
texts(list): the input texts to be segmented, if texts not data
data(dict): key must be 'text', value is the texts to be segmented, if data not texts
use_gpu(bool): whether use gpu to predict or not
batch_size(int): the program deals once with one batch
return_tag: Whether to get tag or not.
Returns:
results(list): the word segmentation results
"""
if use_gpu:
try:
_places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0])
except:
raise RuntimeError(
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES as cuda_device_id."
)
if texts != [] and isinstance(texts, list) and data == {}:
predicted_data = texts
elif texts == [] and isinstance(data, dict) and isinstance(data.get('text', None), list) and data['text']:
predicted_data = data["text"]
else:
raise TypeError("The input data is inconsistent with expectations.")
predicted_data = self.to_unicode(predicted_data)
# drop the empty string like "" in predicted_data
empty_str_indexes = self._get_index(predicted_data)
predicted_data = [data for data in predicted_data if data != ""]
start_idx = 0
iteration = int(math.ceil(len(predicted_data) / batch_size))
results = []
for i in range(iteration):
if i < (iteration - 1):
batch_data = predicted_data[start_idx:(start_idx + batch_size)]
else:
batch_data = predicted_data[start_idx:]
start_idx = start_idx + batch_size
data, lod, shape = self.preprocess(batch_data)
predictor = self.gpu_predictor if use_gpu else self.cpu_predictor
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
input_handle.copy_from_cpu(data)
input_handle.set_lod(lod)
input_handle.reshape(shape)
predictor.run()
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])
batch_result = parse_result(batch_data, output_handle, self.id2label_dict, interventer=self.custom)
results += batch_result
for index in empty_str_indexes:
results.insert(index, {"word": [""], "tag": [""]})
if not return_tag:
for result in results:
result = result.pop("tag")
return results
return results
@runnable
def run_cmd(self, argvs):
"""
Run as a command
"""
self.parser = argparse.ArgumentParser(description="Run the lac module.",
prog='hub run lac',
usage='%(prog)s',
add_help=True)
self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group(
title="Config options", description="Run configuration for controlling module behavior, not required.")
self.add_module_config_arg()
self.add_module_input_arg()
args = self.parser.parse_args(argvs)
try:
input_data = self.check_input_data(args)
except DataFormatError and RuntimeError:
self.parser.print_help()
return None
if args.user_dict:
self.set_user_dict(args.user_dict)
results = self.lexical_analysis(texts=input_data,
use_gpu=args.use_gpu,
batch_size=args.batch_size,
return_tag=args.return_tag)
return results
def get_tags(self):
"""
Get the tags which was used when pretraining lac
Returns:
self.tag_name_dict(dict):lac tags
"""
self.tag_name_dict = {}
with io.open(self.tag_file, encoding="utf8") as f:
for line in f:
tag, tag_name = line.strip().split(" ")
self.tag_name_dict[tag] = tag_name
return self.tag_name_dict
def add_module_config_arg(self):
"""
Add the command config options
"""
self.arg_config_group.add_argument('--use_gpu',
type=ast.literal_eval,
default=False,
help="whether use GPU or not")
self.arg_config_group.add_argument('--batch_size', type=int, default=1, help="batch size for prediction")
self.arg_config_group.add_argument('--user_dict',
type=str,
default=None,
help="customized dictionary for intervening the word segmentation result")
self.arg_config_group.add_argument('--return_tag',
type=ast.literal_eval,
default=True,
help="whether return tags of results or not")
def add_module_input_arg(self):
"""
Add the command input options
"""
self.arg_input_group.add_argument('--input_file', type=str, default=None, help="file contain input data")
self.arg_input_group.add_argument('--input_text', type=str, default=None, help="text to predict")
def check_input_data(self, args):
input_data = []
if args.input_file:
if not os.path.exists(args.input_file):
print("File %s is not exist." % args.input_file)
raise RuntimeError
else:
input_data = txt_parser.parse(args.input_file, use_strip=True)
elif args.input_text:
if args.input_text.strip() != '':
if six.PY2:
input_data = [args.input_text.decode(sys_stdin_encoding()).decode("utf8")]
else:
input_data = [args.input_text]
if input_data == []:
print("ERROR: The input data is inconsistent with expectations.")
raise DataFormatError
return input_data
def create_gradio_app(self):
import gradio as gr
return gr.Interface(self.cut,
gr.Text(label='text'),
gr.JSON(label='results'),
title='lac',
allow_flagging='never')