File size: 20,970 Bytes
699b3f6 |
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
{
"cells": [
{
"cell_type": "markdown",
"id": "BI5kXWukdii1",
"metadata": {
"id": "BI5kXWukdii1"
},
"source": [
"**github_Link**:https://github.com/mohammadVesall/Summarize/blob/main/Summarizer.ipynb"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ec592020-dff5-4077-902d-7e852a6793f5",
"metadata": {
"id": "ec592020-dff5-4077-902d-7e852a6793f5"
},
"outputs": [],
"source": [
"import warnings\n",
"import torch\n",
"import gradio\n",
"from transformers import pipeline\n",
"from bs4 import BeautifulSoup\n",
"import requests"
]
},
{
"cell_type": "markdown",
"id": "T_D-esc6FIsI",
"metadata": {
"id": "T_D-esc6FIsI"
},
"source": [
"# **1.display the orginal text by link**\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cc02cb9c-daf7-4b94-8770-ea9ff4037203",
"metadata": {
"id": "cc02cb9c-daf7-4b94-8770-ea9ff4037203"
},
"outputs": [],
"source": [
"summarizer = pipeline(\"summarization\", model=\"stevhliu/my_awesome_billsum_model\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0aa4d56f-864c-41e0-932a-d804e95dfbc1",
"metadata": {
"id": "0aa4d56f-864c-41e0-932a-d804e95dfbc1"
},
"outputs": [],
"source": [
"url = \"https://medium.com/analytics-vidhya/openai-gpt-3-language-models-are-few-shot-learners-82531b3d3122\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "47a7e4aa-27cd-474c-8444-4b68052b6ea9",
"metadata": {
"id": "47a7e4aa-27cd-474c-8444-4b68052b6ea9"
},
"outputs": [],
"source": [
"r = requests.get(url)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c83bff7e-340e-4bc3-af2b-35ec85c5ce14",
"metadata": {
"id": "c83bff7e-340e-4bc3-af2b-35ec85c5ce14"
},
"outputs": [],
"source": [
"soup = BeautifulSoup(r.text, 'html.parser')\n",
"results = soup.find_all(['hl', 'p'])\n",
"text = [result.text for result in results]\n",
"Article = ''.join(text)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c1c2cc22-752d-42c7-a228-affe13e043a6",
"metadata": {
"id": "c1c2cc22-752d-42c7-a228-affe13e043a6",
"outputId": "fac83567-3eea-4408-88b5-63a11dbd4f65"
},
"outputs": [
{
"data": {
"text/plain": [
"'Sign upSign inSign upSign inSoheil TehranipourFollowAnalytics Vidhya--4ListenShareOpenAI recently published a paper describing GPT-3, a deep-learning model for Natural Language Processing, with 175 Billion parameters(!!!), 100x more than the previous version, GPT-2. The model is pre-trained on nearly half a trillion words and achieves SOTA performance on several NLP benchmarks without fine-tuning.Based on its paper, GPT-3 is an autoregressive language model as opposed to a denoising autoencoder like BERT. I decided to write about some of the comparative differences between those two architectures of language models. The paper is an investigation into what you can do with giant language models. Now this language model is an order of magnitude larger than anyone has ever built a language model.OpenAI’s researchers: “In this work we do not fine-tune GPT-3 because our focus is on task-agnostic performance,”So, they seem appropriate for natural language generation. One of the things I want to explore here, is mainly an academic endeavor. The commercial utility which is something that can help us in industry is not possible with GPT-3 (at least not possible right not) which is only in reach for the very top tech companies like Microsoft,Google or OpenAI.you could learn a complex hierarchy of relationships directly between the tokens it was no longer this sequential representation where you only knew what happened at one time step backwards it was a real paradigm shift and straight away these language processing models produce SOTA results across the board.Generally speaking, there are two architectural patterns that we see with these transformers’ architectures.2. The other type of model is what’s called a denoising autoencoder like BERT and RoBERTa and XLNET and what these do is you feed in an input sentence and then you typically add some noise to it and then you say what you expect. in the photo below, you can check the size of parameters for each NLP effort before GPT-3.Now, it’s time to check some famous terms.The OpenAI researchers state that larger models make increasingly efficient use of in-context information. As can be seen in the plot below, the steeper “in-context learning curves” for large models show improved ability to learn from contextual information.I read somewhere that the whole story of GPT-3 costs “nearly $12 million dollars.” I know that it’s not even a penny for Elon Musk but for using GPT-3 in action for practical uses, this is not what the companies like. Also, One hypothesis is that GPT-3 has so many parameters (half the number of tokens trained on) that it is starting to act like a memory network.As you should know, GPT-2 GPT-2 (a successor to GPT), was trained simply to predict the next word in 40GB of Internet text. GPT-2 is a large transformer-based language model with 1.5 billion parameters, trained on a dataset of 8 million web pages. GPT-2 is trained with a simple objective: predict the next word, given all of the previous words within some text. The diversity of the dataset causes this simple goal to contain naturally occurring demonstrations of many tasks across diverse domains. GPT-2 is a direct scale-up of GPT, with more than 10X the parameters and trained on more than 10X the amount of data. (Based on OpenAI)But, GPT-3 is upgraded with 175 billion parameters, it tailors and escalates the GPT-2 architecture, it also involves adjusted initialization, pre-normalization, and changeable tokenization. It reflects substantial performance on various NLP tasks and benchmarks in three distinct shots, i.e. zero-shot, one-shot and some-shot environments.So, these things seem to be appropriate for things like question answering when you need to point to spans in your input sequence for example if you’re doing in-span question answering the auto-regressive models are quite attractive because you can just keep generating data forever.For training, the researchers have used a combination of model parallelism within each matrix multiply and model parallelism. More information about the training corpus is listed below:GPT-3 was trained on V100 GPU on the part of a high-bandwidth cluster provided by Microsoft. Evaluation of GPT-3 is done under 3 conditions:The results show that GPT-3 showed strong performance with translation, question-answering, and cloze tasks, as well as with unscrambling words and performing 3-digit arithmetic. The researchers claim that GPT-3 can even generate news articles in which human evaluators have difficulty distinguishing from articles written by humans.GPT-3 is an incredibly large model, and one cannot expect to build something like this without fancy computational resources. However, the researchers assure that these models can be efficient once trained, where even a full GPT-3 model generating 74 pages of content from a trained model can cost only a few cents in energy costs.As with GPT-2, OpenAI has not released the trained model or the code, although there is a GitHub repository containing some of the test datasets as well as a collection of text samples generated by the model.OpenAI has recently unveiled the latest epitome of its eye-catching text generator, GPT-3 that has 175 billion parameters. GPT-3 can execute an amazing bandwidth of natural language processing tasks, even without requiring fine-tuning for a specific task. It is capable of performing machine translation, question-answering, reading conceptual tasks, scripting poems, and elementary math.Public domain.----4Analytics VidhyaPh.D. Candidate, Passionate about data science to improve the world./ Linkedin: https://www.linkedin.com/in/soheiltehrani/HelpStatusAboutCareersPressBlogPrivacyTermsText to speechTeams'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Article"
]
},
{
"cell_type": "markdown",
"id": "1MLobC5mGFrX",
"metadata": {
"id": "1MLobC5mGFrX"
},
"source": [
"# **2.Separate into different parts**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fe88051f-e2ab-4c65-9b70-539e934f24c0",
"metadata": {
"id": "fe88051f-e2ab-4c65-9b70-539e934f24c0"
},
"outputs": [],
"source": [
"max_chunk = 500"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6eb89890-c213-4438-9d05-492c1870cf64",
"metadata": {
"id": "6eb89890-c213-4438-9d05-492c1870cf64"
},
"outputs": [],
"source": [
"Article = Article.replace('.', '')\n",
"Article = Article.replace('?', '')\n",
"Article = Article.replace('!', '')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d50737d5-387d-4aa3-ad4a-b6db0bcbf9a6",
"metadata": {
"id": "d50737d5-387d-4aa3-ad4a-b6db0bcbf9a6",
"outputId": "f1d102d3-64d0-4406-e910-cef82d902f4f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"sentences = Article.split(' ')\n",
"current_chunk = 0\n",
"chunks = []\n",
"for sentence in sentences:\n",
" if len(chunks) == current_chunk + 1:\n",
" if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:\n",
" chunks[current_chunk].extend(sentence.split(' '))\n",
" else:\n",
" current_chunk += 1\n",
" chunks.append(sentence.split(' '))\n",
" else:\n",
" print(current_chunk)\n",
" chunks.append(sentence.split(' '))\n",
"\n",
"for chunk_id in range(len(chunks)):\n",
" chunks[chunk_id] = ' '.join(chunks[chunk_id])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "23c0caae-1ce3-42d4-8914-35f15a15c572",
"metadata": {
"id": "23c0caae-1ce3-42d4-8914-35f15a15c572",
"outputId": "c1297323-60c5-408d-e89a-93d4d9a1e9b1"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(chunks)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "109eae32-d1fe-4257-a820-7841e5d6e08b",
"metadata": {
"id": "109eae32-d1fe-4257-a820-7841e5d6e08b",
"outputId": "7fde050f-defe-43ef-fda5-1b7c991b8c00"
},
"outputs": [
{
"data": {
"text/plain": [
"'Sign upSign inSign upSign inSoheil TehranipourFollowAnalytics Vidhya--4ListenShareOpenAI recently published a paper describing GPT-3, a deep-learning model for Natural Language Processing, with 175 Billion parameters(), 100x more than the previous version, GPT-2 The model is pre-trained on nearly half a trillion words and achieves SOTA performance on several NLP benchmarks without fine-tuningBased on its paper, GPT-3 is an autoregressive language model as opposed to a denoising autoencoder like BERT I decided to write about some of the comparative differences between those two architectures of language models The paper is an investigation into what you can do with giant language models Now this language model is an order of magnitude larger than anyone has ever built a language modelOpenAI’s researchers: “In this work we do not fine-tune GPT-3 because our focus is on task-agnostic performance,”So, they seem appropriate for natural language generation One of the things I want to explore here, is mainly an academic endeavor The commercial utility which is something that can help us in industry is not possible with GPT-3 (at least not possible right not) which is only in reach for the very top tech companies like Microsoft,Google or OpenAIyou could learn a complex hierarchy of relationships directly between the tokens it was no longer this sequential representation where you only knew what happened at one time step backwards it was a real paradigm shift and straight away these language processing models produce SOTA results across the boardGenerally speaking, there are two architectural patterns that we see with these transformers’ architectures2 The other type of model is what’s called a denoising autoencoder like BERT and RoBERTa and XLNET and what these do is you feed in an input sentence and then you typically add some noise to it and then you say what you expect in the photo below, you can check the size of parameters for each NLP effort before GPT-3Now, it’s time to check some famous termsThe OpenAI researchers state that larger models make increasingly efficient use of in-context information As can be seen in the plot below, the steeper “in-context learning curves” for large models show improved ability to learn from contextual informationI read somewhere that the whole story of GPT-3 costs “nearly $12 million dollars” I know that it’s not even a penny for Elon Musk but for using GPT-3 in action for practical uses, this is not what the companies like Also, One hypothesis is that GPT-3 has so many parameters (half the number of tokens trained on) that it is starting to act like a memory networkAs you should know, GPT-2 GPT-2 (a successor to GPT), was trained simply to predict the next word in 40GB of Internet text GPT-2 is a large transformer-based language model with 15 billion parameters, trained on a dataset of 8 million web pages GPT-2 is trained with a simple objective: predict the next word, given all of the previous words within some text The diversity of the dataset causes this simple goal to contain naturally occurring'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chunks[0]"
]
},
{
"cell_type": "markdown",
"id": "FPjsoPy7G01E",
"metadata": {
"id": "FPjsoPy7G01E"
},
"source": [
"# **3.Summarizing each part**"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "c9655275-9311-4ec8-8942-f77e415c5e55",
"metadata": {
"id": "c9655275-9311-4ec8-8942-f77e415c5e55",
"outputId": "16e9c2b6-0cff-4560-92af-352afaf0cec5"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Token indices sequence length is longer than the specified maximum sequence length for this model (700 > 512). Running this sequence through the model will result in indexing errors\n"
]
}
],
"source": [
"res = summarizer(chunks, max_length=50, min_length=30, do_sample=False)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "c3377904-21e7-4778-b70b-a3222d55446f",
"metadata": {
"id": "c3377904-21e7-4778-b70b-a3222d55446f",
"outputId": "15d5c0a2-0e15-4259-e5d2-e760d67f0861"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(res)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "352e1671-dd89-4545-9a4f-a9eebc7763ce",
"metadata": {
"id": "352e1671-dd89-4545-9a4f-a9eebc7763ce",
"outputId": "5011c45c-3efb-4574-f2a4-f8e3f7f87a56"
},
"outputs": [
{
"data": {
"text/plain": [
"{'summary_text': 'GPT-3 is a direct scale-up of GPT, with 175 billion parameters and trained on more than 10X the amount of data (Based on OpenAI) It reflects substantial performance on various NLP tasks and benchmarks'}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res[1]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "6557cefe-aba0-43d8-a6a4-88b0aaeadfe7",
"metadata": {
"id": "6557cefe-aba0-43d8-a6a4-88b0aaeadfe7",
"outputId": "9abfd595-131b-43ae-e45b-ec90a7eed89b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'summary_text': 'GPT-3 is a deep-learning model for Natural Language Processing, with 175 Billion parameters(), 100x more than the previous version, GPT-2 The model is pre-trained on nearly half a trillion words and'}\n",
"{'summary_text': 'GPT-3 is a direct scale-up of GPT, with 175 billion parameters and trained on more than 10X the amount of data (Based on OpenAI) It reflects substantial performance on various NLP tasks and benchmarks'}\n"
]
}
],
"source": [
"for i in range(len(res)):\n",
" print(res[i])"
]
},
{
"cell_type": "markdown",
"id": "LI7KM9pLHSja",
"metadata": {
"id": "LI7KM9pLHSja"
},
"source": [
"# *** Display in app**"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "2140e706-db66-410c-8181-9d595bc6a953",
"metadata": {
"id": "2140e706-db66-410c-8181-9d595bc6a953"
},
"outputs": [],
"source": [
"def SUMMARIZE(Url):\n",
" Url = \"https://medium.com/analytics-vidhya/openai-gpt-3-language-models-are-few-shot-learners-82531b3d3122\"\n",
" summarizer = pipeline(\"summarization\", model=\"stevhliu/my_awesome_billsum_model\")\n",
" r = requests.get(Url)\n",
" soup = BeautifulSoup(r.text, 'html.parser')\n",
" results = soup.find_all(['hl', 'p'])\n",
" text = [result.text for result in results]\n",
" Article = ''.join(text)\n",
" sentences = Article.split(' ')\n",
" current_chunk = 0\n",
" chunks = []\n",
" for sentence in sentences:\n",
" if len(chunks) == current_chunk + 1:\n",
" if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:\n",
" chunks[current_chunk].extend(sentence.split(' '))\n",
" else:\n",
" current_chunk += 1\n",
" chunks.append(sentence.split(' '))\n",
" else:\n",
" #print(current_chunk)\n",
" chunks.append(sentence.split(' '))\n",
"\n",
" for chunk_id in range(len(chunks)):\n",
" chunks[chunk_id] = ' '.join(chunks[chunk_id])\n",
" res = summarizer(chunks, max_length=120, min_length=30, do_sample=False)\n",
" for i in range(len(res)):\n",
" return res[i].values()"
]
},
{
"cell_type": "markdown",
"id": "OzG5meMJH5EL",
"metadata": {
"id": "OzG5meMJH5EL"
},
"source": [
"**get the link, show the suumarized text**"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "68c7ff4a-d685-406b-a5a5-cb2dd5834f6f",
"metadata": {
"id": "68c7ff4a-d685-406b-a5a5-cb2dd5834f6f",
"outputId": "ebb84c02-0da2-4103-8763-e77160825cbf"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7864\n",
"\n",
"Could not create share link. Please check your internet connection or our status page: https://status.gradio.app.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interface = gradio.Interface(fn=SUMMARIZE,\n",
" inputs=gradio.TextArea(lines=2, value=\"https://medium.com/analytics-vidhya/openai-gpt-3-language-models-are-few-shot-learners-82531b3d3122\"),\n",
" outputs=gradio.TextArea())\n",
"interface.launch(share=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d068ac5-d649-4f57-b2aa-2130b1a2dade",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|