Downloads · 30 days
11
22% of all-time downloads
Alidiamond/somali-sentiment-analysis
somali-sentiment-analysis is a machine learning model from Alidiamond. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Model Name: Alidiamond/somali-sentiment-analysis
Downloads · 30 days
11
22% of all-time downloads
All-time downloads
49
Public
Parameters
111M
447 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors446 MB · 100%
From the Hugging Face model README
Model Name: Alidiamond/somali-sentiment-analysis
This is a fine-tuned version of castorini/afriberta_base specifically optimized for Somali language sentiment analysis. The model can classify Somali text into positive or negative sentiment categories.
Base Model: castorini/afriberta_base - A multilingual African language model pretrained on 11 African languages including Somali.
Fine-tuned by: Alidiamond
pip install transformers torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Load the fine-tuned model and tokenizer
model_name = "Alidiamond/somali-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Example Somali texts
texts = [
"Waan ku faraxsanahay adeeggan cusub", # "I am happy with this new service" (Positive)
"Waxan necebahay sida ay u dhaqmayaan", # "I hate how they behave" (Negative)
"Barnaamijkan aad buu u wanaagsan yahay", # "This program is very good" (Positive)
]
def predict_sentiment(text):
# Tokenize the input
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=512
)
# Make prediction
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class = torch.argmax(predictions, dim=-1).item()
confidence = predictions[0][predicted_class].item()
# Map prediction to label
labels = {0: "Negative", 1: "Positive"}
return {
"text": text,
"sentiment": labels[predicted_class],
"confidence": confidence
}
# Analyze sentiment
for text in texts:
result = predict_sentiment(text)
print(f"Text: {result['text']}")
print(f"Sentiment: {result['sentiment']} (Confidence: {result['confidence']:.3f})")
print("-" * 50)
def analyze_batch(texts):
"""Process multiple texts at once for better efficiency"""
inputs = tokenizer(
texts,
return_tensors="pt",
truncation=True,
padding=True,
max_length=512
)
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
results = []
for i, text in enumerate(texts):
predicted_class = torch.argmax(predictions[i]).item()
confidence = predictions[i][predicted_class].item()
sentiment = "Positive" if predicted_class == 1 else "Negative"
results.append({
"text": text,
"sentiment": sentiment,
"confidence": confidence
})
return results
# Example batch processing
batch_texts = [
"Mahadsanid adeegga wanaagsan",
"Waxaan codsaneynaa in la hagaajiyo",
"Aad ayaan ugu faraxsan nahay natiijada"
]
batch_results = analyze_batch(batch_texts)
for result in batch_results:
print(f"'{result['text']}' → {result['sentiment']} ({result['confidence']:.3f})")
config.json - Model configurationmodel.safetensors - Model weights (SafeTensors format)tokenizer_config.json - Tokenizer configurationsentencepiece.bpe.model - SentencePiece modelspecial_tokens_map.json - Special tokens mapping<s> - Beginning of sequence</s> - End of sequence<pad> - Padding token<unk> - Unknown token<mask> - Mask tokenThis model is built upon castorini/afriberta_base, which was pretrained on 11 African languages:
Contributions to improve the model's performance on Somali sentiment analysis are welcome. Areas for improvement:
Created by: Alidiamond
Model: Alidiamond/somali-sentiment-analysis
Base Model: castorini/afriberta_base
Language: Somali (so)
Task: Sentiment Analysis (Binary Classification)
Please refer to the original castorini/afriberta_base license terms for usage restrictions and requirements.
If you use this model in your research, please cite both this work and the original AfriBERTa paper:
@misc{alidiamond2024somali,
title={Somali Sentiment Analysis using Fine-tuned AfriBERTa},
author={Alidiamond},
year={2024},
howpublished={\url{https://huggingface.co/Alidiamond/somali-sentiment-analysis}}
}
@article{afriberta,
title={AfriBERTa: Exploring the Viability of Pretrained Multilingual Language Models for Low-resourced Languages},
author={Kelechi Ogueji and Yuxin Zhu and Jimmy Lin},
year={2021},
journal={arXiv preprint arXiv:2104.02516}
}
Input: "Waxaan aad ugu faraxsanahay waxan arkay"
Output: Positive (Confidence: 0.892)
Input: "Ma jecelahay waxa dhacaya"
Output: Negative (Confidence: 0.756)
Input: "Barnaamijkan waa mid aad u wanaagsan"
Output: Positive (Confidence: 0.834)
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# Load model directly from Hugging Face
tokenizer = AutoTokenizer.from_pretrained("Alidiamond/somali-sentiment-analysis")
model = AutoModelForSequenceClassification.from_pretrained("Alidiamond/somali-sentiment-analysis")
# Test with Somali text
text = "Waan ku faraxsanahay adeeggan cusub" # "I am happy with this new service"
result = model(tokenizer(text, return_tensors="pt"))
Note: This model (Alidiamond/somali-sentiment-analysis) is specifically designed for Somali language sentiment analysis. For other African languages, consider using the original castorini/afriberta_base model or fine-tune it for your specific language and task.