jordyvl commited on
Commit
7fd5eb6
1 Parent(s): 1032f38

Made kwarg dependent builder configs for loading DUDE, in next step could make nonbinary_to_imdb mode

Browse files
Files changed (2) hide show
  1. DUDE_loader.py +39 -26
  2. test_loader.py +1 -1
DUDE_loader.py CHANGED
@@ -17,11 +17,10 @@
17
  import os
18
  import copy
19
  import json
20
- from typing import List
21
  import pdf2image
22
  from tqdm import tqdm
23
 
24
-
25
  import datasets
26
 
27
 
@@ -113,7 +112,9 @@ class DUDEConfig(datasets.BuilderConfig):
113
 
114
  def __init__(
115
  self,
116
- binary_mode: bool,
 
 
117
  **kwargs,
118
  ):
119
  """BuilderConfig for DUDE.
@@ -121,9 +122,28 @@ class DUDEConfig(datasets.BuilderConfig):
121
  binary_mode: `boolean`, load binary PDFs/OCR or pass along paths on local file system
122
  **kwargs: keyword arguments forwarded to super.
123
  """
124
- BINARY_MODE = False
125
  super(DUDEConfig, self).__init__(description=_DESCRIPTION, **kwargs)
126
- self.binary_mode = binary_mode or BINARY_MODE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
 
129
  class DUDE(datasets.GeneratorBasedBuilder):
@@ -131,13 +151,11 @@ class DUDE(datasets.GeneratorBasedBuilder):
131
 
132
  VERSION = datasets.Version("0.0.1")
133
 
134
- BUILDER_CONFIGS = [
135
- DUDEConfig(name='DUDE', version=VERSION, binary_mode=False),
136
- DUDEConfig(name='DUDE-binary', version=VERSION, binary_mode=True)
137
- ]
138
-
139
- DEFAULT_CONFIG_NAME = "DUDE" #for some reason not working
140
 
 
 
 
141
 
142
  def _info(self):
143
  features = datasets.Features(
@@ -158,8 +176,12 @@ class DUDE(datasets.GeneratorBasedBuilder):
158
  "answers_variants": datasets.Sequence(datasets.Value("string")),
159
  "answer_type": datasets.Value("string"),
160
  "data_split": datasets.Value("string"),
161
- "document": datasets.Value("binary") if self.config.binary_mode else datasets.Value("string"),
162
- "OCR": datasets.Value("binary") if self.config.binary_mode else datasets.Value("string"),
 
 
 
 
163
  }
164
  )
165
 
@@ -179,19 +201,14 @@ class DUDE(datasets.GeneratorBasedBuilder):
179
  annotations = json.load(open(_URLS[f"annotations"], "r"))
180
  # binaries_archive = dl_manager.iter_archive(binaries_path)
181
 
182
- if self.config.data_dir: #unpacked it to a custom directory
183
  binary_extraction_path = self.config.data_dir
184
  else:
185
- binaries_path = dl_manager.download(_URLS['binaries'])
186
  binary_extraction_path = dl_manager.extract(binaries_path)
187
- binary_extraction_path += "/home/jordy/Downloads/" + _URLS["binaries"].split("/")[
188
- -1
189
- ].replace(
190
- ".tar.gz", ""
191
- ) # weird unpacking behaviour
192
 
193
  splits = []
