-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentimentAnalysis.py
More file actions
22 lines (19 loc) · 1.03 KB
/
SentimentAnalysis.py
File metadata and controls
22 lines (19 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#We'll use the transformers library from Hugging Face and the torch library for deep learning.
#pip install transformers torch
import torch
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
def perform_sentiment_analysis(text):
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
predicted_class = torch.argmax(logits, dim=1).item()
sentiment_labels = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"]
predicted_sentiment = sentiment_labels[predicted_class]
return predicted_sentiment
if __name__ == "__main__":
example_text = "I really enjoyed watching that movie. The acting was superb!"
predicted_sentiment = perform_sentiment_analysis(example_text)
print(f"Predicted sentiment: {predicted_sentiment}")