Spaces:
Sleeping
Sleeping
File size: 8,774 Bytes
cb7cafb f68f6ad cb7cafb f68f6ad cb7cafb f68f6ad cb7cafb f68f6ad cb7cafb f68f6ad cb7cafb f68f6ad cb7cafb |
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.chdir('../')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'c:\\\\mlops projects\\\\text-summarization'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%pwd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from pathlib import Path\n",
"@dataclass(frozen=True)\n",
"class DataTransformationConfig:\n",
" root_dir : Path\n",
" data_path : Path\n",
" tokenizer_name : Path"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from textsummarizer.constants import *\n",
"from textsummarizer.utils.common import read_yaml, create_directories\n",
"\n",
"\n",
"class ConfigurationManager:\n",
" def __init__(\n",
" self,\n",
" config_filepath = CONFIG_FILE_PATH,\n",
" params_filepath = PARAMS_FILE_PATH):\n",
"\n",
" self.config = read_yaml(config_filepath)\n",
" self.params = read_yaml(params_filepath)\n",
"\n",
" create_directories([self.config.artifacts_root])\n",
"\n",
"\n",
" \n",
" def get_data_transformation_config(self) -> DataTransformationConfig:\n",
" config = self.config.data_transformation\n",
"\n",
" create_directories([config.root_dir])\n",
"\n",
" data_transformation_config = DataTransformationConfig(\n",
" root_dir=config.root_dir,\n",
" data_path=config.data_path,\n",
" tokenizer_name = config.tokenizer_name\n",
" )\n",
"\n",
" return data_transformation_config\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2024-08-11 18:13:05,488: INFO: config: PyTorch version 2.2.2+cu121 available.]\n",
"[2024-08-11 18:13:05,490: INFO: config: TensorFlow version 2.12.0 available.]\n"
]
}
],
"source": [
"import os\n",
"from textsummarizer.logging import logger\n",
"from transformers import AutoTokenizer\n",
"from datasets import load_dataset, load_from_disk"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"class DataTransformation:\n",
" def __init__(self, config : DataTransformationConfig):\n",
" self.config = config\n",
" self.tokenizer = AutoTokenizer.from_pretrained(self.config.tokenizer_name)\n",
" \n",
" \n",
" def convert_examples_to_features(self, example_batch):\n",
" input_encoding = self.tokenizer(example_batch['dialogue'], max_length = 1024, truncation = True)\n",
" \n",
" with self.tokenizer.as_target_tokenizer():\n",
" target_encodings = self.tokenizer(example_batch['summary'], max_length = 128, truncation = True )\n",
" \n",
" return {\n",
" 'input_ids' : input_encoding['input_ids'],\n",
" 'attention_mask': input_encoding['attention_mask'],\n",
" 'labels': target_encodings['input_ids']\n",
" }\n",
" \n",
" def convert(self):\n",
" dataset_samsum = load_from_disk(self.config.data_path)\n",
" dataset_samsum_pt = dataset_samsum.map(self.convert_examples_to_features, batched = True)\n",
" dataset_samsum_pt.save_to_disk(os.path.join(self.config.root_dir,\"samsum_dataset\")) "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2024-08-11 18:13:44,678: INFO: common: yaml file: config\\config.yaml loaded successfully]\n",
"[2024-08-11 18:13:44,681: INFO: common: yaml file: params.yaml loaded successfully]\n",
"[2024-08-11 18:13:44,684: INFO: common: created directory at: artifacts]\n",
"[2024-08-11 18:13:44,686: INFO: common: created directory at: artifacts/data_transformation]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\transformers\\tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",
" warnings.warn(\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bdedbcfbff63497081e37ad9b20a6c31",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/14732 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\transformers\\tokenization_utils_base.py:4126: UserWarning: `as_target_tokenizer` is deprecated and will be removed in v5 of Transformers. You can tokenize your labels by using the argument `text_target` of the regular `__call__` method (either in the same call as your input texts if you use the same keyword arguments, or in a separate call.\n",
" warnings.warn(\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3b8826d099004000a2a037e32bbdf1cc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/819 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4a5e1728a7d142d3b767f7b9c8f14c6f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/818 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "54194e4ec3de42738a2107fa26673aef",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/14732 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0209e20f794e4e3ab60ef282b98b8bb3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/819 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0a89fb4c0a96413782a55206d087a2a6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Saving the dataset (0/1 shards): 0%| | 0/818 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"try:\n",
" config = ConfigurationManager()\n",
" data_transformation_config = config.get_data_transformation_config()\n",
" data_transformation = DataTransformation(config=data_transformation_config)\n",
" data_transformation.convert()\n",
"except Exception as e:\n",
" raise e"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|