194
- for split in _SPLITS: # split archive
195
  splits.append(
196
  datasets.SplitGenerator(
197
  name=split,
@@ -209,16 +226,12 @@ class DUDE(datasets.GeneratorBasedBuilder):
209
  extracted_path = os.path.join(binary_extraction_path, "PDF", split, docid + ".pdf")
210
  return extracted_path
211
 
212
-
213
  def retrieve_OCR(docid, ocr_engine="Amazon", format="original"):
214
  extracted_path = os.path.join(
215
  binary_extraction_path, "OCR", ocr_engine, docid + f"_{format}.json"
216
  )
217
  return extracted_path
218
 
219
- question = self.info.features["question"]
220
- answers = self.info.features["answers"]
221
-
222
  annotations = [x for x in annotations if x["data_split"] == split]
223
 
224
  for i, a in enumerate(annotations):
@@ -235,5 +248,5 @@ class DUDE(datasets.GeneratorBasedBuilder):
235
  a["OCR"] = g.read()
236
  else:
237
  a["document"] = docpath
238
- a["OCR"] = ocrpath
239
  yield i, a
 
17
  import os
18
  import copy
19
  import json
20
+ from typing import List, Literal
21
  import pdf2image
22
  from tqdm import tqdm
23
 
 
24
  import datasets
25
 
26
 
 
112
 
113
  def __init__(
114
  self,
115
+ binary_mode: bool = False,
116
+ ocr_engine: Literal["Azure", "Amazon", "Tesseract"] = "Amazon",
117
+ format: Literal["original", "due"] = "original",
118
  **kwargs,
119
  ):
120
  """BuilderConfig for DUDE.
 
122
  binary_mode: `boolean`, load binary PDFs/OCR or pass along paths on local file system
123
  **kwargs: keyword arguments forwarded to super.
124
  """
 
125
  super(DUDEConfig, self).__init__(description=_DESCRIPTION, **kwargs)
126
+ self.binary_mode = binary_mode
127
+ self.ocr_engine = ocr_engine
128
+ self.format = format
129
+
130
+
131
+ def builder_configs(version):
132
+ configurations = []
133
+ for binary_mode in [True, False]:
134
+ for ocr_engine in ["Azure", "Amazon", "Tesseract"]:
135
+ for format in ["original", "due"]:
136
+ binary_name = "bin_" if binary_mode else ""
137
+ configurations.append(
138
+ DUDEConfig(
139
+ name=f"{binary_name}{ocr_engine}_{format}",
140
+ version=version,
141
+ binary_mode=binary_mode,
142
+ ocr_engine=ocr_engine,
143
+ format=format,
144
+ )
145
+ )
146
+ return configurations
147
 
148
 
149
  class DUDE(datasets.GeneratorBasedBuilder):
 
151
 
152
  VERSION = datasets.Version("0.0.1")
153
 
154
+ BUILDER_CONFIGS = builder_configs(VERSION)
 
 
 
 
 
155
 
156
+ DEFAULT_CONFIG_NAME = (
157
+ "Amazon_original" # for some reason not working, need to pass a config anyway
158
+ )
159
 
160
  def _info(self):
161
  features = datasets.Features(
 
176
  "answers_variants": datasets.Sequence(datasets.Value("string")),
177
  "answer_type": datasets.Value("string"),
178
  "data_split": datasets.Value("string"),
179
+ "document": datasets.Value("binary")
180
+ if self.config.binary_mode
181
+ else datasets.Value("string"),
182
+ "OCR": datasets.Value("binary")
183
+ if self.config.binary_mode
184
+ else datasets.Value("string"),
185
  }
186
  )
187
 
 
201
  annotations = json.load(open(_URLS[f"annotations"], "r"))
202
  # binaries_archive = dl_manager.iter_archive(binaries_path)
203
 
204
+ if self.config.data_dir: # unpacked it to a custom directory
205
  binary_extraction_path = self.config.data_dir
206
  else:
207
+ binaries_path = dl_manager.download(_URLS["binaries"])
208
  binary_extraction_path = dl_manager.extract(binaries_path)
 
 
 
 
 
209
 
210
  splits = []
211
+ for split in _SPLITS:
212
  splits.append(
213
  datasets.SplitGenerator(
214
  name=split,
 
226
  extracted_path = os.path.join(binary_extraction_path, "PDF", split, docid + ".pdf")
227
  return extracted_path
228
 
 
229
  def retrieve_OCR(docid, ocr_engine="Amazon", format="original"):
230
  extracted_path = os.path.join(
231
  binary_extraction_path, "OCR", ocr_engine, docid + f"_{format}.json"
232
  )
233
  return extracted_path
234
 
 
 
 
235
  annotations = [x for x in annotations if x["data_split"] == split]
236
 
237
  for i, a in enumerate(annotations):
 
248
  a["OCR"] = g.read()
249
  else:
250
  a["document"] = docpath
251
+ a["OCR"] = ocrpath
252
  yield i, a
test_loader.py CHANGED
@@ -22,7 +22,7 @@ from codetiming import Timer
22
  for binding in ["dict_annotations (new)"]: #"dict_PDF",
23
  with Timer(name=f"{binding}", text=binding + " Elapsed time: {:.4f} seconds"):
24
  if binding == "dict_annotations (new)":
25
- ds = load_dataset("../DUDE_loader/DUDE_loader.py", 'DUDE', data_dir="/home/jordy/Downloads/DUDE_train-val-test_binaries") #ignore_verifications=True, , writer_batch_size=10
26
  else:
27
  ds = load_dataset("jordyvl/DUDE_loader", revision='db20bbf751b14e14e8143170bc201948ef5ac83c')
28
 
 
22
  for binding in ["dict_annotations (new)"]: #"dict_PDF",
23
  with Timer(name=f"{binding}", text=binding + " Elapsed time: {:.4f} seconds"):
24
  if binding == "dict_annotations (new)":
25
+ ds = load_dataset("../DUDE_loader/DUDE_loader.py", 'bin_Amazon_original', data_dir="/home/jordy/Downloads/DUDE_train-val-test_binaries", ocr_engine='Azure') #ignore_verifications=True, , writer_batch_size=10
26
  else:
27
  ds = load_dataset("jordyvl/DUDE_loader", revision='db20bbf751b14e14e8143170bc201948ef5ac83c')
28