Datasets:
Download dataset-preprocessor.py from pszemraj/goodwiki-text2text-completion: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/datasets/pszemraj/goodwiki-text2text-completion/resolve/main/dataset-preprocessor.py
- Command line
-
hf download hf://datasets/pszemraj/goodwiki-text2text-completion/dataset-preprocessor.py
-
curl -L -o dataset-preprocessor.py https://huggingface.co/datasets/pszemraj/goodwiki-text2text-completion/resolve/main/dataset-preprocessor.py
1.63 kB
| from datasets import load_dataset | |
| import re | |
| import random | |
| def split_into_paragraphs(text): | |
| # Split by markdown headers or double newlines | |
| paragraphs = re.split(r'\n\n|(?=^#)', text, flags=re.MULTILINE) | |
| return [p.strip() for p in paragraphs if p.strip()] | |
| def create_input_output_pairs(example): | |
| paragraphs = example['paragraphs'] | |
| n_paragraphs = len(paragraphs) | |
| # Randomly select about half of the paragraphs for input | |
| n_input = max(1, random.randint(n_paragraphs // 2 - 1, n_paragraphs // 2 + 1)) | |
| input_paragraphs = paragraphs[:n_input] | |
| output_paragraphs = paragraphs[n_input:] | |
| return { | |
| 'inputs': ' '.join(input_paragraphs), | |
| 'targets': ' '.join(output_paragraphs) | |
| } | |
| def preprocess_dataset(dataset_name, text_column='text'): | |
| # Load the dataset | |
| dataset = load_dataset(dataset_name) | |
| # Split text into paragraphs | |
| dataset = dataset.map( | |
| lambda example: {'paragraphs': split_into_paragraphs(example[text_column])}, | |
| remove_columns=[text_column] | |
| ) | |
| # Create input-output pairs | |
| preprocessed_dataset = dataset.map( | |
| create_input_output_pairs, | |
| remove_columns=['paragraphs'] | |
| ) | |
| return preprocessed_dataset | |
| # Usage example | |
| if __name__ == "__main__": | |
| # Replace 'your_dataset' with the actual dataset name | |
| dataset_name = 'your_dataset' | |
| preprocessed_dataset = preprocess_dataset(dataset_name) | |
| # Print some examples | |
| print(preprocessed_dataset['train'][:5]) | |
| # Save the preprocessed dataset | |
| preprocessed_dataset.save_to_disk("preprocessed_dataset") | |