Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,25 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TextClassificationPipeline
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
|
| 5 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
@@ -8,7 +27,8 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
| 8 |
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 9 |
text = st.text_area(f'Ciao! This app uses {model_name}.\nEnter your text to test it ❤️')
|
| 10 |
|
|
|
|
| 11 |
if text:
|
| 12 |
out = pipe(text)
|
| 13 |
|
| 14 |
-
st.json(out[0]
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TextClassificationPipeline
|
| 3 |
+
import operator
|
| 4 |
|
| 5 |
+
def get_sentiment(out):
|
| 6 |
+
d = dict()
|
| 7 |
+
for k in out.keys():
|
| 8 |
+
label = out[k]['label']
|
| 9 |
+
score = out[k]['score']
|
| 10 |
+
d[label] = score
|
| 11 |
+
|
| 12 |
+
winning_lab = max(d.iteritems(), key=operator.itemgetter(1))[0]
|
| 13 |
+
winning_score = d[winning_lab]
|
| 14 |
+
return winning_lab, winning_score
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
neg = out[0]
|
| 19 |
+
neu = out[1]
|
| 20 |
+
pos = out[2]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
|
| 24 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 25 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 27 |
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 28 |
text = st.text_area(f'Ciao! This app uses {model_name}.\nEnter your text to test it ❤️')
|
| 29 |
|
| 30 |
+
|
| 31 |
if text:
|
| 32 |
out = pipe(text)
|
| 33 |
|
| 34 |
+
st.json(get_sentiment(out[0])
|