Views
No views yet
1from transformers import pipeline
2
3MODEL = 'Cloudy1225/stackoverflow-roberta-base-sentiment'
4sentiment_task = pipeline(task="sentiment-analysis", model=MODEL)
5sentiment_task(["Excellent, happy to help!",
6 "This can probably be done using JavaScript.",
7 "Yes, but it's tricky, since datetime parsing in SQL is a pain in the neck."])[{'label': 'positive', 'score': 0.9997847676277161},
{'label': 'neutral', 'score': 0.999783456325531},
{'label': 'negative', 'score': 0.9996368885040283}]1from scipy.special import softmax
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
4def preprocess(text):
5 """Preprocess text (username and link placeholders)"""
6 new_text = []
7 for t in text.split(' '):
8 t = '@user' if t.startswith('@') and len(t) > 1 else t
9 t = 'http' if t.startswith('http') else t
10 new_text.append(t)
11 return ' '.join(new_text).strip()
12
13MODEL = 'Cloudy1225/stackoverflow-roberta-base-sentiment'
14tokenizer = AutoTokenizer.from_pretrained(MODEL)
15model = AutoModelForSequenceClassification.from_pretrained(MODEL)
16
17text = "Excellent, happy to help!"
18text = preprocess(text)
19encoded_input = tokenizer(text, return_tensors='pt')
20output = model(**encoded_input)
21scores = output[0][0].detach().numpy()
22scores = softmax(scores)
23print("negative", scores[0])
24print("neutral", scores[1])
25print("positive", scores[2])negative 0.00015578205
neutral 5.9470447e-05
positive 0.99978495@inproceedings{sun2022incorporating,
title={Incorporating Pre-trained Transformer Models into TextCNN for Sentiment Analysis on Software Engineering Texts},
author={Sun, Kexin and Shi, Xiaobo and Gao, Hui and Kuang, Hongyu and Ma, Xiaoxing and Rong, Guoping and Shao, Dong and Zhao, Zheng and Zhang, He},
booktitle={Proceedings of the 13th Asia-Pacific Symposium on Internetware},
pages={127--136},
year={2022}
}