DEV Community

David Mezzetti for NeuML

Posted on • Updated on • Originally published at neuml.hashnode.dev

txtai API Gallery

The txtai API is a web-based service backed by FastAPI. All txtai functionality including similarity search, extractive QA and zero-shot labeling is available via the API.

This article installs the txtai API and shows an example using each of the supported language bindings for txtai.

Install dependencies

Install txtai and all dependencies. Since this article uses the API, we need to install the api extras package.

pip install txtai[api]
Enter fullscreen mode Exit fullscreen mode

Python

The first method we'll try is direct access via Python. We'll use zero-shot labeling for all the examples here. See this article for more details on zero-shot classification.

import os
from IPython.core.display import display, HTML
from txtai.pipeline import Labels

def table(rows):
    html = """
    <style type='text/css'>
    @import url('https://fonts.googleapis.com/css?family=Oswald&display=swap');
    table {
      border-collapse: collapse;
      width: 900px;
    }
    th, td {
        border: 1px solid #9e9e9e;
        padding: 10px;
        font: 20px Oswald;
    }
    </style>
    """

    html += "<table><thead><tr><th>Text</th><th>Label</th></tr></thead>"
    for text, label in rows:
        html += "<tr><td>%s</td><td>%s</td></tr>" % (text, label)
    html += "</table>"

    display(HTML(html))

# Create labels model
labels = Labels()
Enter fullscreen mode Exit fullscreen mode

Apply labels to text

data = ["Wears a red suit and says ho ho",
        "Pulls a flying sleigh",
        "This is cut down and decorated",
        "Santa puts these under the tree",
        "Best way to spend the holidays"]

# List of labels
tags = ["🎅 Santa Clause", "🦌 Reindeer", "🍪 Cookies", "🎄 Christmas Tree", "🎁 Gifts", "👪 Family"]

# Render output to table
table([(text, tags[labels(text, tags)[0][0]]) for text in data])
Enter fullscreen mode Exit fullscreen mode
Text Label
Wears a red suit and says ho ho 🎅 Santa Clause
Pulls a flying sleigh 🦌 Reindeer
This is cut down and decorated 🎄 Christmas Tree
Santa puts these under the tree 🎁 Gifts
Best way to spend the holidays 👪 Family

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.

Start an API instance

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 for a more detailed configuration example.

The API instance is started in the background.

CONFIG=index.yml nohup uvicorn "txtai.api:app" &> api.log &
sleep 90
Enter fullscreen mode Exit fullscreen mode

JavaScript

txtai.js is available via NPM and can be installed as follows.

npm install txtai
Enter fullscreen mode Exit fullscreen mode

For this example, we'll clone the txtai.js project to import the example build configuration.

git clone https://github.com/neuml/txtai.js
Enter fullscreen mode Exit fullscreen mode

Create labels.js

The following file is a JavaScript version of the labels example.

import {Labels} from "txtai";
import {sprintf} from "sprintf-js";

const run = async () => {
    try {
        let labels = new Labels("http://localhost:8000");

        let data = ["Wears a red suit and says ho ho",
                    "Pulls a flying sleigh",
                    "This is cut down and decorated",
                    "Santa puts these under the tree",
                    "Best way to spend the holidays"];

        // List of labels
        let tags = ["🎅 Santa Clause", "🦌 Reindeer", "🍪 Cookies", "🎄 Christmas Tree", "🎁 Gifts", "👪 Family"];

        console.log(sprintf("%-40s %s", "Text", "Label"));
        console.log("-".repeat(75))

        for (let text of data) {
            let label = await labels.label(text, tags);
            label = tags[label[0].id];

            console.log(sprintf("%-40s %s", text, label));
        }
    }
    catch (e) {
        console.trace(e);
    }
};

run();
Enter fullscreen mode Exit fullscreen mode

Build and run labels example

cd txtai.js/examples/node 
npm install
npm run build
Enter fullscreen mode Exit fullscreen mode
node dist/labels.js
Enter fullscreen mode Exit fullscreen mode
Text                                     Label
---------------------------------------------------------------------------
Wears a red suit and says ho ho          🎅 Santa Clause
Pulls a flying sleigh                    🦌 Reindeer
This is cut down and decorated           🎄 Christmas Tree
Santa puts these under the tree          🎁 Gifts
Best way to spend the holidays           👪 Family
Enter fullscreen mode Exit fullscreen mode

The JavaScript program is showing the same results as when natively running through Python!

Java

txtai.java integrates with standard Java build tools (Gradle, Maven, SBT). The following shows how to add txtai as a dependency to Gradle.

implementation 'com.github.neuml:txtai.java:v2.0.0'
Enter fullscreen mode Exit fullscreen mode

For this example, we'll clone the txtai.java project to import the example build configuration.

git clone https://github.com/neuml/txtai.java
Enter fullscreen mode Exit fullscreen mode

Create LabelsDemo.java

The following file is a Java version of the labels example.

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

import txtai.API.IndexResult;
import txtai.Labels;

