( # noqa: C901
dataset_path,
tokenizer,
data_size,
max_hidden_seq_length=384,
shuffle=True,
)
| 263 | |
| 264 | |
| 265 | def get_seq2seq_dataset_from_squad_csv( # noqa: C901 |
| 266 | dataset_path, |
| 267 | tokenizer, |
| 268 | data_size, |
| 269 | max_hidden_seq_length=384, |
| 270 | shuffle=True, |
| 271 | ): |
| 272 | |
| 273 | def get_data_loader(max_hidden_seq_length): |
| 274 | class SquadSeq2SeqDataset(torch.utils.data.Dataset): |
| 275 | def __init__( |
| 276 | self, |
| 277 | dataset_path, |
| 278 | tokenizer, |
| 279 | data_size, |
| 280 | max_hidden_seq_length, |
| 281 | ): |
| 282 | self.max_hidden_seq_length = max_hidden_seq_length |
| 283 | self.tokenizer = tokenizer |
| 284 | self.samples = self._load_and_process(dataset_path, data_size) |
| 285 | |
| 286 | def _load_and_process(self, path, max_samples): |
| 287 | with open(path, "r", encoding="utf-8") as f: |
| 288 | reader = csv.DictReader(f) |
| 289 | rows = list(reader) |
| 290 | if shuffle: |
| 291 | random.shuffle(rows) |
| 292 | samples = [] |
| 293 | for row in rows: |
| 294 | question = row["question"].strip() |
| 295 | context = row["context"].strip() |
| 296 | answer = row["answer"].strip() |
| 297 | if not question or not context or not answer: |
| 298 | continue |
| 299 | input_text = f"question: {question} context: {context}" |
| 300 | target_text = answer |
| 301 | samples.append((input_text, target_text)) |
| 302 | if len(samples) >= max_samples: |
| 303 | break |
| 304 | return samples |
| 305 | |
| 306 | def __len__(self): |
| 307 | return len(self.samples) |
| 308 | |
| 309 | def __getitem__(self, idx): |
| 310 | input_text, target_text = self.samples[idx] |
| 311 | model_input = tokenizer( |
| 312 | input_text, |
| 313 | truncation=True, |
| 314 | padding="max_length", |
| 315 | max_length=self.max_hidden_seq_length, |
| 316 | return_tensors="pt", |
| 317 | ) |
| 318 | label = tokenizer( |
| 319 | target_text, |
| 320 | truncation=True, |
| 321 | padding="max_length", |
| 322 | max_length=64, |
no test coverage detected