Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4model_name="agentlans/deberta-v3-xsmall-tweet-sentiment"
5
6# Put model on GPU or else CPU
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModelForSequenceClassification.from_pretrained(model_name)
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model = model.to(device)
11
12def sentiment(text):
13 """Processes the text using the model and returns its logits.
14 In this case, it's interpreted as the sentiment score for that text."""
15 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
16 with torch.no_grad():
17 logits = model(**inputs).logits.squeeze().cpu()
18 return logits.tolist()
19
20# Example usage
21text = [x.strip() for x in """
22 I absolutely despise this product and regret ever purchasing it.
23 The service at that restaurant was terrible and ruined our entire evening.
24 I'm feeling a bit under the weather today, but it's not too bad.
25 The weather is quite average today, neither good nor bad.
26 The movie was okay, I didn't love it but I didn't hate it either.
27 I'm looking forward to the weekend, it should be nice to relax.
28 This new coffee shop has a really pleasant atmosphere and friendly staff.
29 I'm thrilled with my new job and the opportunities it presents!
30 The concert last night was absolutely incredible, easily the best I've ever seen.
31 I'm overjoyed and grateful for all the love and support from my friends and family.
32""".strip().split("\n")]
33
34for x, s in zip(text, sentiment(text)):
35 print(f"Text: {x}\nSentiment: {round(s, 2)}\n")1Text: I absolutely despise this product and regret ever purchasing it.
2Sentiment: -2.28
3
4Text: The service at that restaurant was terrible and ruined our entire evening.
5Sentiment: -2.38
6
7Text: I'm feeling a bit under the weather today, but it's not too bad.
8Sentiment: 0.25
9
10Text: The weather is quite average today, neither good nor bad.
11Sentiment: -0.14
12
13Text: The movie was okay, I didn't love it but I didn't hate it either.
14Sentiment: 0.06
15
16Text: I'm looking forward to the weekend, it should be nice to relax.
17Sentiment: 2.06
18
19Text: This new coffee shop has a really pleasant atmosphere and friendly staff.
20Sentiment: 2.48
21
22Text: I'm thrilled with my new job and the opportunities it presents!
23Sentiment: 2.66
24
25Text: The concert last night was absolutely incredible, easily the best I've ever seen.
26Sentiment: 2.68
27
28Text: I'm overjoyed and grateful for all the love and support from my friends and family.
29Sentiment: 2.65