TAR functionality
Browse files- unit-test_PDFfolder.py +9 -52
unit-test_PDFfolder.py
CHANGED
@@ -8,34 +8,20 @@ logger = datasets.logging.get_logger(__name__)
|
|
8 |
|
9 |
_DESCRIPTION = "A generic pdf folder"
|
10 |
|
11 |
-
_CLASSES = ["categoryA", "categoryB"]
|
12 |
|
13 |
_URL = "https://huggingface.co/datasets/jordyvl/unit-test_PDFfolder/resolve/main/data/data.tar.gz"
|
14 |
|
15 |
-
#folder
|
16 |
# train
|
17 |
# categoryA
|
18 |
# file1
|
19 |
# test
|
20 |
-
|
21 |
|
22 |
-
#RVL-CDIP_multi
|
23 |
|
24 |
class PdfFolder(datasets.GeneratorBasedBuilder):
|
25 |
def _info(self):
|
26 |
-
|
27 |
-
"""
|
28 |
-
folder = None
|
29 |
-
elif isinstance(self.config.data_files, str):
|
30 |
-
folder = self.config.data_files
|
31 |
-
elif isinstance(self.config.data_files, dict):
|
32 |
-
folder = self.config.data_files.get("train", None)
|
33 |
-
|
34 |
-
if folder is None:
|
35 |
-
raise RuntimeError()
|
36 |
-
"""
|
37 |
-
#classes = sorted([x.name.lower() for x in Path(_URL).glob("*/**")])
|
38 |
-
|
39 |
return datasets.DatasetInfo(
|
40 |
description=_DESCRIPTION,
|
41 |
features=datasets.Features(
|
@@ -58,66 +44,37 @@ class PdfFolder(datasets.GeneratorBasedBuilder):
|
|
58 |
name=datasets.Split.TRAIN,
|
59 |
gen_kwargs={
|
60 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
61 |
-
"supposed_labelset": "train"
|
62 |
},
|
63 |
),
|
64 |
datasets.SplitGenerator(
|
65 |
name=datasets.Split.TEST,
|
66 |
gen_kwargs={
|
67 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
68 |
-
"supposed_labelset": "test"
|
69 |
},
|
70 |
),
|
71 |
datasets.SplitGenerator(
|
72 |
name=datasets.Split.VALIDATION,
|
73 |
gen_kwargs={
|
74 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
75 |
-
"supposed_labelset": "val"
|
76 |
},
|
77 |
),
|
78 |
]
|
79 |
|
80 |
-
|
81 |
-
# if isinstance(self.config.data_files, str):
|
82 |
-
# return [
|
83 |
-
# datasets.SplitGenerator(
|
84 |
-
# name=datasets.Split.TRAIN, gen_kwargs={"archive_path": self.config.data_files}
|
85 |
-
# )
|
86 |
-
# ]
|
87 |
-
|
88 |
-
# splits = []
|
89 |
-
# for split_name, folder in self.config.data_files.items():
|
90 |
-
# splits.append(
|
91 |
-
# datasets.SplitGenerator(name=split_name, gen_kwargs={"archive_path": folder})
|
92 |
-
# )
|
93 |
-
|
94 |
-
# return splits
|
95 |
-
|
96 |
def _generate_examples(self, archive_iterator, supposed_labelset):
|
97 |
|
98 |
-
#could also get the label from somewhere else
|
99 |
-
|
100 |
-
#data/train/categoryB/581261-brown-invoice-10-29-11-04-12-2-13530037073412-pdf.pdf
|
101 |
-
#folder/labelset/label/filename
|
102 |
-
|
103 |
-
#full_path = os.path.join(archive_iterator.args[0], file_path)
|
104 |
-
|
105 |
-
#archive_path = archive_iterator.args[0]
|
106 |
-
|
107 |
extensions = {"pdf", "PDF"}
|
108 |
for file_path, file_obj in archive_iterator:
|
109 |
|
110 |
-
if file_path.split(".")[-1] not in extensions:
|
111 |
continue
|
112 |
|
113 |
folder, labelset, label, filename = file_path.split("/")
|
114 |
if labelset != supposed_labelset:
|
115 |
continue
|
116 |
-
|
117 |
-
images = pdf2image.convert_from_bytes(file_obj.read()) #can only read it once
|
118 |
-
#simple = {"path": file_path, "bytes": file_obj.read(), "labels": label}
|
119 |
|
120 |
-
|
121 |
|
122 |
-
|
123 |
-
#labels.encode_example(path.parent.name.lower())
|
|
|
8 |
|
9 |
_DESCRIPTION = "A generic pdf folder"
|
10 |
|
11 |
+
_CLASSES = ["categoryA", "categoryB"] # define in advance
|
12 |
|
13 |
_URL = "https://huggingface.co/datasets/jordyvl/unit-test_PDFfolder/resolve/main/data/data.tar.gz"
|
14 |
|
15 |
+
# folder
|
16 |
# train
|
17 |
# categoryA
|
18 |
# file1
|
19 |
# test
|
20 |
+
# ...
|
21 |
|
|
|
22 |
|
23 |
class PdfFolder(datasets.GeneratorBasedBuilder):
|
24 |
def _info(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
return datasets.DatasetInfo(
|
26 |
description=_DESCRIPTION,
|
27 |
features=datasets.Features(
|
|
|
44 |
name=datasets.Split.TRAIN,
|
45 |
gen_kwargs={
|
46 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
47 |
+
"supposed_labelset": "train",
|
48 |
},
|
49 |
),
|
50 |
datasets.SplitGenerator(
|
51 |
name=datasets.Split.TEST,
|
52 |
gen_kwargs={
|
53 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
54 |
+
"supposed_labelset": "test",
|
55 |
},
|
56 |
),
|
57 |
datasets.SplitGenerator(
|
58 |
name=datasets.Split.VALIDATION,
|
59 |
gen_kwargs={
|
60 |
"archive_iterator": dl_manager.iter_archive(archive_path),
|
61 |
+
"supposed_labelset": "val",
|
62 |
},
|
63 |
),
|
64 |
]
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
def _generate_examples(self, archive_iterator, supposed_labelset):
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
extensions = {"pdf", "PDF"}
|
69 |
for file_path, file_obj in archive_iterator:
|
70 |
|
71 |
+
if file_path.split(".")[-1] not in extensions: # metadata.jsonlines
|
72 |
continue
|
73 |
|
74 |
folder, labelset, label, filename = file_path.split("/")
|
75 |
if labelset != supposed_labelset:
|
76 |
continue
|
|
|
|
|
|
|
77 |
|
78 |
+
images = pdf2image.convert_from_bytes(file_obj.read())
|
79 |
|
80 |
+
yield file_path, {"file": images, "labels": label}
|
|