-
Notifications
You must be signed in to change notification settings - Fork 9
/
web_document_extractor.py
450 lines (379 loc) · 17 KB
/
web_document_extractor.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import glob
import json
import logging
import math
import os
import tarfile
from copy import deepcopy
import git
from datasets import Dataset, Image, Sequence, Value, concatenate_datasets, load_from_disk
from pathos.multiprocessing import ProcessingPool as Pool
from tqdm import tqdm
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def write_file(path_file, to_write):
f = open(path_file, "w")
f.truncate(0)
f.write(to_write)
f.close()
def html_to_web_documents(
dataset,
dom_tree_simplificator,
pre_extraction_simplificator,
num_proc,
html_column_name="html",
url_column_name="url",
):
def func_html_to_web_documents(example):
html_str = example[html_column_name]
page_url = example[url_column_name]
general_metadata = {}
if all(
[
column_name in example
for column_name in ["url", "warc_filename", "warc_record_offset", "warc_record_length"]
]
):
general_metadata = {
"url": example["url"],
"warc_filename": example["warc_filename"],
"warc_record_offset": example["warc_record_offset"],
"warc_record_length": example["warc_record_length"],
}
try:
selectolax_tree = dom_tree_simplificator(html_str, type_return="selectolax_tree")
list_nodes = pre_extraction_simplificator(selectolax_tree, page_url=page_url)
except Exception:
print("EXCEPTION")
example["texts"] = []
example["images"] = []
example["metadata"] = json.dumps([])
example["general_metadata"] = json.dumps([])
return example
texts = []
images = []
metadata = []
for node in list_nodes:
if node.tag == "-text":
texts.append(node.text)
images.append("")
metadata.append(None)
elif node.tag == "img":
texts.append(None)
images.append(node.media_info["src"])
metadata.append(node.media_info)
example["texts"] = texts
example["images"] = images
example["metadata"] = json.dumps(metadata)
example["general_metadata"] = json.dumps(general_metadata)
return example
logger.info("Starting extracting the documents")
dataset = dataset.map(func_html_to_web_documents, num_proc=num_proc, remove_columns=dataset.column_names)
logger.info("Finished extracting the documents")
return dataset
def get_image_urls(dataset, num_proc, path_save_file_image_urls):
def func_get_image_urls(example):
example["urls"] = [el for el in example["images"] if el]
return example
logger.info("Starting getting the urls of all images")
image_urls = dataset.map(func_get_image_urls, remove_columns=dataset.column_names, num_proc=num_proc)
image_urls = [sub_el for el in image_urls["urls"] for sub_el in el if sub_el]
image_urls = list(set(image_urls))
write_file(path_file=path_save_file_image_urls, to_write="\n".join(image_urls))
logger.info("Finished getting the urls of all images")
def download_images(
path_save_file_image_urls,
path_save_dir_downloaded_images,
number_sample_per_shard,
image_size,
resize_mode,
num_proc,
thread_count,
):
# Before calling this method, set up a DNS solver
# https://github.com/rom1504/img2dataset#setting-up-a-bind9-resolver
logger.info("Starting downloading the images")
os.system(
"img2dataset"
f" --url_list={path_save_file_image_urls} --output_folder={path_save_dir_downloaded_images}"
f" --processes_count={num_proc} --thread_count={thread_count}"
f" --number_sample_per_shard={number_sample_per_shard} --image_size={image_size}"
f" --resize_mode={resize_mode} --output_format=webdataset"
)
logger.info("Finished downloading the images")
def create_dataset_images_from_tar(
tar_paths,
path_save_dir_tmp_datasets_images,
num_proc,
path_save_file_map_url_idx,
path_save_dir_dataset_images,
):
def process_one_tar(args):
(tar_path, idx_tar) = args
with tarfile.open(tar_path) as tar_file:
tar_members = tar_file.getmembers()
name_to_url = {}
name_to_img = {}
url_to_img = {}
for tar_member in tar_members:
if tar_member.name.endswith(".jpg"):
name = tar_member.name.replace(".jpg", "")
tar_member_file = tar_file.extractfile(tar_member)
img = tar_member_file.read()
tar_member_file.close()
name_to_img[name] = img
elif tar_member.name.endswith(".json"):
name = tar_member.name.replace(".json", "")
tar_member_file = tar_file.extractfile(tar_member)
json_val = json.loads(tar_member_file.read())
status = json_val["status"]
url = json_val["url"]
tar_member_file.close()
if status == "success": # Should always happend with webdataset format, not with parquet
name_to_url[name] = url
for name in name_to_url:
url_to_img[name_to_url[name]] = name_to_img[name]
new_urls_indexed = list(url_to_img.keys())
new_datasets_images = Dataset.from_dict(
{"url": list(url_to_img.keys()), "image": list(url_to_img.values())}
)
# We need to save the new datasets and then reload them, since `from_dict` store the dataset
# in the RAM and does not use the disk space
new_datasets_images.save_to_disk(os.path.join(path_save_dir_tmp_datasets_images, str(idx_tar)))
return new_urls_indexed
logger.info("Starting creating the dataset of all images")
args_pool = [(tar_path, idx_tar) for idx_tar, tar_path in enumerate(tar_paths)]
pool = Pool(num_proc)
urls_indexed = pool.map(process_one_tar, args_pool)
urls_indexed = [sub_el for el in urls_indexed for sub_el in el]
map_url_idx = {url: idx for idx, url in enumerate(urls_indexed)}
with open(path_save_file_map_url_idx, "w") as f:
json.dump(map_url_idx, f)
datasets_images = [
load_from_disk(os.path.join(path_save_dir_tmp_datasets_images, str(idx_tar)))
for idx_tar in range(len(tar_paths))
]
dataset_images = concatenate_datasets(datasets_images)
dataset_images.save_to_disk(path_save_dir_dataset_images)
logger.info("Finished creating the dataset of all images")
return dataset_images
def create_dataset_images(
path_save_dir_downloaded_images,
path_save_dir_tmp_datasets_images,
num_proc,
path_save_file_map_url_idx,
path_save_dir_dataset_images,
):
tar_paths = glob.glob(os.path.join(path_save_dir_downloaded_images, "*.tar"))
dataset_images = create_dataset_images_from_tar(
tar_paths=tar_paths,
path_save_dir_tmp_datasets_images=path_save_dir_tmp_datasets_images,
num_proc=num_proc,
path_save_file_map_url_idx=path_save_file_map_url_idx,
path_save_dir_dataset_images=path_save_dir_dataset_images,
)
return dataset_images
def urls_to_images(dataset, dataset_images, map_url_idx, num_proc, some_urls_are_already_retrieved=False):
if some_urls_are_already_retrieved:
if "images_urls" not in dataset.features or "images" not in dataset.features:
raise ValueError(
"If some urls are already retrieved, the dataset must contain the features 'images_urls' and 'images'"
)
def retrieve_image(url):
if url not in map_url_idx:
return None
image = {"path": None, "bytes": dataset_images[map_url_idx[url]]["image"]}
return image
def func_urls_to_images_urls_in_images_col(example):
example["images_urls"] = deepcopy(example["images"])
num_urls = sum([(url is not None and url != "") for url in example["images_urls"]])
example["images"] = [retrieve_image(url) if url else None for url in example["images"]]
num_found = sum([img is not None for img in example["images"]])
num_not_found = num_urls - num_found
example["num_found"] = num_found
example["num_not_found"] = num_not_found
return example
def func_urls_to_images_urls_in_images_urls_col(example):
num_urls = sum([(url is not None and url != "") for url in example["images_urls"]])
example["images"] = [
img if img is not None else retrieve_image(url) if url else None
for img, url in zip(example["images"], example["images_urls"])
]
num_found = sum([img is not None for img in example["images"]])
num_not_found = num_urls - num_found
example["num_found"] = num_found
example["num_not_found"] = num_not_found
return example
func_urls_to_images = (
func_urls_to_images_urls_in_images_urls_col
if some_urls_are_already_retrieved
else func_urls_to_images_urls_in_images_col
)
logger.info("Starting replacing urls by images")
new_features = deepcopy(dataset.features)
new_features["images"] = Sequence(Image())
new_features["images_urls"] = Sequence(Value("string"))
new_features["num_found"] = Value("int32")
new_features["num_not_found"] = Value("int32")
dataset = dataset.map(
func_urls_to_images,
features=new_features,
num_proc=num_proc,
load_from_cache_file=False,
)
logger.info("Finished replacing urls by images")
return dataset
def save_split_sharded_already_splitted_dataset(dataset, path_save_dir_sharded_dataset, shard_size):
def save_split_ds(split_dataset, split_name):
num_shards = math.ceil(len(split_dataset) / shard_size)
for idx in tqdm(range(num_shards)):
shard = split_dataset.shard(num_shards=num_shards, index=idx, contiguous=True)
shard.save_to_disk(os.path.join(path_save_dir_sharded_dataset, split_name, f"shard_{idx}"))
os.makedirs(path_save_dir_sharded_dataset, exist_ok=True)
f = open(os.path.join(path_save_dir_sharded_dataset, "dataset_dict.json"), "w")
f.write('{"splits": ["train", "valid"]}')
f.close()
os.makedirs(os.path.join(path_save_dir_sharded_dataset, "train"), exist_ok=True)
os.makedirs(os.path.join(path_save_dir_sharded_dataset, "valid"), exist_ok=True)
logger.info("Starting sharding the dataset")
train_dataset = dataset["train"]
valid_dataset = dataset["valid"]
save_split_ds(train_dataset, "train")
save_split_ds(valid_dataset, "valid")
logger.info("Finished sharding the dataset")
def save_split_sharded_dataset(dataset, path_save_dir_sharded_dataset, shard_size):
os.makedirs(path_save_dir_sharded_dataset, exist_ok=True)
f = open(os.path.join(path_save_dir_sharded_dataset, "dataset_dict.json"), "w")
f.write('{"splits": ["train", "valid"]}')
f.close()
os.makedirs(os.path.join(path_save_dir_sharded_dataset, "train"), exist_ok=True)
os.makedirs(os.path.join(path_save_dir_sharded_dataset, "valid"), exist_ok=True)
logger.info("Starting sharding the dataset")
num_shards = math.ceil(len(dataset) / shard_size)
for idx in tqdm(range(num_shards)):
shard = dataset.shard(num_shards=num_shards, index=idx, contiguous=True)
if idx < 2:
shard.save_to_disk(os.path.join(path_save_dir_sharded_dataset, "valid", f"shard_{idx}"))
else:
shard.save_to_disk(os.path.join(path_save_dir_sharded_dataset, "train", f"shard_{idx}"))
logger.info("Finished sharding the dataset")
class CommonCrawlWebDocumentExtractor:
def __init__(
self,
html_dataset,
dom_tree_simplificator,
pre_extraction_simplificator,
path_save_dir_dataset,
num_proc,
path_save_file_image_urls,
path_save_dir_downloaded_images,
thread_count,
number_sample_per_shard,
image_size,
resize_mode,
path_save_dir_tmp_datasets_images,
path_save_dir_dataset_images,
path_save_file_map_url_idx,
num_proc_urls_to_images,
path_save_dir_sharded_dataset,
shard_size,
):
self.dataset = html_dataset
self.dom_tree_simplificator = dom_tree_simplificator
self.pre_extraction_simplificator = pre_extraction_simplificator
self.path_save_dir_dataset = path_save_dir_dataset
self.num_proc = num_proc
self.path_save_file_image_urls = path_save_file_image_urls
self.path_save_dir_downloaded_images = path_save_dir_downloaded_images
self.thread_count = thread_count
self.number_sample_per_shard = number_sample_per_shard
self.image_size = image_size
self.resize_mode = resize_mode
self.path_save_dir_tmp_datasets_images = path_save_dir_tmp_datasets_images
self.path_save_dir_dataset_images = path_save_dir_dataset_images
self.path_save_file_map_url_idx = path_save_file_map_url_idx
self.num_proc_urls_to_images = num_proc_urls_to_images
self.path_save_dir_sharded_dataset = path_save_dir_sharded_dataset
self.shard_size = shard_size
def html_to_web_documents(self):
self.dataset = html_to_web_documents(
dataset=self.dataset,
dom_tree_simplificator=self.dom_tree_simplificator,
pre_extraction_simplificator=self.pre_extraction_simplificator,
num_proc=self.num_proc,
)
def get_image_urls(self):
get_image_urls(
dataset=self.dataset, num_proc=self.num_proc, path_save_file_image_urls=self.path_save_file_image_urls
)
def download_images(self):
download_images(
path_save_file_image_urls=self.path_save_file_image_urls,
path_save_dir_downloaded_images=self.path_save_dir_downloaded_images,
number_sample_per_shard=self.number_sample_per_shard,
image_size=self.image_size,
resize_mode=self.resize_mode,
num_proc=self.num_proc,
thread_count=self.thread_count,
)
def create_dataset_images(self):
self.dataset_images = create_dataset_images(
path_save_dir_downloaded_images=self.path_save_dir_downloaded_images,
path_save_dir_tmp_datasets_images=self.path_save_dir_tmp_datasets_images,
num_proc=self.num_proc,
path_save_file_map_url_idx=self.path_save_file_map_url_idx,
path_save_dir_dataset_images=self.path_save_dir_dataset_images,
)
def urls_to_images(self, reload_files=False):
with open(self.path_save_file_map_url_idx) as f:
self.map_url_idx = json.load(f)
# Useful when this method is called independently without
# the previous ones, so we need to load some files
if reload_files:
logger.info("Starting reloading variables for the step urls_to_images")
self.dataset = load_from_disk(self.path_save_dir_dataset)
self.dataset_images = load_from_disk(self.path_save_dir_dataset_images)
logger.info("Finished reloading variables for the step urls_to_images")
else:
try:
_ = self.dataset
_ = self.dataset_images
_ = self.map_url_idx
except Exception:
print("Set `reload_files=True` if you're calling this method alone to define the missing variables")
self.dataset = urls_to_images(
dataset=self.dataset,
dataset_images=self.dataset_images,
map_url_idx=self.map_url_idx,
num_proc=self.num_proc_urls_to_images,
)
def save_dataset(self):
logger.info("Starting saving the dataset")
self.dataset.save_to_disk(self.path_save_dir_dataset, num_proc=self.num_proc)
logger.info("Finished saving the dataset")
def save_commit_hash(self):
logger.info("Starting writing the commit hash")
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
write_file(os.path.join(self.path_save_dir_dataset, "commit_hash.txt"), sha)
logger.info("Finished writing the commit hash")
def save_split_sharded_dataset(self, reload_files=False):
# Useful when this method is called independently without
# the previous ones, so we need to load some files
if reload_files:
self.dataset = load_from_disk(self.path_save_dir_dataset)
else:
try:
_ = self.dataset
except Exception:
print("Set `reload_files=True` if you're calling this method alone to define the missing variables")
save_split_sharded_dataset(
dataset=self.dataset,
path_save_dir_sharded_dataset=self.path_save_dir_sharded_dataset,
shard_size=self.shard_size,
)