Views
No views yet
1import torch
2from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
4def get_sentiment(sentence):
5 bert_dict = {}
6 vectors = tokenizer(sentence, return_tensors='pt').to(device)
7 outputs = bert_model(**vectors).logits
8 probs = torch.nn.functional.softmax(outputs, dim = 1)[0]
9 bert_dict['neg'] = round(probs[0].item(), 3)
10 bert_dict['neu'] = round(probs[1].item(), 3)
11 bert_dict['pos'] = round(probs[2].item(), 3)
12 return bert_dict
13
14MODEL_NAME = 'RashidNLP/Amazon-Deberta-Base-Sentiment'
15device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
16
17bert_model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels = 3).to(device)
18tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
19
20get_sentiment("This is quite a mess you have made")
21