File size: 3,477 Bytes
6e94fbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
long:

{
    "document": "",
    "question": "",
    "long_answer_candidates": ["", "", ""],
    "long_answer_candidate_index": 0
}

short: 

{
    "document": "",
    "question": "",
    "short_answer": ""
}

either:

{
    "document": "",
    "question": "",
    "answer": ""
}

"""


import sys
import jsonlines
from datasets import load_dataset
from huggingface_hub import HfApi


def filter(raw, short_path, long_path, either_path):
    fps = open(short_path, "a")
    writers = jsonlines.Writer(fps)

    fpl = open(long_path, "a")
    writerl = jsonlines.Writer(fpl)

    fpe = open(either_path, "a")
    writere = jsonlines.Writer(fpe)

    count = 0
    long = []
    short = []
    either = []
    for sample in raw:
        try:
            answer = ""

            if sample["short_answers"][0]:
                answer = sample["short_answers"][0]
                
                short.append({
                    "document": sample["document"],
                    "question": sample["question"],
                    "short_answer": answer
                })

            if sample["long_answer_candidate_index"] != -1:
                answer = sample["long_answer_candidates"][sample["long_answer_candidate_index"]]        # long answer will have precedence over short answer
                
                long.append({
                    "document": sample["document"],
                    "question": sample["question"],
                    "long_answer_candidates": sample["long_answer_candidates"],
                    "long_answer_candidate_index": sample["long_answer_candidate_index"]
                })

            if answer:
                count += 1          # count only if there is an answer
                
                either.append({
                    "document": sample["document"],
                    "question": sample["question"],
                    "answer": answer
                })
            
        except Exception as ex:
            # raise ex
            print("Exception: " + str(ex))

        if (count + 1) % 1000 == 0:
            writere.write_all(either)
            either = []

            if short:
                writers.write_all(short)
                short = []
            
            if long:
                writerl.write_all(long)
                long = []

        print("Done: " + str(count), end="\r")


    if either:
        writere.write_all(either)
        either = []

    if short:
        writers.write_all(short)
        short = []
    
    if long:
        writerl.write_all(long)
        long = []

    writere.close()
    fpe.close()

    writers.close()
    fps.close()

    writerl.close()
    fpl.close()


if __name__ == "__main__":
    if len(sys.argv) < 1:
        raise AttributeError("Missing required argument: repository id")

    repo = sys.argv[1]

    api = HfApi()

    train_data = load_dataset(repo, split="train", streaming=True)
    filter(raw=train_data, short_path="data/short/train.jsonl", long_path="data/long/train.jsonl", either_path="data/either/train.jsonl")

    val_data = load_dataset(repo, split="validation", streaming=True)
    filter(raw=val_data, short_path="data/short/validation.jsonl", long_path="data/long/validation.jsonl", either_path="data/either/validation.jsonl")

    api.upload_folder(
        folder_path="data/",
        repo_id=repo,
        repo_type="dataset",
        multi_commits=True,
        multi_commits_verbose=True
    )