File size: 1,169 Bytes
879a213 15d8761 0e22a89 c098053 1435c47 879a213 0e22a89 e84e99c d10eab6 e84e99c 0e22a89 e84e99c d10eab6 e84e99c 0e22a89 1e6f560 870e8c5 879a213 0e22a89 879a213 e84e99c c098053 e84e99c 5d26f3a e84e99c |
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 |
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TextClassificationPipeline
import operator
import matplotlib.pyplot as plt
import pandas as pd
def get_sentiment(out):
d = dict()
for k in out:
print(k)
label = k['label']
score = k['score']
d[label] = score
winning_lab = max(d.items(), key=operator.itemgetter(1))[0]
winning_score = d[winning_lab]
df = pd.DataFrame.from_dict(d, orient = 'index')
df = df.reset_index().rename(columns={'index': 'sentiment', 0:'score'})
return df #winning_lab, winning_score
model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
text = st.text_area(f'Ciao! This app uses {model_name}.\nEnter your text to test it ❤️')
if text:
out = pipe(text)
df = get_sentiment(out[0])
fig, ax = plt.subplots()
ax.bar(df.index, df[0])
st.pyplot(fig)
#st.json(get_sentiment(out[0][0]))
|