Due to the absence of annotated sentiment data for Kiswahili, a cross-lingual pseudo-labeling approach was employed:
The F1-score of 0.6125 represents a meaningful achievement for a language with zero manually annotated sentiment data. The moderate score reflects inherent limitations of the pseudo-labeling approach, including translation noise and cultural differences in sentiment expression.
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# Load model and tokenizer
5tokenizer = AutoTokenizer.from_pretrained("RareElf/kiswahili-sentiment-distilbert")
6model = AutoModelForSequenceClassification.from_pretrained("RareElf/kiswahili-sentiment-distilbert")
7
8# Classify sentiment
9text = "Habari yako, nimefurahi sana kukutana nawe"
10inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
11
12with torch.no_grad():
13 outputs = model(**inputs)
14 probs = torch.softmax(outputs.logits, dim=-1)
15 predicted_class = torch.argmax(probs, dim=-1).item()
16
17sentiment_map = {0: "negative", 1: "positive"}
18print(f"Sentiment: {sentiment_map[predicted_class]}")
19print(f"Confidence: {probs[0][predicted_class].item():.2%}")
1from transformers import pipeline
2
3classifier = pipeline(
4 "text-classification",
5 model="RareElf/kiswahili-sentiment-distilbert"
6)
7
8result = classifier("Hii ni siku nzuri sana")
9print(result)
1@misc{obote2025kiswahili-sentiment,
2 author = {Obote, Kevin},
3 title = {Kiswahili Sentiment Analysis using Pseudo-Labeled DistilBERT},
4 year = {2025},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/RareElf/kiswahili-sentiment-distilbert}
7}