Add Custom model and pipeline to make usage easier.
#1
by
tcapelle
- opened
- config.json +18 -3
- configuration_deberta_multi.py +7 -0
- custom_pipeline.py +34 -0
- special_tokens_map.json +42 -6
- tokenizer.json +0 -0
- tokenizer_config.json +1 -1
config.json
CHANGED
@@ -1,8 +1,22 @@
|
|
1 |
{
|
|
|
2 |
"architectures": [
|
3 |
-
"
|
4 |
],
|
5 |
"attention_probs_dropout_prob": 0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"hidden_act": "gelu",
|
7 |
"hidden_dropout_prob": 0.1,
|
8 |
"hidden_size": 768,
|
@@ -11,9 +25,10 @@
|
|
11 |
"layer_norm_eps": 1e-07,
|
12 |
"max_position_embeddings": 512,
|
13 |
"max_relative_positions": -1,
|
14 |
-
"model_type": "deberta-
|
15 |
"norm_rel_ebd": "layer_norm",
|
16 |
"num_attention_heads": 12,
|
|
|
17 |
"num_hidden_layers": 6,
|
18 |
"pad_token_id": 0,
|
19 |
"pooler_dropout": 0,
|
@@ -28,7 +43,7 @@
|
|
28 |
"relative_attention": true,
|
29 |
"share_att_key": true,
|
30 |
"torch_dtype": "float32",
|
31 |
-
"transformers_version": "4.
|
32 |
"type_vocab_size": 0,
|
33 |
"vocab_size": 128100
|
34 |
}
|
|
|
1 |
{
|
2 |
+
"_name_or_path": "./celadon",
|
3 |
"architectures": [
|
4 |
+
"MultiHeadDebertaForSequenceClassificationModel"
|
5 |
],
|
6 |
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "configuration_deberta_multi.MultiHeadDebertaV2Config",
|
9 |
+
"AutoModelForSequenceClassification": "modelling_deberta_multi.MultiHeadDebertaForSequenceClassificationModel"
|
10 |
+
},
|
11 |
+
"custom_pipelines": {
|
12 |
+
"multi-head-text-classification": {
|
13 |
+
"impl": "custom_pipeline.CustomTextClassificationPipeline",
|
14 |
+
"pt": [
|
15 |
+
"AutoModelForSequenceClassification"
|
16 |
+
],
|
17 |
+
"tf": []
|
18 |
+
}
|
19 |
+
},
|
20 |
"hidden_act": "gelu",
|
21 |
"hidden_dropout_prob": 0.1,
|
22 |
"hidden_size": 768,
|
|
|
25 |
"layer_norm_eps": 1e-07,
|
26 |
"max_position_embeddings": 512,
|
27 |
"max_relative_positions": -1,
|
28 |
+
"model_type": "multi-head-deberta-for-sequence-classification",
|
29 |
"norm_rel_ebd": "layer_norm",
|
30 |
"num_attention_heads": 12,
|
31 |
+
"num_heads": 5,
|
32 |
"num_hidden_layers": 6,
|
33 |
"pad_token_id": 0,
|
34 |
"pooler_dropout": 0,
|
|
|
43 |
"relative_attention": true,
|
44 |
"share_att_key": true,
|
45 |
"torch_dtype": "float32",
|
46 |
+
"transformers_version": "4.46.2",
|
47 |
"type_vocab_size": 0,
|
48 |
"vocab_size": 128100
|
49 |
}
|
configuration_deberta_multi.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import DebertaV2Config
|
2 |
+
|
3 |
+
class MultiHeadDebertaV2Config(DebertaV2Config):
|
4 |
+
model_type = "multi-head-deberta-for-sequence-classification"
|
5 |
+
def __init__(self, num_heads=5, **kwargs):
|
6 |
+
self.num_heads = num_heads
|
7 |
+
super().__init__(**kwargs)
|
custom_pipeline.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
print("Loading Multi head pipeline")
|
2 |
+
from transformers.pipelines import PIPELINE_REGISTRY
|
3 |
+
from transformers import TextClassificationPipeline, AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
class CustomTextClassificationPipeline(TextClassificationPipeline):
|
6 |
+
def __init__(self, model, tokenizer=None, **kwargs):
|
7 |
+
if tokenizer is None:
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model.config._name_or_path)
|
9 |
+
super().__init__(model=model, tokenizer=tokenizer, **kwargs)
|
10 |
+
|
11 |
+
def _sanitize_parameters(self, **kwargs):
|
12 |
+
preprocess_kwargs = {}
|
13 |
+
return preprocess_kwargs, {}, {}
|
14 |
+
|
15 |
+
def preprocess(self, inputs):
|
16 |
+
return self.tokenizer(inputs, return_tensors='pt', truncation=True, padding=True)
|
17 |
+
|
18 |
+
def _forward(self, model_inputs):
|
19 |
+
input_ids = model_inputs['input_ids']
|
20 |
+
attention_mask = (input_ids != 0).long()
|
21 |
+
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask)
|
22 |
+
return outputs
|
23 |
+
|
24 |
+
def postprocess(self, model_outputs):
|
25 |
+
predictions = model_outputs.logits.argmax(dim=-1).squeeze().tolist()
|
26 |
+
categories = ["Race/Origin", "Gender/Sex", "Religion", "Ability", "Violence", "Other"]
|
27 |
+
return dict(zip(categories, predictions))
|
28 |
+
|
29 |
+
|
30 |
+
PIPELINE_REGISTRY.register_pipeline(
|
31 |
+
"multi-head-text-classification",
|
32 |
+
pipeline_class=CustomTextClassificationPipeline,
|
33 |
+
pt_model=AutoModelForSequenceClassification,
|
34 |
+
)
|
special_tokens_map.json
CHANGED
@@ -1,10 +1,46 @@
|
|
1 |
{
|
2 |
-
"bos_token":
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
"unk_token": {
|
9 |
"content": "[UNK]",
|
10 |
"lstrip": false,
|
|
|
1 |
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "[CLS]",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"cls_token": {
|
10 |
+
"content": "[CLS]",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"eos_token": {
|
17 |
+
"content": "[SEP]",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"mask_token": {
|
24 |
+
"content": "[MASK]",
|
25 |
+
"lstrip": false,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
},
|
30 |
+
"pad_token": {
|
31 |
+
"content": "[PAD]",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": false,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false
|
36 |
+
},
|
37 |
+
"sep_token": {
|
38 |
+
"content": "[SEP]",
|
39 |
+
"lstrip": false,
|
40 |
+
"normalized": false,
|
41 |
+
"rstrip": false,
|
42 |
+
"single_word": false
|
43 |
+
},
|
44 |
"unk_token": {
|
45 |
"content": "[UNK]",
|
46 |
"lstrip": false,
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
CHANGED
@@ -47,7 +47,7 @@
|
|
47 |
"do_lower_case": false,
|
48 |
"eos_token": "[SEP]",
|
49 |
"mask_token": "[MASK]",
|
50 |
-
"model_max_length":
|
51 |
"pad_token": "[PAD]",
|
52 |
"sep_token": "[SEP]",
|
53 |
"sp_model_kwargs": {},
|
|
|
47 |
"do_lower_case": false,
|
48 |
"eos_token": "[SEP]",
|
49 |
"mask_token": "[MASK]",
|
50 |
+
"model_max_length": 512,
|
51 |
"pad_token": "[PAD]",
|
52 |
"sep_token": "[SEP]",
|
53 |
"sp_model_kwargs": {},
|