forked from EuroEval/EuroEval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_nqii.py
More file actions
108 lines (86 loc) · 3.5 KB
/
create_nqii.py
File metadata and controls
108 lines (86 loc) · 3.5 KB
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
# /// script
# requires-python = ">=3.10,<4.0"
# dependencies = [
# "datasets==3.5.0",
# "huggingface-hub==0.24.0",
# "pandas==2.2.0",
# "requests==2.32.3",
# ]
# ///
"""Create the NQiI-mini dataset and upload them to the HF Hub."""
import pandas as pd
from datasets.arrow_dataset import Dataset
from datasets.combine import concatenate_datasets
from datasets.dataset_dict import DatasetDict
from datasets.load import load_dataset
from datasets.splits import Split
from huggingface_hub.hf_api import HfApi
from .constants import (
MAX_NUM_CHARS_IN_CONTEXT,
MAX_NUM_CHARS_IN_QUESTION,
MIN_NUM_CHARS_IN_CONTEXT,
MIN_NUM_CHARS_IN_QUESTION,
)
def main() -> None:
"""Create the NQiI-mini dataset and upload them to the HF Hub."""
dataset_id = "vesteinn/icelandic-qa-NQiI"
# Load the datasets from the `alexandrainst` organisation
train = load_dataset(dataset_id, split="train", token=True)
val = load_dataset(dataset_id, split="validation", token=True)
test = load_dataset(dataset_id, split="test", token=True)
# Ensure that the datasets are indeed datasets
assert isinstance(train, Dataset)
assert isinstance(val, Dataset)
assert isinstance(test, Dataset)
# Merge the splits
df = concatenate_datasets([train, val, test]).to_pandas()
# Ensure that `df` is indeed a Pandas DataFrame
assert isinstance(df, pd.DataFrame)
# Only work with samples where the context is not very large or small
lengths = df.context.str.len()
df = df[lengths.between(MIN_NUM_CHARS_IN_CONTEXT, MAX_NUM_CHARS_IN_CONTEXT)]
# Only work with samples where the question is not very large or small
lengths = df.question.str.len()
df = df[lengths.between(MIN_NUM_CHARS_IN_QUESTION, MAX_NUM_CHARS_IN_QUESTION)]
assert isinstance(df, pd.DataFrame)
# Extract information on which examples contain an answer
has_answer: pd.Series = df.answers.map(
lambda dct: len(dct["text"]) > 0 and dct["text"][0] != ""
)
# Only work with the questions having answers in the context
df_with_answer: pd.DataFrame = df.loc[has_answer]
# Create validation split
val_size = 256
val_df = df_with_answer.sample(n=val_size, random_state=4242)
# Create test split
test_size = 1024
df_with_answer_filtered: pd.DataFrame = df_with_answer.loc[
~df_with_answer.index.isin(val_df.index)
]
test_df = df_with_answer_filtered.sample(n=test_size, random_state=4242)
# Create train split
train_size = 1024
full_train_df_with_answer = df_with_answer_filtered.loc[
~df_with_answer_filtered.index.isin(test_df.index)
]
train_df = full_train_df_with_answer.sample(n=train_size, random_state=4242)
val_df = val_df.reset_index(drop=True)
test_df = test_df.reset_index(drop=True)
train_df = train_df.reset_index(drop=True)
assert isinstance(train_df, pd.DataFrame)
assert isinstance(val_df, pd.DataFrame)
assert isinstance(test_df, pd.DataFrame)
# Collect datasets in a dataset dictionary
dataset = DatasetDict(
{
"train": Dataset.from_pandas(train_df, split=Split.TRAIN),
"val": Dataset.from_pandas(val_df, split=Split.VALIDATION),
"test": Dataset.from_pandas(test_df, split=Split.TEST),
}
)
# Push the dataset to the Hugging Face Hub
dataset_id = "EuroEval/nqii-mini"
HfApi().delete_repo(dataset_id, repo_type="dataset", missing_ok=True)
dataset.push_to_hub(dataset_id, private=True)
if __name__ == "__main__":
main()