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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "fe34671e-5117-4b12-a2b5-dc07fbb49021",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"## Testing out Hugging Face Inference API. Goal is to get working model & inference API on Hugging Face hub."
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "markdown",
|
| 13 |
+
"id": "92575bb9-bcdf-4357-aa08-e8814b02eafb",
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"source": [
|
| 16 |
+
"For starters, just trying out inference api from starter script here: https://api-inference.huggingface.co/docs/python/html/detailed_parameters.html#summarization-task\n"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": 1,
|
| 22 |
+
"id": "f44dba0e-72bd-49be-93a8-68895dbf994d",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [
|
| 25 |
+
{
|
| 26 |
+
"data": {
|
| 27 |
+
"text/plain": [
|
| 28 |
+
"[{'summary_text': 'CNN.com is celebrating its 10th anniversary this year. We are celebrating by asking our team members to share their thoughts and ideas. We want to hear from you, our readers, about what you think and what you want to share with the world. Share your ideas and help our team to become a little bit better today.'}]"
|
| 29 |
+
]
|
| 30 |
+
},
|
| 31 |
+
"execution_count": 1,
|
| 32 |
+
"metadata": {},
|
| 33 |
+
"output_type": "execute_result"
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
+
"source": [
|
| 37 |
+
"import json\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"import requests\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"API_TOKEN = \"hugging_face_access_token\"\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"headers = {\"Authorization\": f\"Bearer {API_TOKEN}\"}\n",
|
| 44 |
+
"API_URL = \"https://api-inference.huggingface.co/models/facebook/bart-large-cnn\"\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"# frank question = why POST? Maybe Willian can help answer that\n",
|
| 47 |
+
"def query(payload):\n",
|
| 48 |
+
" data = json.dumps(payload)\n",
|
| 49 |
+
" response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n",
|
| 50 |
+
" return json.loads(response.content.decode(\"utf-8\"))\n",
|
| 51 |
+
"\n",
|
| 52 |
+
"data = query(\n",
|
| 53 |
+
" {\n",
|
| 54 |
+
" \"inputs\": \"Picture this it’s the early morning, I’m sitting down with a hot cup of French roast coffee. I’m putting my AirPods in everything’s quiet. My mind is fresh and there are no distractions. I have a feeling of anticipation. It’s a mix of focus, excitement, and a tiny bit of apprehension. I’m about to hear your voice because some about to listen, to ponder replies. I have the space to connect with my team members to listen to them and learn something about you. This usually forces me to learn something about myself _____. I press play, and the learning begins. Thank you for being engaged here and sharing your ideas and helping our team to become just a little bit better today.\",\n",
|
| 55 |
+
" \"parameters\": {\"do_sample\": False},\n",
|
| 56 |
+
" }\n",
|
| 57 |
+
")\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"data"
|
| 60 |
+
]
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"cell_type": "markdown",
|
| 64 |
+
"id": "3e786be3-d888-4faa-9527-11237fea3882",
|
| 65 |
+
"metadata": {},
|
| 66 |
+
"source": [
|
| 67 |
+
"Ok. Crappy summary, but you can tell it works from the last sentence..."
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"cell_type": "markdown",
|
| 72 |
+
"id": "5d3455b8-ec89-49d2-be27-a2e87218d902",
|
| 73 |
+
"metadata": {},
|
| 74 |
+
"source": [
|
| 75 |
+
"### Next, fine tune linydub and push model to hugging face hub¶"
|
| 76 |
+
]
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"cell_type": "markdown",
|
| 80 |
+
"id": "4cdd2db4-95ea-4212-9f99-8d65f62fb349",
|
| 81 |
+
"metadata": {},
|
| 82 |
+
"source": [
|
| 83 |
+
"We need the model..."
|
| 84 |
+
]
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"cell_type": "code",
|
| 88 |
+
"execution_count": 2,
|
| 89 |
+
"id": "ca901ecf-7369-47f9-bbdd-ddc358cc8ff1",
|
| 90 |
+
"metadata": {},
|
| 91 |
+
"outputs": [],
|
| 92 |
+
"source": [
|
| 93 |
+
"from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"tokenizer = AutoTokenizer.from_pretrained(\"linydub/bart-large-samsum\")\n",
|
| 96 |
+
"\n",
|
| 97 |
+
"model = AutoModelForSeq2SeqLM.from_pretrained(\"linydub/bart-large-samsum\")"
|
| 98 |
+
]
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"cell_type": "markdown",
|
| 102 |
+
"id": "f88d654b-f9de-421d-aab0-bd8522e01e69",
|
| 103 |
+
"metadata": {},
|
| 104 |
+
"source": [
|
| 105 |
+
"We need training data...\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"Let's see what the lindydub training data looks like so we can replicate the format for fine-tuning. https://github.com/linydub/azureml-greenai-txtsum/tree/main/examples/assets/data/hf-samsum/train\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"I had no idea how to open an arrow file, so I got the samsum data from here instead: https://paperswithcode.com/dataset/samsum-corpus\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"And then this blog post helped me figure out the training part: https://medium.com/rocket-mortgage-technology-blog/conversational-summarization-with-natural-language-processing-c073a6bcaa3a\n"
|
| 112 |
+
]
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"cell_type": "code",
|
| 116 |
+
"execution_count": 3,
|
| 117 |
+
"id": "cea91bb7-b1eb-4363-841a-88502a368669",
|
| 118 |
+
"metadata": {},
|
| 119 |
+
"outputs": [],
|
| 120 |
+
"source": [
|
| 121 |
+
"val_path = 'val.json'\n",
|
| 122 |
+
"test_path = 'test.json'\n",
|
| 123 |
+
"train_path = 'train.json'\n",
|
| 124 |
+
"\n",
|
| 125 |
+
"with open(val_path) as in_file:\n",
|
| 126 |
+
" val = json.load(in_file)\n",
|
| 127 |
+
" in_file.close()\n",
|
| 128 |
+
"\n",
|
| 129 |
+
"with open(test_path) as in_file:\n",
|
| 130 |
+
" test = json.load(in_file)\n",
|
| 131 |
+
" in_file.close()\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"with open(train_path) as in_file:\n",
|
| 134 |
+
" train = json.load(in_file)\n",
|
| 135 |
+
" in_file.close()\n",
|
| 136 |
+
" \n",
|
| 137 |
+
"data = train + test + val\n",
|
| 138 |
+
"assert len(data) == len(train) + len(test) + len(val)"
|
| 139 |
+
]
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"cell_type": "code",
|
| 143 |
+
"execution_count": 4,
|
| 144 |
+
"id": "a4fc8ad6-1c3c-4839-9db7-ea811d8d1cc3",
|
| 145 |
+
"metadata": {},
|
| 146 |
+
"outputs": [],
|
| 147 |
+
"source": [
|
| 148 |
+
"import pandas as pd\n",
|
| 149 |
+
"df = pd.DataFrame(data)\n",
|
| 150 |
+
"df['dialogue'] = df['dialogue'].str.replace('\\r', '')\n",
|
| 151 |
+
"df['dialogue'] = df['dialogue'].str.replace('\\n', '')\n",
|
| 152 |
+
"df['summary'] = df['summary'].str.replace('\\r', '')\n",
|
| 153 |
+
"df['summary'] = df['summary'].str.replace('\\n', '')"
|
| 154 |
+
]
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"cell_type": "code",
|
| 158 |
+
"execution_count": 5,
|
| 159 |
+
"id": "4db4c0de-1a68-4977-b10b-694923f692e1",
|
| 160 |
+
"metadata": {},
|
| 161 |
+
"outputs": [],
|
| 162 |
+
"source": [
|
| 163 |
+
"validator = df.head(1)\n",
|
| 164 |
+
"df = df.iloc[1:,]"
|
| 165 |
+
]
|
| 166 |
+
},
|
| 167 |
+
{
|
| 168 |
+
"cell_type": "code",
|
| 169 |
+
"execution_count": 7,
|
| 170 |
+
"id": "83afe4d7-b410-432d-9b1a-a2983f3858cc",
|
| 171 |
+
"metadata": {},
|
| 172 |
+
"outputs": [],
|
| 173 |
+
"source": [
|
| 174 |
+
"import datasets\n",
|
| 175 |
+
"\n",
|
| 176 |
+
"data_as_dataset = datasets.Dataset.from_pandas(df, preserve_index=False)"
|
| 177 |
+
]
|
| 178 |
+
},
|
| 179 |
+
{
|
| 180 |
+
"cell_type": "code",
|
| 181 |
+
"execution_count": 8,
|
| 182 |
+
"id": "5539d549-2306-42cc-9975-aa2fb4ecab43",
|
| 183 |
+
"metadata": {},
|
| 184 |
+
"outputs": [],
|
| 185 |
+
"source": [
|
| 186 |
+
"dd = data_as_dataset.train_test_split(test_size=0.1)"
|
| 187 |
+
]
|
| 188 |
+
},
|
| 189 |
+
{
|
| 190 |
+
"cell_type": "code",
|
| 191 |
+
"execution_count": 9,
|
| 192 |
+
"id": "e013ce63-8252-41f7-8ebb-41e926a889d6",
|
| 193 |
+
"metadata": {},
|
| 194 |
+
"outputs": [],
|
| 195 |
+
"source": [
|
| 196 |
+
"prefix = \"summarize: \"\n",
|
| 197 |
+
"\n",
|
| 198 |
+
"def preprocess_function(examples):\n",
|
| 199 |
+
" inputs = [prefix + doc for doc in examples[\"dialogue\"]]\n",
|
| 200 |
+
" model_inputs = tokenizer(inputs, max_length=1024, truncation=True)\n",
|
| 201 |
+
"\n",
|
| 202 |
+
" with tokenizer.as_target_tokenizer():\n",
|
| 203 |
+
" labels = tokenizer(examples[\"summary\"], max_length=128, truncation=True)\n",
|
| 204 |
+
"\n",
|
| 205 |
+
" model_inputs[\"labels\"] = labels[\"input_ids\"]\n",
|
| 206 |
+
" return model_inputs"
|
| 207 |
+
]
|
| 208 |
+
},
|
| 209 |
+
{
|
| 210 |
+
"cell_type": "code",
|
| 211 |
+
"execution_count": 10,
|
| 212 |
+
"id": "64f15b64-34b2-4849-9d7f-8fe712656613",
|
| 213 |
+
"metadata": {},
|
| 214 |
+
"outputs": [
|
| 215 |
+
{
|
| 216 |
+
"data": {
|
| 217 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 218 |
+
"model_id": "a2ba058964994fd2b55f7c569c000f13",
|
| 219 |
+
"version_major": 2,
|
| 220 |
+
"version_minor": 0
|
| 221 |
+
},
|
| 222 |
+
"text/plain": [
|
| 223 |
+
" 0%| | 0/1 [00:00<?, ?ba/s]"
|
| 224 |
+
]
|
| 225 |
+
},
|
| 226 |
+
"metadata": {},
|
| 227 |
+
"output_type": "display_data"
|
| 228 |
+
},
|
| 229 |
+
{
|
| 230 |
+
"data": {
|
| 231 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 232 |
+
"model_id": "d4a48e263d50461ba596bd5033a1e920",
|
| 233 |
+
"version_major": 2,
|
| 234 |
+
"version_minor": 0
|
| 235 |
+
},
|
| 236 |
+
"text/plain": [
|
| 237 |
+
" 0%| | 0/1 [00:00<?, ?ba/s]"
|
| 238 |
+
]
|
| 239 |
+
},
|
| 240 |
+
"metadata": {},
|
| 241 |
+
"output_type": "display_data"
|
| 242 |
+
}
|
| 243 |
+
],
|
| 244 |
+
"source": [
|
| 245 |
+
"tokenized_dd = dd.map(preprocess_function, batched=True)"
|
| 246 |
+
]
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"cell_type": "code",
|
| 250 |
+
"execution_count": 11,
|
| 251 |
+
"id": "9b5ded79-f0ed-4c0a-8a96-ae694f232aa6",
|
| 252 |
+
"metadata": {},
|
| 253 |
+
"outputs": [],
|
| 254 |
+
"source": [
|
| 255 |
+
"from transformers import Seq2SeqTrainingArguments, Seq2SeqTrainer"
|
| 256 |
+
]
|
| 257 |
+
},
|
| 258 |
+
{
|
| 259 |
+
"cell_type": "code",
|
| 260 |
+
"execution_count": 12,
|
| 261 |
+
"id": "1f051a1f-9292-4d91-a2c3-89f1d03a0935",
|
| 262 |
+
"metadata": {},
|
| 263 |
+
"outputs": [],
|
| 264 |
+
"source": [
|
| 265 |
+
"from transformers import DataCollatorForSeq2Seq\n",
|
| 266 |
+
"\n",
|
| 267 |
+
"data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model)"
|
| 268 |
+
]
|
| 269 |
+
},
|
| 270 |
+
{
|
| 271 |
+
"cell_type": "code",
|
| 272 |
+
"execution_count": 13,
|
| 273 |
+
"id": "c04e187b-6e44-479f-911e-278733884c30",
|
| 274 |
+
"metadata": {},
|
| 275 |
+
"outputs": [],
|
| 276 |
+
"source": [
|
| 277 |
+
"from pynvml import *\n",
|
| 278 |
+
"\n",
|
| 279 |
+
"def print_gpu_utilization():\n",
|
| 280 |
+
" nvmlInit()\n",
|
| 281 |
+
" handle = nvmlDeviceGetHandleByIndex(0)\n",
|
| 282 |
+
" info = nvmlDeviceGetMemoryInfo(handle)\n",
|
| 283 |
+
" print(f\"GPU memory occupied: {info.used//1024**2} MB.\")"
|
| 284 |
+
]
|
| 285 |
+
},
|
| 286 |
+
{
|
| 287 |
+
"cell_type": "code",
|
| 288 |
+
"execution_count": 14,
|
| 289 |
+
"id": "bdbaf892-ce5d-4fba-8133-db147de51d53",
|
| 290 |
+
"metadata": {},
|
| 291 |
+
"outputs": [
|
| 292 |
+
{
|
| 293 |
+
"name": "stdout",
|
| 294 |
+
"output_type": "stream",
|
| 295 |
+
"text": [
|
| 296 |
+
"GPU memory occupied: 0 MB.\n"
|
| 297 |
+
]
|
| 298 |
+
}
|
| 299 |
+
],
|
| 300 |
+
"source": [
|
| 301 |
+
"print_gpu_utilization()"
|
| 302 |
+
]
|
| 303 |
+
},
|
| 304 |
+
{
|
| 305 |
+
"cell_type": "code",
|
| 306 |
+
"execution_count": 15,
|
| 307 |
+
"id": "ed721ebc-11c9-431d-a303-dcd1d39a6a6a",
|
| 308 |
+
"metadata": {},
|
| 309 |
+
"outputs": [
|
| 310 |
+
{
|
| 311 |
+
"name": "stderr",
|
| 312 |
+
"output_type": "stream",
|
| 313 |
+
"text": [
|
| 314 |
+
"Using amp half precision backend\n",
|
| 315 |
+
"The following columns in the training set don't have a corresponding argument in `BartForConditionalGeneration.forward` and have been ignored: id, dialogue, summary. If id, dialogue, summary are not expected by `BartForConditionalGeneration.forward`, you can safely ignore this message.\n",
|
| 316 |
+
"/opt/conda/lib/python3.7/site-packages/transformers/optimization.py:309: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning\n",
|
| 317 |
+
" FutureWarning,\n",
|
| 318 |
+
"***** Running training *****\n",
|
| 319 |
+
" Num examples = 12\n",
|
| 320 |
+
" Num Epochs = 3\n",
|
| 321 |
+
" Instantaneous batch size per device = 8\n",
|
| 322 |
+
" Total train batch size (w. parallel, distributed & accumulation) = 8\n",
|
| 323 |
+
" Gradient Accumulation steps = 1\n",
|
| 324 |
+
" Total optimization steps = 6\n"
|
| 325 |
+
]
|
| 326 |
+
},
|
| 327 |
+
{
|
| 328 |
+
"data": {
|
| 329 |
+
"text/html": [
|
| 330 |
+
"\n",
|
| 331 |
+
" <div>\n",
|
| 332 |
+
" \n",
|
| 333 |
+
" <progress value='6' max='6' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
| 334 |
+
" [6/6 00:02, Epoch 3/3]\n",
|
| 335 |
+
" </div>\n",
|
| 336 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
| 337 |
+
" <thead>\n",
|
| 338 |
+
" <tr style=\"text-align: left;\">\n",
|
| 339 |
+
" <th>Epoch</th>\n",
|
| 340 |
+
" <th>Training Loss</th>\n",
|
| 341 |
+
" <th>Validation Loss</th>\n",
|
| 342 |
+
" </tr>\n",
|
| 343 |
+
" </thead>\n",
|
| 344 |
+
" <tbody>\n",
|
| 345 |
+
" <tr>\n",
|
| 346 |
+
" <td>1</td>\n",
|
| 347 |
+
" <td>No log</td>\n",
|
| 348 |
+
" <td>1.015452</td>\n",
|
| 349 |
+
" </tr>\n",
|
| 350 |
+
" <tr>\n",
|
| 351 |
+
" <td>2</td>\n",
|
| 352 |
+
" <td>No log</td>\n",
|
| 353 |
+
" <td>1.071723</td>\n",
|
| 354 |
+
" </tr>\n",
|
| 355 |
+
" <tr>\n",
|
| 356 |
+
" <td>3</td>\n",
|
| 357 |
+
" <td>No log</td>\n",
|
| 358 |
+
" <td>1.088332</td>\n",
|
| 359 |
+
" </tr>\n",
|
| 360 |
+
" </tbody>\n",
|
| 361 |
+
"</table><p>"
|
| 362 |
+
],
|
| 363 |
+
"text/plain": [
|
| 364 |
+
"<IPython.core.display.HTML object>"
|
| 365 |
+
]
|
| 366 |
+
},
|
| 367 |
+
"metadata": {},
|
| 368 |
+
"output_type": "display_data"
|
| 369 |
+
},
|
| 370 |
+
{
|
| 371 |
+
"name": "stderr",
|
| 372 |
+
"output_type": "stream",
|
| 373 |
+
"text": [
|
| 374 |
+
"The following columns in the evaluation set don't have a corresponding argument in `BartForConditionalGeneration.forward` and have been ignored: id, dialogue, summary. If id, dialogue, summary are not expected by `BartForConditionalGeneration.forward`, you can safely ignore this message.\n",
|
| 375 |
+
"***** Running Evaluation *****\n",
|
| 376 |
+
" Num examples = 2\n",
|
| 377 |
+
" Batch size = 8\n",
|
| 378 |
+
"The following columns in the evaluation set don't have a corresponding argument in `BartForConditionalGeneration.forward` and have been ignored: id, dialogue, summary. If id, dialogue, summary are not expected by `BartForConditionalGeneration.forward`, you can safely ignore this message.\n",
|
| 379 |
+
"***** Running Evaluation *****\n",
|
| 380 |
+
" Num examples = 2\n",
|
| 381 |
+
" Batch size = 8\n",
|
| 382 |
+
"The following columns in the evaluation set don't have a corresponding argument in `BartForConditionalGeneration.forward` and have been ignored: id, dialogue, summary. If id, dialogue, summary are not expected by `BartForConditionalGeneration.forward`, you can safely ignore this message.\n",
|
| 383 |
+
"***** Running Evaluation *****\n",
|
| 384 |
+
" Num examples = 2\n",
|
| 385 |
+
" Batch size = 8\n",
|
| 386 |
+
"\n",
|
| 387 |
+
"\n",
|
| 388 |
+
"Training completed. Do not forget to share your model on huggingface.co/models =)\n",
|
| 389 |
+
"\n",
|
| 390 |
+
"\n"
|
| 391 |
+
]
|
| 392 |
+
},
|
| 393 |
+
{
|
| 394 |
+
"data": {
|
| 395 |
+
"text/plain": [
|
| 396 |
+
"TrainOutput(global_step=6, training_loss=1.0018877983093262, metrics={'train_runtime': 4.8179, 'train_samples_per_second': 7.472, 'train_steps_per_second': 1.245, 'total_flos': 22551432265728.0, 'train_loss': 1.0018877983093262, 'epoch': 3.0})"
|
| 397 |
+
]
|
| 398 |
+
},
|
| 399 |
+
"execution_count": 15,
|
| 400 |
+
"metadata": {},
|
| 401 |
+
"output_type": "execute_result"
|
| 402 |
+
}
|
| 403 |
+
],
|
| 404 |
+
"source": [
|
| 405 |
+
"training_args = Seq2SeqTrainingArguments(\n",
|
| 406 |
+
" output_dir=\"./results\",\n",
|
| 407 |
+
" evaluation_strategy=\"epoch\",\n",
|
| 408 |
+
" learning_rate=2e-5,\n",
|
| 409 |
+
" per_device_train_batch_size=8,\n",
|
| 410 |
+
" per_device_eval_batch_size=8,\n",
|
| 411 |
+
" weight_decay=0.01,\n",
|
| 412 |
+
" save_total_limit=3,\n",
|
| 413 |
+
" num_train_epochs=3,\n",
|
| 414 |
+
" fp16=True,\n",
|
| 415 |
+
")\n",
|
| 416 |
+
"\n",
|
| 417 |
+
"trainer = Seq2SeqTrainer(\n",
|
| 418 |
+
" model=model,\n",
|
| 419 |
+
" args=training_args,\n",
|
| 420 |
+
" train_dataset=tokenized_dd[\"train\"],\n",
|
| 421 |
+
" eval_dataset=tokenized_dd[\"test\"],\n",
|
| 422 |
+
" tokenizer=tokenizer,\n",
|
| 423 |
+
" data_collator=data_collator,\n",
|
| 424 |
+
")\n",
|
| 425 |
+
"\n",
|
| 426 |
+
"trainer.train()"
|
| 427 |
+
]
|
| 428 |
+
},
|
| 429 |
+
{
|
| 430 |
+
"cell_type": "code",
|
| 431 |
+
"execution_count": 17,
|
| 432 |
+
"id": "eece8dd8-29ab-45cf-9a3e-7cded880f385",
|
| 433 |
+
"metadata": {},
|
| 434 |
+
"outputs": [
|
| 435 |
+
{
|
| 436 |
+
"data": {
|
| 437 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 438 |
+
"model_id": "784ecc7b7797495f95651cd2e61a7c3d",
|
| 439 |
+
"version_major": 2,
|
| 440 |
+
"version_minor": 0
|
| 441 |
+
},
|
| 442 |
+
"text/plain": [
|
| 443 |
+
"VBox(children=(HTML(value='<center>\\n<img src=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
|
| 444 |
+
]
|
| 445 |
+
},
|
| 446 |
+
"metadata": {},
|
| 447 |
+
"output_type": "display_data"
|
| 448 |
+
}
|
| 449 |
+
],
|
| 450 |
+
"source": [
|
| 451 |
+
"from huggingface_hub import notebook_login\n",
|
| 452 |
+
"\n",
|
| 453 |
+
"notebook_login()"
|
| 454 |
+
]
|
| 455 |
+
},
|
| 456 |
+
{
|
| 457 |
+
"cell_type": "code",
|
| 458 |
+
"execution_count": 18,
|
| 459 |
+
"id": "e4a7de93-5acc-4173-b6fe-98ae15e2373a",
|
| 460 |
+
"metadata": {},
|
| 461 |
+
"outputs": [
|
| 462 |
+
{
|
| 463 |
+
"name": "stdout",
|
| 464 |
+
"output_type": "stream",
|
| 465 |
+
"text": [
|
| 466 |
+
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
| 467 |
+
"To disable this warning, you can either:\n",
|
| 468 |
+
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
| 469 |
+
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
|
| 470 |
+
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
| 471 |
+
"To disable this warning, you can either:\n",
|
| 472 |
+
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
| 473 |
+
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
|
| 474 |
+
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
| 475 |
+
"To disable this warning, you can either:\n",
|
| 476 |
+
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
| 477 |
+
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
|
| 478 |
+
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
| 479 |
+
"To disable this warning, you can either:\n",
|
| 480 |
+
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
| 481 |
+
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
| 482 |
+
]
|
| 483 |
+
},
|
| 484 |
+
{
|
| 485 |
+
"ename": "OSError",
|
| 486 |
+
"evalue": "Tried to clone a repository in a non-empty folder that isn't a git repository. If you really want to do this, do it manually:\ngit init && git remote add origin && git pull origin main\n or clone repo to a new folder and move your existing files there afterwards.",
|
| 487 |
+
"output_type": "error",
|
| 488 |
+
"traceback": [
|
| 489 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 490 |
+
"\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)",
|
| 491 |
+
"\u001b[0;32m/tmp/ipykernel_1257/1405518398.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpush_to_hub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
| 492 |
+
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/transformers/trainer.py\u001b[0m in \u001b[0;36mpush_to_hub\u001b[0;34m(self, commit_message, blocking, **kwargs)\u001b[0m\n\u001b[1;32m 2827\u001b[0m \u001b[0;31m# it might fail.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2828\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"repo\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2829\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit_git_repo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2830\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2831\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshould_save\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 493 |
+
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/transformers/trainer.py\u001b[0m in \u001b[0;36minit_git_repo\u001b[0;34m(self, at_init)\u001b[0m\n\u001b[1;32m 2709\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutput_dir\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2710\u001b[0m \u001b[0mclone_from\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrepo_name\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2711\u001b[0;31m \u001b[0muse_auth_token\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0muse_auth_token\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2712\u001b[0m )\n\u001b[1;32m 2713\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mEnvironmentError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 494 |
+
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/huggingface_hub/repository.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, local_dir, clone_from, repo_type, use_auth_token, git_user, git_email, revision, private, skip_lfs_files)\u001b[0m\n\u001b[1;32m 419\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 420\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mclone_from\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 421\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclone_from\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrepo_url\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mclone_from\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 422\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 423\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_git_repo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlocal_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 495 |
+
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/huggingface_hub/repository.py\u001b[0m in \u001b[0;36mclone_from\u001b[0;34m(self, repo_url, use_auth_token)\u001b[0m\n\u001b[1;32m 620\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0min_repository\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 621\u001b[0m raise EnvironmentError(\n\u001b[0;32m--> 622\u001b[0;31m \u001b[0;34m\"Tried to clone a repository in a non-empty folder that isn't a git repository. If you really \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 623\u001b[0m \u001b[0;34m\"want to do this, do it manually:\\n\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 624\u001b[0m \u001b[0;34m\"git init && git remote add origin && git pull origin main\\n\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 496 |
+
"\u001b[0;31mOSError\u001b[0m: Tried to clone a repository in a non-empty folder that isn't a git repository. If you really want to do this, do it manually:\ngit init && git remote add origin && git pull origin main\n or clone repo to a new folder and move your existing files there afterwards."
|
| 497 |
+
]
|
| 498 |
+
}
|
| 499 |
+
],
|
| 500 |
+
"source": [
|
| 501 |
+
"trainer.push_to_hub()"
|
| 502 |
+
]
|
| 503 |
+
},
|
| 504 |
+
{
|
| 505 |
+
"cell_type": "code",
|
| 506 |
+
"execution_count": null,
|
| 507 |
+
"id": "53e539de-637d-4127-99c5-4f3dab3ab286",
|
| 508 |
+
"metadata": {},
|
| 509 |
+
"outputs": [],
|
| 510 |
+
"source": []
|
| 511 |
+
}
|
| 512 |
+
],
|
| 513 |
+
"metadata": {
|
| 514 |
+
"environment": {
|
| 515 |
+
"kernel": "python3",
|
| 516 |
+
"name": "pytorch-gpu.1-10.m87",
|
| 517 |
+
"type": "gcloud",
|
| 518 |
+
"uri": "gcr.io/deeplearning-platform-release/pytorch-gpu.1-10:m87"
|
| 519 |
+
},
|
| 520 |
+
"kernelspec": {
|
| 521 |
+
"display_name": "Python 3",
|
| 522 |
+
"language": "python",
|
| 523 |
+
"name": "python3"
|
| 524 |
+
},
|
| 525 |
+
"language_info": {
|
| 526 |
+
"codemirror_mode": {
|
| 527 |
+
"name": "ipython",
|
| 528 |
+
"version": 3
|
| 529 |
+
},
|
| 530 |
+
"file_extension": ".py",
|
| 531 |
+
"mimetype": "text/x-python",
|
| 532 |
+
"name": "python",
|
| 533 |
+
"nbconvert_exporter": "python",
|
| 534 |
+
"pygments_lexer": "ipython3",
|
| 535 |
+
"version": "3.7.12"
|
| 536 |
+
}
|
| 537 |
+
},
|
| 538 |
+
"nbformat": 4,
|
| 539 |
+
"nbformat_minor": 5
|
| 540 |
+
}
|
summarize-linydub.ipynb
CHANGED
|
@@ -503,10 +503,104 @@
|
|
| 503 |
},
|
| 504 |
{
|
| 505 |
"cell_type": "code",
|
| 506 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
"id": "53e539de-637d-4127-99c5-4f3dab3ab286",
|
| 508 |
"metadata": {},
|
| 509 |
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
"source": []
|
| 511 |
}
|
| 512 |
],
|
|
|
|
| 503 |
},
|
| 504 |
{
|
| 505 |
"cell_type": "code",
|
| 506 |
+
"execution_count": 19,
|
| 507 |
+
"id": "fa530ecc-2343-4d8c-9255-0eb2c807f24d",
|
| 508 |
+
"metadata": {},
|
| 509 |
+
"outputs": [
|
| 510 |
+
{
|
| 511 |
+
"name": "stdout",
|
| 512 |
+
"output_type": "stream",
|
| 513 |
+
"text": [
|
| 514 |
+
"original text preprocessed: \n",
|
| 515 |
+
" Amanda: I baked cookies. Do you want some?Jerry: Sure!Amanda: I'll bring you tomorrow :-)\n"
|
| 516 |
+
]
|
| 517 |
+
}
|
| 518 |
+
],
|
| 519 |
+
"source": [
|
| 520 |
+
"preprocess_text = validator[\"dialogue\"].values[0].strip().replace(\"\\n\",\"\")\n",
|
| 521 |
+
"prepared_Text = \"summarize: \" + preprocess_text\n",
|
| 522 |
+
"print (\"original text preprocessed: \\n\", preprocess_text)"
|
| 523 |
+
]
|
| 524 |
+
},
|
| 525 |
+
{
|
| 526 |
+
"cell_type": "code",
|
| 527 |
+
"execution_count": 21,
|
| 528 |
"id": "53e539de-637d-4127-99c5-4f3dab3ab286",
|
| 529 |
"metadata": {},
|
| 530 |
"outputs": [],
|
| 531 |
+
"source": [
|
| 532 |
+
"tokenized_text = tokenizer.encode(prepared_Text, return_tensors=\"pt\").to(device='cuda')"
|
| 533 |
+
]
|
| 534 |
+
},
|
| 535 |
+
{
|
| 536 |
+
"cell_type": "code",
|
| 537 |
+
"execution_count": 22,
|
| 538 |
+
"id": "c0be5361-7b03-4ca3-a9b6-3ec2b8f264bb",
|
| 539 |
+
"metadata": {},
|
| 540 |
+
"outputs": [],
|
| 541 |
+
"source": [
|
| 542 |
+
"summary_ids = model.generate(tokenized_text,\n",
|
| 543 |
+
" num_beams=4,\n",
|
| 544 |
+
" no_repeat_ngram_size=2,\n",
|
| 545 |
+
" min_length=30,\n",
|
| 546 |
+
" max_length=100,\n",
|
| 547 |
+
" early_stopping=True)"
|
| 548 |
+
]
|
| 549 |
+
},
|
| 550 |
+
{
|
| 551 |
+
"cell_type": "code",
|
| 552 |
+
"execution_count": 23,
|
| 553 |
+
"id": "1dbf6a8b-89dd-4f9b-89b9-bad7c3ac41ff",
|
| 554 |
+
"metadata": {},
|
| 555 |
+
"outputs": [
|
| 556 |
+
{
|
| 557 |
+
"name": "stdout",
|
| 558 |
+
"output_type": "stream",
|
| 559 |
+
"text": [
|
| 560 |
+
"\n",
|
| 561 |
+
"\n",
|
| 562 |
+
"Summarized text: \n",
|
| 563 |
+
" Amanda baked cookies and will bring them to Jerry tomorrow. Jerry is happy to have some cookies. Amanda is going to bring Jerry cookies tomorrow as well.\n"
|
| 564 |
+
]
|
| 565 |
+
}
|
| 566 |
+
],
|
| 567 |
+
"source": [
|
| 568 |
+
"output = tokenizer.decode(summary_ids[0], skip_special_tokens=True)\n",
|
| 569 |
+
"print (\"\\n\\nSummarized text: \\n\",output)"
|
| 570 |
+
]
|
| 571 |
+
},
|
| 572 |
+
{
|
| 573 |
+
"cell_type": "code",
|
| 574 |
+
"execution_count": 26,
|
| 575 |
+
"id": "7535952b-7d24-4716-85fa-3e6ab142baa5",
|
| 576 |
+
"metadata": {},
|
| 577 |
+
"outputs": [
|
| 578 |
+
{
|
| 579 |
+
"ename": "PermissionError",
|
| 580 |
+
"evalue": "[Errno 13] Permission denied: '/first_success_save_model'",
|
| 581 |
+
"output_type": "error",
|
| 582 |
+
"traceback": [
|
| 583 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 584 |
+
"\u001b[0;31mPermissionError\u001b[0m Traceback (most recent call last)",
|
| 585 |
+
"\u001b[0;32m/tmp/ipykernel_1257/4217802359.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mpt_save_directory\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"/first_success_save_model\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtokenizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave_pretrained\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpt_save_directory\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave_pretrained\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpt_save_directory\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 586 |
+
"\u001b[0;32m/opt/conda/lib/python3.7/site-packages/transformers/tokenization_utils_base.py\u001b[0m in \u001b[0;36msave_pretrained\u001b[0;34m(self, save_directory, legacy_format, filename_prefix, push_to_hub, **kwargs)\u001b[0m\n\u001b[1;32m 2043\u001b[0m \u001b[0mrepo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_create_or_get_repo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msave_directory\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2044\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2045\u001b[0;31m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmakedirs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msave_directory\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexist_ok\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2046\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2047\u001b[0m special_tokens_map_file = os.path.join(\n",
|
| 587 |
+
"\u001b[0;32m/opt/conda/lib/python3.7/os.py\u001b[0m in \u001b[0;36mmakedirs\u001b[0;34m(name, mode, exist_ok)\u001b[0m\n\u001b[1;32m 221\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 223\u001b[0;31m \u001b[0mmkdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 224\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 225\u001b[0m \u001b[0;31m# Cannot rely on checking for EEXIST, since the operating system\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
| 588 |
+
"\u001b[0;31mPermissionError\u001b[0m: [Errno 13] Permission denied: '/first_success_save_model'"
|
| 589 |
+
]
|
| 590 |
+
}
|
| 591 |
+
],
|
| 592 |
+
"source": [
|
| 593 |
+
"pt_save_directory = \"/first_success_save_model\"\n",
|
| 594 |
+
"tokenizer.save_pretrained(pt_save_directory)\n",
|
| 595 |
+
"model.save_pretrained(pt_save_directory)"
|
| 596 |
+
]
|
| 597 |
+
},
|
| 598 |
+
{
|
| 599 |
+
"cell_type": "code",
|
| 600 |
+
"execution_count": null,
|
| 601 |
+
"id": "12f3280a-60c9-42d0-a2e8-2e581a46e68f",
|
| 602 |
+
"metadata": {},
|
| 603 |
+
"outputs": [],
|
| 604 |
"source": []
|
| 605 |
}
|
| 606 |
],
|