Text Classification
Transformers
Safetensors
English
bert
sentiment analysis
text classification
news
reviews
text-embeddings-inference
Instructions to use mervp/SentimentBERT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mervp/SentimentBERT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="mervp/SentimentBERT")# Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("mervp/SentimentBERT") model = AutoModelForSequenceClassification.from_pretrained("mervp/SentimentBERT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
with torch.no_grad():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|