devrim commited on
Commit
f0b7f83
1 Parent(s): 605ae57

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -53,3 +53,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
53
  *.jpg filter=lfs diff=lfs merge=lfs -text
54
  *.jpeg filter=lfs diff=lfs merge=lfs -text
55
  *.webp filter=lfs diff=lfs merge=lfs -text
 
 
53
  *.jpg filter=lfs diff=lfs merge=lfs -text
54
  *.jpeg filter=lfs diff=lfs merge=lfs -text
55
  *.webp filter=lfs diff=lfs merge=lfs -text
56
+ philosophy_subject/train.json filter=lfs diff=lfs merge=lfs -text
identity_type/test.json ADDED
The diff for this file is too large to render. See raw diff
 
identity_type/train.json ADDED
The diff for this file is too large to render. See raw diff
 
identity_type/val.json ADDED
The diff for this file is too large to render. See raw diff
 
literature_movement/test.json ADDED
The diff for this file is too large to render. See raw diff
 
literature_movement/train.json ADDED
The diff for this file is too large to render. See raw diff
 
literature_movement/val.json ADDED
The diff for this file is too large to render. See raw diff
 
modernization_subject/test.json ADDED
The diff for this file is too large to render. See raw diff
 
modernization_subject/train.json ADDED
The diff for this file is too large to render. See raw diff
 
modernization_subject/val.json ADDED
The diff for this file is too large to render. See raw diff
 
philosophy_subject/test.json ADDED
The diff for this file is too large to render. See raw diff
 
philosophy_subject/train.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b3cb3bacc89adb71ae23198c448769889f53516e4eaf68f64f048ebaa6d5f18c
3
+ size 11271458
philosophy_subject/val.json ADDED
The diff for this file is too large to render. See raw diff
 
