Views
No views yet
bert-base-uncased0: Negative1: Positive| Model | SST-2 Accuracy | Yelp Accuracy | Amazon Accuracy | IMDB Accuracy |
|---|---|---|---|---|
| philipobiorah/bert-imdb-model | 0.89 | 0.89 | 0.89 | 0.96 |
| DistilBERT-SST-2 | 0.94 | 0.85 | 0.85 | 0.89 |
| RoBERTa-Sentiment | 0.40 | 0.42 | 0.47 | 0.79 |
| Logistic Regression | 0.83 | 0.91 | 0.86 | 0.85 |
| Naive Bayes | 0.77 | 0.86 | 0.84 | 0.85 |
1from transformers import BertTokenizer, BertForSequenceClassification
2import torch
3
4model_name = "philipobiorah/bert-imdb-model"
5
6# Load tokenizer and model
7tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
8model = BertForSequenceClassification.from_pretrained(model_name)
9
10# Define function for sentiment prediction with confidence score
11def predict_sentiment(text):
12 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
13
14 with torch.no_grad():
15 logits = model(**inputs).logits
16
17 # Convert logits to probabilities
18 probabilities = torch.nn.functional.softmax(logits, dim=1)[0]
19
20 # Get predicted class (0 = Negative, 1 = Positive)
21 sentiment_idx = probabilities.argmax().item()
22 confidence = probabilities[sentiment_idx].item() * 100 # Convert to percentage
23
24 sentiment_label = "Positive" if sentiment_idx == 1 else "Negative"
25
26 return {"sentiment": sentiment_label, "confidence": round(confidence, 2)}
27
28# Test the model
29result1 = predict_sentiment("This movie was absolutely fantastic!")
30result2 = predict_sentiment("I really disliked this movie, it was terrible.")
31
32print(f"Sentiment: {result1['sentiment']}, Confidence: {result1['confidence']}%")
33print(f"Sentiment: {result2['sentiment']}, Confidence: {result2['confidence']}%")
34
35
36
37from transformers import pipeline
38
39pipe = pipeline("text-classification", model="philipobiorah/bert-imdb-model")
40
41text_to_classify = "This movie was fantastic! I loved every minute of it."
42result = pipe(text_to_classify)
43print(result)
44
45text_to_classify_2 = "The acting was terrible and the plot made no sense."
46result_2 = pipe(text_to_classify_2)
47print(result_2)
48