3v324v23 commited on
Commit
7eb2b91
·
1 Parent(s): 3395d01

updated qampari.py

Browse files
Files changed (2) hide show
  1. mapped_qampari.py +88 -30
  2. qampari.py +2 -1
mapped_qampari.py CHANGED
@@ -12,6 +12,25 @@ from datasets.fingerprint import Hasher
12
  import pickle
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def mega_hash(func,dataset_name,dataset_config,dataset_obj,split):
16
  hasher = Hasher()
17
  hasher.update(repr(dataset_obj))
@@ -29,28 +48,27 @@ logger = datasets.logging.get_logger(__name__)
29
  _CITATION = """ """
30
 
31
  _DESCRIPTION = """ """
 
 
 
 
 
 
 
 
 
32
 
33
 
 
34
 
35
-
36
-
37
- mapped_features = Features({"source": Value(dtype="string"),
38
- "target": Value(dtype="string"),
39
- "meta":{
40
- "chunk_id":Value(dtype="string"),
41
- "qid":Value(dtype="string"),
42
- "question":Value(dtype="string"),
43
- "title":Value(dtype="string"),
44
- "text":Value(dtype="string"),
45
- }
46
- })
47
 
48
 
49
 
50
  class MappedQampariConfig(datasets.BuilderConfig):
51
  """BuilderConfig for MappedQampariDPR."""
52
 
53
- def __init__(self, features=None, retriever=None, feature_format=None, **kwargs):
54
 
55
  super(MappedQampariConfig, self).__init__(**kwargs)
56
  self.features = features
@@ -79,7 +97,7 @@ def to_source_target(example):
79
  for c, a, t, _, _, cid in zip(ctx, ans_for, title, question, qids, cids):
80
  source.append(f"Title: {t}\nText: {c}\nQuestion: {question}\n")
81
  target.append(f"Answer: {ans_mapping[a.split('__')[-2]]}")
82
- meta_list.append({"chunk_id": cid, "qid": qids, "question": question, "title": t, "text": c})
83
  for ctx, title, question, qids, cids in zip(
84
  example["hard_negative_ctxs.text"],
85
  example["hard_negative_ctxs.title"],
@@ -90,7 +108,7 @@ def to_source_target(example):
90
  for c, t, _, _, cid in zip(ctx, title, question, qids, cids):
91
  source.append(f"Title: {t}\nText: {c}\nQuestion: {question}\n")
92
  target.append("Not relevant")
93
- meta_list.append({"chunk_id": cid, "qid": qids, "question": question, "title": t, "text": c})
94
  return {"target": target, "source": source, "meta": meta_list}
95
 
96
 
@@ -116,9 +134,33 @@ class MappedQampari(datasets.GeneratorBasedBuilder):
116
  name="reranking_bm25",
117
  version=datasets.Version("1.0.1", ""),
118
  description="MappedQampari dataset in DPR format with the bm25 retrieval results",
119
- features=mapped_features,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  retriever="bm25",
121
- feature_format="dpr",
122
  ),
123
  ]
124
 
@@ -158,19 +200,35 @@ class MappedQampari(datasets.GeneratorBasedBuilder):
158
 
159
  def _generate_examples(self, split):
160
  """This function returns the examples in the raw (text) form."""
161
- dataset_config = "reranking_bm25"
162
- flattened_dataset = load_dataset("/home/ohadr/ssd/dalle-mini/qampari/qampari.py",dataset_config,split=split).flatten()
163
- fingerprint = mega_hash(to_source_target,"qampari",
164
- dataset_config,flattened_dataset,split)
165
- transformed_dataset = flattened_dataset.map(
166
- to_source_target,
167
- batched=True,
168
- remove_columns=flattened_dataset.column_names,
169
- new_fingerprint = fingerprint
170
- )
171
-
172
- for i,element in enumerate(transformed_dataset):
173
- yield i,element
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
 
176
 
 
12
  import pickle
13
 
14
 
15
+
16
+ def to_dict_element(el, cols):
17
+ bucked_fields = more_itertools.bucket(cols, key=lambda x: x.split(".")[0])
18
+ final_dict = {}
19
+ for parent_name in set(x.split(".")[0] for x in cols):
20
+
21
+ fields = [y.split(".")[-1] for y in list(bucked_fields[parent_name])]
22
+ if len(fields) == 1 and fields[0] == parent_name:
23
+ final_dict[parent_name] = el[fields[0]]
24
+ else:
25
+ parent_list = []
26
+ zipped_fields = list(zip(*[el[f"{parent_name}.{child}"] for child in fields]))
27
+ for x in zipped_fields:
28
+ parent_list.append({k: v for k, v in zip(fields, x)})
29
+ final_dict[parent_name] = parent_list
30
+ return final_dict
31
+
32
+
33
+
34
  def mega_hash(func,dataset_name,dataset_config,dataset_obj,split):
35
  hasher = Hasher()
36
  hasher.update(repr(dataset_obj))
 
48
  _CITATION = """ """
49
 
50
  _DESCRIPTION = """ """
51
+ base_features = {"source": Value(dtype="string"),
52
+ "meta":{
53
+ "id":Value(dtype="string"),
54
+ "qid":Value(dtype="string"),
55
+ "question":Value(dtype="string"),
56
+ "title":Value(dtype="string"),
57
+ "text":Value(dtype="string"),
58
+ }
59
+ }
60
 
61
 
