File size: 2,240 Bytes
27efaa8
9a466a5
61e15f9
9a466a5
61e15f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27efaa8
61e15f9
27efaa8
61e15f9
 
 
 
76ab457
27efaa8
61e15f9
 
 
 
27efaa8
61e15f9
 
9e79811
27efaa8
369c0cf
61e15f9
369c0cf
61e15f9
 
 
 
 
bbd3710
61e15f9
bbad79d
61e15f9
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
import os
import io
import datasets
from PIL import ImageOps, Image

_CITATION = """\
@InProceedings{huggingface:dataset,
title = {Processed KHATT paragrpah dataset},
author={Ahmed Alnaggar},
year={2024}
}
"""

_DESCRIPTION = """\
A curated version of KHATT paragraph dataset containing 3996 images and their crossponding Arabic paragraphs
"""
_HOMEPAGE = "https://huggingface.co/datasets/a-alnaggar/khatt-paragraphs"

_LICENSE = ""

_REPO = "https://huggingface.co/datasets/a-alnaggar/khatt-paragraphs"

class KhattPara(datasets.GeneratorBasedBuilder):

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    'text': datasets.Value("string"),
                    'image': datasets.Image(),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        images_archive = dl_manager.download(f"{_REPO}/resolve/main/khatt-paragraphs-images.tar.gz")
        image_iters = dl_manager.iter_archive(images_archive)
        text_archive = dl_manager.download_and_extract(f"{_REPO}/resolve/main/khatt-paragraphs-text.tar.gz")
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "images": image_iters,
                    "text_archive_path": text_archive
                }
            ),
        ]
        
    def _generate_examples(self, images, text_archive_path):
        """Returns inverted image and Arabic text."""
        for idx, (filepath, image) in enumerate(images):
            text_path = os.path.join("proc_text", str(os.path.basename(filepath)[:-4] + ".txt"))
            text = self.read_arabic_text_file(os.path.join(text_archive_path,text_path))

            yield idx, {
                "image": {"path": filepath, "bytes": image.read()},
                "text": text,
            }

    @staticmethod
    def read_arabic_text_file(file_path):
        with open(file_path, 'r', encoding='windows-1256') as file:
            lines = file.readlines()
        return ''.join(lines)