Download utilitarianism.py from tasksource/utilitarianism: direct link, hf CLI and curl.
- Browser
- Download file 4.17 kB
-
https://huggingface.co/datasets/tasksource/utilitarianism/resolve/main/utilitarianism.py
- Command line
-
hf download hf://datasets/tasksource/utilitarianism/utilitarianism.py
-
curl -L -o utilitarianism.py https://huggingface.co/datasets/tasksource/utilitarianism/resolve/main/utilitarianism.py
4.17 kB
| # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| import json | |
| import csv | |
| import os | |
| import random | |
| import datasets | |
| # Find for instance the citation on arxiv or on the dataset repo/website | |
| _CITATION = r""" | |
| @article{hendrycks2020ethics, | |
| title={Aligning AI With Shared Human Values}, | |
| author={Dan Hendrycks and Collin Burns and Steven Basart and Andrew Critch and Jerry Li and Dawn Song and Jacob Steinhardt}, | |
| journal={arXiv preprint arXiv:2008.02275}, | |
| year={2020} | |
| } | |
| @inproceedings{sileo2021analysis, | |
| title={Analysis and Prediction of NLP Models Via Task Embeddings}, | |
| author={Damien Sileo and Marie-Francine Moens}, | |
| booktitle = "Proceedings of the 12th Language Resources and Evaluation Conference", | |
| year={2022}, | |
| } | |
| """ | |
| # You can copy an official description | |
| _DESCRIPTION = """""" | |
| _HOMEPAGE = "" | |
| _LICENSE = "Creative Commons Attribution-NonCommercial 4.0 International Public License" | |
| # The HuggingFace dataset library don't host the datasets but only point to the original files | |
| # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method) | |
| _URLs = {"default": "https://www.dropbox.com/s/041prrjylv0tf0h/ethics.zip?dl=1"} | |
| class Imppres(datasets.GeneratorBasedBuilder): | |
| VERSION = datasets.Version("1.1.0") | |
| def _info(self): | |
| features = datasets.Features( | |
| { | |
| "better_choice": datasets.Value("string"), | |
| "worst_choice": datasets.Value("string"), | |
| "comparison": datasets.Value("string"), | |
| "label": datasets.Value("int32"), | |
| }) | |
| 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=features, # Here we define them above because they are different between the two configurations | |
| # If there's a common (input, target) tuple from the features, | |
| # specify them here. 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, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| """Returns SplitGenerators.""" | |
| my_urls = _URLs["default"] | |
| base_config = "utilitarianism" | |
| data_dir = os.path.join(dl_manager.download_and_extract(my_urls), "ethics", base_config) | |
| return [ | |
| datasets.SplitGenerator( | |
| name=split, | |
| # These kwargs will be passed to _generate_examples | |
| gen_kwargs={ | |
| "filepath": os.path.join(data_dir, f"util_{split}.csv"), | |
| "split": split, | |
| }, | |
| ) for split in ['train','test'] | |
| ] | |
| def _generate_examples(self, filepath, split): | |
| """Yields examples.""" | |
| with open(filepath, encoding="utf-8") as f: | |
| reader = csv.reader(f) | |
| for id_, line in enumerate(reader): | |
| random.seed(id_) | |
| label=random.randint(0,1) | |
| yield id_, { | |
| "label":label, | |
| "better_choice": line[0], | |
| "worst_choice": line[1], | |
| "comparison":f'"{line[1-label]}" is better than "{line[label]}"' | |
| } | |