Assess sentiment of text across 100+ languages using a fine-tuned E5-small model. Returns numeric sentiment scores for filtering, analysis, and content moderation in multilingual contexts.
Assess the sentiment of text across 100+ languages using the `agentlans/multilingual-e5-small-aligned-sentiment` model from HuggingFace. This skill enables sentiment scoring for multilingual content analysis, filtering, and comparative corpus assessment.
This skill provides multilingual sentiment assessment capabilities by:
1. Loading the pre-trained `multilingual-e5-small-aligned-sentiment` model
2. Processing text input in any of 100+ supported languages
3. Returning a numeric sentiment score indicating the emotional tone
4. Supporting batch processing for multiple texts
5. Automatically utilizing GPU acceleration when available
The model is based on the E5-small architecture and has been fine-tuned specifically for cross-lingual sentiment assessment, demonstrating consistent performance across language boundaries.
The model supports 100+ languages including: English, Spanish, French, German, Chinese, Japanese, Arabic, Russian, Portuguese, Italian, Korean, Hindi, Dutch, Polish, Turkish, Vietnamese, Indonesian, Thai, and many more (see full language list in model card).
Before using this skill, ensure you have the required dependencies:
```bash
pip install transformers torch
```
1. **Initialize the model** by loading the tokenizer and model from HuggingFace
2. **Prepare your text** in any supported language
3. **Call the sentiment assessment function** to get a numeric score
4. **Interpret the results** where higher scores indicate more positive sentiment
**Step 1: Import required libraries**
```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
```
**Step 2: Load the model and tokenizer**
```python
model_name = "agentlans/multilingual-e5-small-aligned-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
```
**Step 3: Create a sentiment assessment function**
```python
def sentiment(text):
"""Assess the sentiment of the input text.
Args:
text: String containing the text to analyze
Returns:
List of sentiment scores (logits)
"""
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
with torch.no_grad():
logits = model(**inputs).logits.squeeze().cpu()
return logits.tolist()
```
**Step 4: Analyze text sentiment**
```python
score = sentiment("Life is full of opportunities!")
print(f"Sentiment score: {score}")
texts = [
"I love this product!", # English
"¡Me encanta este producto!", # Spanish
"我喜欢这个产品!", # Chinese
"J'adore ce produit!" # French
]
for text in texts:
score = sentiment(text)
print(f"{text}: {score}")
```
**Step 5: Use for content filtering or analysis**
When filtering or analyzing content, compare scores against thresholds:
**Batch Processing:**
```python
def batch_sentiment(texts):
"""Process multiple texts efficiently."""
inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True).to(device)
with torch.no_grad():
logits = model(**inputs).logits.cpu()
return logits.tolist()
batch_texts = ["Great day!", "Terrible experience", "It's okay"]
batch_scores = batch_sentiment(batch_texts)
```
**Content Filtering Pipeline:**
```python
def filter_by_sentiment(texts, threshold=0.0):
"""Filter texts based on sentiment threshold."""
scores = batch_sentiment(texts)
return [(text, score) for text, score in zip(texts, scores) if score[0] > threshold]
```
1. **Content Moderation**: Filter user-generated content across multiple languages
2. **Market Research**: Analyze customer feedback sentiment in international markets
3. **Social Media Monitoring**: Track sentiment trends across multilingual platforms
4. **Customer Support**: Prioritize negative sentiment messages for urgent response
5. **Corpus Analysis**: Compare sentiment distribution across translated documents
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/multilingual-sentiment-analysis/raw