File size: 16,453 Bytes
c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 c1c3729 87876a4 |
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 |
import abc
from typing import Optional
import datasets
import io
import os
import json
import hashlib
import warnings
import csv
import textwrap
# Tested with pandas=2.2.2
import pandas as pd
# Tested with pillow==10.4.0
import PIL
import PIL.Image
import PIL.PngImagePlugin
# Tested with PyMuPDF==1.24.7 PyMuPDFb==1.24.6
import pymupdf
# Tested with reportlab==4.2.2
import reportlab
import reportlab.lib.colors
import reportlab.lib.pagesizes
import reportlab.platypus
import reportlab.pdfgen
# Once properly assembled, the datasets should have these columns
ASSEMBLED_COLUMNS = (
'sample_id',
'dataset_name',
'task_name',
'query',
'annotations',
'image',
'query_info',
'annotations_info',
'image_info',
'image_sha256'
)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Now scroll down to the very bottom for `get_bigdocs_75m`! #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def pdf_to_image(pdf_stream, crop: bool=False) -> PIL.PngImagePlugin.PngImageFile:
doc = pymupdf.open(stream=pdf_stream)
# Render each page to an image in a list of images
images = [
PIL.Image.open(io.BytesIO(page.get_pixmap(dpi=144).tobytes("png")))
for page in doc
]
# Crop the footer if crop is True
if crop:
for i in range(len(images)):
images[i] = images[i].crop(
(120, 120, images[i].width - 120, images[i].height - 140)
)
# Determine the total width and height of the combined image
total_width = max(im.width for im in images)
total_height = sum(im.height for im in images)
# Create a new image with the combined size
combined_image = PIL.Image.new("RGB", (total_width, total_height), "white")
# Paste each page image into the combined image
y_offset = 0
for im in images:
combined_image.paste(im, (0, y_offset))
y_offset += im.height
# At this point combined_image is what we want, but under the wrong Python type
# Place into buffer using PNG image format
buffer = io.BytesIO()
combined_image.save(buffer, "png")
# Reload as PIL.PngImagePlugin.PngImageFile
return PIL.Image.open(buffer)
WHITE = reportlab.lib.colors.white
BLACK = reportlab.lib.colors.black
TABLE_STYLE = reportlab.platypus.TableStyle([
("BACKGROUND", (0, 0), (-1, 0), WHITE),
("TEXTCOLOR", (0, 0), (-1, 0), BLACK),
("ALIGN", (0, 0), (-1, -1), "CENTER"),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("BOTTOMPADDING", (0, 0), (-1, 0), 12),
("BACKGROUND", (0, 1), (-1, -1), WHITE),
("GRID", (0, 0), (-1, -1), 1, BLACK),
])
def csv_to_image(csv_path: str) -> PIL.PngImagePlugin.PngImageFile:
# Load the data
with open(csv_path, newline="") as csv_stream:
reader = csv.reader(csv_stream, delimiter="#")
data = [row for row in reader]
# Create a table with CSV data
table = reportlab.platypus.Table(data)
table.setStyle(TABLE_STYLE)
# Virtual PDF file
pdf_stream = io.BytesIO()
# Build a document from the table
reportlab.platypus.SimpleDocTemplate(pdf_stream, pagesize=reportlab.lib.pagesizes.letter).build([table])
return pdf_to_image(pdf_stream)
def text_to_image(text: str, font_size: int = 10) -> PIL.PngImagePlugin.PngImageFile:
pdf_stream = io.BytesIO()
c = reportlab.pdfgen.canvas.Canvas(
pdf_stream,
pagesize=reportlab.lib.pagesizes.letter
)
c.setFont("Helvetica", font_size)
# Wrap the text for better handling in PDF
wrapped_text = textwrap.wrap(text, width=100)
# Starting position on the page
x_position = 72 # 1 inch from the left margin
y_position = 11 * 72 - 72 # Start 1 inch from the top of an 11-inch page
text_height = font_size * 1.2 # Approximate line height
for line in wrapped_text:
if y_position < 72: # Check if we're near the bottom of the page
c.showPage()
c.setFont("Helvetica", font_size)
y_position = 11 * 72 - 72 # Reset position to top of new page
c.drawString(x_position, y_position, line)
y_position -= text_height # Move down for next line
c.save()
return pdf_to_image(pdf_stream, crop=True)
def csv_to_markdown(csv_path: str, sep: str) -> str:
# Format with pandas, but ensure there are no consecutive spaces
df = pd.read_csv(csv_path, sep=sep)
return " ".join(df.to_markdown(index=False).split())
def get_sha256(
image: PIL.PngImagePlugin.PngImageFile,
b: Optional[bytes]=None
) -> str:
# Ignore image if bytes representation b is already provided
if b is None:
buffer = io.BytesIO()
image.save(buffer, "png")
b = buffer.getvalue()
m = hashlib.sha256()
m.update(b)
return m.hexdigest()
class Assembler(abc.ABC):
def __init__(
self,
user_local_path: Optional[str],
raise_on_missing: Optional[bool],
use_bad_sha256: Optional[bool]
):
self.user_local_path = user_local_path
self.raise_on_missing = raise_on_missing
self.use_bad_sha256 = use_bad_sha256
@abc.abstractmethod
def __call__(self, sample) -> dict:
"""Processor called by `map` on each sample"""
def keep_image(
self,
image: Optional[PIL.PngImagePlugin.PngImageFile],
expected_sha256: str,
b: Optional[bytes]=None
) -> bool:
if image is None:
# No image to check
return False
if self.use_bad_sha256:
# We're going to use the image whether or not the sha256 is good
return True
sha256 = get_sha256(image, b)
if sha256 != expected_sha256:
# Give warning if didn't explicitly set use_bad_sha256 to False
if self.use_bad_sha256 is None:
warnings.warn(f"Skipping due to bad sha256", RuntimeWarning)
# Sample will be filtered out
return False
else:
# Sample will be used
return True
class AssembleFromDisk(Assembler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
assert self.user_local_path is not None, f"user_local_path is mandatory for this dataset"
def __call__(self, sample) -> dict:
img_path = os.path.join(self.user_local_path, sample['img_id'])
# Load the image
try:
image = PIL.Image.open(img_path)
except Exception as e:
if self.raise_on_missing:
raise RuntimeError(f"Error loading image at {img_path}\n{e}")
if self.raise_on_missing is None:
warnings.warn(f"Skipping due to error loading image {img_path}\n{e}", RuntimeWarning)
image = None # Sample will be filtered out
if image is not None:
# Place into `buffer` using PNG image format
buffer = io.BytesIO()
image.save(buffer, "png")
# Reload the image with guaranteed PNG format
image = PIL.Image.open(buffer)
# Check sha256
if not self.keep_image(image, sample["image_sha256"], b=buffer.getvalue()):
image = None
return {"image": image}
class AssembleTabFact(Assembler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Get annotations
json_path = os.path.join(self.user_local_path, "tokenized_data/total_examples.json")
with open(json_path, "rt") as fp:
self.tables_data = json.load(fp)
def __call__(self, sample) -> dict:
csv_path = os.path.join(self.user_local_path, "data/all_csv", sample["img_id"])
image = csv_to_image(csv_path)
# Check sha256
if not self.keep_image(image, sample["image_sha256"]):
# Skip both image and annotations (will be filtered out)
return {"image": None, "annotations": [""]}
# Annotations
if sample["task_name"] == "table_parsing2md":
annotations = [csv_to_markdown(csv_path, sep="#")]
else:
facts, entails, title = self.tables_data[sample["img_id"]]
if sample["task_name"] == "caption":
# The "caption" is the table's title
annotations = [title]
else:
assert sample["task_name"] == "summary"
# select only entries in facts with entailment label as 1
facts = [fact for fact, entailment in zip(facts, entails) if entailment == 1]
# concat facts with numbered bullets and new lines
annotations = ["\n".join([f"{i+1}. {fact}" for i, fact in enumerate(facts)])]
return {"image": image, "annotations": annotations}
class AssembleOpen4Business(Assembler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Get annotations
self.texts, self.summaries = {}, {}
for split in ["train", "val", "test"]:
with open(os.path.join(self.user_local_path, f"{split}.source"), "rt") as fp:
# Don't strip here because that may alter the image sha256
self.texts[split] = list(line for line in fp)
with open(os.path.join(self.user_local_path, f"{split}.target"), "rt") as fp:
self.summaries[split] = list(line.strip() for line in fp)
def __call__(self, sample) -> dict:
split, line_number = sample["img_id"].split("_")
line_number = int(line_number)
print(split, line_number, sample["task_name"])
try:
text = self.texts[split][line_number]
image = text_to_image(text)
# Check sha256
if not self.keep_image(image, sample["image_sha256"]):
# Skip both image and annotations (will be filtered out)
return {"image": None, "annotations": [""]}
# Annotations
if sample["task_name"] == "extraction":
# Don't forget the strip!
annotations = [text.strip()]
else:
assert sample["task_name"] == "summary"
annotations = [self.summaries[split][line_number]]
except Exception as e:
print(f"EXCEPTION on {split}, {line_number}, {sample['task_name']}. Original error: {e.__str__}")
raise e
return {"image": image, "annotations": annotations}
class AssembleWikiTQ(Assembler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Get annotations
self.annotations = pd.concat([
pd.read_csv(
os.path.join(self.user_local_path, "data", tsv_file),
sep="\t", on_bad_lines="skip", index_col="id"
) for tsv_file in {
"training.tsv",
"pristine-seen-tables.tsv",
"pristine-unseen-tables.tsv"
}
])
def __call__(self, sample) -> dict:
question, context, answer = self.annotations.loc[sample["img_id"]]
csv_path = os.path.join(self.user_local_path, context)
image = csv_to_image(csv_path)
# Check sha256
if not self.keep_image(image, sample["image_sha256"]):
# Skip both image and annotations (will be filtered out)
return {"image": None, "annotations": [""]}
# Annotations
if sample["task_name"] == "table_parsing2md":
annotations = [csv_to_markdown(csv_path, sep=",")]
return {"image": image, "annotations": annotations}
else:
assert sample["task_name"] == "qa"
query = [sample["query"][0].format(question=question)]
answers = str(answer).split("|")
answer = " or ".join(answers) if len(answers) > 1 else answers[0]
annotations = [sample["annotations"][0].format(answer=answer)]
return {"image": image, "query": query, "annotations": annotations}
KNOWN_ASSEMBLERS = {
"ArxivOCR": None,
"ArxivTableCap": None,
"COCOtext": AssembleFromDisk,
"pubtables-1m": AssembleFromDisk,
"TextOCR": AssembleFromDisk,
"TabFact": AssembleTabFact,
"Open4Business": AssembleOpen4Business,
"WikiTQ": AssembleWikiTQ,
}
# # # # # # # # # # # # # # # # # # # # # # #
# This is the function you are looking for! #
# # # # # # # # # # # # # # # # # # # # # # #
def get_bigdocs_75m(
formal_name: str,
user_local_path: Optional[str]=None,
*,
load_from_cache_file: Optional[bool]=None,
num_proc: Optional[int]=4,
writer_batch_size: int=100,
raise_on_missing: Optional[bool]=None,
use_bad_sha256: Optional[bool]=None,
bigdocs_load_dataset_kwargs: Optional[dict]=None,
unprocessed: Optional[datasets.DatasetDict]=None
) -> datasets.DatasetDict:
"""
Get a subset of BigDocs-7.5M
Some parts of BigDocs-7.5M are distributed without their "image" column,
and instead have an "img_id" column. The present function substitutes
such images back in.
For the following `formal_name`, the the user is responsible to download
the specified dataset and specify its location through `user_local_path`.
- COCOtext: http://images.cocodataset.org/zips/train2014.zip
- pubtables-1m: https://www.microsoft.com/en-us/research/publication/pubtables-1m
- TextOCR: https://dl.fbaipublicfiles.com/textvqa/images/train_val_images.zip
- TabFact: https://github.com/wenhuchen/Table-Fact-Checking
- Open4Business: https://github.com/amanpreet692/Open4Business
- WikiTQ: https://github.com/ppasupat/WikiTableQuestions
Args:
formal_name (`str`): The desired subset of BigDocs-7.5M .
user_local_path (`Optional[str]`): The local path containing the images to be linked.
load_from_cache_file (`Optional[bool]): Passed to `map`, `filter` and the likes.
num_proc (`Optional[int]): Passed to `map`, `filter` and the likes.
writer_batch_size (`int`, defaults to 100): Passed to `map`. Too large values may cause OOM.
raise_on_missing (`Optional[bool]`):
Determines what to do when there is an error loading an image.
- `True`: raise an error.
- `None`: print a warning and skip the sample (default).
- `False`: silently skip the sample.
use_bad_sha256 (`Optional[bool]):
Determines what to do when the sha256 integrity test fails.
- `True`: ignore the sha256 integrity test.
- `None`: print a warning and skip samples with bad sha256 (default).
- `False`: silently skip entries with bad sha256.
bigdocs_load_dataset_kwargs (`Optional[dict]`): Arguments passed to datasets.load_dataset when retrieving ServiceNow/BigDocs-7.5M .
unprocessed (Optional[datasets.DatasetDict]): If provided, will be used in stead of ServiceNow/BigDocs-7.5M .
"""
# Get the unprocessed ServiceNow/BigDocs-7.5M
if unprocessed is None:
if bigdocs_load_dataset_kwargs is None:
bigdocs_load_dataset_kwargs = {}
unprocessed = datasets.load_dataset(
"ServiceNow/BigDocs-7.5M",
formal_name,
**bigdocs_load_dataset_kwargs
)
# Get the correct processor
try:
assembler = KNOWN_ASSEMBLERS[formal_name]
except KeyError:
raise ValueError(f"Unknown formal_name: {formal_name}")
# Do the processing
if assembler is None:
assert user_local_path is None
processed = unprocessed
else:
processor = assembler(user_local_path, raise_on_missing, use_bad_sha256)
processed = unprocessed.map(
processor,
remove_columns="img_id",
load_from_cache_file=load_from_cache_file,
num_proc=num_proc,
writer_batch_size=writer_batch_size
)
# Drop missing images (we can skip if we raised on missing images).
if not raise_on_missing:
processed = processed.filter((lambda image: image is not None), input_columns="image", num_proc=num_proc)
# Column order
processed = processed.select_columns(list(ASSEMBLED_COLUMNS))
return processed
|