File size: 1,164 Bytes
2729775
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e52e8cc
 
2729775
e52e8cc
2729775
 
 
 
 
 
 
 
e52e8cc
2729775
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer
import numpy as np
from scipy.special import softmax

MODEL = "Davlan/naija-twitter-sentiment-afriberta-large"
tokenizer = AutoTokenizer.from_pretrained(MODEL)

# PT
model = AutoModelForSequenceClassification.from_pretrained(MODEL)


def get_senti(text):
    encoded_input = tokenizer(text, return_tensors='pt')
    output = model(**encoded_input)
    scores = output[0][0].detach().numpy()
    scores = softmax(scores)
    
    id2label = {0:"positive✅", 1:"neutral😐", 2:"negative❌"}
    
    ranking = np.argsort(scores)
    ranking = ranking[::-1]
    out = []
    for i in range(scores.shape[0]):
        l = id2label[ranking[i]]
        s = scores[ranking[i]]
        out.append(f"{i+1}) {l} {np.round(float(s), 4)}")
    
    out.append('\nNOTE: "The higher the values, the higher the sentiment for that class & vice-versa"')
    return "\n".join(out)
    



import gradio as gr


demo = gr.Interface(
    fn=get_senti,
    inputs=gr.Textbox(lines=2, placeholder="Enter Words Here (Igbo, Yoruba, Hausa Pidgin)..."),
    outputs="text",
)
demo.launch()