-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathparse.py
330 lines (266 loc) · 9.31 KB
/
parse.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
evol-instruct
koalpaca v1.1
kullm v2 - dolly, gpt4all 2개만
changpt/ko-lima-vicuna
HAERAE-HUB/KoInstruct-QA
heegyu/korquad-chat-v1
heegyu/kowow
NomaDamas/Ko-StrategyQA
"""
import click
import os
import json, jsonlines
from tqdm import tqdm
import requests
import pandas as pd
import random
from datasets import load_dataset
from traceback import print_exc
from pprint import pprint
from itertools import chain
def download(url, filename, read=False, encoding="utf-8"):
os.makedirs("download", exist_ok=True)
filename = os.path.join("download", filename)
if not os.path.exists(filename):
with open(filename, "w", encoding=encoding) as f:
print(f"download {url} -> {filename}")
text = requests.get(url).text
f.write(text)
elif read:
with open(filename, encoding="utf-8") as f:
text = f.read()
else:
return None
return text
def load_jsonl_text(text):
return [json.loads(x) for x in text.split("\n") if x]
HUMAN_FROMS = ["user"]
BOT_FROMS = ["gpt", "bard"]
def write_to_file(iterable, item_handler, filename):
fout = jsonlines.open(f"data/{filename}.json", "w")
for item in tqdm(iterable, desc=filename):
try:
if item_handler is not None:
conv = item_handler(item)
else:
conv = item["conversations"]
if conv is None:
continue
for c in conv:
if c["from"] in HUMAN_FROMS:
c["from"] = "human"
elif c["from"] in BOT_FROMS:
c["from"] = "bot"
item = {
"conversations": conv
}
fout.write(item)
except:
pprint(conv)
print_exc()
raise KeyboardInterrupt()
fout.close()
def process_alpaca_format(dataset, output_name, inst_key="instruction", output_key="output", input_key="input"):
def handler(item):
output = [
{
"from": "human",
"value": item[inst_key]
},
{
"from": "bot",
"value": item[output_key]
}
]
item_input = item.get(input_key, "")
if item_input and item_input != "nan":
output.insert(0, {
"from": "input",
"value": item_input
})
return output
write_to_file(dataset, handler, output_name)
@click.group()
def cli():
pass
@cli.command()
def strategyqa():
url_items = "https://huggingface.co/datasets/NomaDamas/Ko-StrategyQA/raw/main/ko-strategy-qa_full.json"
# url_para = "https://huggingface.co/datasets/NomaDamas/Ko-StrategyQA/resolve/main/ko-strategy-qa_paragraphs.csv"
items = json.loads(download(url_items, "ko-strategy-qa_full.json", read=True))
# download(url_para, "ko-strategy-qa_paragraphs.csv", encoding="cp949")
# df = pd.read_csv("download/ko-strategy-qa_paragraphs.csv")
# print(df)
# pprint(list(items.values())[:10])
response = {
"True": ["그렇습니다", "예", "네", "맞아요", "예 맞습니다"],
"False": ["아닙니다", "아니요", "아닙니다", "그렇지 않아요"]
}
def handler(item):
res = random.choice(response[item["answer"]])
output = [
{
"from": "human",
"value": item["question"]
},
{
"from": "bot",
"value": res + " " + " ".join(item["facts"])
}
]
return output
write_to_file(items.values(), handler, "strategy_qa")
@cli.command()
def korquadchat():
dataset = load_dataset("heegyu/korquad-chat-v1", split="train")
def conv_mapper(conv):
if conv.startswith("<usr>") or conv.startswith("A :") or conv.startswith("A:"):
who = "human"
text = conv.replace("<usr>", "").replace("A :", "").replace("A:", "").strip()
elif conv.startswith("<sys>"):
who = "input"
text = conv.replace("<sys>", "").strip()
elif conv.startswith("<bot>") or conv.startswith("B:") or conv.startswith("B :"):
who = "bot"
text = conv.replace("<bot>", "").replace("B:", "").replace("B :", "").strip()
else:
raise ValueError(f"이상한거 발견 '{conv}'")
return {
"from": who,
"value": text
}
def handler(item):
if "\nC:" in item["text"]:
return None
convs = item["text"].strip().split("\n")
try:
convs = [conv_mapper(x.strip()) for x in convs if x.strip()]
except ValueError as e:
print_exc()
print(item)
return None
return convs
write_to_file(dataset, handler, "korquadchat")
@cli.command()
def koinstruct_qa():
dataset = load_dataset("HAERAE-HUB/KoInstruct-QA", split="train")
process_alpaca_format(dataset, "koinstruct_qa")
@cli.command()
def kowow():
# dataset = load_dataset("heegyu/kowow", split="train")
url = "https://huggingface.co/datasets/heegyu/kowow/resolve/main/train_ko.json"
dataset = json.loads(download(url, "kowow.json", True))
speaker2from = {
"0_Wizard": "bot",
"1_Wizard": "bot",
"1_Apprentice": "human",
"0_Apprentice": "human"
}
def parse_item(item):
if item["text"] == "no_passages_used":
return None
output = [{
"from": speaker2from[item["speaker"]],
"value": item["text"]
}]
if "checked_sentence" in item and len(item["checked_sentence"]) > 0:
output.insert(0, {
"from": "input",
"value": list(item["checked_sentence"].values())[0]
})
return output
def handler(item):
convs = item["dialog"]
convs = map(parse_item, convs)
convs = filter(lambda x: x is not None, convs)
# print(list(convs))
convs = chain(*convs)
convs = list(convs)
for i in range(1, len(convs)):
if convs[i]['from'] == 'input':
a = convs[i - 1]
b = convs[i]
convs[i - 1] = b
convs[i] = a
return convs
write_to_file(dataset, handler, "kowow")
@cli.command()
def kullm_v2_dolly_gpt4all():
dataset = load_dataset("nlpai-lab/kullm-v2", split="train")
def filter_fn(item):
if "dolly" in item["id"]:
return True
if "gpt4all" in item["id"]:
return True
else:
return False
dataset = dataset.filter(filter_fn)
process_alpaca_format(dataset, "kullm_v2")
@cli.command()
def sharegpt_clean_nocode():
# url = "https://huggingface.co/datasets/dbdu/ShareGPT-74k-ko/resolve/main/part2_ko_cleaned.json?download=true"
dataset = load_dataset("dbdu/ShareGPT-74k-ko", data_files={"train": "part2_ko_cleaned.json"})["train"]
write_to_file(dataset, None, "sharegpt_clean_nocode")
@cli.command()
def kolima():
dataset = load_dataset("changpt/ko-lima-vicuna", split="train")
write_to_file(dataset, None, "kolima")
@cli.command()
def evol_instruct():
url = "https://raw.githubusercontent.com/lcw99/evolve-instruct/main/evol_instruct.json"
def handler(item):
return [
{
"from": "human",
"value": item["input"]
},
{
"from": "bot",
"value": item["output"]
},
]
items = json.loads(requests.get(url).text)
write_to_file(items, handler, "evol_instruct")
@cli.command()
def koalpaca_v1_1():
def handler(item):
return [
{
"from": "user",
"value": item["instruction"]
},
{
"from": "bot",
"value": item["output"]
},
]
items = load_jsonl_text(requests.get("https://raw.githubusercontent.com/Beomi/KoAlpaca/main/KoAlpaca_v1.1.jsonl").text)
write_to_file(items, handler, "koalpaca_v1.1")
@cli.command()
def kocot_2000():
dataset = load_dataset("kyujinpy/KoCoT_2000", split="train")
process_alpaca_format(dataset, "kocot_2000", inst_key="source", output_key="rationale")
@cli.command()
def openorca_ko():
dataset = load_dataset("kyujinpy/OpenOrca-KO", split="train")
process_alpaca_format(dataset, "openorca_ko")
@cli.command()
def wikiqa_dedup():
dataset = load_dataset("HumanF-MarkrAI/WIKI_QA_Near_dedup", split="train")
process_alpaca_format(dataset, "wikiqa_dedup")
@cli.command()
def kopen_platypus():
dataset = load_dataset("kyujinpy/KOpen-platypus", split="train")
process_alpaca_format(dataset, "kopen_platypus")
@cli.command()
def hrc():
dataset = load_dataset("heegyu/HRC", split="train")
process_alpaca_format(dataset, "hrc", inst_key="질의", output_key="상담례, 결정례 기반 답변")
@cli.command()
def kor_ethical_question_answer():
dataset = load_dataset("MrBananaHuman/kor_ethical_question_answer", split="train")
dataset = dataset.filter(lambda x: x["label"] == 0)
process_alpaca_format(dataset, "kor_ethical_question_answer", inst_key="question", output_key="answer")
if __name__ == "__main__":
cli()