forked from NVIDIA/NeMo-Curator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier_filtering.py
107 lines (86 loc) · 3.34 KB
/
classifier_filtering.py
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
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# 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 argparse
import random
import fasttext
import nemo_curator as nc
from nemo_curator.datasets import DocumentDataset
from nemo_curator.filters import FastTextQualityFilter
from nemo_curator.modifiers import FastTextLabelModifier
from nemo_curator.utils.distributed_utils import get_client, read_data, write_to_disk
from nemo_curator.utils.file_utils import get_all_files_paths_under
from nemo_curator.utils.script_utils import ArgumentHelper
def load_dataset(input_data_dir):
files = list(get_all_files_paths_under(input_data_dir))
raw_data = read_data(files, file_type="jsonl", backend="pandas", add_filename=True)
dataset = DocumentDataset(raw_data)
return dataset
def create_samples(data_path, label, num_samples):
raw_dataset = load_dataset(data_path)
label_quality = nc.Modify(FastTextLabelModifier(label))
labeled_dataset = label_quality(raw_dataset)
labeled_samples = labeled_dataset.df.sample(
frac=num_samples / len(labeled_dataset.df)
)
return labeled_samples["text"].compute().values.tolist()
def main(args):
# Params
low_quality_data_path = "/path/to/low_quality"
high_quality_data_path = "/path/to/high_quality"
num_low_quality_samples = 1000
num_high_quality_samples = 1000
filtered_output = "/path/to/output"
# Prepare samples for the classifier
client = get_client(**ArgumentHelper.parse_client_args(args))
low_quality_samples = create_samples(
low_quality_data_path, "__label__lq", num_low_quality_samples
)
high_quality_samples = create_samples(
high_quality_data_path, "__label__hq", num_high_quality_samples
)
train_samples = low_quality_samples + high_quality_samples
random.shuffle(train_samples)
train_file = "./fasttext.train"
model_path = "./fasttext_model.bin"
with open(train_file, "w") as f:
for sample in train_samples:
f.write(sample)
f.write("\n")
# Train fastText classifier
model = fasttext.train_supervised(
input=train_file,
lr=0.01,
dim=100,
epoch=5,
wordNgrams=2,
)
model.save_model(model_path)
# Filter data
target_dataset = load_dataset(low_quality_data_path)
filter_pipeline = nc.ScoreFilter(
FastTextQualityFilter(model_path),
score_field="quality_score",
score_type=float,
)
filtered_dataset = filter_pipeline(target_dataset)
# Write filtered dataset
write_to_disk(filtered_dataset.df, filtered_output, write_to_filename=True)
def attach_args(
parser=argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
),
):
return ArgumentHelper(parser).add_distributed_args()
if __name__ == "__main__":
main(attach_args().parse_args())