import os import json import datasets from PIL import Image _DESCRIPTION = """ The Arxiv Figure Table Database (AFTdb) facilitates the linking of documentary objects, such as figures and tables, with their captions. This enables a comprehensive description of document-oriented images (excluding images from cameras). For the table component, the character structure is preserved in addition to the image of the table and its caption. This database is ideal for multimodal processing of documentary images. """ _LICENSE = "apache-2.0" _CITATION = """ @online{DeAFTdb, AUTHOR = {Cyrile Delestre}, URL = {https://huggingface.co/datasets/cmarkea/aftdb}, YEAR = {2024}, KEYWORDS = {NLP ; Multimodal} } """ _NB_TAR_FIGURE = [158, 4] # train, test _NB_TAR_TABLE = [17, 1] # train, test def extract_files_tar(all_path, data_dir, nb_files): paths_train = [ os.path.join(data_dir, f"train-{ii:03d}.tar") for ii in range(nb_files[0]) ] paths_test = [ os.path.join(data_dir, f"test-{ii:03d}.tar") for ii in range(nb_files[1]) ] all_path['train'] += paths_train all_path['test'] += paths_test class AFTConfig(datasets.BuilderConfig): """Builder Config for AFTdb""" def __init__(self, nb_files_figure, nb_files_table, **kwargs): super().__init__(version=datasets.__version__, **kwargs) self.nb_files_figure = nb_files_figure self.nb_files_table = nb_files_table class AFT_Dataset(datasets.GeneratorBasedBuilder): """Arxiv Figure Table database (AFTdb)""" BUILDER_CONFIGS = [ AFTConfig( name="figure", description=( "Dataset containing scientific article figures associated " "with their caption, summary, and article title." ), data_dir="./data/arxiv_dataset/{type}", # A modiféer sur Huggingface Hub nb_files_figure=_NB_TAR_FIGURE, nb_files_table=None ), AFTConfig( name="table", description=( "Dataset containing tables in JPG image format from " "scientific articles, along with the corresponding textual " "representation of the table, including its caption, summary, " "and article title." ), data_dir="./data/arxiv_dataset/{type}", # A modiféer sur Huggingface Hub nb_files_figure=None, nb_files_table=_NB_TAR_TABLE ), AFTConfig( name="figure+table", description=( "Dataset containing figure and tables in JPG image format " "from scientific articles, along with the corresponding " "textual representation of the table, including its caption, " "summary, and article title." ), data_dir="./data/arxiv_dataset/{type}", # A modiféer sur Huggingface Hub nb_files_figure=_NB_TAR_FIGURE, nb_files_table=_NB_TAR_TABLE ) ] DEFAULT_CONFIG_NAME = "figure+table" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { 'id': datasets.Value('string'), 'paper_id': datasets.Value('string'), 'type': datasets.Value('string'), 'authors': datasets.Value('string'), 'categories': datasets.Value('string'), 'title': { 'english': datasets.Value('string'), 'french': datasets.Value('string') }, 'summary': { 'english': datasets.Value('string'), 'french': datasets.Value('string') }, 'caption': { 'english': datasets.Value('string'), 'french': datasets.Value('string') }, 'image': datasets.Image(), 'data': datasets.Value('string'), 'newcommands': datasets.Sequence(datasets.Value('string')) } ), citation=_CITATION, license=_LICENSE ) def _split_generators(self, dl_manager: datasets.DownloadManager): all_path = dict(train=[], test=[]) if self.config.nb_files_figure: extract_files_tar( all_path=all_path, data_dir=self.config.data_dir.format(type='figure'), nb_files=self.config.nb_files_figure ) if self.config.nb_files_table: extract_files_tar( all_path=all_path, data_dir=self.config.data_dir.format(type='table'), nb_files=self.config.nb_files_table ) if dl_manager.is_streaming: downloaded_files = dl_manager.download(all_path) downloaded_files['train'] = [ dl_manager.iter_archive(ii) for ii in downloaded_files['train'] ] downloaded_files['test'] = [ dl_manager.iter_archive(ii) for ii in downloaded_files['test'] ] else: downloaded_files = dl_manager.download_and_extract(all_path) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ 'filepaths': downloaded_files['train'], 'is_streaming': dl_manager.is_streaming } ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "filepaths": downloaded_files['test'], 'is_streaming': dl_manager.is_streaming } ) ] def _generate_examples(self, filepaths, is_streaming): if is_streaming: _json, _jpg, _id_json, _id_img = False, False, '', '' for iter_tar in filepaths: for path, file_obj in iter_tar: if path.endswith('.json'): metadata = json.load(file_obj) _id_json = path.split('.')[0] _json = True if path.endswith('.jpg'): img = Image.open(file_obj) _id_img = path.split('.')[0] _jpg = True if _json and _jpg: assert _id_json == _id_img _json, _jpg = False, False yield metadata['id'], { 'id': metadata['id'], 'paper_id': metadata['paper_id'], 'type': metadata['type'], 'authors': metadata['authors'], 'categories': metadata['categories'], 'title': metadata['title'], 'summary': metadata['summary'], 'caption': metadata['caption'], 'image': img, 'data': metadata['data'], 'newcommands': metadata['newcommands'] } else: for path in filepaths: all_file = os.listdir(path) all_id_obs = sorted( set(map(lambda x: x.split('.')[0], all_file)) ) for id_obs in all_id_obs: path_metadata = os.path.join( path, f"{id_obs}.metadata.json" ) path_image = os.path.join(path, f"{id_obs}.image.jpg") metadata = json.load(open(path_metadata, 'r')) img = Image.open(path_image) yield id_obs, { 'id': metadata['id'], 'paper_id': metadata['paper_id'], 'type': metadata['type'], 'authors': metadata['authors'], 'categories': metadata['categories'], 'title': metadata['title'], 'summary': metadata['summary'], 'caption': metadata['caption'], 'image': img, 'data': metadata['data'], 'newcommands': metadata['newcommands'] }