Anthos23 commited on
Commit
e84e99c
1 Parent(s): 9a7476e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -1,18 +1,21 @@
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TextClassificationPipeline
3
  import operator
 
4
 
5
  def get_sentiment(out):
6
- print(out)
7
  d = dict()
8
- for k in out.keys():
9
- label = out[k]['label']
10
- score = out[k]['score']
11
- d[label] = score
12
-
13
- winning_lab = max(d.iteritems(), key=operator.itemgetter(1))[0]
 
14
  winning_score = d[winning_lab]
15
- return winning_lab, winning_score
 
 
16
 
17
  model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
18
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
@@ -24,5 +27,9 @@ text = st.text_area(f'Ciao! This app uses {model_name}.\nEnter your text to tes
24
 
25
  if text:
26
  out = pipe(text)
 
 
 
 
27
 
28
- st.json(get_sentiment(out[0][0]))
 
1
  import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TextClassificationPipeline
3
  import operator
4
+ import plotly.express as px
5
 
6
  def get_sentiment(out):
 
7
  d = dict()
8
+ for k in out:
9
+ print(k)
10
+ label = k['label']
11
+ score = k['score']
12
+ d[label] = score.reset_index().rename(columns={'index': 'sentiment', 0:'score'})
13
+
14
+ winning_lab = max(d.items(), key=operator.itemgetter(1))[0]
15
  winning_score = d[winning_lab]
16
+
17
+ df = pd.DataFrame.from_dict(d, orient = 'index')
18
+ return df #winning_lab, winning_score
19
 
20
  model_name = "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
21
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
 
27
 
28
  if text:
29
  out = pipe(text)
30
+ df = get_sentiment(out[0])
31
+ fig = px.pie(df, values='score', names='sentiment')
32
+
33
+ st.pyplot(fig)
34
 
35
+ #st.json(get_sentiment(out[0][0]))