KoichiYasuoka commited on
Commit
96bf3ed
1 Parent(s): f123bef

initial release

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