BR-TaxQA-R / rag-rfb.py
eduseiti's picture
Trying to fix the features definition
a149530
raw
history blame
5.86 kB
# 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
"""rag-rfb dataset."""
import datasets
import json
import numpy as np
import glob
import os
_CITATION = """
place holder
"""
_URL = "https://github.com/unicamp-dl/rag-rfb"
_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/rag-rfb/resolve/main/questions_QA_2024_v1.0.json",
"2024-sources": "https://huggingface.co/datasets/unicamp-dl/rag-rfb/resolve/main/2024-sources",
}
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(filepath):
all_files = np.sort(glob.glob(filepath + "/*.txt"))
for idx, which_file in enumerate(all_files):
with open(which_file, encoding="utf-8") as input_file:
file_data = input_file.read()
features = {"file": os.path.basename(which_file),
"text": file_data}
yield idx, features
class RAG_RFB(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = (
[
datasets.BuilderConfig(
name="2024-questions",
description="Questions from 2024 Questions & Answers document.",
version=datasets.Version("1.0.0"),
),
datasets.BuilderConfig(
name="2024-sources",
description="Legal documents referred by 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(filepath)