Upload dummy.py with huggingface_hub
Browse files
dummy.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import datasets
|
4 |
+
from glob import glob
|
5 |
+
import zipfile
|
6 |
+
|
7 |
+
class dummy(datasets.GeneratorBasedBuilder):
|
8 |
+
def _info(self):
|
9 |
+
return datasets.DatasetInfo(features=datasets.Features({'image':datasets.Image(),'text':datasets.Value('string')}))
|
10 |
+
|
11 |
+
def extract_all(self, dir):
|
12 |
+
zip_files = glob(dir+'/**/**.zip', recursive=True)
|
13 |
+
for file in zip_files:
|
14 |
+
with zipfile.ZipFile(file) as item:
|
15 |
+
item.extractall('/'.join(file.split('/')[:-1]))
|
16 |
+
|
17 |
+
|
18 |
+
def get_all_files(self, dir):
|
19 |
+
files = []
|
20 |
+
valid_file_ext = ['txt', 'csv', 'tsv', 'xlsx', 'xls', 'xml', 'json', 'jsonl', 'html', 'wav', 'mp3', 'jpg', 'png']
|
21 |
+
for ext in valid_file_ext:
|
22 |
+
files += glob(f"{dir}/**/**.{ext}", recursive = True)
|
23 |
+
return files
|
24 |
+
|
25 |
+
def _split_generators(self, dl_manager):
|
26 |
+
url = [os.path.abspath(os.path.expanduser(dl_manager.manual_dir))]
|
27 |
+
downloaded_files = dl_manager.download_and_extract(url)
|
28 |
+
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')),} })]
|
29 |
+
|
30 |
+
|
31 |
+
def read_image(self, filepath):
|
32 |
+
if filepath.endswith('.jpg') or filepath.endswith('.png'):
|
33 |
+
raw_data = {'bytes':[filepath]}
|
34 |
+
else:
|
35 |
+
raw_data = {'text':[open(filepath).read()]}
|
36 |
+
return pd.DataFrame(raw_data)
|
37 |
+
|
38 |
+
def _generate_examples(self, filepaths):
|
39 |
+
_id = 0
|
40 |
+
for i,filepath in enumerate(filepaths['inputs']):
|
41 |
+
df = self.read_image(filepath)
|
42 |
+
dfs = [df]
|
43 |
+
dfs.append(self.read_image(filepaths['targets1'][i]))
|
44 |
+
df = pd.concat(dfs, axis = 1)
|
45 |
+
if len(df.columns) != 2:
|
46 |
+
continue
|
47 |
+
df.columns = ['image', 'text']
|
48 |
+
for _, record in df.iterrows():
|
49 |
+
yield str(_id), {'image':record['image'],'text':record['text']}
|
50 |
+
_id += 1
|
51 |
+
|