samsum_dataset.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. # For dataset details visit: https://huggingface.co/datasets/samsum
  4. import copy
  5. import datasets
  6. def get_preprocessed_samsum(dataset_config, tokenizer, split):
  7. dataset = datasets.load_dataset("samsum", split=split)
  8. prompt = (
  9. f"Summarize this dialog:\n{{dialog}}\n---\nSummary:\n"
  10. )
  11. def apply_prompt_template(sample):
  12. return {
  13. "prompt": prompt.format(dialog=sample["dialogue"]),
  14. "summary": sample["summary"],
  15. }
  16. dataset = dataset.map(apply_prompt_template, remove_columns=list(dataset.features))
  17. def tokenize_add_label(sample):
  18. prompt = tokenizer.encode(tokenizer.bos_token + sample["prompt"], add_special_tokens=False)
  19. summary = tokenizer.encode(sample["summary"] + tokenizer.eos_token, add_special_tokens=False)
  20. sample = {
  21. "input_ids": prompt + summary,
  22. "attention_mask" : [1] * (len(prompt) + len(summary)),
  23. "labels": [-100] * len(prompt) + summary,
  24. }
  25. return sample
  26. dataset = dataset.map(tokenize_add_label, remove_columns=list(dataset.features))
  27. return dataset