Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Refreshed example notebooks, added notebook for labeling. Closes #40

davidmezzetti223245bae099
diff --git a/examples/01_Introducing_txtai.ipynb b/examples/01_Introducing_txtai.ipynb
index e063bc0e..4aefbca9 100644
--- a/examples/01_Introducing_txtai.ipynb
+++ b/examples/01_Introducing_txtai.ipynb
@@ -28,8 +28,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "POWZoSJR6XzK",
- "colab_type": "text"
+ "id": "POWZoSJR6XzK"
},
"source": [
"# Part 1: Introducing txtai\n",
@@ -38,8 +37,8 @@
"\n",
"NeuML uses txtai and/or the concepts behind it to power all of our Natural Language Processing (NLP) applications. Example applications:\n",
"\n",
- "- [cord19q](https://github.com/neuml/cord19q) - COVID-19 literature analysis\n",
"- [paperai](https://github.com/neuml/paperai) - AI-powered literature discovery and review engine for medical/scientific papers\n",
+ "- [tldrstory](https://github.com/neuml/tldrstory) - AI-powered understanding of headlines and story text\n",
"- [neuspo](https://neuspo.com) - a fact-driven, real-time sports event and news site\n",
"- [codequestion](https://github.com/neuml/codequestion) - Ask coding questions directly from the terminal\n",
"\n",
@@ -56,8 +55,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "qa_PPKVX6XzN",
- "colab_type": "text"
+ "id": "qa_PPKVX6XzN"
},
"source": [
"# Install dependencies\n",
@@ -72,9 +70,7 @@
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
"trusted": true,
"_kg_hide-output": true,
- "id": "24q-1n5i6XzQ",
- "colab_type": "code",
- "colab": {}
+ "id": "24q-1n5i6XzQ"
},
"source": [
"%%capture\n",
@@ -86,8 +82,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "vHxb_jm16Xzd",
- "colab_type": "text"
+ "id": "vHxb_jm16Xzd"
},
"source": [
"# Create an Embeddings instance\n",
@@ -99,9 +94,7 @@
"cell_type": "code",
"metadata": {
"trusted": true,
- "id": "QxX9EtIc6Xzg",
- "colab_type": "code",
- "colab": {}
+ "id": "QxX9EtIc6Xzg"
},
"source": [
"%%capture\n",
@@ -117,8 +110,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "JAO61bcy6Xzo",
- "colab_type": "text"
+ "id": "JAO61bcy6Xzo"
},
"source": [
"# Running similarity queries\n",
@@ -133,12 +125,10 @@
"_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
"trusted": true,
"id": "2j_CFGDR6Xzp",
- "colab_type": "code",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 191
+ "base_uri": "https://localhost:8080/"
},
- "outputId": "a92c2d3e-1b6d-473a-bc7e-7adc067fa868"
+ "outputId": "b481b5d9-3c61-413a-a0d8-472f30c8d2d1"
},
"source": [
"import numpy as np\n",
@@ -182,8 +172,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "kIMbLW0t6Xzw",
- "colab_type": "text"
+ "id": "kIMbLW0t6Xzw"
},
"source": [
"The example above shows for almost all of the queries, the actual text isn't stored in the list of text sections. This is the true power of transformer models over token based search. What you get out of the box is 🔥🔥🔥!"
@@ -192,13 +181,12 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "DLIjSzbq6Xzx",
- "colab_type": "text"
+ "id": "DLIjSzbq6Xzx"
},
"source": [
"# Building an Embeddings index\n",
"\n",
- "For small lists of texts, the method above works. But for larger repositories of documents, it doesn't make sense to tokenize and convert to embeddings on each query. txtai supports building pre-computed indices which significantly improve performance. \n",
+ "For small lists of texts, the method above works. But for larger repositories of documents, it doesn't make sense to tokenize and convert to embeddings on each query. txtai supports building pre-computed indices which signficantly improve performance. \n",
"\n",
"Building on the previous example, the following example runs an index method to build and store the text embeddings. In this case, only the query is converted to an embeddings vector each search. "
]
@@ -208,12 +196,10 @@
"metadata": {
"trusted": true,
"id": "cXfZtdHD6Xzy",
- "colab_type": "code",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 191
+ "base_uri": "https://localhost:8080/"
},
- "outputId": "eca8eb29-7ac8-445b-f9b9-6b5cea3280a5"
+ "outputId": "bd7f32c4-7905-4de3-c8c2-8c6c8bdf5a77"
},
"source": [
"# Create an index for the list of sections\n",
@@ -254,8 +240,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "6TCVl6QA6Xz5",
- "colab_type": "text"
+ "id": "6TCVl6QA6Xz5"
},
"source": [
"# Embeddings load/save\n",
@@ -268,12 +253,10 @@
"metadata": {
"trusted": true,
"id": "5gyO90Hc6Xz7",
- "colab_type": "code",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 34
+ "base_uri": "https://localhost:8080/"
},
- "outputId": "92e1358d-4dcf-4982-8811-061f0bfa9a88"
+ "outputId": "85314afd-0772-49ef-9b94-e4a0bedc4153"
},
"source": [
"embeddings.save(\"index\")\n",
@@ -300,12 +283,10 @@
"metadata": {
"trusted": true,
"id": "d2uCotAE6X0J",
- "colab_type": "code",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 34
+ "base_uri": "https://localhost:8080/"
},
- "outputId": "14f16d54-ca02-4099-9478-24ba8054d3e3"
+ "outputId": "09393584-f149-4a61-94bb-9e5fbf309132"
},
"source": [
"!ls index"
@@ -324,8 +305,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "aDIF3tYt6X0O",
- "colab_type": "text"
+ "id": "aDIF3tYt6X0O"
},
"source": [
"# Embedding methods\n",
@@ -347,8 +327,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "0Nw7nbHg6X0P",
- "colab_type": "text"
+ "id": "0Nw7nbHg6X0P"
},
"source": [
"# Next\n",
@@ -356,4 +335,4 @@
]
}
]
-}
+}
\ No newline at end of file
diff --git a/examples/02_Extractive_QA_with_txtai.ipynb b/examples/02_Extractive_QA_with_txtai.ipynb
index 78be37b9..f6518bd5 100644
--- a/examples/02_Extractive_QA_with_txtai.ipynb
+++ b/examples/02_Extractive_QA_with_txtai.ipynb
@@ -15,8 +15,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "vwELCooy4ljr",
- "colab_type": "text"
+ "id": "vwELCooy4ljr"
},
"source": [
"# Part 2: Extractive QA with txtai\n",
@@ -27,8 +26,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "ew7orE2O441o",
- "colab_type": "text"
+ "id": "ew7orE2O441o"
},
"source": [
"# Install dependencies\n",
@@ -39,9 +37,7 @@
{
"cell_type": "code",
"metadata": {
- "id": "LPQTb25tASIG",
- "colab_type": "code",
- "colab": {}
+ "id": "LPQTb25tASIG"
},
"source": [
"%%capture\n",
@@ -53,8 +49,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "_YnqorRKAbLu",
- "colab_type": "text"
+ "id": "_YnqorRKAbLu"
},
"source": [
"# Create an Embeddings and Extractor instances\n",
@@ -69,9 +64,7 @@
{
"cell_type": "code",
"metadata": {
- "id": "OUc9gqTyAYnm",
- "colab_type": "code",
- "colab": {}
+ "id": "OUc9gqTyAYnm"
},
"source": [
"%%capture\n",
@@ -92,12 +85,10 @@
"cell_type": "code",
"metadata": {
"id": "4X5z3UjnAGe7",
- "colab_type": "code",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 330
+ "base_uri": "https://localhost:8080/"
},
- "outputId": "4e376662-57d1-4841-eeb5-622aaaf286c3"
+ "outputId": "d3427fe6-b04d-4cde-a7e4-824fc1430447"
},
"source": [
"sections = [\"Giants hit 3 HRs to down Dodgers\",\n",
@@ -163,8 +154,7 @@
{
"cell_type": "markdown",
"metadata": {
- "id": "k-9gHzrp8ZXA",
- "colab_type": "text"
+ "id": "k-9gHzrp8ZXA"
},
"source": [
"# Next\n",
diff --git a/examples/03_Build_an_Embeddings_index_from_a_data_source.ipynb b/examples/03_Build_an_Embeddings_index_from_a_data_source.ipynb
index 14226870..0f5be883 100644
--- a/examples/03_Build_an_Embeddings_index_from_a_data_source.ipynb
+++ b/examples/03_Build_an_Embeddings_index_from_a_data_source.ipynb
@@ -68,11 +68,10 @@
"cell_type": "code",
"metadata": {
"id": "TONQ4_Kv9dtd",
- "outputId": "5d7b5fdf-388c-43c2-c2ac-68dac1028811",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 315
- }
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "f0c7ee46-f427-458e-f3d3-913cd13bd473"
},
"source": [
"!wget https://github.com/neuml/txtai/releases/download/v1.1.0/tests.gz\n",
@@ -84,21 +83,21 @@
{
"output_type": "stream",
"text": [
- "--2020-10-20 21:43:52-- https://github.com/neuml/txtai/releases/download/v1.1.0/tests.gz\n",
- "Resolving github.com (github.com)... 192.30.255.112\n",
- "Connecting to github.com (github.com)|192.30.255.112|:443... connected.\n",
+ "--2020-11-20 17:28:20-- https://github.com/neuml/txtai/releases/download/v1.1.0/tests.gz\n",
+ "Resolving github.com (github.com)... 140.82.113.3\n",
+ "Connecting to github.com (github.com)|140.82.113.3|:443... connected.\n",
"HTTP request sent, awaiting response... 302 Found\n",
- "Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201020%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201020T214229Z&X-Amz-Expires=300&X-Amz-Signature=4add756b1621527d4e29cc454f5e0d1546f7f2fa145c03d3b706a8ea75e84aa4&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream [following]\n",
- "--2020-10-20 21:43:52-- https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201020%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201020T214229Z&X-Amz-Expires=300&X-Amz-Signature=4add756b1621527d4e29cc454f5e0d1546f7f2fa145c03d3b706a8ea75e84aa4&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream\n",
- "Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.32.148\n",
- "Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.32.148|:443... connected.\n",
+ "Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201120T172820Z&X-Amz-Expires=300&X-Amz-Signature=6fc939f24fb1b982c6c87602c2ed115447314011f9c8eda31f2a107b3d374d54&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream [following]\n",
+ "--2020-11-20 17:28:20-- https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201120T172820Z&X-Amz-Expires=300&X-Amz-Signature=6fc939f24fb1b982c6c87602c2ed115447314011f9c8eda31f2a107b3d374d54&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream\n",
+ "Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.163.3\n",
+ "Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.163.3|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 2456199 (2.3M) [application/octet-stream]\n",
"Saving to: ‘tests.gz’\n",
"\n",
- "tests.gz 100%[===================>] 2.34M 5.23MB/s in 0.4s \n",
+ "tests.gz 100%[===================>] 2.34M --.-KB/s in 0.1s \n",
"\n",
- "2020-10-20 21:43:53 (5.23 MB/s) - ‘tests.gz’ saved [2456199/2456199]\n",
+ "2020-11-20 17:28:20 (22.9 MB/s) - ‘tests.gz’ saved [2456199/2456199]\n",
"\n"
],
"name": "stdout"
@@ -125,11 +124,10 @@
"cell_type": "code",
"metadata": {
"id": "fJcn-CAH-u3K",
- "outputId": "72feece5-e3e6-4c67-8d83-fe4b73dc2064",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 156
- }
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "af389b28-4016-4d1b-8ade-643c1b6ef8dd"
},
"source": [
"import os\n",
@@ -175,9 +173,9 @@
"Converting vectors to magnitude format\n",
"total 78948\n",
"-rw-r--r-- 1 root root 8065024 Aug 25 01:44 articles.sqlite\n",
- "-rw-r--r-- 1 root root 24145920 Oct 20 21:46 cord19-300d.magnitude\n",
- "-rw-r--r-- 1 root root 48625387 Oct 20 21:46 cord19-300d.txt\n",
- "drwxr-xr-x 1 root root 4096 Oct 14 16:31 sample_data\n"
+ "-rw-r--r-- 1 root root 24145920 Nov 20 17:31 cord19-300d.magnitude\n",
+ "-rw-r--r-- 1 root root 48625387 Nov 20 17:31 cord19-300d.txt\n",
+ "drwxr-xr-x 1 root root 4096 Nov 13 17:33 sample_data\n"
],
"name": "stdout"
}
@@ -198,11 +196,10 @@
"cell_type": "code",
"metadata": {
"id": "5PrrxGRPGHqX",
- "outputId": "7c56c254-d0cd-452a-85ad-d6be2b2f454e",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 52
- }
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "dae51d29-676f-41b4-c9a0-0e2b7e837405"
},
"source": [
"import sqlite3\n",
@@ -284,11 +281,11 @@
"cell_type": "code",
"metadata": {
"id": "CRbDhvvDKEl-",
- "outputId": "cda3fdff-bd63-4de3-b20f-7b79832e0ce2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 293
- }
+ },
+ "outputId": "b982c2b4-97fd-4d22-b275-78eeba3ba04e"
},
"source": [
"import pandas as pd\n",
@@ -412,11 +409,11 @@
"cell_type": "code",
"metadata": {
"id": "19fmKawThs6d",
- "outputId": "4092bbbe-f152-4471-f69a-c8097eb74ea1",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 293
- }
+ },
+ "outputId": "550db0d6-2a86-4766-8bf0-14c18f8eb5f3"
},
"source": [
"db = sqlite3.connect(\"articles.sqlite\")\n",
diff --git a/examples/04_Extractive_QA_with_Elasticsearch.ipynb b/examples/04_Extractive_QA_with_Elasticsearch.ipynb
index 6c28bc0b..1b790cdf 100644
--- a/examples/04_Extractive_QA_with_Elasticsearch.ipynb
+++ b/examples/04_Extractive_QA_with_Elasticsearch.ipynb
@@ -4,7 +4,8 @@
"metadata": {
"colab": {
"name": "04 - Extractive QA with Elasticsearch",
- "provenance": []
+ "provenance": [],
+ "collapsed_sections": []
},
"kernelspec": {
"name": "python3",
@@ -89,11 +90,10 @@
"cell_type": "code",
"metadata": {
"id": "8tVrIqSq-KBa",
- "outputId": "13d7c544-f0ff-4930-8a84-936fc8d9d68a",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 315
- }
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "f9ccc4f4-dad6-477a-e149-0f0bc3649be7"
},
"source": [
"!wget https://github.com/neuml/txtai/releases/download/v1.1.0/tests.gz\n",
@@ -105,21 +105,21 @@
{
"output_type": "stream",
"text": [
- "--2020-10-20 22:00:03-- https://github.com/neuml/txtai/releases/download/v1.1.0/tests.gz\n",
- "Resolving github.com (github.com)... 52.69.186.44\n",
- "Connecting to github.com (github.com)|52.69.186.44|:443... connected.\n",
+ "--2020-11-20 17:29:03-- https://github.com/neuml/txtai/releases/download/v1.1.0/tests.gz\n",
+ "Resolving github.com (github.com)... 140.82.112.4\n",
+ "Connecting to github.com (github.com)|140.82.112.4|:443... connected.\n",
"HTTP request sent, awaiting response... 302 Found\n",
- "Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201020%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201020T220003Z&X-Amz-Expires=300&X-Amz-Signature=171812f33d30980a4ab40547678d0ca1622cb5e884ab9ce18cff2763b7d28606&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream [following]\n",
- "--2020-10-20 22:00:03-- https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201020%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201020T220003Z&X-Amz-Expires=300&X-Amz-Signature=171812f33d30980a4ab40547678d0ca1622cb5e884ab9ce18cff2763b7d28606&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream\n",
- "Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.133.75\n",
- "Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.133.75|:443... connected.\n",
+ "Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201120T172904Z&X-Amz-Expires=300&X-Amz-Signature=b7134321c17bee4f62cf6efdb2e3af1d0f97a294dc718450612ef543a7098cf0&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream [following]\n",
+ "--2020-11-20 17:29:03-- https://github-production-release-asset-2e65be.s3.amazonaws.com/286301447/080d8800-e653-11ea-8d02-c0c858a09e7a?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201120T172904Z&X-Amz-Expires=300&X-Amz-Signature=b7134321c17bee4f62cf6efdb2e3af1d0f97a294dc718450612ef543a7098cf0&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=286301447&response-content-disposition=attachment%3B%20filename%3Dtests.gz&response-content-type=application%2Foctet-stream\n",
+ "Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.108.36\n",
+ "Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.108.36|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 2456199 (2.3M) [application/octet-stream]\n",
"Saving to: ‘tests.gz’\n",
"\n",
- "tests.gz 100%[===================>] 2.34M 1.87MB/s in 1.3s \n",
+ "tests.gz 100%[===================>] 2.34M --.-KB/s in 0.1s \n",
"\n",
- "2020-10-20 22:00:05 (1.87 MB/s) - ‘tests.gz’ saved [2456199/2456199]\n",
+ "2020-11-20 17:29:04 (21.3 MB/s) - ‘tests.gz’ saved [2456199/2456199]\n",
"\n"
],
"name": "stdout"
@@ -141,11 +141,10 @@
"cell_type": "code",
"metadata": {
"id": "So-OBvUT61QD",
- "outputId": "c9059774-1c3f-49bd-9d6c-4bd060e6bfb2",
"colab": {
- "base_uri": "https://localhost:8080/",
- "height": 34
- }
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "0521f5c1-14d2-4a0a-aa11-82de676a7f5a"
},
"source": [
"import sqlite3\n",
@@ -223,11 +222,11 @@
"cell_type": "code",
"metadata": {
"id": "ucd9mwSfFTMm",
- "outputId": "7609d9dc-f169-49a3-fbbc-2c22e0f41b1a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 293
- }
+ },
+ "outputId": "2d70a5d6-61b6-49f7-d55d-ee3ac1b120db"
},
"source": [
"import pandas as pd\n",
@@ -270,6 +269,12 @@
" </thead>\n",
" <tbody>\n",
" <tr>\n",
+ " <td>Management of osteoarthritis during COVID‐19 pandemic</td>\n",
+ " <td>2020-05-21 00:00:00</td>\n",
+ " <td>https://doi.org/10.1002/cpt.1910</td>\n",
+ " <td>Indeed, risk factors are sex, obesity, genetic factors and mechanical factors (3) .</td>\n",
+ " </tr>\n",
+ " <tr>\n",
" <td>Prevalence and Impact of Myocardial Injury in Patients Hospitalized with COVID-19 Infection</td>\n",
" <td>2020-04-24 00:00:00</td>\n",
" <td>http://medrxiv.org/cgi/content/short/2020.04.20.20072702v1?rss=1</td>\n",
@@ -282,22 +287,16 @@
" <td>Risk factors associated with subsequent death include older age, hypertension, diabetes, ischemic heart disease, obesity and chronic lung disease; however, sometimes there are no obvious risk factors .</td>\n",
" </tr>\n",
" <tr>\n",
- " <td>Early Safety Indicators of COVID-19 ConvalescentPlasma in 5,000 Patients</td>\n",
- " <td>2020-05-14 00:00:00</td>\n",
- " <td>https://www.ncbi.nlm.nih.gov/pubmed/32511566/</td>\n",
- " <td>TRALI often presents as bilateral pulmonary edema with little evidence of circulatory overload, and TRALI is further categorized into two types based on the absence of acute respiratory distress syndrome (ARDS) risk factors (type I) or presence of ARDS risk factors (type II) (18) .</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <td>Associations with covid-19 hospitalisation amongst 406,793 adults: the UK Biobank prospective cohort study</td>\n",
- " <td>2020-05-11 00:00:00</td>\n",
- " <td>http://medrxiv.org/cgi/content/short/2020.05.06.20092957v1?rss=1</td>\n",
- " <td>In addition, many risk factors for covid-19 documented in the literature are highly correlated and it is not clear which may be independently related to risk.</td>\n",
+ " <td>COVID-19 and associations with frailty and multimorbidity: a prospective analysis of UK Biobank participants</td>\n",
+ " <td>2020-07-23 00:00:00</td>\n",
+ " <td>https://www.ncbi.nlm.nih.gov/pubmed/32705587/</td>\n",
+ " <td>BACKGROUND: Frailty and multimorbidity have been suggested as risk factors for severe COVID-19 disease.</td>\n",
" </tr>\n",
" <tr>\n",
- " <td>Associations with covid-19 hospitalisation amongst 406,793 adults: the UK Biobank prospective cohort study</td>\n",
- " <td>2020-05-11 00:00:00</td>\n",
- " <td>http://medrxiv.org/cgi/content/short/2020.05.06.20092957v1?rss=1</td>\n",
- " <td>The large numbers of covariables available in this cohort also enabled multivariable adjustment, permitting assessment of independent risk factors.</td>\n",
+ " <td>COVID-19: what has been learned and to be learned about the novel coronavirus disease</td>\n",
+ " <td>2020-03-15 00:00:00</td>\n",
+ " <td>https://doi.org/10.7150/ijbs.45134</td>\n",
+ " <td>• Three major risk factors for COVID-19 were sex (male), age (≥60), and severe pneumonia.</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
@@ -335,11 +334,11 @@
"cell_type": "code",
"metadata": {
"id": "Yv75Lh-cOpL9",
- "outputId": "b66df081-6be7-47ee-8fdd-396a9cacce94",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 328
- }
+ },
+ "outputId": "d4a1ae7c-7708-4207-f9df-178dc7795ac3"
},
"source": [
"document = {\n",
@@ -450,6 +449,16 @@
}
}
]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "PpV4qxIPT6XO"
+ },
+ "source": [
+ "# Next\n",
+ "Part 5 of this series uses zero-shot classification to apply labels to input text.\n"
+ ]
}
]
}
\ No newline at end of file
diff --git a/examples/05_Labeling_with_zero_shot_classification.ipynb b/examples/05_Labeling_with_zero_shot_classification.ipynb
new file mode 100644
index 00000000..32167a24
--- /dev/null
+++ b/examples/05_Labeling_with_zero_shot_classification.ipynb
@@ -0,0 +1,202 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "05 - Labeling with zero-shot classification",
+ "provenance": [],
+ "collapsed_sections": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "4Pjmz-RORV8E"
+ },
+ "source": [
+ "# Part 5: Labeling with zero-shot classification\n",
+ "\n",
+ "This notebook shows how zero-shot classification can be used to perform text classification/labeling. txtai provides a light-weight wrapper around the zero-shot-classification pipeline in Hugging Face Transformers. This method works impressively well out of the box. Kudos to the Hugging Face team for the phenomenal work on zero-shot classification!\n",
+ "\n",
+ "The examples in this notebook pick the best matching label using a list of labels for a snippet of text.\n",
+ "\n",
+ "[tldrstory](https://github.com/neuml/tldrstory) has full-stack implementation of a zero-shot classification system using Streamlit, FastAPI and Hugging Face Transformers. There is also a [Medium article describing tldrstory](https://towardsdatascience.com/tldrstory-ai-powered-understanding-of-headlines-and-story-text-fc86abd702fc) and zero-shot classification. \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "Dk31rbYjSTYm"
+ },
+ "source": [
+ "# Install dependencies\n",
+ "\n",
+ "Install txtai and all dependencies"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "XMQuuun2R06J"
+ },
+ "source": [
+ "%%capture\n",
+ "!pip install git+https://github.com/neuml/txtai"
+ ],
+ "execution_count": 1,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "PNPJ95cdTKSS"
+ },
+ "source": [
+ "# Create a Labels instance\n",
+ "\n",
+ "The Labels instance is the main entrypoint for zero-shot classification. This is a light-weight wrapper around the zero-shot-classification pipeline in Hugging Face Transformers.\n",
+ "\n",
+ "In addition to the default model, additional models can be found on the [Hugging Face model hub](https://huggingface.co/models?search=mnli).\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "nTDwXOUeTH2-"
+ },
+ "source": [
+ "%%capture\n",
+ "\n",
+ "from txtai.labels import Labels\n",
+ "\n",
+ "# Create labels model\n",
+ "labels = Labels()\n",
+ "\n",
+ "# Alternate models can be used via passing the model path as shown below\n",
+ "# labels = Labels(\"roberta-large-mnli\")"
+ ],
+ "execution_count": 2,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "-vGR_piwZZO6"
+ },
+ "source": [
+ "# Applying labels to text\n",
+ "\n",
+ "The example below shows how a zero-shot classifier can be applied to arbitary text. The default model for the zero-shot classification pipeline is *bart-large-mnli*. \n",
+ "\n",
+ "Look at the results below. It's nothing short of amazing✨ how well it performs. These aren't all simple even for a human. For example, intercepted was purposely picked as that is more common in football than basketball. The amount of knowledge stored in larger Transformer models continues to impress me. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "-K2YJJzsVtfq",
+ "outputId": "4bd3e29c-d7ef-44ab-825b-29435af98893"
+ },
+ "source": [
+ "sections = [\"Dodgers lose again, give up 3 HRs in a loss to the Giants\",\n",
+ " \"Giants 5 Cardinals 4 final in extra innings\",\n",
+ " \"Dodgers drop Game 2 against the Giants, 5-4\",\n",
+ " \"Flyers 4 Lightning 1 final. 45 saves for the Lightning.\",\n",
+ " \"Slashing, penalty, 2 minute power play coming up\",\n",
+ " \"What a stick save!\",\n",
+ " \"Leads the NFL in sacks with 9.5\",\n",
+ " \"UCF 38 Temple 13\",\n",
+ " \"With the 30 yard completion, down to the 10 yard line\",\n",
+ " \"Drains the 3pt shot!!, 0:15 remaining in the game\",\n",
+ " \"Intercepted! Drives down the court and shoots for the win\",\n",
+ " \"Massive dunk!!! they are now up by 15 with 2 minutes to go\"]\n",
+ "\n",
+ "# List of labels\n",
+ "tags = [\"Baseball\", \"Football\", \"Hockey\", \"Basketball\"]\n",
+ "\n",
+ "print(\"%-75s %s\" % (\"Text\", \"Label\"))\n",
+ "\n",
+ "for text in sections:\n",
+ " print(\"%-75s %s\" % (text, labels(text, tags)[0][0]))"
+ ],
+ "execution_count": 3,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Text Label\n",
+ "Dodgers lose again, give up 3 HRs in a loss to the Giants Baseball\n",
+ "Giants 5 Cardinals 4 final in extra innings Baseball\n",
+ "Dodgers drop Game 2 against the Giants, 5-4 Baseball\n",
+ "Flyers 4 Lightning 1 final. 45 saves for the Lightning. Hockey\n",
+ "Slashing, penalty, 2 minute power play coming up Hockey\n",
+ "What a stick save! Hockey\n",
+ "Leads the NFL in sacks with 9.5 Football\n",
+ "UCF 38 Temple 13 Football\n",
+ "With the 30 yard completion, down to the 10 yard line Football\n",
+ "Drains the 3pt shot!!, 0:15 remaining in the game Basketball\n",
+ "Intercepted! Drives down the court and shoots for the win Basketball\n",
+ "Massive dunk!!! they are now up by 15 with 2 minutes to go Basketball\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "t-tGAzCxsHLy"
+ },
+ "source": [
+ "# Let's try emoji 😀\n",
+ "\n",
+ "Does the model have knowledge of emoji? Check out the run below, sure looks like it does! Notice the labels are applied based on the perspective from which the information is presented. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "uIf064M9pbjn",
+ "outputId": "34ebdc56-37c9-41cd-917d-56863d1371ee"
+ },
+ "source": [
+ "tags = [\"😀\", \"😡\"]\n",
+ "\n",
+ "for text in sections:\n",
+ " print(\"%-75s %s\" % (text, labels(text, tags)[0][0]))"
+ ],
+ "execution_count": 4,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "text": [
+ "Dodgers lose again, give up 3 HRs in a loss to the Giants 😡\n",
+ "Giants 5 Cardinals 4 final in extra innings 😀\n",
+ "Dodgers drop Game 2 against the Giants, 5-4 😡\n",
+ "Flyers 4 Lightning 1 final. 45 saves for the Lightning. 😀\n",
+ "Slashing, penalty, 2 minute power play coming up 😡\n",
+ "What a stick save! 😀\n",
+ "Leads the NFL in sacks with 9.5 😀\n",
+ "UCF 38 Temple 13 😀\n",
+ "With the 30 yard completion, down to the 10 yard line 😀\n",
+ "Drains the 3pt shot!!, 0:15 remaining in the game 😀\n",
+ "Intercepted! Drives down the court and shoots for the win 😀\n",
+ "Massive dunk!!! they are now up by 15 with 2 minutes to go 😀\n"
+ ],
+ "name": "stdout"
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file