Vivek Iyer commited on
Commit
42bc5e7
Β·
1 Parent(s): 2f7b656

Adds dataset.py for split processing:

Browse files
chinese/chinese.json β†’ chinese.json RENAMED
File without changes
dataset.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import datasets
4
+
5
+ class XLAlpacaEvalDatasetConfig(datasets.BuilderConfig):
6
+ def __init__(self, split_name=None, **kwargs):
7
+ super().__init__(**kwargs)
8
+ self.split_name = split_name
9
+
10
+ class XLAlpacaEvalDataset(datasets.GeneratorBasedBuilder):
11
+ @classmethod
12
+ def _build_configs(cls, data_dir):
13
+ # Auto-detect all .json files in the data_dir, one config per file
14
+ split_names = [os.path.splitext(f)[0]
15
+ for f in os.listdir(data_dir)
16
+ if f.endswith(".json")]
17
+ return [
18
+ XLAlpacaEvalDatasetConfig(
19
+ name=split_name,
20
+ version=datasets.Version("1.0.0"),
21
+ description=f"Dataset for split {split_name}",
22
+ split_name=split_name,
23
+ )
24
+ for split_name in split_names
25
+ ]
26
+
27
+ def _info(self):
28
+ return datasets.DatasetInfo(
29
+ features=datasets.Features({
30
+ "data": datasets.Value("string"),
31
+ }),
32
+ )
33
+
34
+ def _split_generators(self, dl_manager):
35
+ data_dir = self.config.data_dir or "."
36
+ # One split per config (i.e., per .json file)
37
+ json_file = f"{self.config.split_name}.json"
38
+ split_path = os.path.join(data_dir, json_file)
39
+ return [
40
+ datasets.SplitGenerator(
41
+ name=self.config.split_name,
42
+ gen_kwargs={"json_file": split_path},
43
+ )
44
+ ]
45
+
46
+ def _generate_examples(self, json_file):
47
+ with open(json_file, "r", encoding="utf-8") as f:
48
+ data = json.load(f)
49
+ if isinstance(data, list):
50
+ for idx, example in enumerate(data):
51
+ yield idx, {"data": json.dumps(example)}
52
+ else:
53
+ yield 0, {"data": json.dumps(data)}
54
+
55
+ # For auto-config support (so users don't have to specify split names manually)
56
+ def get_builder_configs():
57
+ data_dir = os.path.dirname(__file__)
58
+ return XLAlpacaEvalDataset._build_configs(data_dir)
english/english.json β†’ english.json RENAMED
File without changes
finnish/finnish.json β†’ finnish.json RENAMED
File without changes
french/french.json β†’ french.json RENAMED
File without changes
german/german.json β†’ german.json RENAMED
File without changes
hindi/hindi.json β†’ hindi.json RENAMED
File without changes
hungarian/hungarian.json β†’ hungarian.json RENAMED
File without changes
irish/irish.json β†’ irish.json RENAMED
File without changes
lithuanian/lithuanian.json β†’ lithuanian.json RENAMED
File without changes
maltese/maltese.json β†’ maltese.json RENAMED
File without changes
portuguese/portuguese.json β†’ portuguese.json RENAMED
File without changes
turkish/turkish.json β†’ turkish.json RENAMED
File without changes