KoichiYasuoka commited on
Commit
6f312d2
1 Parent(s): c1d6938

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-base-japanese-wikipedia-ud-head
23
+
24
+ ## Model Description
25
+
26
+ This is a DeBERTa(V2) model pretrained on 青空文庫 for dependency-parsing (head-detection on long-unit-words) as question-answering, derived from [deberta-base-japanese-wikipedia](https://huggingface.co/KoichiYasuoka/deberta-base-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-base-japanese-wikipedia-ud-head")
34
+ model=AutoModelForQuestionAnswering.from_pretrained("KoichiYasuoka/deberta-base-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-base-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": 768,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 3072,
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": 12,
18
+ "num_hidden_layers": 12,
19
+ "pad_token_id": 1,
20
+ "pooler_dropout": 0,
21
+ "pooler_hidden_act": "gelu",
22
+ "pooler_hidden_size": 768,
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": 768,
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": 3072,
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": 12,
119
+ "num_hidden_layers": 12,
120
+ "pad_token_id": 1,
121
+ "pooler_dropout": 0,
122
+ "pooler_hidden_act": "gelu",
123
+ "pooler_hidden_size": 768,
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:423343f1b03611981be584d5b49b2d9903fb3e977298416953716ab89f5ba110
3
+ size 440319475
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:d4d0c84d139b98520551dc35b55ee63a40bf6e3eed3983a986234cb463feabdb
3
+ size 440178035
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": 768,
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": 3072,
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": 12,
99
+ "num_hidden_layers": 12,
100
+ "pad_token_id": 1,
101
+ "pooler_dropout": 0,
102
+ "pooler_hidden_act": "gelu",
103
+ "pooler_hidden_size": 768,
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:74d7b076bf46d851cb9643edb9b8c5b0dd16cc8ceffb1864bf0f692949653f44
3
+ size 440288755
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"}