File size: 10,928 Bytes
7e2f9ea 8b59881 7e2f9ea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
---
language: en
license: mit
tags:
- sentiment-analysis
- customer-reviews
- transformers
- distilbert
- text-classification
datasets:
- IberaSoft/ecommerce-reviews-sentiment
metrics:
- accuracy
- f1
widget:
- text: This product exceeded my expectations! Fast shipping and great quality.
example_title: Positive Review
- text: Terrible experience. Product broke after one week and customer service was
unhelpful.
example_title: Negative Review
- text: It's okay, nothing special. Does what it's supposed to do.
example_title: Neutral Review
model-index:
- name: customer-sentiment-analyzer
results:
- task:
type: text-classification
name: Sentiment Analysis
dataset:
name: E-commerce Reviews
type: IberaSoft/ecommerce-reviews-sentiment
metrics:
- type: accuracy
value: 90.2
name: Accuracy
- type: f1
value: 0.89
name: F1 Score
---
# π― Customer Sentiment Analyzer
> Fine-tuned DistilBERT model for analyzing customer review sentiment in e-commerce and SaaS domains.
[](https://huggingface.co/IberaSoft/customer-sentiment-analyzer)
[](https://huggingface.co/datasets/IberaSoft/ecommerce-reviews-sentiment)
[](https://huggingface.co/spaces/IberaSoft/sentiment-analyzer-demo)
[](LICENSE)
## π Model Description
This model is a fine-tuned version of [`distilbert-base-uncased`](https://huggingface.co/distilbert-base-uncased) on a custom dataset of 20,000 customer reviews from e-commerce and SaaS platforms. It classifies text into three sentiment categories: **positive**, **negative**, and **neutral**.
### Key Features
- β
**Fast Inference**: ~35ms per prediction (CPU)
- β
**High Accuracy**: 90.2% on test set
- β
**Domain-Specific**: Trained on customer reviews
- β
**Production-Ready**: Optimized for real-world deployment
- β
**Multi-Class**: Handles positive, negative, and neutral sentiments
## π Quick Start
### Using Transformers Pipeline
```python
from transformers import pipeline
# Load the model
classifier = pipeline(
"sentiment-analysis",
model="IberaSoft/customer-sentiment-analyzer"
)
# Analyze sentiment
result = classifier("This product is amazing! Highly recommend.")
print(result)
# [{'label': 'positive', 'score': 0.9823}]
```
### Using AutoModel
```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Load model and tokenizer
model_name = "IberaSoft/customer-sentiment-analyzer"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Prepare text
text = "Great quality but shipping took forever"
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
# Get prediction
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
# Map to labels
labels = ['negative', 'neutral', 'positive']
predicted_class = predictions.argmax().item()
confidence = predictions[0][predicted_class].item()
print(f"Sentiment: {labels[predicted_class]}")
print(f"Confidence: {confidence:.2%}")
```
### Batch Processing
```python
from transformers import pipeline
classifier = pipeline(
"sentiment-analysis",
model="IberaSoft/customer-sentiment-analyzer",
device=0 # Use GPU if available
)
reviews = [
"Excellent product, will buy again!",
"Disappointed with the quality.",
"It's okay, nothing special."
]
results = classifier(reviews)
for review, result in zip(reviews, results):
print(f"{review[:30]}... β {result['label']} ({result['score']:.2f})")
```
## π Model Performance
### Evaluation Metrics
| Metric | Score |
|--------|-------|
| **Accuracy** | 90.2% |
| **F1 Score (Macro)** | 0.89 |
| **Precision** | 0.90 |
| **Recall** | 0.89 |
### Per-Class Performance
| Class | Precision | Recall | F1-Score | Support |
|-------|-----------|--------|----------|---------|
| **Positive** | 0.92 | 0.91 | 0.91 | 800 |
| **Negative** | 0.89 | 0.90 | 0.89 | 700 |
| **Neutral** | 0.88 | 0.86 | 0.87 | 500 |
### Confusion Matrix
```
Predicted
Pos Neu Neg
Actual Pos [ 728 45 27 ]
Neu [ 38 430 32 ]
Neg [ 22 48 630 ]
```
### Inference Speed
| Batch Size | CPU (ms) | GPU (ms) |
|------------|----------|----------|
| 1 | 35 | 8 |
| 8 | 180 | 25 |
| 32 | 650 | 75 |
*Tested on Intel i7-11700K (CPU) and NVIDIA RTX 3080 (GPU)*
## π― Intended Use
### Primary Use Cases
- **Customer Support**: Automatically triage support tickets by sentiment
- **Product Reviews**: Analyze product feedback at scale
- **Brand Monitoring**: Track customer sentiment over time
- **Market Research**: Understand customer opinions
- **Quality Assurance**: Flag negative feedback for review
### Out-of-Scope Use
β Medical or health-related sentiment analysis
β Financial advice or stock sentiment (not trained on financial data)
β Political sentiment analysis (potential bias)
β Languages other than English
β Detecting sarcasm or irony (limited capability)
## π Training Details
### Training Data
The model was fine-tuned on **20,000 labeled customer reviews** consisting of:
- **Amazon Customer Reviews**: 8,000 reviews
- **Yelp Business Reviews**: 7,000 reviews
- **SaaS Product Reviews**: 5,000 reviews (G2, Capterra, TrustRadius)
**Dataset Distribution**:
- Training: 15,000 (75%)
- Validation: 3,000 (15%)
- Test: 2,000 (10%)
**Class Balance**:
- Positive: 40% (8,000 reviews)
- Negative: 35% (7,000 reviews)
- Neutral: 25% (5,000 reviews)
π¦ **[View Dataset on HuggingFace](https://huggingface.co/datasets/IberaSoft/ecommerce-reviews-sentiment)**
### Training Procedure
**Base Model**: `distilbert-base-uncased` (66M parameters)
**Hyperparameters**:
```yaml
learning_rate: 2e-5
batch_size: 16
epochs: 3
warmup_steps: 500
weight_decay: 0.01
max_length: 512
optimizer: AdamW
scheduler: linear with warmup
```
**Training Environment**:
- **Hardware**: NVIDIA Tesla V100 (16GB)
- **Training Time**: ~2.5 hours
- **Framework**: PyTorch 2.1, Transformers 4.36
- **Mixed Precision**: FP16
**Training Code**: [GitHub Repository](https://github.com/IberaSoft/sentiment-analysis-api)
### Preprocessing
Text preprocessing steps:
1. Lowercase conversion
2. URL removal
3. Excessive whitespace normalization
4. Emoji handling (converted to text)
5. HTML tag removal
6. Truncation to 512 tokens
## β οΈ Limitations and Bias
### Known Limitations
1. **English Only**: Trained exclusively on English text
2. **Domain Specificity**: Best performance on e-commerce/SaaS reviews
3. **Sarcasm**: May misclassify sarcastic reviews
4. **Context Length**: Limited to 512 tokens (~350 words)
5. **Informal Language**: May struggle with heavy slang or abbreviations
### Potential Biases
- **Product Category Bias**: Training data skewed toward electronics and software
- **Platform Bias**: Amazon and Yelp reviews may have different characteristics
- **Temporal Bias**: Reviews collected 2020-2023
- **Rating Correlation**: 5-star reviews assumed positive (may not always be true)
### Recommendations
- β
Test on your specific domain before production use
- β
Implement human review for edge cases
- β
Monitor performance on your data distribution
- β
Consider retraining for specialized domains
- β
Use confidence scores to flag uncertain predictions
## π§ Optimization
### Model Size Reduction
**Standard Model**: 268 MB
**Quantized (INT8)**: 67 MB (4x smaller, <2% accuracy drop)
```python
from optimum.onnxruntime import ORTModelForSequenceClassification
# Convert to ONNX with quantization
model = ORTModelForSequenceClassification.from_pretrained(
"IberaSoft/customer-sentiment-analyzer",
export=True,
provider="CPUExecutionProvider"
)
# Save quantized model
model.save_pretrained("./optimized_model")
```
### Performance Tips
```python
import torch
# Use GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device)
# Enable inference mode
model.eval()
torch.set_grad_enabled(False)
# Batch processing for better throughput
classifier = pipeline(
"sentiment-analysis",
model=model,
tokenizer=tokenizer,
batch_size=32,
device=0 if device == "cuda" else -1
)
```
## π Production Deployment
### FastAPI Example
```python
from fastapi import FastAPI
from transformers import pipeline
from pydantic import BaseModel
app = FastAPI()
# Load model once at startup
classifier = pipeline(
"sentiment-analysis",
model="IberaSoft/customer-sentiment-analyzer"
)
class ReviewRequest(BaseModel):
text: str
@app.post("/predict")
def predict_sentiment(request: ReviewRequest):
result = classifier(request.text)[0]
return {
"sentiment": result["label"],
"confidence": round(result["score"], 4)
}
```
### Docker Deployment
```dockerfile
FROM python:3.11-slim
RUN pip install transformers torch fastapi uvicorn
# Download model during build
RUN python -c "from transformers import pipeline; \
pipeline('sentiment-analysis', \
model='IberaSoft/customer-sentiment-analyzer')"
COPY app.py .
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
```
**Full API**: [GitHub Repository](https://github.com/IberaSoft/sentiment-analysis-api)
## π Citation
If you use this model in your research or application, please cite:
```bibtex
@misc{customer-sentiment-analyzer,
author = {Your Name},
title = {Customer Sentiment Analyzer: Fine-tuned DistilBERT for E-commerce Reviews},
year = {2026},
publisher = {HuggingFace},
howpublished = {\url{https://huggingface.co/IberaSoft/customer-sentiment-analyzer}},
}
```
## π License
This model is licensed under the **MIT License**. See [LICENSE](LICENSE) for details.
The base model `distilbert-base-uncased` is licensed under Apache 2.0.
## π€ Contributing
Found an issue or want to improve the model?
- π [Report bugs](https://github.com/IberaSoft/sentiment-analysis-api/issues)
- π‘ [Suggest features](https://github.com/IberaSoft/sentiment-analysis-api/issues)
- π§ [Submit pull requests](https://github.com/IberaSoft/sentiment-analysis-api/pulls)
## π Acknowledgments
- **HuggingFace** for the Transformers library and model hub
- **DistilBERT Authors** for the efficient base model
- **Dataset Contributors** for publicly available reviews
- **Community** for feedback and testing
---
<div align="center">
### β Star this model if you find it useful!
**Try the live demo**: [HuggingFace Spaces](https://huggingface.co/spaces/IberaSoft/sentiment-analyzer-demo)
</div>
|