SpeedOfMagic commited on
Commit
ae41410
·
verified ·
1 Parent(s): 41f9621

Create polygraph_xsum.py

Browse files
Files changed (1) hide show
  1. polygraph_xsum.py +72 -0
polygraph_xsum.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import os
3
+ import json
4
+
5
+ _DESCRIPTION = "lm-polygraph wrapper for xsum dataset"
6
+
7
+ _LANGS = ["en"]
8
+
9
+
10
+ _DATA_DIRECTORY = "."
11
+ VERSION = datasets.Version("0.0.1")
12
+ _SPLITS = ["train", "validation", "test"]
13
+ _CONFIG = {
14
+ "dataset": "xsum"
15
+ }
16
+
17
+
18
+ def _prepare_dataset(dataset):
19
+ return dataset["document"], dataset["summary"]
20
+
21
+
22
+ class PolygraphXsum(datasets.GeneratorBasedBuilder):
23
+ """lm-polygraph wrapper for xsum dataset"""
24
+
25
+
26
+ def _info(self):
27
+ features = datasets.Features(
28
+ {
29
+ "input": datasets.Value("string"),
30
+ "output": datasets.Value("string"),
31
+ }
32
+ )
33
+
34
+ return datasets.DatasetInfo(
35
+ description=_DESCRIPTION,
36
+ features=features,
37
+ )
38
+
39
+ def _split_generators(self, dl_manager):
40
+ dataset = datasets.load_dataset(_CONFIG["dataset"], trust_remote_code=True)
41
+
42
+ def download_custom_dataset(src_url: str, dst_path: str):
43
+ split = src_url
44
+ x, y = _prepare_dataset(dataset)
45
+ result_dataset = Dataset.from_dict({"input": x, "output": y})
46
+ result_dataset.to_json(dst_path)
47
+ downloaded_files = dl_manager.download_custom(_SPLITS, download_custom_dataset)
48
+
49
+ return [
50
+ datasets.SplitGenerator(
51
+ name=datasets.Split.TRAIN,
52
+ gen_kwargs={
53
+ "filepath": downloaded_files["train"],
54
+ "lang": lang,
55
+ }),
56
+ datasets.SplitGenerator(
57
+ name=datasets.Split.VALIDATION,
58
+ gen_kwargs={
59
+ "filepath": downloaded_files["validation"],
60
+ }),
61
+ datasets.SplitGenerator(
62
+ name=datasets.Split.TEST,
63
+ gen_kwargs={
64
+ "filepath": downloaded_files["test"],
65
+ })
66
+ ]
67
+
68
+ def _generate_examples(self, filepath, lang):
69
+ with open(filepath, encoding="utf-8") as f:
70
+ dataset = Dataset.from_json(f.read())
71
+ for i in range(len(item)):
72
+ yield i, dataset[i]