public class LabelsDemo {
    public static void main(String[] args) {
        try {
            Labels labels = new Labels("http://localhost:8000");

            List <String> data = 
                Arrays.asList("Wears a red suit and says ho ho",
                              "Pulls a flying sleigh",
                              "This is cut down and decorated",
                              "Santa puts these under the tree",
                              "Best way to spend the holidays");

            // List of labels
            List<String> tags = Arrays.asList("🎅 Santa Clause", "🦌 Reindeer", "🍪 Cookies", "🎄 Christmas Tree", "🎁 Gifts", "👪 Family");

            System.out.printf("%-40s %s%n", "Text", "Label");
            System.out.println(new String(new char[75]).replace("\0", "-"));

            for (String text: data) {
                List<IndexResult> label = labels.label(text, tags);
                System.out.printf("%-40s %s%n", text, tags.get(label.get(0).id));
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
cd txtai.java/examples
../gradlew -q --console=plain labels 2> /dev/null
Enter fullscreen mode Exit fullscreen mode
Text                                     Label
---------------------------------------------------------------------------
Wears a red suit and says ho ho          🎅 Santa Clause
Pulls a flying sleigh                    🦌 Reindeer
This is cut down and decorated           🎄 Christmas Tree
Santa puts these under the tree          🎁 Gifts
Best way to spend the holidays           👪 Family
Enter fullscreen mode Exit fullscreen mode

The Java program is showing the same results as when natively running through Python!

Rust

txtai.rs is available via crates.io and can be installed by adding the following to your cargo.toml file.

[dependencies]
txtai = { version = "2.0" }
tokio = { version = "0.2", features = ["full"] }
Enter fullscreen mode Exit fullscreen mode

For this example, we'll clone the txtai.rs project to import the example build configuration. First we need to install Rust.

apt-get install rustc
git clone https://github.com/neuml/txtai.rs
Enter fullscreen mode Exit fullscreen mode

Create labels.rs

The following file is a Rust version of the labels example.

use std::error::Error;

use txtai::labels::Labels;

pub async fn labels() -> Result<(), Box<dyn Error>> {
    let labels = Labels::new("http://localhost:8000");

    let data = ["Wears a red suit and says ho ho",
                "Pulls a flying sleigh",
                "This is cut down and decorated",
                "Santa puts these under the tree",
                "Best way to spend the holidays"];

    println!("{:<40} {}", "Text", "Label");
    println!("{}", "-".repeat(75));

    for text in data.iter() {
        let tags = vec!["🎅 Santa Clause", "🦌 Reindeer", "🍪 Cookies", "🎄 Christmas Tree", "🎁 Gifts", "👪 Family"];
        let label = labels.label(text, &tags).await?[0].id;

        println!("{:<40} {}", text, tags[label]);
    }

    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

Build and run labels example

cd txtai.rs/examples/demo
cargo build
Enter fullscreen mode Exit fullscreen mode
cargo run labels
Enter fullscreen mode Exit fullscreen mode
Text                                     Label
---------------------------------------------------------------------------
Wears a red suit and says ho ho          🎅 Santa Clause
Pulls a flying sleigh                    🦌 Reindeer
This is cut down and decorated           🎄 Christmas Tree
Santa puts these under the tree          🎁 Gifts
Best way to spend the holidays           👪 Family
Enter fullscreen mode Exit fullscreen mode

The Rust program is showing the same results as when natively running through Python!

Go

txtai.go can be installed by adding the following import statement. When using modules, txtai.go will automatically be installed. Otherwise use go get.

import "github.com/neuml/txtai.go"
Enter fullscreen mode Exit fullscreen mode

For this example, we'll create a standalone process for labeling. First we need to install Go.

apt install golang-go
go get "github.com/neuml/txtai.go"
Enter fullscreen mode Exit fullscreen mode

Create labels.go

The following file is a Go version of the labels example.

package main

import (
    "fmt"
    "strings"
    "github.com/neuml/txtai.go"
)

func main() {
    labels := txtai.Labels("http://localhost:8000")

    data := []string{"Wears a red suit and says ho ho",
                   "Pulls a flying sleigh",
                   "This is cut down and decorated",
                   "Santa puts these under the tree",
                   "Best way to spend the holidays"}

    // List of labels
    tags := []string{"🎅 Santa Clause", "🦌 Reindeer", "🍪 Cookies", "🎄 Christmas Tree", "🎁 Gifts", "👪 Family"}

    fmt.Printf("%-40s %s\n", "Text", "Label")
    fmt.Println(strings.Repeat("-", 75))

    for _, text := range data {
        label := labels.Label(text, tags)
        fmt.Printf("%-40s %s\n", text, tags[label[0].Id])
    }
}
Enter fullscreen mode Exit fullscreen mode
go run labels.go
Enter fullscreen mode Exit fullscreen mode
Text                                     Label
---------------------------------------------------------------------------
Wears a red suit and says ho ho          🎅 Santa Clause
Pulls a flying sleigh                    🦌 Reindeer
This is cut down and decorated           🎄 Christmas Tree
Santa puts these under the tree          🎁 Gifts
Best way to spend the holidays           👪 Family
Enter fullscreen mode Exit fullscreen mode

The Go program is showing the same results as when natively running through Python!

Top comments (2)

Collapse
 
rhrabie profile image
rhrabie

is there any tutorial ' how to deploy txtai to aws lambda"

Collapse
 
davidmezzetti profile image
David Mezzetti

Not currently but that is a great idea! Will look into that.