-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize_dataset.py
193 lines (148 loc) · 6.5 KB
/
tokenize_dataset.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# -*-coding:utf-8 -*-
""" Tokenize dataset for later use
This script generates tokenized dataset to be used for training and testing.
It creates ands saves into data/tokenizers a dataset where the 'test' split belongs
to unbiased dataset and 'train_val' is the data that is yet to be filtered before
fine-tuning.
Note on folder structure:
└── args.base_dir
├── data
│ └── *medical_corpus_clean_preprocessed.tsv*
│
├── filtering
│ ├── *method1_level1.tsv*
│ ├── *method2_level1.tsv*
│ ├── *method1_level2.tsv*
│ ...
│ └── *methodn_leveln.ysv*
├── logs
│ ├── *method1_level1*
│ ...
│ └── *methodn_leveln*
├── models
│ ├── *method1_level1*
│ ...
│ └── *methodn_leveln*
├── tokenizers
│ └── *dataset_tokenized*
└── training
@author: jorgedelpozolerida
@date: 17/11/2023
"""
import os
import argparse
import logging
import pandas as pd
import torch
from datasets import Dataset, DatasetDict, ClassLabel
from transformers import MBart50Tokenizer
logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__)
# Mapping of column names for languages to mBART50 language codes
MAPPING_LANG = {
"pol": "pl_PL",
"eng": "en_XX"
}
def read_csv_corrupt(path2file, cols_used):
try:
d = pd.read_csv(path2file, sep='\t', usecols=cols_used)
except pd.errors.ParserError:
d = pd.read_csv(path2file, sep=',', usecols=cols_used)
return d
def preprocess_function(examples, tokenizer,
src_col = "eng", target_col = "pol"):
"""
Preprocess input data for model training.
Args:
examples: A batch of examples from the dataset.
tokenizer: The tokenizer to use for encoding the text.
src_col: The source column name.
target_col: The target column name.
Returns:
A dictionary containing tokenized inputs and labels.
"""
inputs = [doc for doc in examples[src_col]]
targets = [doc for doc in examples[target_col]]
model_inputs = tokenizer(inputs, max_length=210, truncation=True, padding='max_length', return_tensors='pt')
with tokenizer.as_target_tokenizer():
labels = tokenizer(targets, max_length=210, truncation=True, padding='max_length', return_tensors='pt')
model_inputs["labels"] = labels["input_ids"].squeeze()
return model_inputs
def ensure_dir(dir):
if not os.path.exists(dir):
os.mkdir(dir)
return dir
def main(args):
assert os.path.isdir(args.base_dir)
assert args.source_lang != args.target_lang
data_folder = ensure_dir(os.path.join(args.base_dir, "data"))
tokenizer_folder = ensure_dir(os.path.join(args.base_dir, "tokenizers")) # where to save tokenized dataset
model_name = "facebook/mbart-large-50-one-to-many-mmt"
dataset_dir = os.path.join(tokenizer_folder, args.dataset_name)
if os.path.exists(dataset_dir):
raise ValueError(f"Dataset dir exists already, remove: {dataset_dir}")
# Load tokenizer
tokenizer = MBart50Tokenizer.from_pretrained(model_name,
src_lang=MAPPING_LANG[args.source_lang],
tgt_lang=MAPPING_LANG[args.target_lang]
)
_logger.info("Loaded tokenizer")
# Mapping function with additional arguments
preprocess_args = {
"tokenizer": tokenizer,
"src_col": args.source_lang,
"target_col": args.target_lang
}
# TRAINING dataset ---------------------------------------------------------
# Load the training dataset
df_train = read_csv_corrupt(
os.path.join(data_folder, args.train_corpus_name),
cols_used=['id', 'pol', 'eng', 'src']
)
# Load the training dataset into Hugging Face datasets
train_dataset = Dataset.from_pandas(df_train, preserve_index=False)
tokenized_train = train_dataset.map(lambda x: preprocess_function(x, **preprocess_args), batched=True)
# handle src column to allow future stratified splits
unique_classes = sorted(set(tokenized_train['src']))
class_label_feature = ClassLabel(names=unique_classes)
tokenized_train = tokenized_train.cast_column('src', class_label_feature)
_logger.info("Training-Val set generated")
# TEST dataset -------------------------------------------------------------
df_test = read_csv_corrupt(
os.path.join(data_folder, args.test_corpus_name),
cols_used=['id', 'pol', 'eng']
)
# Load the test dataset into Hugging Face datasets
test_dataset = Dataset.from_pandas(df_test, preserve_index=False)
tokenized_test = test_dataset.map(lambda x: preprocess_function(x, **preprocess_args), batched=True)
_logger.info("Test set generated")
# COMBINED -----------------------------------------------------------------
# Combine the splits to form the final dataset
final_dataset = DatasetDict({
'train_val': tokenized_train,
'test': tokenized_test
})
final_dataset.save_to_disk(dataset_dir)
_logger.info(f"Succesfully tokenized dataset into: {dataset_dir}")
def parse_args():
'''
Parses all script arguments.
'''
parser = argparse.ArgumentParser()
parser.add_argument('--base_dir', type=str, default=None, required=True,
help='Path where all subfolders are present for the project')
parser.add_argument('--dataset_name', type=str, default="dataset_tokenized",
help='Name of dataset to create')
parser.add_argument('--source_lang', type=str, default="eng", choices=['eng', 'pol'],
help='Source language')
parser.add_argument('--target_lang', type=str, default="pol", choices=['eng', 'pol'],
help='Target language')
parser.add_argument('--train_corpus_name', type=str, default="trainval_set_v3.tsv",
help='Name of training corpus. Train-val splits taken from here.')
parser.add_argument('--test_corpus_name', type=str, default="test_set_v3.tsv", #TODO: change this
help='Name of test corpus')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(args)