Update pages/1_Earnings_Sentiment_Analysis_π_.py
Browse files
pages/1_Earnings_Sentiment_Analysis_π_.py
CHANGED
@@ -9,104 +9,107 @@ st.set_page_config(page_title="Earnings Sentiment Analysis", page_icon="π")
|
|
9 |
st.sidebar.header("Sentiment Analysis")
|
10 |
st.markdown("## Earnings Sentiment Analysis with FinBert-Tone")
|
11 |
|
12 |
-
if st.session_state.url
|
13 |
|
14 |
results, title = inference(st.session_state.url,st.session_state.upload)
|
15 |
|
16 |
-
st.subheader(title)
|
17 |
-
|
18 |
-
earnings_passages = results['text']
|
19 |
-
|
20 |
-
with open('earnings.txt','w') as f:
|
21 |
-
f.write(earnings_passages)
|
22 |
|
23 |
-
|
24 |
-
earnings_passages = f.read()
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
st.
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
)
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
)
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
)
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
9 |
st.sidebar.header("Sentiment Analysis")
|
10 |
st.markdown("## Earnings Sentiment Analysis with FinBert-Tone")
|
11 |
|
12 |
+
if st.session_state.url or st.session_state.upload:
|
13 |
|
14 |
results, title = inference(st.session_state.url,st.session_state.upload)
|
15 |
|
16 |
+
st.subheader(title)
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
earnings_passages = results['text']
|
|
|
19 |
|
20 |
+
with open('earnings.txt','w') as f:
|
21 |
+
f.write(earnings_passages)
|
22 |
+
|
23 |
+
with open('earnings.txt','r') as f:
|
24 |
+
earnings_passages = f.read()
|
25 |
+
|
26 |
+
earnings_sentiment, earnings_sentences = sent_pipe(earnings_passages)
|
27 |
|
28 |
+
with st.expander("See Transcribed Earnings Text"):
|
29 |
+
st.write(f"Number of Sentences: {len(earnings_ssentences)}")
|
30 |
+
|
31 |
+
st.write(earnings_passages)
|
32 |
+
|
33 |
+
|
34 |
+
## Save to a dataframe for ease of visualization
|
35 |
+
sen_df = pd.DataFrame(earnings_sentiment)
|
36 |
+
sen_df['text'] = earnings_sentences
|
37 |
+
grouped = pd.DataFrame(sen_df['label'].value_counts()).reset_index()
|
38 |
+
grouped.columns = ['sentiment','count']
|
39 |
+
|
40 |
+
# Display number of positive, negative and neutral sentiments
|
41 |
+
fig = px.bar(grouped, x='sentiment', y='count', color='sentiment', color_discrete_map={"Negative":"firebrick","Neutral":\
|
42 |
+
"navajowhite","Positive":"darkgreen"},\
|
43 |
+
title='Earnings Sentiment')
|
44 |
+
|
45 |
+
fig.update_layout(
|
46 |
+
showlegend=False,
|
47 |
+
autosize=True,
|
48 |
+
margin=dict(
|
49 |
+
l=50,
|
50 |
+
r=50,
|
51 |
+
b=50,
|
52 |
+
t=50,
|
53 |
+
pad=4
|
54 |
+
)
|
55 |
)
|
56 |
+
|
57 |
+
col1, col2 = st.columns(2)
|
58 |
+
|
59 |
+
col1.plotly_chart(fig)
|
60 |
+
|
61 |
+
## Display sentiment score
|
62 |
+
pos_perc = grouped[grouped['sentiment']=='Positive']['count'].iloc[0]*100/sen_df.shape[0]
|
63 |
+
neg_perc = grouped[grouped['sentiment']=='Negative']['count'].iloc[0]*100/sen_df.shape[0]
|
64 |
+
neu_perc = grouped[grouped['sentiment']=='Neutral']['count'].iloc[0]*100/sen_df.shape[0]
|
65 |
+
|
66 |
+
sentiment_score = neu_perc+pos_perc-neg_perc
|
67 |
+
|
68 |
+
fig = go.Figure()
|
69 |
+
|
70 |
+
fig.add_trace(go.Indicator(
|
71 |
+
mode = "delta",
|
72 |
+
value = sentiment_score,
|
73 |
+
domain = {'row': 1, 'column': 1}))
|
74 |
+
|
75 |
+
fig.update_layout(
|
76 |
+
template = {'data' : {'indicator': [{
|
77 |
+
'title': {'text': "Sentiment score"},
|
78 |
+
'mode' : "number+delta+gauge",
|
79 |
+
'delta' : {'reference': 50}}]
|
80 |
+
}},
|
81 |
+
autosize=False,
|
82 |
+
width=400,
|
83 |
+
height=500,
|
84 |
+
margin=dict(
|
85 |
+
l=20,
|
86 |
+
r=50,
|
87 |
+
b=50,
|
88 |
+
pad=4
|
89 |
+
)
|
90 |
)
|
91 |
+
|
92 |
+
col2.plotly_chart(fig)
|
93 |
+
|
94 |
+
## Display negative sentence locations
|
95 |
+
fig = px.scatter(sen_df, y='label', color='label', size='score', hover_data=['text'], color_discrete_map={"Negative":"firebrick","Neutral":"navajowhite","Positive":"darkgreen"}, title='Sentiment Score Distribution')
|
96 |
+
|
97 |
+
|
98 |
+
fig.update_layout(
|
99 |
+
showlegend=False,
|
100 |
+
autosize=False,
|
101 |
+
width=1000,
|
102 |
+
height=500,
|
103 |
+
margin=dict(
|
104 |
+
l=50,
|
105 |
+
r=50,
|
106 |
+
b=50,
|
107 |
+
t=50,
|
108 |
+
pad=4
|
109 |
+
)
|
110 |
)
|
111 |
+
|
112 |
+
st.plotly_chart(fig)
|
113 |
+
|
114 |
+
else:
|
115 |
+
st.write("No YouTube URL or file upload detected")
|