russian_third_level.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Russian Literary Dataset from late 19th century up to early 20th century."""
2
+
3
+ import json
4
+ import os
5
+ import warnings
6
+ from typing import Dict, List, Tuple
7
+
8
+ import datasets
9
+ import numpy as np
10
+ from transformers import PreTrainedTokenizerBase
11
+
12
+ _DESCRIPTION = """Third level categorization of Russian articles."""
13
+
14
+ _HOMEPAGE = ""
15
+
16
+ _LICENSE = ""
17
+
18
+ _NAMES = [
19
+ "identity_type",
20
+ "literature_movement",
21
+ "modernization_subject",
22
+ "philosophy_subject",
23
+ "subject_genre"
24
+ ]
25
+
26
+ # Label information as `(num_labels, is_multi_label)` tuples
27
+ _LABELS: Dict[str, Tuple[int, bool]] = {
28
+ "identity_type": (2, True),
29
+ "literature_movement": (3, True),
30
+ "modernization_subject": (4, True),
31
+ "philosophy_subject": (5, True),
32
+ "subject_genre": (3, True),
33
+ }
34
+
35
+
36
+ def generate_urls(name: str) -> Dict[str, str]:
37
+ return {
38
+ "train": os.path.join(name, "train.json"),
39
+ "val": os.path.join(name, "val.json"),
40
+ "test": os.path.join(name, "test.json"),
41
+ }
42
+
43
+
44
+ class NonwestlitThirdLevelConfig(datasets.BuilderConfig):
45
+ """BuilderConfig for Dataset."""
46
+
47
+ def __init__(
48
+ self, tokenizer: PreTrainedTokenizerBase = None, max_sequence_length: int = None, **kwargs
49
+ ):
50
+ """BuilderConfig for Dataset.
51
+
52
+ Args:
53
+ **kwargs: keyword arguments forwarded to super.
54
+ """
55
+ super(NonwestlitThirdLevelConfig, self).__init__(**kwargs)
56
+ self.tokenizer = tokenizer
57
+ self.max_sequence_length = max_sequence_length
58
+
59
+ @property
60
+ def features(self):
61
+ if self.name == "literary_text_type":
62
+ labels = datasets.Value("uint8")
63
+ else:
64
+ labels = datasets.Sequence(datasets.Value("uint8"))
65
+ return {
66
+ "labels": labels,
67
+ "input_ids": datasets.Value("string"),
68
+ "title": datasets.Value("string"),
69
+ "iid": datasets.Value("uint32"),
70
+ "chunk_id": datasets.Value("uint32"),
71
+ }
72
+
73
+
74
+ class NonwestlitThirdLevelDataset(datasets.GeneratorBasedBuilder):
75
+ VERSION = datasets.Version("0.0.1")
76
+
77
+ BUILDER_CONFIGS = [
78
+ NonwestlitThirdLevelConfig(name=name, version=version, description=name)
79
+ for name, version in zip(_NAMES, [VERSION] * len(_NAMES))
80
+ ]
81
+ BUILDER_CONFIG_CLASS = NonwestlitThirdLevelConfig
82
+ __current_id = 1
83
+ __current_chunk_id = 1
84
+
85
+ @property
86
+ def __next_id(self):
87
+ cid = self.__current_id
88
+ self.__current_id += 1
89
+ return cid
90
+
91
+ @property
92
+ def __next_chunk_id(self):
93
+ cid = self.__current_chunk_id
94
+ self.__current_chunk_id += 1
95
+ return cid
96
+
97
+ @property
98
+ def label_info(self) -> Tuple[int, bool]:
99
+ return _LABELS[self.config.name]
100
+
101
+ def __reset_chunk_id(self):
102
+ self.__current_chunk_id = 1
103
+
104
+ def _info(self):
105
+ if self.config.tokenizer is None:
106
+ raise RuntimeError(
107
+ "For HF Datasets and for chunking to be carried out, 'tokenizer' must be given."
108
+ )
109
+ if "llama" in self.config.tokenizer.name_or_path:
110
+ warnings.warn(
111
+ "It is suggested to pass 'max_sequence_length' argument for Llama-2 model family. There "
112
+ "might be errors for the data processing parts as `model_max_len` attributes are set to"
113
+ "MAX_INT64 (?)."
114
+ )
115
+ return datasets.DatasetInfo(
116
+ description=_DESCRIPTION,
117
+ features=datasets.Features(self.config.features),
118
+ )
119
+
120
+ def _split_generators(self, dl_manager):
121
+ urls = generate_urls(self.config.name)
122
+ data_dir = dl_manager.download_and_extract(urls)
123
+ return [
124
+ datasets.SplitGenerator(
125
+ name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_dir["train"]}
126
+ ),
127
+ datasets.SplitGenerator(
128
+ name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_dir["val"]}
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST, gen_kwargs={"filepath": data_dir["test"]}
132
+ ),
133
+ ]
134
+
135
+ def prepare_articles(self, article: str) -> List[str]:
136
+ tokenizer = self.config.tokenizer
137
+ model_inputs = tokenizer(
138
+ article,
139
+ truncation=True,
140
+ padding=True,
141
+ max_length=self.config.max_sequence_length,
142
+ return_overflowing_tokens=True,
143
+ )
144
+ return tokenizer.batch_decode(model_inputs["input_ids"], skip_special_tokens=True)
145
+
146
+ def _to_one_hot(self, labels: List[int], num_labels: int) -> List[int]:
147
+ x = np.zeros(num_labels, dtype=np.float16)
148
+ x[labels] = 1.0
149
+ return x.tolist()
150
+
151
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
152
+ def _generate_examples(self, filepath: str):
153
+ with open(filepath, encoding="utf-8") as f:
154
+ dataset = json.load(f)
155
+
156
+ num_labels, multi_label = self.label_info
157
+ chunk_id = 0
158
+ for instance in dataset:
159
+ iid = instance.get("id", self.__next_id)
160
+ label = instance.get("label")
161
+ if label is None:
162
+ if not multi_label:
163
+ continue
164
+ else:
165
+ label = self._to_one_hot(labels=[], num_labels=num_labels)
166
+ elif isinstance(label, int):
167
+ label = int(label) - 1
168
+ elif isinstance(label, str):
169
+ if multi_label:
170
+ label = [int(l) - 1 for l in label.split(",")]
171
+ label = self._to_one_hot(label, num_labels)
172
+ else:
173
+ label = int(label) - 1
174
+
175
+ article = self.prepare_articles(instance["article"])
176
+ self.__reset_chunk_id()
177
+ for chunk in article:
178
+ chunk_inputs = {
179
+ "iid": iid,
180
+ "chunk_id": self.__next_chunk_id,
181
+ "title": instance["title"],
182
+ "input_ids": chunk,
183
+ "labels": label,
184
+ }
185
+ yield chunk_id, chunk_inputs
186
+ chunk_id += 1
subject_genre/test.json ADDED
The diff for this file is too large to render. See raw diff
 
subject_genre/train.json ADDED
The diff for this file is too large to render. See raw diff
 
subject_genre/val.json ADDED
The diff for this file is too large to render. See raw diff