# coding=utf-8 # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors. # # 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. # Lint as: python3 """BR-TaxQA-R dataset.""" import datasets import json import numpy as np import glob import os _CITATION = """ place holder """ _URL = "https://github.com/unicamp-dl/rag-receita" _DESCRIPTION = """ Retrieval Augmented Generation (RAG) dataset for Brazilian Federal Revenue Service (Receita Federal do Brasil ― RFB). """ _URLS = { "2024.questions": "https://huggingface.co/datasets/unicamp-dl/BR-TaxQA-R/resolve/main/questions_QA_2024_v1.1.json", "2024.sources": "https://huggingface.co/datasets/unicamp-dl/BR-TaxQA-R/resolve/main/referred_legal_documents_QA_2024_v1.1.json", "2024.caselaw": "https://huggingface.co/datasets/unicamp-dl/BR-TaxQA-R/resolve/main/acordaos_CARF_2023.json" } def generate_examples_questions(filepath): with open(filepath, encoding="utf-8") as input_file: questions = json.load(input_file) for (idx, question) in enumerate(questions): # Convert the "all_formatted_references" dictionary to a list to avoid multiple nulled rows all_formatted_references = [] for reference in np.sort(list(question['all_formatted_references'].keys())): all_formatted_references += question['all_formatted_references'][reference] question['all_formatted_references'] = all_formatted_references yield idx, question def generate_examples_sources_and_caselaw(filepath): with open(filepath, encoding="utf-8") as input_file: references = json.load(input_file) for idx, reference in enumerate(references): features = {"file": reference['filename'], "text": reference['filedata']} yield idx, features class BR_TAXQA_R(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = ( datasets.BuilderConfig( name="2024.questions", description="Questions from 2024 Questions & Answers document.", version=datasets.Version("1.1.0"), ), datasets.BuilderConfig( name="2024.sources", description="Legal documents referred by the 2024 Questions & Answers document.", version=datasets.Version("1.1.0"), ), datasets.BuilderConfig( name="2024.caselaw", description="Case Law documents from 2023, directly related to the 2024 Questions & Answers document.", version=datasets.Version("1.0.0"), ) ) DEFAULT_CONFIG_NAME = "2024.questions" def _info(self): name = self.config.name if "questions" in name: features = { "question_number": datasets.Value("string"), "question_summary": datasets.Value("string"), "question_text": datasets.Value("string"), "answer": datasets.Sequence(datasets.Value("string"), length=-1), "answer_cleaned": datasets.Sequence(datasets.Value("string"), length=-1), "references": datasets.Sequence(datasets.Value("string"), length=-1), "linked_questions": datasets.Sequence(datasets.Value("string"), length=-1), "formatted_references": datasets.Sequence({"título": datasets.Value("string"), "artigos": datasets.Sequence(datasets.Value("string"), length=-1), "anexos": datasets.Sequence(datasets.Value("string"), length=-1), "file": datasets.Value("string")}), "embedded_references": datasets.Sequence(datasets.Value("string"), length=-1), "formatted_embedded_references": datasets.Sequence({"título": datasets.Value("string"), "artigos": datasets.Sequence(datasets.Value("string"), length=-1), "anexos": datasets.Sequence(datasets.Value("string"), length=-1), "file": datasets.Value("string")}), "all_formatted_references": datasets.Sequence({"título": datasets.Value("string"), "artigos": datasets.Sequence(datasets.Value("string"), length=-1), "anexos": datasets.Sequence(datasets.Value("string"), length=-1), "file": datasets.Value("string")}) } else: features = { "file": datasets.Value("string"), "text": datasets.Value("string"), } return datasets.DatasetInfo( description=f"{_DESCRIPTION}\n{self.config.description}", features=datasets.Features(features), supervised_keys=None, homepage=_URL, citation=_CITATION, ) def _split_generators(self, dl_manager): url = _URLS[self.config.name] dl_path = dl_manager.download_and_extract(url) return (datasets.SplitGenerator(name=self.config.name, gen_kwargs={"filepath": dl_path}),) def _generate_examples(self, filepath, args=None): """Yields examples.""" if "questions" in self.config.name: return generate_examples_questions(filepath) else: return generate_examples_sources_and_caselaw(filepath)