forked from EuroEval/EuroEval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_cs_gec.py
More file actions
95 lines (76 loc) · 3.04 KB
/
create_cs_gec.py
File metadata and controls
95 lines (76 loc) · 3.04 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
# /// 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",
# "scikit-learn<1.6.0",
# ]
# ///
"""Create the CS GEC linguistic acceptability dataset."""
import logging
import pandas as pd
from datasets import Dataset, DatasetDict, Split, load_dataset
from huggingface_hub import HfApi
from sklearn.model_selection import train_test_split
from .constants import MAX_NUM_CHARS_IN_DOCUMENT, MIN_NUM_CHARS_IN_DOCUMENT # noqa
logging.basicConfig(format="%(asctime)s ⋅ %(message)s", level=logging.INFO)
logger = logging.getLogger("create_cs_gec")
def main() -> None:
"""Create the CS GEC linguistic acceptability dataset and upload to HF Hub."""
# Load the dataset using Hugging Face datasets library
dataset = load_dataset("CZLC/cs_gec")
assert isinstance(dataset, DatasetDict)
# Convert the train and test datasets to pandas DataFrames
train_df = dataset["train"].to_pandas()
test_df = dataset["test"].to_pandas()
assert isinstance(train_df, pd.DataFrame)
assert isinstance(test_df, pd.DataFrame)
# Rename columns to match the expected format
train_df = train_df.rename(columns={"query": "text", "gold": "label"})
test_df = test_df.rename(columns={"query": "text", "gold": "label"})
train_df["label"] = train_df["label"].apply(
lambda x: "correct" if x == 1 else "incorrect"
)
test_df["label"] = test_df["label"].apply(
lambda x: "correct" if x == 1 else "incorrect"
)
# Filter data based on text length
train_df = filter_by_text_length(train_df)
test_df = filter_by_text_length(test_df)
# Sample a larger number of rows to create train and val splits
sampled_train_df = train_df.sample(n=1280, random_state=42) # 1024 + 256
# Create a validation split from the sampled train data
train_df, val_df = train_test_split(
sampled_train_df, train_size=1024, random_state=42
)
# Sample the desired number of rows for the test split
test_df = test_df.sample(n=2048, random_state=42)
# Create a DatasetDict
dataset_dict = DatasetDict(
{
"train": Dataset.from_pandas(
train_df, split=Split.TRAIN, preserve_index=False
),
"val": Dataset.from_pandas(
val_df, split=Split.VALIDATION, preserve_index=False
),
"test": Dataset.from_pandas(
test_df, split=Split.TEST, preserve_index=False
),
}
)
# Upload to Hugging Face Hub
dataset_id = "EuroEval/cs-gec-mini"
HfApi().delete_repo(dataset_id, repo_type="dataset", missing_ok=True)
dataset_dict.push_to_hub(dataset_id, private=True)
def filter_by_text_length(df: pd.DataFrame) -> pd.DataFrame:
"""Filter the dataframe by text length."""
df["text_len"] = df.text.str.len()
return df.query(
"text_len >= @MIN_NUM_CHARS_IN_DOCUMENT and "
"text_len <= @MAX_NUM_CHARS_IN_DOCUMENT"
)
if __name__ == "__main__":
main()