mervp commited on
Commit
33f6ac6
·
verified ·
1 Parent(s): 105e633

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +18 -8
README.md CHANGED
@@ -50,11 +50,21 @@ import torch
50
  model = AutoModelForSequenceClassification.from_pretrained("mervp/SentimentBERT")
51
  tokenizer = AutoTokenizer.from_pretrained("mervp/SentimentBERT")
52
 
53
- text = "The government’s response to the crisis was surprisingly effective."
54
- inputs = tokenizer(text, return_tensors="pt")
55
-
56
- with torch.no_grad():
57
- logits = model(**inputs).logits
58
-
59
- predicted_class = torch.argmax(logits, dim=1).item()
60
- print(["Negative", "Neutral", "Positive"][predicted_class])
 
 
 
 
 
 
 
 
 
 
 
50
  model = AutoModelForSequenceClassification.from_pretrained("mervp/SentimentBERT")
51
  tokenizer = AutoTokenizer.from_pretrained("mervp/SentimentBERT")
52
 
53
+ def predict_sentiment(text):
54
+ model.eval()
55
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
56
+ with torch.no_grad():
57
+ outputs = model(**inputs)
58
+ logits = outputs.logits
59
+ prediction = torch.argmax(logits, dim=-1).item()
60
+ label = model.config.id2label[prediction]
61
+ return label
62
+
63
+ print(predict_sentiment("What a beautiful day.")) # positive
64
+ print(predict_sentiment("The service was excellent.")) # positive
65
+ print(predict_sentiment("He did a fantastic job.")) # positive
66
+ print(predict_sentiment("The experience was terrible.")) # negative
67
+ print(predict_sentiment("Everything went wrong.")) # negative
68
+ print(predict_sentiment("He opened the door and walked in.")) # neutral
69
+ print(predict_sentiment("They are meeting at 5 PM.")) # neutral
70
+ print(predict_sentiment("She has a cat.")) # neutral