KoichiYasuoka commited on
Commit
f2b0090
1 Parent(s): b6a72ce

initial release

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