Views
No views yet
1from transformers import AutoModelForSequenceClassification
2from transformers import AutoTokenizer
3import numpy as np
4from scipy.special import softmax
5
6MODEL = "Davlan/afrisenti-twitter-sentiment-afroxlmr-large"
7tokenizer = AutoTokenizer.from_pretrained(MODEL)
8
9# PT
10model = AutoModelForSequenceClassification.from_pretrained(MODEL)
11
12text = "I like you"
13encoded_input = tokenizer(text, return_tensors='pt')
14output = model(**encoded_input)
15scores = output[0][0].detach().numpy()
16scores = softmax(scores)
17
18id2label = {0:"positive", 1:"neutral", 2:"negative"}
19
20ranking = np.argsort(scores)
21ranking = ranking[::-1]
22for i in range(scores.shape[0]):
23 l = id2label[ranking[i]]
24 s = scores[ranking[i]]
25 print(f"{i+1}) {l} {np.round(float(s), 4)}")@article{Muhammad2023AfriSentiAT,
title={AfriSenti: A Twitter Sentiment Analysis Benchmark for African Languages},
author={Shamsuddeen Hassan Muhammad and Idris Abdulmumin and Abinew Ali Ayele and Nedjma Djouhra Ousidhoum and David Ifeoluwa Adelani and Seid Muhie Yimam and Ibrahim Said Ahmad and Meriem Beloucif and Saif M. Mohammad and Sebastian Ruder and Oumaima Hourrane and Pavel Brazdil and Felermino D'ario M'ario Ant'onio Ali and Davis C. Davis and Salomey Osei and Bello Shehu Bello and Falalu Ibrahim and Tajuddeen Rabiu Gwadabe and Samuel Rutunda and Tadesse Destaw Belay and Wendimu Baye Messelle and Hailu Beshada Balcha and Sisay Adugna Chala and Hagos Tesfahun Gebremichael and Bernard Opoku and Steven Arthur},
journal={ArXiv},
year={2023},
volume={abs/2302.08956},
url={https://api.semanticscholar.org/CorpusID:257019629}
}