{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lIYdn1woOS1n", "outputId": "796938b0-82b5-4cb9-9f17-79d8761c7cb7" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.8/50.8 kB\u001b[0m \u001b[31m866.3 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m471.6/471.6 kB\u001b[0m \u001b[31m6.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m116.3/116.3 kB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m177.6/177.6 kB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m20.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.8/134.8 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m194.1/194.1 kB\u001b[0m \u001b[31m11.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m239.5/239.5 kB\u001b[0m \u001b[31m14.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m124.6/124.6 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m447.9/447.9 kB\u001b[0m \u001b[31m18.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "!pip install -U datasets -q" ] }, { "cell_type": "code", "source": [ "from datasets import load_dataset\n", "import re\n", "import random\n", "\n", "def split_into_paragraphs(text):\n", " # Split by markdown headers or double newlines\n", " paragraphs = re.split(r'\\n\\n|(?=^#)', text, flags=re.MULTILINE)\n", " return [p.strip() for p in paragraphs if p.strip()]\n", "\n", "def create_input_output_pairs(example):\n", " paragraphs = example['paragraphs']\n", " n_paragraphs = len(paragraphs)\n", "\n", " # Randomly select about half of the paragraphs for input\n", " n_input = max(1, random.randint(n_paragraphs // 2 - 1, n_paragraphs // 2 + 1))\n", "\n", " input_paragraphs = paragraphs[:n_input]\n", " output_paragraphs = paragraphs[n_input:]\n", "\n", " return {\n", " 'inputs': ' '.join(input_paragraphs),\n", " 'targets': ' '.join(output_paragraphs)\n", " }\n", "\n", "def preprocess_dataset(dataset_name, text_column='text'):\n", " # Load the dataset\n", " dataset = load_dataset(dataset_name)\n", "\n", " # Split text into paragraphs\n", " dataset = dataset.map(\n", " lambda example: {'paragraphs': split_into_paragraphs(example[text_column])},\n", " remove_columns=[text_column]\n", " )\n", "\n", " # Create input-output pairs\n", " preprocessed_dataset = dataset.map(\n", " create_input_output_pairs,\n", " remove_columns=['paragraphs']\n", " )\n", "\n", " return preprocessed_dataset\n", "\n", "\n", "\n", "" ], "metadata": { "id": "rZKvItPSubSU" }, "execution_count": 2, "outputs": [] }, { "cell_type": "code", "source": [ "dataset_name = 'euirim/goodwiki'\n", "\n", "preprocessed_dataset = preprocess_dataset(dataset_name, text_column='markdown')\n", "\n", "# Print some examples\n", "print(preprocessed_dataset['train'][:5])" ], "metadata": { "id": "umd-Dh6eueSh", "outputId": "6b2dbcb0-43e9-491d-ba35-16a8724c3ac8", "colab": { "base_uri": "https://localhost:8080/", "height": 214, "referenced_widgets": [ "aa7eb04fa8d048399ff493a88e53f5d0", "72b9c8a93acd4065910a334ed3d05be8", "a2b96136bf2e4f01b7411f110cb91247", "27534958a29443ff8b8f94a700530d82", "49f19a566754409fbe08462119f674d4", "b3581e413e01446a90be2ce2a4504478", "c9a89abab2c341cdbab3a942f89c592f", "0af4f64fddee4019a449f91cb4fbf2fd", "9a2f14444c284530a882c53c20640461", "1b2881efaea148d1bf3aada91e8eaa35", "c5e896ea82314fef92b3955186f14b5d", "d7a7432086e24616ad9d68ccfe45d072", "d8725d691b03417bad20c264c8e99c22", "4a0cdd3d317a4e7ca6132e182a2a74dc", "5d8cf8f088894318bd6db9bbe9f81183", "fdd107b3f8164cfdbae0361120a0d937", "19cf2d5ed8014599958520aff01db6b7", "1e860b9529264b498c22e1c4f31d9278", "a55412c7d3c543618eb97058a698df62", "66dc14928b674d6b94c46081475873be", "2c7c205c644541b2bfba08f45df2a737", "5864e444f1424517a692e1dee1975fe1", "2efcdbc0f5af4f9fb5dba2a4e2fd0086", "bdc71edf355e442b98f57e533efa799b", "96336091034247ac908351cd631880dc", "d9919061b2664273a7f41c05703625d9", "bd02d51ea06b4a5a8ac607bee3ae622f", "355f2382649b4f7e9cfed0dd9e3d7d22", "df98c3576d54491d884c441f9506da7e", "c64cdaf18b584068920492494a30a200", "d8b4cfd5aac34b33ae3cc01d8c702b5f", "ab92ae9e7488485c83b570a5a0bdb583", "8533bbf9bd1a4f26adcbb812046523a7", "0845ecf4258043979b9c9055841dd7c4", "9189339c5d66416083f73ca60f2f794f", "dc9f6d2ab60044f288da94eadb60d555", "3c1f5edb8ef943029f7e6c25723f1257", "4ca640ab33c14c16b07354ade611dfed", "dc3f4225d4cd4d91ad4feacf2814a726", "e400856b11ff49daa1f9a9eec09ff2e3", "724bc22672ac4b1982fd04031309f72f", "94cf80ba74a44e43b4730798909e7a91", "4b488da44540478e95f90af3fa7af69a", "f75838cc015243c696b37627cae98cb3", "2fa3ab3d26b64238a55b6fc4c3a82f37", "20ad46d73e6b4f0199d0b58b340c43dc", "adc77b3ac36a46bca7b2e9d17c5f696c", "1a30613e6c134371b0044a23c234c1cd", "cfc23b9580d8487a9ca3f0fc668b5214", "b69a379b8f2c4ff2a50b0fbf24b18c82", "13c31a8dda7044e695fae8170b3b69d8", "a69c6176328e4241a0a6b84823ddf7de", "bf7a356f7c6742ff9049f31b0ac01cad", "a2a9e56f917d4751b74edd3d98d14f16", "2bde088c53f64c12af05215a8ca5ae8a" ] } }, "execution_count": 3, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "README.md: 0%| | 0.00/10.4k [00:00