File size: 4,200 Bytes
be12e47
 
 
 
 
 
 
b42069c
c4293bf
be12e47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4293bf
 
be12e47
 
c4293bf
be12e47
 
 
 
 
 
 
2514ae8
be12e47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import zipfile
from collections.abc import Sequence
from dataclasses import dataclass
from typing import TYPE_CHECKING, Union

import datasets
from datasets.data_files import DataFilesDict
from datasets.packaged_modules.imagefolder import imagefolder

if TYPE_CHECKING:
    import numpy as np
    import PIL.Image

logger = datasets.utils.logging.get_logger(__name__)


@dataclass
class Comic(datasets.Image):
    """Comic feature that extends Image feature for CBZ files."""

    def encode_example(
        self, value: Sequence[Union[str, bytes, dict, "np.ndarray", "PIL.Image.Image"]]
    ) -> dict:
        """Encode example into a format for Arrow.

        Args:
            value (`Sequence` of `str`, `bytes`, `dict`, `np.ndarray`, or
            `PIL.Image.Image`):
                Sequence of data passed as input to Comic feature. Each element can be:
                - A path to a local image file
                - Raw bytes of an image file
                - A dictionary containing image data
                - A numpy array representing an image
                - A PIL Image object

        Returns:
            `dict` with "path" and "bytes" fields for each image in the sequence
        """
        return [super().encode_example(img) for img in value]

    def decode_example(
        self, value: dict, token_per_repo_id=None
    ) -> list["PIL.Image.Image"]:
        """Decode example CBZ file into image data.

        Args:
            value (`str` or `dict`):
                Either a string with absolute path to CBZ file, or a dictionary
                with keys:
                - `path`: String with absolute path to CBZ file.
                - `bytes`: The bytes of the CBZ file.
            token_per_repo_id (`dict`, *optional*):
                To access and decode files from private repositories on the Hub.

        Returns:
            `List[PIL.Image.Image]`: List of images from CBZ
        """
        if isinstance(value, str):
            zip_file = value
        else:
            zip_file = (
                io.BytesIO(value["bytes"]) if value.get("bytes") else value["path"]
            )

        with zipfile.ZipFile(zip_file, "r") as zip_ref:
            return [
                super().decode_example(img_file)
                for img_file in sorted(zip_ref.namelist())
            ]


class NhentaiConfig(imagefolder.ImageFolderConfig):
    """BuilderConfig for NhentaiFolder."""


class Nhentai(imagefolder.ImageFolder):
    BASE_COLUMN_NAME = "comic"
    BASE_FEATURE = Comic
    BUILDER_CONFIG_CLASS = NhentaiConfig
    BUILDER_CONFIGS = [
        NhentaiConfig(
            name="nhentai",
            description="Default configuration for nhentai dataset",
            data_dir="data"
        )
    ]
    DEFAULT_CONFIG_NAME = "nhentai"
    EXTENSIONS: list[str] = [".cbz"]
    VERSION = datasets.Version("1.0.0")

    def _info(self):
        """Returns the dataset metadata."""
        return datasets.DatasetInfo(
            description="Dataset of comic books in CBZ format",
            features=datasets.Features(
                {
                    "comic": Comic(),
                    "title": datasets.Value("string"),
                    "media_id": datasets.Value("int64"),
                    "num_favorites": datasets.Value("int64"),
                    "tag": datasets.Sequence(datasets.Value("string")),
                    "language": datasets.Sequence(datasets.Value("string")),
                    "artist": datasets.Sequence(datasets.Value("string")),
                    "category": datasets.Sequence(datasets.Value("string")),
                    "num_pages": datasets.Value("int64"),
                    "scanlator": datasets.Value("string"),
                    "group": datasets.Sequence(datasets.Value("string")),
                    "parody": datasets.Sequence(datasets.Value("string")),
                    "character": datasets.Sequence(datasets.Value("string")),
                    "epos": datasets.Value("int64"),
                    "file_name": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="https://nhentai.net/",
        )