This model is a fine-tuned version of
bert-base-uncased for
three-class sentiment analysis of English product reviews. It classifies text into
Negative,
Neutral, or
Positive sentiment with
80.85% accuracy on the evaluation set.
1"Terrible quality, broke after one use" → NEGATIVE (0)
2"Package arrived on time, no issues" → NEUTRAL (1)
3"Absolutely love this product! Worth every penny." → POSITIVE (2)
1from transformers import pipeline
2
3classifier = pipeline(
4 "text-classification",
5 model="Ruslan10/bert-base-uncased-sentiment",
6 tokenizer="bert-base-uncased",
7 device=0 # Use GPU if available (remove for CPU)
8)
9
10reviews = [
11 "Terrible product, completely disappointed.",
12 "Item as described, arrived on time.",
13 "Absolutely fantastic! Exceeded all expectations."
14]
15
16results = classifier(reviews)
17print(results)
18# Output:
19# [{'label': 'LABEL_0', 'score': 0.98}, # Negative
20# {'label': 'LABEL_1', 'score': 0.87}, # Neutral
21# {'label': 'LABEL_2', 'score': 0.95}] # Positive
1label_map = {"LABEL_0": "NEGATIVE", "LABEL_1": "NEUTRAL", "LABEL_2": "POSITIVE"}
2
3for result in results:
4 result["label"] = label_map[result["label"]]
5 print(f"{result['label']}: {result['score']:.2%}")
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
5model = AutoModelForSequenceClassification.from_pretrained(
6 "Ruslan10/bert-base-uncased-sentiment"
7)
8
9text = "This product changed my life! Highly recommend."
10inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
11outputs = model(**inputs)
12probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
13predicted_class = torch.argmax(probs, dim=1).item()
14
15labels = {0: "NEGATIVE", 1: "NEUTRAL", 2: "POSITIVE"}
16print(f"Sentiment: {labels[predicted_class]} (confidence: {probs[0][predicted_class]:.2%})")