mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
25 KiB
25 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[api]In [ ]:
%%capture
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()In [ ]:
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])| 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 |
In [ ]:
%%writefile index.yml
# Labels settings
labels:Writing index.yml
In [ ]:
!CONFIG=index.yml nohup uvicorn "txtai.api:app" &> api.log &
!sleep 90In [ ]:
%%capture
!git clone https://github.com/neuml/txtai.jsIn [ ]:
%%writefile txtai.js/examples/node/src/labels.js
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();
Overwriting txtai.js/examples/node/src/labels.js
In [ ]:
%%capture
os.chdir("txtai.js/examples/node")
!npm install
!npm run buildIn [ ]:
!node dist/labels.jsText 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
In [ ]:
%%capture
os.chdir("/content")
!git clone https://github.com/neuml/txtai.javaIn [ ]:
%%writefile txtai.java/examples/src/main/java/LabelsDemo.java
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();
}
}
}
Overwriting txtai.java/examples/src/main/java/LabelsDemo.java
In [ ]:
os.chdir("txtai.java/examples")
!../gradlew -q --console=plain labels 2> /dev/nullText 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 [m
In [ ]:
%%capture
os.chdir("/content")
!apt-get install rustc
!git clone https://github.com/neuml/txtai.rsIn [ ]:
%%writefile txtai.rs/examples/demo/src/labels.rs
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(())
}Overwriting txtai.rs/examples/demo/src/labels.rs
In [ ]:
%%capture
os.chdir("txtai.rs/examples/demo")
!cargo buildIn [ ]:
!cargo run labels[0m[0m[1m[32m Finished[0m dev [unoptimized + debuginfo] target(s) in 0.07s [0m[0m[1m[32m Running[0m `target/debug/demo labels` 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
In [ ]:
%%capture
os.chdir("/content")
!apt install golang-go
!go get "github.com/neuml/txtai.go"In [ ]:
%%writefile labels.go
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])
}
}Writing labels.go
In [ ]:
!go run labels.goText 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