Spaces:
Build error
Build error
euvieeugenio
commited on
Commit
•
c501e89
1
Parent(s):
a2fc296
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from typing import List
|
4 |
+
from transformers import AutoTokenizer
|
5 |
+
from transformers import AutoModelForSequenceClassification
|
6 |
+
from transformers import TFAutoModelForSequenceClassification
|
7 |
+
from scipy.special import softmax
|
8 |
+
|
9 |
+
task = 'sentiment'
|
10 |
+
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
12 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
13 |
+
|
14 |
+
def polarity_scores_roberta(example):
|
15 |
+
encoded_text = tokenizer(example, truncation=True, return_tensors='pt')
|
16 |
+
output = model(**encoded_text)
|
17 |
+
scores = output[0][0].detach().numpy()
|
18 |
+
scores = softmax(scores)
|
19 |
+
scores_dict = {
|
20 |
+
'Roberta_NEG' : scores[0],
|
21 |
+
'Roberta_NEU' : scores[1],
|
22 |
+
'Roberta_POS' : scores[2]
|
23 |
+
}
|
24 |
+
return scores_dict
|
25 |
+
|
26 |
+
text = st.text_area('enter some texts!')
|
27 |
+
|
28 |
+
if text:
|
29 |
+
out = polarity_scores_roberta(text)
|
30 |
+
st.json(out)
|