txtai

Форк
0
/
08_API_Gallery.ipynb 
836 строк · 25.2 Кб
1
{
2
  "nbformat": 4,
3
  "nbformat_minor": 0,
4
  "metadata": {
5
    "colab": {
6
      "provenance": []
7
    },
8
    "kernelspec": {
9
      "display_name": "Python 3",
10
      "name": "python3"
11
    }
12
  },
13
  "cells": [
14
    {
15
      "cell_type": "markdown",
16
      "metadata": {
17
        "id": "4Pjmz-RORV8E"
18
      },
19
      "source": [
20
        "# API Gallery\n",
21
        "\n",
22
        "The txtai API is a web-based service backed by [FastAPI](https://fastapi.tiangolo.com/). All txtai functionality including similarity search, extractive QA and zero-shot labeling is available via the API.\n",
23
        "\n",
24
        "This notebook installs the txtai API and shows an example using each of the supported language bindings for txtai."
25
      ]
26
    },
27
    {
28
      "cell_type": "markdown",
29
      "metadata": {
30
        "id": "Dk31rbYjSTYm"
31
      },
32
      "source": [
33
        "# Install dependencies\n",
34
        "\n",
35
        "Install `txtai` and all dependencies. Since this notebook uses the API, we need to install the api extras package."
36
      ]
37
    },
38
    {
39
      "cell_type": "code",
40
      "metadata": {
41
        "id": "XMQuuun2R06J"
42
      },
43
      "source": [
44
        "%%capture\n",
45
        "!pip install git+https://github.com/neuml/txtai#egg=txtai[api]"
46
      ],
47
      "execution_count": null,
48
      "outputs": []
49
    },
50
    {
51
      "cell_type": "markdown",
52
      "metadata": {
53
        "id": "-vGR_piwZZO6"
54
      },
55
      "source": [
56
        "# Python\n",
57
        "\n",
58
        "The first method we'll try is direct access via Python. We'll use zero-shot labeling for all the examples here. See [this notebook](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/07_Apply_labels_with_zero_shot_classification.ipynb) for more details on zero-shot classification. "
59
      ]
60
    },
61
    {
62
      "cell_type": "markdown",
63
      "metadata": {
64
        "id": "P4q72tkRMMkR"
65
      },
66
      "source": [
67
        "## Configure Labels instance"
68
      ]
69
    },
70
    {
71
      "cell_type": "code",
72
      "metadata": {
73
        "id": "8Dy_TJ0iM38Q"
74
      },
75
      "source": [
76
        "%%capture\n",
77
        "import os\n",
78
        "from IPython.core.display import display, HTML\n",
79
        "from txtai.pipeline import Labels\n",
80
        "\n",
81
        "def table(rows):\n",
82
        "    html = \"\"\"\n",
83
        "    <style type='text/css'>\n",
84
        "    @import url('https://fonts.googleapis.com/css?family=Oswald&display=swap');\n",
85
        "    table {\n",
86
        "      border-collapse: collapse;\n",
87
        "      width: 900px;\n",
88
        "    }\n",
89
        "    th, td {\n",
90
        "        border: 1px solid #9e9e9e;\n",
91
        "        padding: 10px;\n",
92
        "        font: 20px Oswald;\n",
93
        "    }\n",
94
        "    </style>\n",
95
        "    \"\"\"\n",
96
        "\n",
97
        "    html += \"<table><thead><tr><th>Text</th><th>Label</th></tr></thead>\"\n",
98
        "    for text, label in rows:\n",
99
        "        html += \"<tr><td>%s</td><td>%s</td></tr>\" % (text, label)\n",
100
        "    html += \"</table>\"\n",
101
        "\n",
102
        "    display(HTML(html))\n",
103
        "\n",
104
        "# Create labels model\n",
105
        "labels = Labels()"
106
      ],
107
      "execution_count": null,
108
      "outputs": []
109
    },
110
    {
111
      "cell_type": "markdown",
112
      "metadata": {
113
        "id": "L4B73tGkMT6Q"
114
      },
115
      "source": [
116
        "## Apply labels to text"
117
      ]
118
    },
119
    {
120
      "cell_type": "code",
121
      "metadata": {
122
        "colab": {
123
          "base_uri": "https://localhost:8080/",
124
          "height": 324
125
        },
126
        "id": "-K2YJJzsVtfq",
127
        "outputId": "65782fd8-51fb-4531-8e8b-f28bca678fa0"
128
      },
129
      "source": [
130
        "data = [\"Wears a red suit and says ho ho\",\n",
131
        "        \"Pulls a flying sleigh\",\n",
132
        "        \"This is cut down and decorated\",\n",
133
        "        \"Santa puts these under the tree\",\n",
134
        "        \"Best way to spend the holidays\"]\n",
135
        "\n",
136
        "# List of labels\n",
137
        "tags = [\"🎅 Santa Clause\", \"🦌 Reindeer\", \"🍪 Cookies\", \"🎄 Christmas Tree\", \"🎁 Gifts\", \"👪 Family\"]\n",
138
        "\n",
139
        "# Render output to table\n",
140
        "table([(text, tags[labels(text, tags)[0][0]]) for text in data])"
141
      ],
142
      "execution_count": null,
143
      "outputs": [
144
        {
145
          "output_type": "display_data",
146
          "data": {
147
            "text/html": [
148
              "\n",
149
              "    <style type='text/css'>\n",
150
              "    @import url('https://fonts.googleapis.com/css?family=Oswald&display=swap');\n",
151
              "    table {\n",
152
              "      border-collapse: collapse;\n",
153
              "      width: 900px;\n",
154
              "    }\n",
155
              "    th, td {\n",
156
              "        border: 1px solid #9e9e9e;\n",
157
              "        padding: 10px;\n",
158
              "        font: 20px Oswald;\n",
159
              "    }\n",
160
              "    </style>\n",
161
              "    <table><thead><tr><th>Text</th><th>Label</th></tr></thead><tr><td>Wears a red suit and says ho ho</td><td>🎅 Santa Clause</td></tr><tr><td>Pulls a flying sleigh</td><td>🦌 Reindeer</td></tr><tr><td>This is cut down and decorated</td><td>🎄 Christmas Tree</td></tr><tr><td>Santa puts these under the tree</td><td>🎁 Gifts</td></tr><tr><td>Best way to spend the holidays</td><td>👪 Family</td></tr></table>"
162
            ],
163
            "text/plain": [
164
              "<IPython.core.display.HTML object>"
165
            ]
166
          },
167
          "metadata": {}
168
        }
169
      ]
170
    },
171
    {
172
      "cell_type": "markdown",
173
      "metadata": {
174
        "id": "UF_bImkLHTMs"
175
      },
176
      "source": [
177
        "Once again we see the power of zero-shot labeling. The model wasn't trained on any data specific to this example. Still amazed with how much knowledge is stored in large NLP models."
178
      ]
179
    },
180
    {
181
      "cell_type": "markdown",
182
      "metadata": {
183
        "id": "PNPJ95cdTKSS"
184
      },
185
      "source": [
186
        "# Start an API instance\n",
187
        "\n",
188
        "Now we'll start an API instance to run the remaining examples. The API needs a configuration file to run. The example below is simplified to only include labeling. See [this link](https://github.com/neuml/txtai#api) for a more detailed configuration example.\n",
189
        "\n",
190
        "The API instance is started in the background.\n"
191
      ]
192
    },
193
    {
194
      "cell_type": "code",
195
      "metadata": {
196
        "colab": {
197
          "base_uri": "https://localhost:8080/"
198
        },
199
        "id": "nTDwXOUeTH2-",
200
        "outputId": "2220a3c9-1cff-4c2f-b21e-13dd2d7cb816"
201
      },
202
      "source": [
203
        "%%writefile index.yml\n",
204
        "\n",
205
        "# Labels settings\n",
206
        "labels:"
207
      ],
208
      "execution_count": null,
209
      "outputs": [
210
        {
211
          "output_type": "stream",
212
          "name": "stdout",
213
          "text": [
214
            "Writing index.yml\n"
215
          ]
216
        }
217
      ]
218
    },
219
    {
220
      "cell_type": "code",
221
      "metadata": {
222
        "id": "nGITHxUyRzyp"
223
      },
224
      "source": [
225
        "!CONFIG=index.yml nohup uvicorn \"txtai.api:app\" &> api.log &\n",
226
        "!sleep 90"
227
      ],
228
      "execution_count": null,
229
      "outputs": []
230
    },
231
    {
232
      "cell_type": "markdown",
233
      "metadata": {
234
        "id": "NHvBFZeSd9AG"
235
      },
236
      "source": [
237
        "# JavaScript\n",
238
        "\n",
239
        "txtai.js is available via NPM and can be installed as follows.\n",
240
        "\n",
241
        "```bash\n",
242
        "npm install txtai\n",
243
        "```\n",
244
        "\n",
245
        "For this example, we'll clone the txtai.js project to import the example build configuration."
246
      ]
247
    },
248
    {
249
      "cell_type": "code",
250
      "metadata": {
251
        "id": "b52knObEdcCr"
252
      },
253
      "source": [
254
        "%%capture\n",
255
        "!git clone https://github.com/neuml/txtai.js"
256
      ],
257
      "execution_count": null,
258
      "outputs": []
259
    },
260
    {
261
      "cell_type": "markdown",
262
      "metadata": {
263
        "id": "rUGS0t-JMsS9"
264
      },
265
      "source": [
266
        "## Create labels.js\n",
267
        "\n",
268
        "The following file is a JavaScript version of the labels example."
269
      ]
270
    },
271
    {
272
      "cell_type": "code",
273
      "metadata": {
274
        "colab": {
275
          "base_uri": "https://localhost:8080/"
276
        },
277
        "id": "zJbKRTSJV-kd",
278
        "outputId": "6c111b5d-6e55-4dac-c6c2-0988c2a834da"
279
      },
280
      "source": [
281
        "%%writefile txtai.js/examples/node/src/labels.js\n",
282
        "import {Labels} from \"txtai\";\n",
283
        "import {sprintf} from \"sprintf-js\";\n",
284
        "\n",
285
        "const run = async () => {\n",
286
        "    try {\n",
287
        "        let labels = new Labels(\"http://localhost:8000\");\n",
288
        "\n",
289
        "        let data = [\"Wears a red suit and says ho ho\",\n",
290
        "                    \"Pulls a flying sleigh\",\n",
291
        "                    \"This is cut down and decorated\",\n",
292
        "                    \"Santa puts these under the tree\",\n",
293
        "                    \"Best way to spend the holidays\"];\n",
294
        "\n",
295
        "        // List of labels\n",
296
        "        let tags = [\"🎅 Santa Clause\", \"🦌 Reindeer\", \"🍪 Cookies\", \"🎄 Christmas Tree\", \"🎁 Gifts\", \"👪 Family\"];\n",
297
        "\n",
298
        "        console.log(sprintf(\"%-40s %s\", \"Text\", \"Label\"));\n",
299
        "        console.log(\"-\".repeat(75))\n",
300
        "\n",
301
        "        for (let text of data) {\n",
302
        "            let label = await labels.label(text, tags);\n",
303
        "            label = tags[label[0].id];\n",
304
        "\n",
305
        "            console.log(sprintf(\"%-40s %s\", text, label));\n",
306
        "        }\n",
307
        "    }\n",
308
        "    catch (e) {\n",
309
        "        console.trace(e);\n",
310
        "    }\n",
311
        "};\n",
312
        "\n",
313
        "run();\n"
314
      ],
315
      "execution_count": null,
316
      "outputs": [
317
        {
318
          "output_type": "stream",
319
          "name": "stdout",
320
          "text": [
321
            "Overwriting txtai.js/examples/node/src/labels.js\n"
322
          ]
323
        }
324
      ]
325
    },
326
    {
327
      "cell_type": "markdown",
328
      "metadata": {
329
        "id": "nTBs11j-GtD-"
330
      },
331
      "source": [
332
        "## Build and run labels example\n",
333
        "\n",
334
        "\n",
335
        "\n"
336
      ]
337
    },
338
    {
339
      "cell_type": "code",
340
      "metadata": {
341
        "id": "kC5Oub6wa1nK"
342
      },
343
      "source": [
344
        "%%capture\n",
345
        "os.chdir(\"txtai.js/examples/node\")\n",
346
        "!npm install\n",
347
        "!npm run build"
348
      ],
349
      "execution_count": null,
350
      "outputs": []
351
    },
352
    {
353
      "cell_type": "code",
354
      "metadata": {
355
        "colab": {
356
          "base_uri": "https://localhost:8080/"
357
        },
358
        "id": "ckOHNqyaeL-B",
359
        "outputId": "6d8e745c-52d1-4456-fc46-2ff8fda2e675"
360
      },
361
      "source": [
362
        "!node dist/labels.js"
363
      ],
364
      "execution_count": null,
365
      "outputs": [
366
        {
367
          "output_type": "stream",
368
          "name": "stdout",
369
          "text": [
370
            "Text                                     Label\n",
371
            "---------------------------------------------------------------------------\n",
372
            "Wears a red suit and says ho ho          🎅 Santa Clause\n",
373
            "Pulls a flying sleigh                    🦌 Reindeer\n",
374
            "This is cut down and decorated           🎄 Christmas Tree\n",
375
            "Santa puts these under the tree          🎁 Gifts\n",
376
            "Best way to spend the holidays           👪 Family\n"
377
          ]
378
        }
379
      ]
380
    },
381
    {
382
      "cell_type": "markdown",
383
      "metadata": {
384
        "id": "1yukBIMYG5OE"
385
      },
386
      "source": [
387
        "The JavaScript program is showing the same results as when natively running through Python!"
388
      ]
389
    },
390
    {
391
      "cell_type": "markdown",
392
      "metadata": {
393
        "id": "nNiMgvg0p2BG"
394
      },
395
      "source": [
396
        "# Java\n",
397
        "\n",
398
        "txtai.java integrates with standard Java build tools (Gradle, Maven, SBT). The following shows how to add txtai as a dependency to Gradle.\n",
399
        "\n",
400
        "```gradle\n",
401
        "implementation 'com.github.neuml:txtai.java:v4.0.0'\n",
402
        "```\n",
403
        "\n",
404
        "For this example, we'll clone the txtai.java project to import the example build configuration."
405
      ]
406
    },
407
    {
408
      "cell_type": "code",
409
      "metadata": {
410
        "id": "qs2ai8lhqmga"
411
      },
412
      "source": [
413
        "%%capture\n",
414
        "os.chdir(\"/content\")\n",
415
        "!git clone https://github.com/neuml/txtai.java"
416
      ],
417
      "execution_count": null,
418
      "outputs": []
419
    },
420
    {
421
      "cell_type": "markdown",
422
      "metadata": {
423
        "id": "o8QFvzXkNFgq"
424
      },
425
      "source": [
426
        "## Create LabelsDemo.java\n",
427
        "\n",
428
        "The following file is a Java version of the labels example."
429
      ]
430
    },
431
    {
432
      "cell_type": "code",
433
      "metadata": {
434
        "colab": {
435
          "base_uri": "https://localhost:8080/"
436
        },
437
        "id": "v73L8Gw0p6fh",
438
        "outputId": "a7f797f2-a91f-4033-89c7-4baf76204d93"
439
      },
440
      "source": [
441
        "%%writefile txtai.java/examples/src/main/java/LabelsDemo.java\n",
442
        "import java.util.Arrays;\n",
443
        "import java.util.ArrayList;\n",
444
        "import java.util.List;\n",
445
        "\n",
446
        "import txtai.API.IndexResult;\n",
447
        "import txtai.Labels;\n",
448
        "\n",
449
        "public class LabelsDemo {\n",
450
        "    public static void main(String[] args) {\n",
451
        "        try {\n",
452
        "            Labels labels = new Labels(\"http://localhost:8000\");\n",
453
        "\n",
454
        "            List <String> data = \n",
455
        "                Arrays.asList(\"Wears a red suit and says ho ho\",\n",
456
        "                              \"Pulls a flying sleigh\",\n",
457
        "                              \"This is cut down and decorated\",\n",
458
        "                              \"Santa puts these under the tree\",\n",
459
        "                              \"Best way to spend the holidays\");\n",
460
        "\n",
461
        "            // List of labels\n",
462
        "            List<String> tags = Arrays.asList(\"🎅 Santa Clause\", \"🦌 Reindeer\", \"🍪 Cookies\", \"🎄 Christmas Tree\", \"🎁 Gifts\", \"👪 Family\");\n",
463
        "\n",
464
        "            System.out.printf(\"%-40s %s%n\", \"Text\", \"Label\");\n",
465
        "            System.out.println(new String(new char[75]).replace(\"\\0\", \"-\"));\n",
466
        "\n",
467
        "            for (String text: data) {\n",
468
        "                List<IndexResult> label = labels.label(text, tags);\n",
469
        "                System.out.printf(\"%-40s %s%n\", text, tags.get(label.get(0).id));\n",
470
        "            }\n",
471
        "        }\n",
472
        "        catch (Exception ex) {\n",
473
        "            ex.printStackTrace();\n",
474
        "        }\n",
475
        "    }\n",
476
        "}\n"
477
      ],
478
      "execution_count": null,
479
      "outputs": [
480
        {
481
          "output_type": "stream",
482
          "name": "stdout",
483
          "text": [
484
            "Overwriting txtai.java/examples/src/main/java/LabelsDemo.java\n"
485
          ]
486
        }
487
      ]
488
    },
489
    {
490
      "cell_type": "markdown",
491
      "metadata": {
492
        "id": "wZv7eMIOLnRC"
493
      },
494
      "source": [
495
        "## Build and run labels example"
496
      ]
497
    },
498
    {
499
      "cell_type": "code",
500
      "metadata": {
501
        "colab": {
502
          "base_uri": "https://localhost:8080/"
503
        },
504
        "id": "N2Mm3Gl5sH1z",
505
        "outputId": "b5249daf-e5a1-4b71-b64c-2b3c6748e846"
506
      },
507
      "source": [
508
        "os.chdir(\"txtai.java/examples\")\n",
509
        "!../gradlew -q --console=plain labels 2> /dev/null"
510
      ],
511
      "execution_count": null,
512
      "outputs": [
513
        {
514
          "output_type": "stream",
515
          "name": "stdout",
516
          "text": [
517
            "Text                                     Label\n",
518
            "---------------------------------------------------------------------------\n",
519
            "Wears a red suit and says ho ho          🎅 Santa Clause\n",
520
            "Pulls a flying sleigh                    🦌 Reindeer\n",
521
            "This is cut down and decorated           🎄 Christmas Tree\n",
522
            "Santa puts these under the tree          🎁 Gifts\n",
523
            "Best way to spend the holidays           👪 Family\n",
524
            "\u001b[m"
525
          ]
526
        }
527
      ]
528
    },
529
    {
530
      "cell_type": "markdown",
531
      "metadata": {
532
        "id": "iHpQvUAgNp7j"
533
      },
534
      "source": [
535
        "The Java program is showing the same results as when natively running through Python!"
536
      ]
537
    },
538
    {
539
      "cell_type": "markdown",
540
      "metadata": {
541
        "id": "zU6jK2UL7D5H"
542
      },
543
      "source": [
544
        "# Rust\n",
545
        "\n",
546
        "txtai.rs is available via crates.io and can be installed by adding the following to your cargo.toml file.\n",
547
        "\n",
548
        "```toml\n",
549
        "[dependencies]\n",
550
        "txtai = { version = \"4.0\" }\n",
551
        "tokio = { version = \"0.2\", features = [\"full\"] }\n",
552
        "```\n",
553
        "\n",
554
        "For this example, we'll clone the txtai.rs project to import the example build configuration. First we need to install Rust."
555
      ]
556
    },
557
    {
558
      "cell_type": "code",
559
      "metadata": {
560
        "id": "Ob4aswkx7jRh"
561
      },
562
      "source": [
563
        "%%capture\n",
564
        "os.chdir(\"/content\")\n",
565
        "!apt-get install rustc\n",
566
        "!git clone https://github.com/neuml/txtai.rs"
567
      ],
568
      "execution_count": null,
569
      "outputs": []
570
    },
571
    {
572
      "cell_type": "markdown",
573
      "metadata": {
574
        "id": "evEQQXBuObZn"
575
      },
576
      "source": [
577
        "## Create labels.rs\n",
578
        "\n",
579
        "The following file is a Rust version of the labels example."
580
      ]
581
    },
582
    {
583
      "cell_type": "code",
584
      "metadata": {
585
        "colab": {
586
          "base_uri": "https://localhost:8080/"
587
        },
588
        "id": "jjggKnKQ7jQO",
589
        "outputId": "76a2b1d9-2889-47b0-a3af-5d71a763bb0b"
590
      },
591
      "source": [
592
        "%%writefile txtai.rs/examples/demo/src/labels.rs\n",
593
        "use std::error::Error;\n",
594
        "\n",
595
        "use txtai::labels::Labels;\n",
596
        "\n",
597
        "pub async fn labels() -> Result<(), Box<dyn Error>> {\n",
598
        "    let labels = Labels::new(\"http://localhost:8000\");\n",
599
        "\n",
600
        "    let data = [\"Wears a red suit and says ho ho\",\n",
601
        "                \"Pulls a flying sleigh\",\n",
602
        "                \"This is cut down and decorated\",\n",
603
        "                \"Santa puts these under the tree\",\n",
604
        "                \"Best way to spend the holidays\"];\n",
605
        "\n",
606
        "    println!(\"{:<40} {}\", \"Text\", \"Label\");\n",
607
        "    println!(\"{}\", \"-\".repeat(75));\n",
608
        "\n",
609
        "    for text in data.iter() {\n",
610
        "        let tags = vec![\"🎅 Santa Clause\", \"🦌 Reindeer\", \"🍪 Cookies\", \"🎄 Christmas Tree\", \"🎁 Gifts\", \"👪 Family\"];\n",
611
        "        let label = labels.label(text, &tags).await?[0].id;\n",
612
        "\n",
613
        "        println!(\"{:<40} {}\", text, tags[label]);\n",
614
        "    }\n",
615
        "\n",
616
        "    Ok(())\n",
617
        "}"
618
      ],
619
      "execution_count": null,
620
      "outputs": [
621
        {
622
          "output_type": "stream",
623
          "name": "stdout",
624
          "text": [
625
            "Overwriting txtai.rs/examples/demo/src/labels.rs\n"
626
          ]
627
        }
628
      ]
629
    },
630
    {
631
      "cell_type": "markdown",
632
      "metadata": {
633
        "id": "gFFPZO8sQZC4"
634
      },
635
      "source": [
636
        "## Build and run labels example\n",
637
        "\n",
638
        "\n",
639
        "\n"
640
      ]
641
    },
642
    {
643
      "cell_type": "code",
644
      "metadata": {
645
        "id": "wuoAidGz9T4g"
646
      },
647
      "source": [
648
        "%%capture\n",
649
        "os.chdir(\"txtai.rs/examples/demo\")\n",
650
        "!cargo build"
651
      ],
652
      "execution_count": null,
653
      "outputs": []
654
    },
655
    {
656
      "cell_type": "code",
657
      "metadata": {
658
        "colab": {
659
          "base_uri": "https://localhost:8080/"
660
        },
661
        "id": "-_v_FbL0-yPk",
662
        "outputId": "821333f5-5f90-4f89-c2eb-673c2e14e4fe"
663
      },
664
      "source": [
665
        "!cargo run labels"
666
      ],
667
      "execution_count": null,
668
      "outputs": [
669
        {
670
          "output_type": "stream",
671
          "name": "stdout",
672
          "text": [
673
            "\u001b[0m\u001b[0m\u001b[1m\u001b[32m    Finished\u001b[0m dev [unoptimized + debuginfo] target(s) in 0.07s\n",
674
            "\u001b[0m\u001b[0m\u001b[1m\u001b[32m     Running\u001b[0m `target/debug/demo labels`\n",
675
            "Text                                     Label\n",
676
            "---------------------------------------------------------------------------\n",
677
            "Wears a red suit and says ho ho          🎅 Santa Clause\n",
678
            "Pulls a flying sleigh                    🦌 Reindeer\n",
679
            "This is cut down and decorated           🎄 Christmas Tree\n",
680
            "Santa puts these under the tree          🎁 Gifts\n",
681
            "Best way to spend the holidays           👪 Family\n"
682
          ]
683
        }
684
      ]
685
    },
686
    {
687
      "cell_type": "markdown",
688
      "metadata": {
689
        "id": "kDmS89TPS3kb"
690
      },
691
      "source": [
692
        "The Rust program is showing the same results as when natively running through Python!"
693
      ]
694
    },
695
    {
696
      "cell_type": "markdown",
697
      "metadata": {
698
        "id": "ezznN4I8_CCQ"
699
      },
700
      "source": [
701
        "# Go\n",
702
        "\n",
703
        "txtai.go can be installed by adding the following import statement. When using modules, txtai.go will automatically be installed. Otherwise use `go get`.\n",
704
        "\n",
705
        "```golang\n",
706
        "import \"github.com/neuml/txtai.go\"\n",
707
        "```\n",
708
        "\n",
709
        "For this example, we'll create a standalone process for labeling. First we need to install Go."
710
      ]
711
    },
712
    {
713
      "cell_type": "code",
714
      "metadata": {
715
        "id": "b-b6fhLQ_DpQ"
716
      },
717
      "source": [
718
        "%%capture\n",
719
        "os.chdir(\"/content\")\n",
720
        "!apt install golang-go\n",
721
        "!go get \"github.com/neuml/txtai.go\""
722
      ],
723
      "execution_count": null,
724
      "outputs": []
725
    },
726
    {
727
      "cell_type": "markdown",
728
      "metadata": {
729
        "id": "Dw-I6jOGR6vA"
730
      },
731
      "source": [
732
        "## Create labels.go\n",
733
        "\n",
734
        "The following file is a Go version of the labels example."
735
      ]
736
    },
737
    {
738
      "cell_type": "code",
739
      "metadata": {
740
        "id": "bLBJwkN4ANpi",
741
        "colab": {
742
          "base_uri": "https://localhost:8080/"
743
        },
744
        "outputId": "883ea7b2-2fbc-471c-e0bb-59ef5172a6a4"
745
      },
746
      "source": [
747
        "%%writefile labels.go\n",
748
        "package main\n",
749
        "\n",
750
        "import (\n",
751
        "\t\"fmt\"\n",
752
        "\t\"strings\"\n",
753
        "\t\"github.com/neuml/txtai.go\"\n",
754
        ")\n",
755
        "\n",
756
        "func main() {\n",
757
        "\tlabels := txtai.Labels(\"http://localhost:8000\")\n",
758
        "\n",
759
        "\tdata := []string{\"Wears a red suit and says ho ho\",\n",
760
        "                   \"Pulls a flying sleigh\",\n",
761
        "                   \"This is cut down and decorated\",\n",
762
        "                   \"Santa puts these under the tree\",\n",
763
        "                   \"Best way to spend the holidays\"}\n",
764
        "\n",
765
        "\t// List of labels\n",
766
        "\ttags := []string{\"🎅 Santa Clause\", \"🦌 Reindeer\", \"🍪 Cookies\", \"🎄 Christmas Tree\", \"🎁 Gifts\", \"👪 Family\"}\n",
767
        "\n",
768
        "\tfmt.Printf(\"%-40s %s\\n\", \"Text\", \"Label\")\n",
769
        "\tfmt.Println(strings.Repeat(\"-\", 75))\n",
770
        "\n",
771
        "\tfor _, text := range data {\n",
772
        "\t\tlabel := labels.Label(text, tags)\n",
773
        "\t\tfmt.Printf(\"%-40s %s\\n\", text, tags[label[0].Id])\n",
774
        "\t}\n",
775
        "}"
776
      ],
777
      "execution_count": null,
778
      "outputs": [
779
        {
780
          "output_type": "stream",
781
          "name": "stdout",
782
          "text": [
783
            "Writing labels.go\n"
784
          ]
785
        }
786
      ]
787
    },
788
    {
789
      "cell_type": "markdown",
790
      "metadata": {
791
        "id": "PJ2XzzDbSeZh"
792
      },
793
      "source": [
794
        "## Build and run labels example\n"
795
      ]
796
    },
797
    {
798
      "cell_type": "code",
799
      "metadata": {
800
        "id": "l1xnUbtdAy0p",
801
        "colab": {
802
          "base_uri": "https://localhost:8080/"
803
        },
804
        "outputId": "5bc6015c-5c9c-4d8a-daf7-6897ec6cbd80"
805
      },
806
      "source": [
807
        "!go run labels.go"
808
      ],
809
      "execution_count": null,
810
      "outputs": [
811
        {
812
          "output_type": "stream",
813
          "name": "stdout",
814
          "text": [
815
            "Text                                     Label\n",
816
            "---------------------------------------------------------------------------\n",
817
            "Wears a red suit and says ho ho          🎅 Santa Clause\n",
818
            "Pulls a flying sleigh                    🦌 Reindeer\n",
819
            "This is cut down and decorated           🎄 Christmas Tree\n",
820
            "Santa puts these under the tree          🎁 Gifts\n",
821
            "Best way to spend the holidays           👪 Family\n"
822
          ]
823
        }
824
      ]
825
    },
826
    {
827
      "cell_type": "markdown",
828
      "metadata": {
829
        "id": "oml43X5eS6YB"
830
      },
831
      "source": [
832
        "The Go program is showing the same results as when natively running through Python!"
833
      ]
834
    }
835
  ]
836
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.