KoichiYasuoka commited on
Commit
5a68911
1 Parent(s): 105292f

initial release

Browse files
README.md ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - "ja"
4
+ tags:
5
+ - "japanese"
6
+ - "question-answering"
7
+ - "dependency-parsing"
8
+ datasets:
9
+ - "universal_dependencies"
10
+ license: "cc-by-sa-4.0"
11
+ pipeline_tag: "question-answering"
12
+ widget:
13
+ - text: "国語"
14
+ context: "全学年にわたって小学校の国語の教科書に挿し絵が用いられている"
15
+ - text: "教科書"
16
+ context: "全学年にわたって小学校の国語の教科書に挿し絵が用いられている"
17
+ - text: "の"
18
+ context: "全学年にわたって小学校の国語[MASK]教科書に挿し絵が用いられている"
19
+ ---
20
+
21
+ # deberta-large-japanese-unidic-ud-head
22
+
23
+ ## Model Description
24
+
25
+ This is a DeBERTa(V2) model pretrained on 青空文庫 for dependency-parsing (head-detection on long-unit-words) as question-answering, derived from [deberta-large-japanese-unidic](https://huggingface.co/KoichiYasuoka/deberta-large-japanese-unidic) and [UD_Japanese-GSDLUW](https://github.com/UniversalDependencies/UD_Japanese-GSDLUW). Use [MASK] inside `context` to avoid ambiguity when specifying a multiple-used word as `question`.
26
+
27
+ ## How to Use
28
+
29
+ ```py
30
+ import torch
31
+ from transformers import AutoTokenizer,AutoModelForQuestionAnswering
32
+ tokenizer=AutoTokenizer.from_pretrained("KoichiYasuoka/deberta-large-japanese-unidic-ud-head")
33
+ model=AutoModelForQuestionAnswering.from_pretrained("KoichiYasuoka/deberta-large-japanese-unidic-ud-head")
34
+ question="国語"
35
+ context="全学年にわたって小学校の国語の教科書に挿し絵が用いられている"
36
+ inputs=tokenizer(question,context,return_tensors="pt")
37
+ outputs=model(**inputs)
38
+ start,end=torch.argmax(outputs.start_logits),torch.argmax(outputs.end_logits)
39
+ print(tokenizer.convert_ids_to_tokens(inputs["input_ids"][0,start:end+1]))
40
+ ```
41
+
42
+ or
43
+
44
+ ```py
45
+ from transformers import (AutoTokenizer,AutoModelForQuestionAnswering,
46
+ AutoModelForTokenClassification,AutoConfig,TokenClassificationPipeline)
47
+ class TaggerPipeline(TokenClassificationPipeline):
48
+ def __call__(self,text):
49
+ d=super().__call__(text)
50
+ if len(d)>0 and ("start" not in d[0] or d[0]["start"]==None):
51
+ import tokenizations
52
+ v=[x["word"] for x in d]
53
+ a2b,b2a=tokenizations.get_alignments(v,text)
54
+ for i,t in enumerate(a2b):
55
+ s,e=(0,0) if t==[] else (t[0],t[-1]+1)
56
+ if t==[] and v[i]==self.tokenizer.unk_token:
57
+ s=([[-1]]+[x for x in a2b[0:i] if x>[]])[-1][-1]+1
58
+ e=([x for x in a2b[i+1:] if x>[]]+[[len(text)]])[0][0]
59
+ d[i]["start"],d[i]["end"]=s,e
60
+ return d
61
+ class TransformersUD(object):
62
+ def __init__(self,bert):
63
+ import os
64
+ self.tokenizer=AutoTokenizer.from_pretrained(bert)
65
+ self.model=AutoModelForQuestionAnswering.from_pretrained(bert)
66
+ x=AutoModelForTokenClassification.from_pretrained
67
+ if os.path.isdir(bert):
68
+ d,t=x(os.path.join(bert,"deprel")),x(os.path.join(bert,"tagger"))
69
+ else:
70
+ from transformers.file_utils import hf_bucket_url
71
+ c=AutoConfig.from_pretrained(hf_bucket_url(bert,"deprel/config.json"))
72
+ d=x(hf_bucket_url(bert,"deprel/pytorch_model.bin"),config=c)
73
+ s=AutoConfig.from_pretrained(hf_bucket_url(bert,"tagger/config.json"))
74
+ t=x(hf_bucket_url(bert,"tagger/pytorch_model.bin"),config=s)
75
+ self.deprel=TaggerPipeline(model=d,tokenizer=self.tokenizer,
76
+ aggregation_strategy="simple")
77
+ self.tagger=TaggerPipeline(model=t,tokenizer=self.tokenizer)
78
+ def __call__(self,text):
79
+ import numpy,torch,ufal.chu_liu_edmonds
80
+ w=[(t["start"],t["end"],t["entity_group"]) for t in self.deprel(text)]
81
+ z,n={t["start"]:t["entity"].split("|") for t in self.tagger(text)},len(w)
82
+ r,m=[text[s:e] for s,e,p in w],numpy.full((n+1,n+1),numpy.nan)
83
+ v,c=self.tokenizer(r,add_special_tokens=False)["input_ids"],[]
84
+ for i,t in enumerate(v):
85
+ q=[self.tokenizer.cls_token_id]+t+[self.tokenizer.sep_token_id]
86
+ c.append([q]+v[0:i]+[[self.tokenizer.mask_token_id]]+v[i+1:]+[[q[-1]]])
87
+ b=[[len(sum(x[0:j+1],[])) for j in range(len(x))] for x in c]
88
+ d=self.model(input_ids=torch.tensor([sum(x,[]) for x in c]),
89
+ token_type_ids=torch.tensor([[0]*x[0]+[1]*(x[-1]-x[0]) for x in b]))
90
+ s,e=d.start_logits.tolist(),d.end_logits.tolist()
91
+ for i in range(n):
92
+ for j in range(n):
93
+ m[i+1,0 if i==j else j+1]=s[i][b[i][j]]+e[i][b[i][j+1]-1]
94
+ h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
95
+ if [0 for i in h if i==0]!=[0]:
96
+ i=([p for s,e,p in w]+["root"]).index("root")
97
+ j=i+1 if i<n else numpy.nanargmax(m[:,0])
98
+ m[0:j,0]=m[j+1:,0]=numpy.nan
99
+ h=ufal.chu_liu_edmonds.chu_liu_edmonds(m)[0]
100
+ u="# text = "+text.replace("\n"," ")+"\n"
101
+ for i,(s,e,p) in enumerate(w,1):
102
+ p="root" if h[i]==0 else "dep" if p=="root" else p
103
+ u+="\t".join([str(i),r[i-1],"_",z[s][0][2:],"_","|".join(z[s][1:]),
104
+ str(h[i]),p,"_","_" if i<n and w[i][0]<e else "SpaceAfter=No"])+"\n"
105
+ return u+"\n"
106
+
107
+ nlp=TransformersUD("KoichiYasuoka/deberta-large-japanese-unidic-ud-head")
108
+ print(nlp("全学年にわたって小学校の国語の教科書に挿し絵が用いられ���いる"))
109
+ ```
110
+
111
+ [fugashi](https://pypi.org/project/fugashi) [unidic-lite](https://pypi.org/project/unidic-lite) [pytokenizations](https://pypi.org/project/pytokenizations) and [ufal.chu-liu-edmonds](https://pypi.org/project/ufal.chu-liu-edmonds/) required.
config.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "DebertaV2ForQuestionAnswering"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "eos_token_id": 2,
8
+ "hidden_act": "gelu",
9
+ "hidden_dropout_prob": 0.1,
10
+ "hidden_size": 1024,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 4096,
13
+ "layer_norm_eps": 1e-07,
14
+ "max_position_embeddings": 512,
15
+ "max_relative_positions": -1,
16
+ "model_type": "deberta-v2",
17
+ "num_attention_heads": 16,
18
+ "num_hidden_layers": 24,
19
+ "pad_token_id": 1,
20
+ "pooler_dropout": 0,
21
+ "pooler_hidden_act": "gelu",
22
+ "pooler_hidden_size": 1024,
23
+ "pos_att_type": null,
24
+ "position_biased_input": true,
25
+ "relative_attention": false,
26
+ "tokenizer_class": "BertJapaneseTokenizer",
27
+ "torch_dtype": "float32",
28
+ "transformers_version": "4.19.4",
29
+ "type_vocab_size": 0,
30
+ "vocab_size": 32000
31
+ }
deprel/config.json ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "DebertaV2ForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "eos_token_id": 2,
8
+ "hidden_act": "gelu",
9
+ "hidden_dropout_prob": 0.1,
10
+ "hidden_size": 1024,
11
+ "id2label": {
12
+ "0": "B-acl",
13
+ "1": "B-advcl",
14
+ "2": "B-advmod",
15
+ "3": "B-amod",
16
+ "4": "B-aux",
17
+ "5": "B-case",
18
+ "6": "B-cc",
19
+ "7": "B-ccomp",
20
+ "8": "B-compound",
21
+ "9": "B-cop",
22
+ "10": "B-csubj",
23
+ "11": "B-dep",
24
+ "12": "B-det",
25
+ "13": "B-discourse",
26
+ "14": "B-dislocated",
27
+ "15": "B-fixed",
28
+ "16": "B-mark",
29
+ "17": "B-nmod",
30
+ "18": "B-nsubj",
31
+ "19": "B-nummod",
32
+ "20": "B-obj",
33
+ "21": "B-obl",
34
+ "22": "B-punct",
35
+ "23": "B-root",
36
+ "24": "I-acl",
37
+ "25": "I-advcl",
38
+ "26": "I-advmod",
39
+ "27": "I-amod",
40
+ "28": "I-aux",
41
+ "29": "I-case",
42
+ "30": "I-cc",
43
+ "31": "I-ccomp",
44
+ "32": "I-compound",
45
+ "33": "I-csubj",
46
+ "34": "I-dep",
47
+ "35": "I-discourse",
48
+ "36": "I-dislocated",
49
+ "37": "I-fixed",
50
+ "38": "I-mark",
51
+ "39": "I-nmod",
52
+ "40": "I-nsubj",
53
+ "41": "I-nummod",
54
+ "42": "I-obj",
55
+ "43": "I-obl",
56
+ "44": "I-punct",
57
+ "45": "I-root"
58
+ },
59
+ "initializer_range": 0.02,
60
+ "intermediate_size": 4096,
61
+ "label2id": {
62
+ "B-acl": 0,
63
+ "B-advcl": 1,
64
+ "B-advmod": 2,
65
+ "B-amod": 3,
66
+ "B-aux": 4,
67
+ "B-case": 5,
68
+ "B-cc": 6,
69
+ "B-ccomp": 7,
70
+ "B-compound": 8,
71
+ "B-cop": 9,
72
+ "B-csubj": 10,
73
+ "B-dep": 11,
74
+ "B-det": 12,
75
+ "B-discourse": 13,
76
+ "B-dislocated": 14,
77
+ "B-fixed": 15,
78
+ "B-mark": 16,
79
+ "B-nmod": 17,
80
+ "B-nsubj": 18,
81
+ "B-nummod": 19,
82
+ "B-obj": 20,
83
+ "B-obl": 21,
84
+ "B-punct": 22,
85
+ "B-root": 23,
86
+ "I-acl": 24,
87
+ "I-advcl": 25,
88
+ "I-advmod": 26,
89
+ "I-amod": 27,
90
+ "I-aux": 28,
91
+ "I-case": 29,
92
+ "I-cc": 30,
93
+ "I-ccomp": 31,
94
+ "I-compound": 32,
95
+ "I-csubj": 33,
96
+ "I-dep": 34,
97
+ "I-discourse": 35,
98
+ "I-dislocated": 36,
99
+ "I-fixed": 37,
100
+ "I-mark": 38,
101
+ "I-nmod": 39,
102
+ "I-nsubj": 40,
103
+ "I-nummod": 41,
104
+ "I-obj": 42,
105
+ "I-obl": 43,
106
+ "I-punct": 44,
107
+ "I-root": 45
108
+ },
109
+ "layer_norm_eps": 1e-07,
110
+ "max_position_embeddings": 512,
111
+ "max_relative_positions": -1,
112
+ "model_type": "deberta-v2",
113
+ "num_attention_heads": 16,
114
+ "num_hidden_layers": 24,
115
+ "pad_token_id": 1,
116
+ "pooler_dropout": 0,
117
+ "pooler_hidden_act": "gelu",
118
+ "pooler_hidden_size": 1024,
119
+ "pos_att_type": null,
120
+ "position_biased_input": true,
121
+ "relative_attention": false,
122
+ "tokenizer_class": "BertJapaneseTokenizer",
123
+ "torch_dtype": "float32",
124
+ "transformers_version": "4.19.4",
125
+ "type_vocab_size": 0,
126
+ "vocab_size": 32000
127
+ }
deprel/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1b76204a00e2ebc32a592fdd6408ff61a27cc9d2fbd1c1e5176fe7f58801f1b
3
+ size 1342740275
deprel/special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
deprel/tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "do_lower_case": false, "do_word_tokenize": true, "do_subword_tokenize": true, "word_tokenizer_type": "mecab", "subword_tokenizer_type": "wordpiece", "never_split": ["[CLS]", "[PAD]", "[SEP]", "[UNK]", "[MASK]"], "mecab_kwargs": {"mecab_dic": "unidic_lite"}, "model_max_length": 512, "tokenizer_class": "BertJapaneseTokenizer"}
deprel/vocab.txt ADDED
The diff for this file is too large to render. See raw diff
 
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:53fd6776eae30439ef3af59a4ba242444f7ff45ecbfb7c8d4cd366084cd5bac6
3
+ size 1342559923
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tagger/config.json ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "DebertaV2ForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "eos_token_id": 2,
8
+ "hidden_act": "gelu",
9
+ "hidden_dropout_prob": 0.1,
10
+ "hidden_size": 1024,
11
+ "id2label": {
12
+ "0": "B-ADJ|_",
13
+ "1": "B-ADP|_",
14
+ "2": "B-ADV|_",
15
+ "3": "B-AUX|Polarity=Neg",
16
+ "4": "B-AUX|_",
17
+ "5": "B-CCONJ|_",
18
+ "6": "B-DET|_",
19
+ "7": "B-INTJ|_",
20
+ "8": "B-NOUN|Polarity=Neg",
21
+ "9": "B-NOUN|_",
22
+ "10": "B-NUM|_",
23
+ "11": "B-PART|_",
24
+ "12": "B-PRON|_",
25
+ "13": "B-PROPN|_",
26
+ "14": "B-PUNCT|_",
27
+ "15": "B-SCONJ|_",
28
+ "16": "B-SYM|_",
29
+ "17": "B-VERB|_",
30
+ "18": "B-X|_",
31
+ "19": "I-ADJ|_",
32
+ "20": "I-ADP|_",
33
+ "21": "I-ADV|_",
34
+ "22": "I-AUX|Polarity=Neg",
35
+ "23": "I-AUX|_",
36
+ "24": "I-CCONJ|_",
37
+ "25": "I-INTJ|_",
38
+ "26": "I-NOUN|_",
39
+ "27": "I-NUM|_",
40
+ "28": "I-PART|_",
41
+ "29": "I-PRON|_",
42
+ "30": "I-PROPN|_",
43
+ "31": "I-PUNCT|_",
44
+ "32": "I-SCONJ|_",
45
+ "33": "I-SYM|_",
46
+ "34": "I-VERB|_",
47
+ "35": "I-X|_"
48
+ },
49
+ "initializer_range": 0.02,
50
+ "intermediate_size": 4096,
51
+ "label2id": {
52
+ "B-ADJ|_": 0,
53
+ "B-ADP|_": 1,
54
+ "B-ADV|_": 2,
55
+ "B-AUX|Polarity=Neg": 3,
56
+ "B-AUX|_": 4,
57
+ "B-CCONJ|_": 5,
58
+ "B-DET|_": 6,
59
+ "B-INTJ|_": 7,
60
+ "B-NOUN|Polarity=Neg": 8,
61
+ "B-NOUN|_": 9,
62
+ "B-NUM|_": 10,
63
+ "B-PART|_": 11,
64
+ "B-PRON|_": 12,
65
+ "B-PROPN|_": 13,
66
+ "B-PUNCT|_": 14,
67
+ "B-SCONJ|_": 15,
68
+ "B-SYM|_": 16,
69
+ "B-VERB|_": 17,
70
+ "B-X|_": 18,
71
+ "I-ADJ|_": 19,
72
+ "I-ADP|_": 20,
73
+ "I-ADV|_": 21,
74
+ "I-AUX|Polarity=Neg": 22,
75
+ "I-AUX|_": 23,
76
+ "I-CCONJ|_": 24,
77
+ "I-INTJ|_": 25,
78
+ "I-NOUN|_": 26,
79
+ "I-NUM|_": 27,
80
+ "I-PART|_": 28,
81
+ "I-PRON|_": 29,
82
+ "I-PROPN|_": 30,
83
+ "I-PUNCT|_": 31,
84
+ "I-SCONJ|_": 32,
85
+ "I-SYM|_": 33,
86
+ "I-VERB|_": 34,
87
+ "I-X|_": 35
88
+ },
89
+ "layer_norm_eps": 1e-07,
90
+ "max_position_embeddings": 512,
91
+ "max_relative_positions": -1,
92
+ "model_type": "deberta-v2",
93
+ "num_attention_heads": 16,
94
+ "num_hidden_layers": 24,
95
+ "pad_token_id": 1,
96
+ "pooler_dropout": 0,
97
+ "pooler_hidden_act": "gelu",
98
+ "pooler_hidden_size": 1024,
99
+ "pos_att_type": null,
100
+ "position_biased_input": true,
101
+ "relative_attention": false,
102
+ "tokenizer_class": "BertJapaneseTokenizer",
103
+ "torch_dtype": "float32",
104
+ "transformers_version": "4.19.4",
105
+ "type_vocab_size": 0,
106
+ "vocab_size": 32000
107
+ }
tagger/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:774dc23b70f8ddea398ff14ea4e07e6528e2f457344696d20a8f7fbb206f01e1
3
+ size 1342699315
tagger/special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tagger/tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "do_lower_case": false, "do_word_tokenize": true, "do_subword_tokenize": true, "word_tokenizer_type": "mecab", "subword_tokenizer_type": "wordpiece", "never_split": ["[CLS]", "[PAD]", "[SEP]", "[UNK]", "[MASK]"], "mecab_kwargs": {"mecab_dic": "unidic_lite"}, "model_max_length": 512, "tokenizer_class": "BertJapaneseTokenizer"}
tagger/vocab.txt ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "do_lower_case": false, "do_word_tokenize": true, "do_subword_tokenize": true, "word_tokenizer_type": "mecab", "subword_tokenizer_type": "wordpiece", "never_split": ["[CLS]", "[PAD]", "[SEP]", "[UNK]", "[MASK]"], "mecab_kwargs": {"mecab_dic": "unidic_lite"}, "model_max_length": 512, "tokenizer_class": "BertJapaneseTokenizer"}
vocab.txt ADDED
The diff for this file is too large to render. See raw diff