|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Transformers, what can they do?" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "Install the Transformers, Datasets, and Evaluate libraries to run this notebook." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "!pip install datasets evaluate transformers[sentencepiece]" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "data": { |
| 33 | + "text/plain": [ |
| 34 | + "[{'label': 'POSITIVE', 'score': 0.9598047137260437}]" |
| 35 | + ] |
| 36 | + }, |
| 37 | + "execution_count": null, |
| 38 | + "metadata": {}, |
| 39 | + "output_type": "execute_result" |
| 40 | + } |
| 41 | + ], |
| 42 | + "source": [ |
| 43 | + "from transformers import pipeline\n", |
| 44 | + "\n", |
| 45 | + "classifier = pipeline(\"sentiment-analysis\")\n", |
| 46 | + "classifier(\"I've been waiting for a HuggingFace course my whole life.\")" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": null, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "data": { |
| 56 | + "text/plain": [ |
| 57 | + "[{'label': 'POSITIVE', 'score': 0.9598047137260437},\n", |
| 58 | + " {'label': 'NEGATIVE', 'score': 0.9994558095932007}]" |
| 59 | + ] |
| 60 | + }, |
| 61 | + "execution_count": null, |
| 62 | + "metadata": {}, |
| 63 | + "output_type": "execute_result" |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "classifier(\n", |
| 68 | + " [\"I've been waiting for a HuggingFace course my whole life.\", \"I hate this so much!\"]\n", |
| 69 | + ")" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "data": { |
| 79 | + "text/plain": [ |
| 80 | + "{'sequence': 'This is a course about the Transformers library',\n", |
| 81 | + " 'labels': ['education', 'business', 'politics'],\n", |
| 82 | + " 'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]}" |
| 83 | + ] |
| 84 | + }, |
| 85 | + "execution_count": null, |
| 86 | + "metadata": {}, |
| 87 | + "output_type": "execute_result" |
| 88 | + } |
| 89 | + ], |
| 90 | + "source": [ |
| 91 | + "from transformers import pipeline\n", |
| 92 | + "\n", |
| 93 | + "classifier = pipeline(\"zero-shot-classification\")\n", |
| 94 | + "classifier(\n", |
| 95 | + " \"This is a course about the Transformers library\",\n", |
| 96 | + " candidate_labels=[\"education\", \"politics\", \"business\"],\n", |
| 97 | + ")" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": null, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [ |
| 105 | + { |
| 106 | + "data": { |
| 107 | + "text/plain": [ |
| 108 | + "[{'generated_text': 'In this course, we will teach you how to understand and use '\n", |
| 109 | + " 'data flow and data interchange when handling user data. We '\n", |
| 110 | + " 'will be working with one or more of the most commonly used '\n", |
| 111 | + " 'data flows — data flows of various types, as seen by the '\n", |
| 112 | + " 'HTTP'}]" |
| 113 | + ] |
| 114 | + }, |
| 115 | + "execution_count": null, |
| 116 | + "metadata": {}, |
| 117 | + "output_type": "execute_result" |
| 118 | + } |
| 119 | + ], |
| 120 | + "source": [ |
| 121 | + "from transformers import pipeline\n", |
| 122 | + "\n", |
| 123 | + "generator = pipeline(\"text-generation\")\n", |
| 124 | + "generator(\"In this course, we will teach you how to\")" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": null, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "data": { |
| 134 | + "text/plain": [ |
| 135 | + "[{'generated_text': 'In this course, we will teach you how to manipulate the world and '\n", |
| 136 | + " 'move your mental and physical capabilities to your advantage.'},\n", |
| 137 | + " {'generated_text': 'In this course, we will teach you how to become an expert and '\n", |
| 138 | + " 'practice realtime, and with a hands on experience on both real '\n", |
| 139 | + " 'time and real'}]" |
| 140 | + ] |
| 141 | + }, |
| 142 | + "execution_count": null, |
| 143 | + "metadata": {}, |
| 144 | + "output_type": "execute_result" |
| 145 | + } |
| 146 | + ], |
| 147 | + "source": [ |
| 148 | + "from transformers import pipeline\n", |
| 149 | + "\n", |
| 150 | + "generator = pipeline(\"text-generation\", model=\"distilgpt2\")\n", |
| 151 | + "generator(\n", |
| 152 | + " \"In this course, we will teach you how to\",\n", |
| 153 | + " max_length=30,\n", |
| 154 | + " num_return_sequences=2,\n", |
| 155 | + ")" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": null, |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [ |
| 163 | + { |
| 164 | + "data": { |
| 165 | + "text/plain": [ |
| 166 | + "[{'sequence': 'This course will teach you all about mathematical models.',\n", |
| 167 | + " 'score': 0.19619831442832947,\n", |
| 168 | + " 'token': 30412,\n", |
| 169 | + " 'token_str': ' mathematical'},\n", |
| 170 | + " {'sequence': 'This course will teach you all about computational models.',\n", |
| 171 | + " 'score': 0.04052725434303284,\n", |
| 172 | + " 'token': 38163,\n", |
| 173 | + " 'token_str': ' computational'}]" |
| 174 | + ] |
| 175 | + }, |
| 176 | + "execution_count": null, |
| 177 | + "metadata": {}, |
| 178 | + "output_type": "execute_result" |
| 179 | + } |
| 180 | + ], |
| 181 | + "source": [ |
| 182 | + "from transformers import pipeline\n", |
| 183 | + "\n", |
| 184 | + "unmasker = pipeline(\"fill-mask\")\n", |
| 185 | + "unmasker(\"This course will teach you all about <mask> models.\", top_k=2)" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": null, |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [ |
| 193 | + { |
| 194 | + "data": { |
| 195 | + "text/plain": [ |
| 196 | + "[{'entity_group': 'PER', 'score': 0.99816, 'word': 'Sylvain', 'start': 11, 'end': 18}, \n", |
| 197 | + " {'entity_group': 'ORG', 'score': 0.97960, 'word': 'Hugging Face', 'start': 33, 'end': 45}, \n", |
| 198 | + " {'entity_group': 'LOC', 'score': 0.99321, 'word': 'Brooklyn', 'start': 49, 'end': 57}\n", |
| 199 | + "]" |
| 200 | + ] |
| 201 | + }, |
| 202 | + "execution_count": null, |
| 203 | + "metadata": {}, |
| 204 | + "output_type": "execute_result" |
| 205 | + } |
| 206 | + ], |
| 207 | + "source": [ |
| 208 | + "from transformers import pipeline\n", |
| 209 | + "\n", |
| 210 | + "ner = pipeline(\"ner\", grouped_entities=True)\n", |
| 211 | + "ner(\"My name is Sylvain and I work at Hugging Face in Brooklyn.\")" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": null, |
| 217 | + "metadata": {}, |
| 218 | + "outputs": [ |
| 219 | + { |
| 220 | + "data": { |
| 221 | + "text/plain": [ |
| 222 | + "{'score': 0.6385916471481323, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}" |
| 223 | + ] |
| 224 | + }, |
| 225 | + "execution_count": null, |
| 226 | + "metadata": {}, |
| 227 | + "output_type": "execute_result" |
| 228 | + } |
| 229 | + ], |
| 230 | + "source": [ |
| 231 | + "from transformers import pipeline\n", |
| 232 | + "\n", |
| 233 | + "question_answerer = pipeline(\"question-answering\")\n", |
| 234 | + "question_answerer(\n", |
| 235 | + " question=\"Where do I work?\",\n", |
| 236 | + " context=\"My name is Sylvain and I work at Hugging Face in Brooklyn\",\n", |
| 237 | + ")" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "metadata": {}, |
| 244 | + "outputs": [ |
| 245 | + { |
| 246 | + "data": { |
| 247 | + "text/plain": [ |
| 248 | + "[{'summary_text': ' America has changed dramatically during recent years . The '\n", |
| 249 | + " 'number of engineering graduates in the U.S. has declined in '\n", |
| 250 | + " 'traditional engineering disciplines such as mechanical, civil '\n", |
| 251 | + " ', electrical, chemical, and aeronautical engineering . Rapidly '\n", |
| 252 | + " 'developing economies such as China and India, as well as other '\n", |
| 253 | + " 'industrial countries in Europe and Asia, continue to encourage '\n", |
| 254 | + " 'and advance engineering .'}]" |
| 255 | + ] |
| 256 | + }, |
| 257 | + "execution_count": null, |
| 258 | + "metadata": {}, |
| 259 | + "output_type": "execute_result" |
| 260 | + } |
| 261 | + ], |
| 262 | + "source": [ |
| 263 | + "from transformers import pipeline\n", |
| 264 | + "\n", |
| 265 | + "summarizer = pipeline(\"summarization\")\n", |
| 266 | + "summarizer(\n", |
| 267 | + " \"\"\"\n", |
| 268 | + " America has changed dramatically during recent years. Not only has the number of \n", |
| 269 | + " graduates in traditional engineering disciplines such as mechanical, civil, \n", |
| 270 | + " electrical, chemical, and aeronautical engineering declined, but in most of \n", |
| 271 | + " the premier American universities engineering curricula now concentrate on \n", |
| 272 | + " and encourage largely the study of engineering science. As a result, there \n", |
| 273 | + " are declining offerings in engineering subjects dealing with infrastructure, \n", |
| 274 | + " the environment, and related issues, and greater concentration on high \n", |
| 275 | + " technology subjects, largely supporting increasingly complex scientific \n", |
| 276 | + " developments. While the latter is important, it should not be at the expense \n", |
| 277 | + " of more traditional engineering.\n", |
| 278 | + "\n", |
| 279 | + " Rapidly developing economies such as China and India, as well as other \n", |
| 280 | + " industrial countries in Europe and Asia, continue to encourage and advance \n", |
| 281 | + " the teaching of engineering. Both China and India, respectively, graduate \n", |
| 282 | + " six and eight times as many traditional engineers as does the United States. \n", |
| 283 | + " Other industrial countries at minimum maintain their output, while America \n", |
| 284 | + " suffers an increasingly serious decline in the number of engineering graduates \n", |
| 285 | + " and a lack of well-educated engineers.\n", |
| 286 | + "\"\"\"\n", |
| 287 | + ")" |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "cell_type": "code", |
| 292 | + "execution_count": null, |
| 293 | + "metadata": {}, |
| 294 | + "outputs": [ |
| 295 | + { |
| 296 | + "data": { |
| 297 | + "text/plain": [ |
| 298 | + "[{'translation_text': 'This course is produced by Hugging Face.'}]" |
| 299 | + ] |
| 300 | + }, |
| 301 | + "execution_count": null, |
| 302 | + "metadata": {}, |
| 303 | + "output_type": "execute_result" |
| 304 | + } |
| 305 | + ], |
| 306 | + "source": [ |
| 307 | + "from transformers import pipeline\n", |
| 308 | + "\n", |
| 309 | + "translator = pipeline(\"translation\", model=\"Helsinki-NLP/opus-mt-fr-en\")\n", |
| 310 | + "translator(\"Ce cours est produit par Hugging Face.\")" |
| 311 | + ] |
| 312 | + } |
| 313 | + ], |
| 314 | + "metadata": { |
| 315 | + "colab": { |
| 316 | + "name": "Transformers, what can they do?", |
| 317 | + "provenance": [] |
| 318 | + } |
| 319 | + }, |
| 320 | + "nbformat": 4, |
| 321 | + "nbformat_minor": 4 |
| 322 | +} |
0 commit comments