62
+ reranking_mapped_features = Features({**base_features,"target": Value(dtype="string"),})
63
 
64
+ inference_mapped_features = Features(base_features)
 
 
 
 
 
 
 
 
 
 
 
65
 
66
 
67
 
68
  class MappedQampariConfig(datasets.BuilderConfig):
69
  """BuilderConfig for MappedQampariDPR."""
70
 
71
+ def __init__(self, features=None, retriever=None,feature_format=None, **kwargs):
72
 
73
  super(MappedQampariConfig, self).__init__(**kwargs)
74
  self.features = features
 
97
  for c, a, t, _, _, cid in zip(ctx, ans_for, title, question, qids, cids):
98
  source.append(f"Title: {t}\nText: {c}\nQuestion: {question}\n")
99
  target.append(f"Answer: {ans_mapping[a.split('__')[-2]]}")
100
+ meta_list.append({"id": cid, "qid": qids, "question": question, "title": t, "text": c})
101
  for ctx, title, question, qids, cids in zip(
102
  example["hard_negative_ctxs.text"],
103
  example["hard_negative_ctxs.title"],
 
108
  for c, t, _, _, cid in zip(ctx, title, question, qids, cids):
109
  source.append(f"Title: {t}\nText: {c}\nQuestion: {question}\n")
110
  target.append("Not relevant")
111
+ meta_list.append({"id": cid, "qid": qids, "question": question, "title": t, "text": c})
112
  return {"target": target, "source": source, "meta": meta_list}
113
 
114
 
 
134
  name="reranking_bm25",
135
  version=datasets.Version("1.0.1", ""),
136
  description="MappedQampari dataset in DPR format with the bm25 retrieval results",
137
+ features=reranking_mapped_features,
138
+ retriever="bm25",
139
+ feature_format="reranking",
140
+ ),
141
+ MappedQampariConfig(
142
+ name="reranking_dprnq",
143
+ version=datasets.Version("1.0.1", ""),
144
+ description="MappedQampari dataset in DPR format with the bm25 retrieval results",
145
+ features=reranking_mapped_features,
146
+ retriever="dprnq",
147
+ feature_format="reranking",
148
+ ),
149
+ MappedQampariConfig(
150
+ name="inference_dprnq",
151
+ version=datasets.Version("1.0.1", ""),
152
+ description="MappedQampari dataset in DPR format with the bm25 retrieval results",
153
+ features=inference_mapped_features,
154
+ retriever="dprnq",
155
+ feature_format="inference",
156
+ ),
157
+ MappedQampariConfig(
158
+ name="inference_bm25",
159
+ version=datasets.Version("1.0.1", ""),
160
+ description="MappedQampari dataset in DPR format with the bm25 retrieval results",
161
+ features=inference_mapped_features,
162
  retriever="bm25",
163
+ feature_format="inference",
164
  ),
165
  ]
166
 
 
200
 
201
  def _generate_examples(self, split):
202
  """This function returns the examples in the raw (text) form."""
203
+ flattened_dataset = load_dataset("/home/ohadr/ssd/dalle-mini/qampari/qampari.py",
204
+ self.info.config_name,
205
+ split=split).flatten()
206
+ if self.feature_format=="reranking":
207
+ fingerprint = mega_hash(to_source_target,"qampari",
208
+ self.info.config_name,flattened_dataset,split)
209
+ transformed_dataset = flattened_dataset.map(
210
+ to_source_target,
211
+ batched=True,
212
+ remove_columns=flattened_dataset.column_names,
213
+ new_fingerprint = fingerprint
214
+ )
215
+ for i,element in enumerate(transformed_dataset):
216
+ yield i,element
217
+ elif self.feature_format=="inference":
218
+ for i,element in enumerate(flattened_dataset):
219
+ element = to_dict_element(element,cols=flattened_dataset.column_names)
220
+ for j,ctx in enumerate(element['ctxs']):
221
+ qid,ctx,question = element['qid'],ctx,element["question"]
222
+ ctx.pop("score",None)
223
+ source_element = {"source": f"Title: {ctx['title']}\nText: {ctx['text']}\nQuestion: {question}\n",
224
+ "meta":{**ctx,
225
+ "qid":qid,
226
+ "question":question}
227
+ }
228
+ yield f"{qid}__{ctx['id']}__{j}", source_element
229
+
230
+ else:
231
+ assert False
232
 
233
 
234
 
qampari.py CHANGED
@@ -191,8 +191,9 @@ class Qampari(datasets.GeneratorBasedBuilder):
191
 
192
  def _split_generators(self, dl_manager):
193
  filepath = dl_manager.download_and_extract(_URL)
 
194
  filepath = f"{filepath}/qampari_retrievers"
195
- print(filepath)
196
  # filepath = "/home/joberant/home/ohadr/testbed/notebooks/qampari_retrievers"
197
 
198
  return [
 
191
 
192
  def _split_generators(self, dl_manager):
193
  filepath = dl_manager.download_and_extract(_URL)
194
+ # filepath = "/home/ohadr/.cache/huggingface/datasets/downloads/extracted/2e80c1d1256188099e12bd5542e05f537e4741d2cfbd2bfc83ae9aaf90623c09/qampari_retrievers"
195
  filepath = f"{filepath}/qampari_retrievers"
196
+ # print(filepath)
197
  # filepath = "/home/joberant/home/ohadr/testbed/notebooks/qampari_retrievers"
198
 
199
  return [