Views
No views yet
bert-base-uncasedsafetensorsbert-base-uncased1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4model = AutoModelForSequenceClassification.from_pretrained("mervp/SentimentBERT")
5tokenizer = AutoTokenizer.from_pretrained("mervp/SentimentBERT")
6
7def predict_sentiment(text):
8 model.eval()
9 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
10 with torch.no_grad():
11 outputs = model(**inputs)
12 logits = outputs.logits
13 prediction = torch.argmax(logits, dim=-1).item()
14 label = model.config.id2label[prediction]
15 return label
16
17print(predict_sentiment("What a beautiful day.")) # positive
18print(predict_sentiment("The service was excellent.")) # positive
19print(predict_sentiment("He did a fantastic job.")) # positive
20print(predict_sentiment("The experience was terrible.")) # negative
21print(predict_sentiment("Everything went wrong.")) # negative
22print(predict_sentiment("He opened the door and walked in.")) # neutral
23print(predict_sentiment("They are meeting at 5 PM.")) # neutral
24print(predict_sentiment("She has a cat.")) # neutral