Views
No views yet
1from transformers import pipeline
2sentiment_task = pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)
3sentiment_task("Covid cases are increasing fast!")[{'label': 'Negative', 'score': 0.7236}]1from transformers import AutoModelForSequenceClassification
2from transformers import TFAutoModelForSequenceClassification
3from transformers import AutoTokenizer, AutoConfig
4import numpy as np
5from scipy.special import softmax
6# Preprocess text (username and link placeholders)
7def preprocess(text):
8 new_text = []
9 for t in text.split(" "):
10 t = '@user' if t.startswith('@') and len(t) > 1 else t
11 t = 'http' if t.startswith('http') else t
12 new_text.append(t)
13 return " ".join(new_text)
14MODEL = f"cardiffnlp/twitter-roberta-base-sentiment-latest"
15tokenizer = AutoTokenizer.from_pretrained(MODEL)
16config = AutoConfig.from_pretrained(MODEL)
17# PT
18model = AutoModelForSequenceClassification.from_pretrained(MODEL)
19#model.save_pretrained(MODEL)
20text = "Covid cases are increasing fast!"
21text = preprocess(text)
22encoded_input = tokenizer(text, return_tensors='pt')
23output = model(**encoded_input)
24scores = output[0][0].detach().numpy()
25scores = softmax(scores)
26# # TF
27# model = TFAutoModelForSequenceClassification.from_pretrained(MODEL)
28# model.save_pretrained(MODEL)
29# text = "Covid cases are increasing fast!"
30# encoded_input = tokenizer(text, return_tensors='tf')
31# output = model(encoded_input)
32# scores = output[0][0].numpy()
33# scores = softmax(scores)
34# Print labels and scores
35ranking = np.argsort(scores)
36ranking = ranking[::-1]
37for i in range(scores.shape[0]):
38 l = config.id2label[ranking[i]]
39 s = scores[ranking[i]]
40 print(f"{i+1}) {l} {np.round(float(s), 4)}")1) Negative 0.7236
2) Neutral 0.2287
3) Positive 0.0477@inproceedings{camacho-collados-etal-2022-tweetnlp,
title = "{T}weet{NLP}: Cutting-Edge Natural Language Processing for Social Media",
author = "Camacho-collados, Jose and
Rezaee, Kiamehr and
Riahi, Talayeh and
Ushio, Asahi and
Loureiro, Daniel and
Antypas, Dimosthenis and
Boisson, Joanne and
Espinosa Anke, Luis and
Liu, Fangyu and
Mart{\'\i}nez C{\'a}mara, Eugenio" and others,
booktitle = "Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing: System Demonstrations",
month = dec,
year = "2022",
address = "Abu Dhabi, UAE",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2022.emnlp-demos.5",
pages = "38--49"
}
@inproceedings{loureiro-etal-2022-timelms,
title = "{T}ime{LM}s: Diachronic Language Models from {T}witter",
author = "Loureiro, Daniel and
Barbieri, Francesco and
Neves, Leonardo and
Espinosa Anke, Luis and
Camacho-collados, Jose",
booktitle = "Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics: System Demonstrations",
month = may,
year = "2022",
address = "Dublin, Ireland",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2022.acl-demo.25",
doi = "10.18653/v1/2022.acl-demo.25",
pages = "251--260"
}