Update rvl_cdip_n_mp.py

#1
by jordyvl - opened
Files changed (1) hide show
  1. rvl_cdip_n_mp.py +36 -25
rvl_cdip_n_mp.py CHANGED
@@ -52,8 +52,8 @@ _HOMEPAGE = "https://www.cs.cmu.edu/~aharley/rvl-cdip/"
52
  _LICENSE = "https://www.industrydocuments.ucsf.edu/help/copyright/"
53
 
54
 
55
- SOURCE = "bdpc/rvl_cdip_mp"
56
- _URL = f"https://huggingface.co/datasets/{SOURCE}/resolve/main/data.gz"
57
  _BACKOFF_folder = "/mnt/lerna/data/RVL-CDIP-NO/RVL-CDIP-N_pdf/data"
58
 
59
  _CLASSES = [
@@ -104,9 +104,7 @@ class RvlCdipNMp(datasets.GeneratorBasedBuilder):
104
  if isinstance(self.config.data_dir, str):
105
  folder = self.config.data_dir # contains the folder structure at someone local disk
106
  else:
107
- if not os.path.exists(_BACKOFF_folder):
108
- raise ValueError("No data folder found. Please set data_dir or data_files.")
109
- folder = _BACKOFF_folder # my local path, others should set data_dir or data_files
110
  self.config.data_dir = folder
111
 
112
  return datasets.DatasetInfo(
@@ -133,26 +131,39 @@ class RvlCdipNMp(datasets.GeneratorBasedBuilder):
133
 
134
  return [datasets.SplitGenerator(name="test", gen_kwargs={"archive_path": data_files})]
135
 
136
- def _generate_examples(self, archive_path):
137
  labels = self.info.features["labels"]
138
-
139
  extensions = {".pdf", ".PDF"}
140
 
141
- for i, path in tqdm(enumerate(Path(archive_path).glob("**/*")), desc=f"{archive_path}"):
142
- if path.suffix in extensions:
143
- try:
144
- if _MODE == "binary":
145
- images = open_pdf_binary(path)
146
- # batched_conversion(path)
147
- else:
148
- images = path
149
-
150
- a = dict(
151
- id=path.name,
152
- file=images,
153
- labels=labels.encode_example(path.parent.name.lower()),
154
- )
155
- yield path.name, a
156
-
157
- except Exception as e:
158
- logger.warning(f"{e} failed to parse {i}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  _LICENSE = "https://www.industrydocuments.ucsf.edu/help/copyright/"
53
 
54
 
55
+ SOURCE = "bdpc/rvl_cdip_n_mp"
56
+ _URL = f"https://huggingface.co/datasets/{SOURCE}/resolve/main/data.tar.gz"
57
  _BACKOFF_folder = "/mnt/lerna/data/RVL-CDIP-NO/RVL-CDIP-N_pdf/data"
58
 
59
  _CLASSES = [
 
104
  if isinstance(self.config.data_dir, str):
105
  folder = self.config.data_dir # contains the folder structure at someone local disk
106
  else:
107
+ folder = _URL if not os.path.exists(_BACKOFF_folder) else _BACKOFF_folder
 
 
108
  self.config.data_dir = folder
109
 
110
  return datasets.DatasetInfo(
 
131
 
132
  return [datasets.SplitGenerator(name="test", gen_kwargs={"archive_path": data_files})]
133
 
134
+ def generate_example(self, path, file=None):
135
  labels = self.info.features["labels"]
 
136
  extensions = {".pdf", ".PDF"}
137
 
138
+ path = Path(path) # ensure path is a pathlib object
139
+ if path.suffix in extensions:
140
+ if file is None:
141
+ if _MODE == "binary":
142
+ file = open_pdf_binary(path)
143
+ # batched_conversion(path)
144
+ else:
145
+ file = path
146
+
147
+ a = dict(
148
+ id=path.name,
149
+ file=file,
150
+ labels=labels.encode_example(path.parent.name.lower()),
151
+ )
152
+
153
+ return path.name, a
154
+
155
+ def _generate_examples(self, archive_path):
156
+ if self.config.data_dir.endswith(".tar.gz"):
157
+ iterator = archive_path
158
+ else:
159
+ iterator = Path(archive_path).glob("**/*")
160
+
161
+ for i, path in tqdm(enumerate(iterator), desc=f"{archive_path}"):
162
+ file = None
163
+ if isinstance(path, tuple):
164
+ path = path[0]
165
+ file = path[1]
166
+ try:
167
+ yield self.generate_example(path, file=file)
168
+ except Exception as e:
169
+ logger.warning(f"{e} failed to parse {path}")