bert-base-multilingual-uncased-sentiment for Kjarni
Star ratings from 1 to 5 for product reviews in English, Dutch, German, French, Italian and Spanish, packaged to run locally with Kjarni: one native library, with no Python runtime, server or cloud service. The model is about 670 MB and downloads on first use.
Usage
Tested with Kjarni 0.8.2. In Kjarni the model is called bert-sentiment-multilingual.
Command line
curl -fsSL https://kjarni.ai/install.sh | sh # Windows: irm https://kjarni.ai/install.ps1 | iex
kjarni classify -m bert-sentiment-multilingual "Das Produkt kam kaputt an und der Support hat nie geantwortet."
Model: bert-sentiment-multilingual Device: Cpu
Input "Das Produkt kam kaputt an und der Support hat nie geantwort..."
✓ 1 star ████████████████░░░░ 79.4%
2 stars ███░░░░░░░░░░░░░░░░░ 16.7%
3 stars █░░░░░░░░░░░░░░░░░░░ 3.6%
4 stars ░░░░░░░░░░░░░░░░░░░░ 0.3%
5 stars ░░░░░░░░░░░░░░░░░░░░ 0.1%
Python
# pip install kjarni
from kjarni import Classifier
classifier = Classifier(model="bert-sentiment-multilingual", quiet=True)
for review in [
"Absolutely perfect, exactly what I wanted.",
"Das Produkt kam kaputt an und der Support hat nie geantwortet.",
"C'est correct, sans plus.",
"Muy buena calidad, aunque el envío tardó un poco.",
]:
result = classifier.classify(review)
print(f"{result.label:<7} {result.score:.2f} {review}")
5 stars 0.98 Absolutely perfect, exactly what I wanted.
1 star 0.79 Das Produkt kam kaputt an und der Support hat nie geantwortet.
3 stars 0.76 C'est correct, sans plus.
4 stars 0.65 Muy buena calidad, aunque el envío tardó un poco.
Rust
// cargo add kjarni anyhow tokio --features tokio/full
use kjarni::Classifier;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let classifier = Classifier::new("bert-sentiment-multilingual").await?;
for review in [
"Absolutely perfect, exactly what I wanted.",
"Das Produkt kam kaputt an und der Support hat nie geantwortet.",
"C'est correct, sans plus.",
"Muy buena calidad, aunque el envío tardó un poco.",
] {
let result = classifier.classify(review).await?;
println!("{:<7} {:.2} {review}", result.label, result.score);
}
Ok(())
}
C#
// dotnet add package Kjarni
using Kjarni;
using var classifier = new Classifier("bert-sentiment-multilingual", quiet: true);
foreach (var review in new[]
{
"Absolutely perfect, exactly what I wanted.",
"Das Produkt kam kaputt an und der Support hat nie geantwortet.",
"C'est correct, sans plus.",
"Muy buena calidad, aunque el envío tardó un poco.",
})
{
var result = classifier.Classify(review);
Console.WriteLine($"{result.Label,-7} {result.Score:F2} {review}");
}
C++ (C++23)
#include <kjarni.hpp>
#include <cstdio>
int main() {
kjarni::ClassifierOptions options;
options.model = "bert-sentiment-multilingual";
auto classifier = kjarni::Classifier::create(options);
if (!classifier) return 1;
for (const char* review : {"Absolutely perfect, exactly what I wanted.",
"Das Produkt kam kaputt an und der Support hat nie geantwortet.",
"C'est correct, sans plus.",
"Muy buena calidad, aunque el envío tardó un poco."}) {
auto labels = classifier->classify(review);
if (!labels) return 1;
const auto& top = labels->front();
std::printf("%-7s %.2f %s\n", top.name.c_str(), top.score, review);
}
}
Go
// go get github.com/kjarni-ai/kjarni-go
package main
import (
"fmt"
"log"
kjarni "github.com/kjarni-ai/kjarni-go"
)
func main() {
classifier, err := kjarni.NewClassifier("bert-sentiment-multilingual", kjarni.WithQuiet(true))
if err != nil {
log.Fatal(err)
}
defer classifier.Close()
for _, review := range []string{
"Absolutely perfect, exactly what I wanted.",
"Das Produkt kam kaputt an und der Support hat nie geantwortet.",
"C'est correct, sans plus.",
"Muy buena calidad, aunque el envío tardó un poco.",
} {
result, err := classifier.Classify(review)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%-7s %.2f %s\n", result.Label, result.Score, review)
}
}
See the Kjarni documentation for other languages and installation details.
What changed from the original
This is not a new model. model.safetensors is the original's file, byte for byte.
Only the packaging differs:
- A
tokenizer.jsonis added, made from the original'svocab.txt. - The configuration and tokenizer files are re-saved by a newer version of
transformers, and
config.jsonnames the model class asBertForSequenceClassification, as the original does.
The files also load in transformers. On the four examples above, Kjarni's scores match transformers running the original model to four decimal places.
Accuracy
From the original model card, on 5,000 held-out product reviews per language. "Exact" is the share of reviews given exactly the reviewer's number of stars; "off by one" is the share within one star of it.
| Language | Exact | Off by one |
|---|---|---|
| English | 67% | 95% |
| Dutch | 57% | 93% |
| German | 61% | 94% |
| French | 59% | 94% |
| Italian | 59% | 95% |
| Spanish | 58% | 95% |
Attribution
The model is nlptown/bert-base-multilingual-uncased-sentiment, by NLP Town. It is bert-base-multilingual-uncased fine-tuned on about 630,000 product reviews in six languages. All credit for the model, its training and its evaluation belongs to them. NLP Town offers an updated version of the model on its website.
Kjarni and this packaging are not affiliated with or endorsed by NLP Town.
This repository is released under the MIT License, the same license as the original model.