-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader.py
276 lines (246 loc) · 9.58 KB
/
data_loader.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
import argparse, csv, json, os, re, wget
from string import ascii_uppercase
import itertools
import numpy as np
import pandas as pd
from convokit import Corpus, download
from mappings import *
from latex_prompt_exporter import export_latex
def deep_get(recursive_key, recursive_dict):
level = recursive_dict
for key in recursive_key.split("."):
level = level[key]
return level
def boolify(df):
column_names = df.select_dtypes(include=[np.number]).columns
m = df[df.select_dtypes(include=[np.number]).columns].max().reset_index(name="max")
n = m.loc[m["max"] == 1, "max"]
p = column_names[n.index]
df[p] = df[p].astype(bool)
return df
def get_context_column(df, context_column, colname=True):
def row(r, context_column):
if colname or (len(context_column) > 1):
return "\n\n".join([f"{col}: {r[col]}" for col in context_column])
else:
return "\n\n".join([r[col] for col in context_column])
if type(context_column) == tuple:
return [row(r, context_column) for _, r in df.iterrows()]
return df[context_column]
def truncate(text, length=2048):
# print(text, " ".join(text.split(" ")[:length]))
return " ".join(text.split(" ")[:length])
def build_prompts(df, prompt_template):
cols = re.findall(r"{\$([A-Za-z_ ]+)}", prompt_template)
trunc_length = 2048 // max(len(cols), 1)
prompts = []
for _, row in df.iterrows():
prompt = prompt_template
for col in cols:
prompt = prompt.replace(f"{{${col}}}", truncate(row[col], trunc_length))
prompts.append(prompt)
return prompts
def csv_process(dataset, save_dir, local=False, jsonl=False):
context_column, label_columns = csv_column_map[dataset]
additional_labels = []
if type(label_columns) == tuple:
additional_labels = label_columns[1:]
label_columns = label_columns[0]
df = pd.DataFrame()
if local:
df = pd.read_csv("{}/{}.csv".format(save_dir, dataset))
elif jsonl:
filename = "{}/{}.jsonl".format(save_dir, dataset)
if not os.path.exists(filename):
filename = wget.download(jsonl_download[dataset], out=filename)
with open(filename, "r") as infile:
data = data = {i: json.loads(L) for i, L in enumerate(infile.readlines())}
df = pd.DataFrame.from_dict(data).T
else:
filename = "{}/{}.csv".format(save_dir, dataset)
if not os.path.exists(filename):
filename = wget.download(csv_download[dataset], out=filename)
# df = pd.read_csv(filename)
if type(context_column) in {str, tuple}:
df = pd.read_csv(filename)
else:
df = pd.read_csv(filename, header=None)
df["context"] = get_context_column(df, context_column) # df[context_column]
df["labels"] = df[label_columns]
if additional_labels:
df["additional_labels"] = get_context_column(
df, additional_labels, colname=False
)
df = boolify(df)
df["prompts"] = build_prompts(
df, prompts_templates[dataset]
) # [prompts_templates[dataset]] * len(df["labels"])
if dataset in drop_labels:
df = df[~df["labels"].isin(drop_labels[dataset])]
if additional_labels:
df = df[["context", "labels", "prompts", "additional_labels"]]
else:
df = df[["context", "labels", "prompts"]]
if not os.path.exists(save_dir):
os.makedirs(save_dir)
export_latex(
dataset,
df["context"][0],
df["prompts"][0],
df["labels"][0],
"./latex/{}.json".format(dataset),
)
df.to_json("{}/{}.json".format(save_dir, dataset))
def sentence_alphaenumerate(text):
def iter_all_strings():
for size in itertools.count(1):
for s in itertools.product(ascii_uppercase, repeat=size):
yield "".join(s)
string = ""
for i, a in enumerate(iter_all_strings()):
sents = text.split(". ")
if i >= len(sents):
break
string += f"{a}: {sents[i]}"
if i < len(sents) - 1:
string += ".\n"
return string
def alphaenumerate_process(dataset, save_dir):
context_column, label_columns = csv_column_map[dataset]
df = pd.read_csv("{}/{}.csv".format(save_dir, dataset))
df["labels"] = df[label_columns]
df = boolify(df)
df["context"] = [
sentence_alphaenumerate(text) for text in df[context_column].values
]
df["prompts"] = build_prompts(df, prompts_templates[dataset])
print(df.head()["prompts"])
df = df[["context", "labels", "prompts"]]
if not os.path.exists(save_dir):
os.makedirs(save_dir)
export_latex(
dataset,
df["context"][0],
df["prompts"][0],
df["labels"][0],
"./latex/{}.json".format(dataset),
)
df.to_json("{}/{}.json".format(save_dir, dataset))
def convokit_process(dataset, save_dir):
corpus = Corpus(
filename=download(
convokit_ds[dataset],
),
)
df = corpus.get_utterances_dataframe()
contexts = []
conversation_ids = []
label_utterance_ids = []
speaker_utterance_maps = []
label_type, label_field, context_length = convokit_labels[dataset]
for _, convo in df.groupby("conversation_id").__iter__():
if context_length == "all":
convo = convo.sort_values(by="timestamp")
context = convo["text"].values.tolist()
speakers = convo["speaker"].values.tolist()
speakers = [
speaker if type(speaker) == type("str") else speaker.id
for speaker in speakers
]
utterance_ids = convo.index.values.tolist()
context = [
"{}: {}".format(speaker, context)
for context, speaker in zip(context, speakers)
]
contexts.append("\n".join(context))
conversation_ids.append(convo["conversation_id"].values.tolist()[0])
label_utterance_ids.append(utterance_ids[-1])
speaker_utterance_maps.append(
{
speaker: convo.index[convo["speaker"] == speaker].values.tolist()[0]
for speaker in set(speakers)
}
)
elif context_length == "first_two":
convo = convo.sort_values(by="timestamp")
first = convo.index.values.tolist()[0]
initial = corpus.get_utterance(first)
initial_message = "{}: {}".format(
initial.speaker.id,
initial.text,
)
replies = convo[convo["reply_to"] == first]
context = replies["text"].values.tolist()
speakers = replies["speaker"].values.tolist()
reply_messages = [
"{}: {}".format(speaker, context)
for context, speaker in zip(context, speakers)
]
context = ["\n".join([initial_message, reply]) for reply in reply_messages]
contexts.extend(context)
conversation_ids.extend(replies["conversation_id"].values.tolist())
label_utterance_ids.extend(replies.index.values.tolist())
labels = []
prompts = []
if label_type == "utterance":
for utterance_id in label_utterance_ids:
utterance = corpus.get_utterance(utterance_id).to_dict()
label = deep_get(label_field, utterance)
labels.append(label)
prompts.append(prompts_templates[dataset])
elif label_type == "speaker":
speaker_contexts = []
for i, utterances in enumerate(speaker_utterance_maps):
for speaker, utterance_id in utterances.items():
speaker_contexts.append(contexts[i])
utterance = corpus.get_utterance(utterance_id).to_dict()
label = deep_get(label_field, utterance)
labels.append(label)
prompts.append(
prompts_templates[dataset].replace("{$speaker}", speaker)
)
contexts = speaker_contexts
elif label_type == "conversation":
for conversation_id in conversation_ids:
conversation = corpus.get_conversation(conversation_id).to_dict()
label = deep_get(label_field, conversation)
labels.append(label)
prompts.append(prompts_templates[dataset])
assert len(contexts) == len(labels) and len(contexts) == len(prompts)
raw_data = {"context": contexts, "labels": labels, "prompts": prompts}
data_f = pd.DataFrame.from_dict(raw_data)
if dataset in drop_labels:
data_f = data_f[~data_f["labels"].isin(drop_labels[dataset])]
if not os.path.exists(save_dir):
os.makedirs(save_dir)
export_latex(
dataset,
contexts[0],
prompts[0],
labels[0],
"./latex/{}.json".format(dataset),
)
data_f.to_json(save_dir + "/{}.json".format(dataset))
def main(dataset, save_dir):
if dataset in convokit_ds:
convokit_process(dataset, save_dir)
elif dataset in {"hippocorpus"}:
alphaenumerate_process(dataset, save_dir)
elif dataset in csv_download:
csv_process(dataset, save_dir)
elif dataset in jsonl_download:
csv_process(dataset, save_dir, jsonl=True)
else:
csv_process(dataset, save_dir, local=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
"-d",
type=str,
default="power",
choices=list(prompts_templates.keys()),
)
parser.add_argument("--save_dir", "-s", type=str, default="processed")
args = parser.parse_args()
main(args.dataset, args.save_dir)