Spaces:
Running
Running
Commit ·
b5678c8
1
Parent(s): efe12df
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,34 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def greet(name):
|
| 4 |
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
iface = gr.Interface(fn=
|
| 7 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer
|
| 3 |
+
from transformers import AutoModelForSequenceClassification
|
| 4 |
+
from scipy.special import softmax
|
| 5 |
+
|
| 6 |
+
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 9 |
+
|
| 10 |
+
def polarity_scores_roberta(example):
|
| 11 |
+
encoded_text = tokenizer(example, return_tensors='pt')
|
| 12 |
+
output = model(**encoded_text)
|
| 13 |
+
scores = output[0][0].detach().numpy()
|
| 14 |
+
scores = softmax(scores)
|
| 15 |
+
scores_dict = {
|
| 16 |
+
'roberta_neg' : scores[0],
|
| 17 |
+
'roberta_neu' : scores[1],
|
| 18 |
+
'roberta_pos' : scores[2]
|
| 19 |
+
}
|
| 20 |
+
x=max(scores[0],scores[1],scores[2])
|
| 21 |
+
if x==scores[0]:
|
| 22 |
+
print('Negative')
|
| 23 |
+
elif x==scores[1]:
|
| 24 |
+
print('Neutral')
|
| 25 |
+
else:
|
| 26 |
+
print('Positive')
|
| 27 |
+
|
| 28 |
+
|
| 29 |
|
| 30 |
def greet(name):
|
| 31 |
return "Hello " + name + "!!"
|
| 32 |
|
| 33 |
+
iface = gr.Interface(fn=polarity_scores_roberta, inputs="text", outputs="text")
|
| 34 |
iface.launch()
|