import os import pandas as pd import datasets from glob import glob import zipfile class dummy(datasets.GeneratorBasedBuilder): def _info(self): return datasets.DatasetInfo(features=datasets.Features({'image':datasets.Image(),'text':datasets.Value('string')})) def extract_all(self, dir): zip_files = glob(dir+'/**/**.zip', recursive=True) for file in zip_files: with zipfile.ZipFile(file) as item: item.extractall('/'.join(file.split('/')[:-1])) def get_all_files(self, dir): files = [] valid_file_ext = ['txt', 'csv', 'tsv', 'xlsx', 'xls', 'xml', 'json', 'jsonl', 'html', 'wav', 'mp3', 'jpg', 'png'] for ext in valid_file_ext: files += glob(f"{dir}/**/**.{ext}", recursive = True) return files def _split_generators(self, dl_manager): url = [os.path.abspath(os.path.expanduser(dl_manager.manual_dir))] downloaded_files = dl_manager.download_and_extract(url) return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'filepaths':{'inputs':sorted(glob(downloaded_files[0]+'/data/**.png')),'targets1':sorted(glob(downloaded_files[0]+'/data/**.txt')),} })] def read_image(self, filepath): if filepath.endswith('.jpg') or filepath.endswith('.png'): raw_data = {'bytes':[filepath]} else: raw_data = {'text':[open(filepath).read()]} return pd.DataFrame(raw_data) def _generate_examples(self, filepaths): _id = 0 for i,filepath in enumerate(filepaths['inputs']): df = self.read_image(filepath) dfs = [df] dfs.append(self.read_image(filepaths['targets1'][i])) df = pd.concat(dfs, axis = 1) if len(df.columns) != 2: continue df.columns = ['image', 'text'] for _, record in df.iterrows(): yield str(_id), {'image':record['image'],'text':record['text']} _id += 1