Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSequenceClassification
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
import numpy as np
|
4 |
+
from scipy.special import softmax
|
5 |
+
|
6 |
+
MODEL = "Davlan/naija-twitter-sentiment-afriberta-large"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
8 |
+
|
9 |
+
# PT
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
11 |
+
|
12 |
+
|
13 |
+
def get_senti(text):
|
14 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
15 |
+
output = model(**encoded_input)
|
16 |
+
scores = output[0][0].detach().numpy()
|
17 |
+
scores = softmax(scores)
|
18 |
+
|
19 |
+
id2label = {0:"positive✅", 1:"neutral😐", 2:"negative❌"}
|
20 |
+
|
21 |
+
ranking = np.argsort(scores)
|
22 |
+
ranking = ranking[::-1]
|
23 |
+
out = []
|
24 |
+
for i in range(scores.shape[0]):
|
25 |
+
l = id2label[ranking[i]]
|
26 |
+
s = scores[ranking[i]]
|
27 |
+
out.append(f"{i+1}) {l} {np.round(float(s), 4)}")
|
28 |
+
return "\n".join(out)
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=get_senti,
|
37 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter Words Here..."),
|
38 |
+
outputs="text",
|
39 |
+
)
|
40 |
+
demo.launch()
|