Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upd dp-ner with extended version #189

Merged
merged 10 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions annotators/NER_deeppavlov/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ COPY $SRC_DIR /src
WORKDIR /src

RUN python -m deeppavlov install $CONFIG
RUN python -m deeppavlov download $CONFIG

CMD gunicorn --workers=1 --timeout 500 server:app -b 0.0.0.0:8021
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
{
"dataset_reader": {
"class_name": "conll2003_reader",
"data_path": "{DOWNLOADS_PATH}/conll2003/",
"dataset_name": "conll2003",
"provide_pos": false
},
"dataset_iterator": {
"class_name": "data_learning_iterator"
},
"chainer": {
"in": [
"x"
],
"in_y": [
"y"
],
"pipe": [
{
"class_name": "torch_transformers_ner_preprocessor",
"vocab_file": "{TRANSFORMER}",
"do_lower_case": false,
"max_seq_length": 512,
"max_subword_length": 15,
"token_masking_prob": 0.0,
"in": [
"x"
],
"out": [
"x_tokens",
"x_subword_tokens",
"x_subword_tok_ids",
"startofword_markers",
"attention_mask"
]
},
{
"id": "tag_vocab",
"class_name": "simple_vocab",
"unk_token": [
"O"
],
"pad_with_zeros": true,
"save_path": "{MODEL_PATH}/tag.dict",
"load_path": "{MODEL_PATH}/tag.dict",
"fit_on": [
"y"
],
"in": [
"y"
],
"out": [
"y_ind"
]
},
{
"class_name": "torch_transformers_sequence_tagger",
"n_tags": "#tag_vocab.len",
"pretrained_bert": "{TRANSFORMER}",
"attention_probs_keep_prob": 0.5,
"return_probas": false,
"use_crf": true,
"encoder_layer_ids": [
-1
],
"optimizer": "AdamW",
"optimizer_parameters": {
"lr": 2e-05,
"weight_decay": 1e-06,
"betas": [
0.9,
0.999
],
"eps": 1e-06
},
"clip_norm": 1.0,
"min_learning_rate": 1e-07,
"learning_rate_drop_patience": 20,
"learning_rate_drop_div": 1.5,
"load_before_drop": true,
"save_path": "{MODEL_PATH}/model",
"load_path": "{MODEL_PATH}/model",
"in": [
"x_subword_tok_ids",
"attention_mask",
"startofword_markers"
],
"in_y": [
"y_ind"
],
"out": [
"y_pred_ind",
"probas"
]
},
{
"ref": "tag_vocab",
"in": [
"y_pred_ind"
],
"out": [
"y_pred"
]
}
],
"out": [
"x_tokens",
"y_pred"
]
},
"train": {
"epochs": 50,
"batch_size": 100,
"metrics": [
{
"name": "ner_f1",
"inputs": [
"y",
"y_pred"
]
},
{
"name": "ner_token_f1",
"inputs": [
"y",
"y_pred"
],
"print_results": true
}
],
"validation_patience": 100,
"val_every_n_batches": 50,
"log_every_n_batches": 50,
"show_examples": false,
"pytest_max_batches": 2,
"pytest_batch_size": 8,
"evaluation_targets": [
"test"
],
"class_name": "torch_trainer"
},
"metadata": {
"variables": {
"ROOT_PATH": "~/.deeppavlov",
"DOWNLOADS_PATH": "~/.deeppavlov/downloads",
"MODELS_PATH": "~/.deeppavlov/models",
"TRANSFORMER": "bert-base-multilingual-cased",
"MODEL_PATH": "{MODELS_PATH}/ner/mbert_dream_with_numbers_rus_ext"
},
"download": [
{
"url": "http://files.deeppavlov.ai/v1/ner/mbert_dream_with_numbers.tar.gz",
"subdir": "{MODEL_PATH}"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"dataset_reader": {
"class_name": "conll2003_reader",
"data_path": "{DOWNLOADS_PATH}/conll2003/",
"dataset_name": "conll2003",
"provide_pos": false
},
"dataset_iterator": {
"class_name": "data_learning_iterator"
},
"chainer": {
"in": [
"x"
],
"in_y": [
"y"
],
"pipe": [
{
"class_name": "torch_transformers_ner_preprocessor",
"vocab_file": "{TRANSFORMER}",
"do_lower_case": false,
"max_seq_length": 512,
"max_subword_length": 15,
"token_masking_prob": 0.0,
"in": [
"x"
],
"out": [
"x_tokens",
"x_subword_tokens",
"x_subword_tok_ids",
"startofword_markers",
"attention_mask"
]
},
{
"id": "tag_vocab",
"class_name": "simple_vocab",
"unk_token": [
"O"
],
"pad_with_zeros": true,
"save_path": "{MODEL_PATH}/tag.dict",
"load_path": "{MODEL_PATH}/tag.dict",
"fit_on": [
"y"
],
"in": [
"y"
],
"out": [
"y_ind"
]
},
{
"class_name": "torch_transformers_sequence_tagger",
"n_tags": "#tag_vocab.len",
"pretrained_bert": "{TRANSFORMER}",
"attention_probs_keep_prob": 0.5,
"return_probas": false,
"use_crf": true,
"encoder_layer_ids": [
-1
],
"optimizer": "AdamW",
"optimizer_parameters": {
"lr": 2e-05,
"weight_decay": 1e-06,
"betas": [
0.9,
0.999
],
"eps": 1e-06
},
"clip_norm": 1.0,
"min_learning_rate": 1e-07,
"learning_rate_drop_patience": 20,
"learning_rate_drop_div": 1.5,
"load_before_drop": true,
"save_path": "{MODEL_PATH}/model",
"load_path": "{MODEL_PATH}/model",
"in": [
"x_subword_tok_ids",
"attention_mask",
"startofword_markers"
],
"in_y": [
"y_ind"
],
"out": [
"y_pred_ind",
"probas"
]
},
{
"ref": "tag_vocab",
"in": [
"y_pred_ind"
],
"out": [
"y_pred"
]
}
],
"out": [
"x_tokens",
"y_pred"
]
},
"train": {
"epochs": 50,
"batch_size": 150,
"metrics": [
{
"name": "ner_f1",
"inputs": [
"y",
"y_pred"
]
},
{
"name": "ner_token_f1",
"inputs": [
"y",
"y_pred"
]
}
],
"validation_patience": 100,
"val_every_n_batches": 50,
"log_every_n_batches": 50,
"show_examples": false,
"pytest_max_batches": 2,
"pytest_batch_size": 8,
"evaluation_targets": [
"test"
],
"class_name": "torch_trainer"
},
"metadata": {
"variables": {
"ROOT_PATH": "~/.deeppavlov",
"DOWNLOADS_PATH": "~/.deeppavlov/downloads",
"MODELS_PATH": "~/.deeppavlov/models",
"TRANSFORMER": "distilbert-base-multilingual-cased",
"MODEL_PATH": "{MODELS_PATH}/ner/mbert_dream_distil_with_numbers_rus_ext"
},
"download": [
{
"url": "http://files.deeppavlov.ai/v1/ner/mbert_dream_distil_with_numbers.tar.gz",
"subdir": "{MODEL_PATH}"
}
]
}
}
59 changes: 46 additions & 13 deletions annotators/NER_deeppavlov/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,50 @@
raise e


def convert_prediction(s, token, tag):
start_pos = s.find(token)
return {
"confidence": 1,
"text": token,
"type": tag.replace("B-", "").replace("I-", ""),
"start_pos": start_pos,
"end_pos": start_pos + len(token),
}
def convert_prediction(sents, pred_labels):
entities = []
for sent, tags in zip(sents, pred_labels):
entities.append([])
start = end = -1
for i, (word, tag) in enumerate(zip(sent, tags)):
if tag[0] == "B":
if start != -1:
entities[-1].append(
{
"start_pos": start,
"end_pos": end,
"type": tags[start].split("-")[1],
"text": " ".join(sent[start:end]),
"confidence": 1,
}
)
start = i
end = i + 1
elif tag[0] == "I":
end = i + 1
else:
if start != -1:
entities[-1].append(
{
"start_pos": start,
"end_pos": end,
"type": tags[start].split("-")[1],
"text": " ".join(sent[start:end]),
"confidence": 1,
}
)
start = -1
if start != -1:
entities[-1].append(
{
"start_pos": start,
"end_pos": end,
"type": tags[start].split("-")[1],
"text": " ".join(sent[start:end]),
"confidence": 1,
}
)
return entities


def get_result(request):
Expand All @@ -51,10 +86,8 @@ def get_result(request):
dialog_ids.append(i)

tokens_batch, tags_batch = ner_model(samples)
good_preds = [
[convert_prediction(s, token, tag) for token, tag in zip(tokens, tags) if tag != "O"]
for s, tokens, tags in zip(samples, tokens_batch, tags_batch)
]
logger.info(f"NER model predictions: tokens: {tokens_batch}, tags: {tags_batch}")
good_preds = convert_prediction(tokens_batch, tags_batch)
dialog_ids = np.array(dialog_ids)

ret = []
Expand Down
Loading