# USAGE: this script is used to create an image dataset that is NOT hosted on HuggingFace but points to the original files
#        to download and generate the dataset.
import os

import datasets
from datasets.tasks import ImageClassification


_DESCRIPTION = """\
Images collected using Wild Sage Nodes to detect wild fires.
"""

_HOMEPAGE = "https://sagecontinuum.org/"

_LICENSE = "MIT"

# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URLS = "https://web.lcrc.anl.gov/public/waggle/datasets/smoke-example.tar"

_NAMES = [
    "cloud",
    "other",
    "smoke"
]

_PROMPT = "What is shown in the image?"
_CHOICES = _NAMES

class smokedataset_QA(datasets.GeneratorBasedBuilder):

    def _info(self):

        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features= datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.ClassLabel(names=_NAMES),
                    "prompt": datasets.Value(dtype='string'),
                    "choices": datasets.Sequence(datasets.Value("string"))
                }
            ),
            # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
            # specify them. They'll be used if as_supervised=True in builder.as_dataset.
            supervised_keys=None,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE
            # Citation for the dataset
            # citation=_CITATION,
        )
        
    # This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
    # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
    def _split_generators(self, dl_manager):


        # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
        # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
        # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
        data_dir = dl_manager.download(_URLS)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "images": dl_manager.iter_archive(data_dir),
                    "split": "test",
                    "prompt": _PROMPT,
                    "choices": _CHOICES
                },      
            )
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, images, prompt, choices, split):
        for file_path, file_obj in images:
            label = file_path.split("/")[1]
            yield file_path,{
                        "image": {"path": file_path, "bytes": file_obj.read()},
                        "label": label,
                        "prompt": prompt,
                        "choices": choices
                    }