import streamlit as st import pandas as pd import plotly_express as px import plotly.graph_objects as go from functions import * import validators st.set_page_config(page_title="Earnings Sentiment Analysis", page_icon="📈") st.sidebar.header("Sentiment Analysis") st.markdown("## Earnings Sentiment Analysis with FinBert-Tone") if st.session_state.url or st.session_state.upload: results, title = inference(st.session_state.url,st.session_state.upload) st.subheader(title) earnings_passages = results['text'] st.session_state['earnings_passages'] = earnings_passages with open('earnings.txt','w') as f: f.write(earnings_passages) with open('earnings.txt','r') as f: earnings_passages = f.read() earnings_sentiment, earnings_sentences = sentiment_pipe(earnings_passages) with st.expander("See Transcribed Earnings Text"): st.write(f"Number of Sentences: {len(earnings_sentences)}") st.write(earnings_passages) ## Save to a dataframe for ease of visualization sen_df = pd.DataFrame(earnings_sentiment) sen_df['text'] = earnings_sentences grouped = pd.DataFrame(sen_df['label'].value_counts()).reset_index() grouped.columns = ['sentiment','count'] # Display number of positive, negative and neutral sentiments fig = px.bar(grouped, x='sentiment', y='count', color='sentiment', color_discrete_map={"Negative":"firebrick","Neutral":\ "navajowhite","Positive":"darkgreen"},\ title='Earnings Sentiment') fig.update_layout( showlegend=False, autosize=True, margin=dict( l=25, r=25, b=25, t=50, pad=2 ) ) st.plotly_chart(fig) ## Display sentiment score pos_perc = grouped[grouped['sentiment']=='Positive']['count'].iloc[0]*100/sen_df.shape[0] neg_perc = grouped[grouped['sentiment']=='Negative']['count'].iloc[0]*100/sen_df.shape[0] neu_perc = grouped[grouped['sentiment']=='Neutral']['count'].iloc[0]*100/sen_df.shape[0] sentiment_score = neu_perc+pos_perc-neg_perc fig_1 = go.Figure() fig_1.add_trace(go.Indicator( mode = "delta", value = sentiment_score, domain = {'row': 1, 'column': 1})) fig_1.update_layout( template = {'data' : {'indicator': [{ 'title': {'text': "Sentiment Score"}, 'mode' : "number+delta+gauge", 'delta' : {'reference': 50}}] }}, autosize=False, width=250, height=250, margin=dict( l=5, r=5, b=5, pad=2 ) ) with st.sidebar: st.plotly_chart(fig_1) ## Display negative sentence locations 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') fig.update_layout( showlegend=False, autosize=True, width=1000, height=500, margin=dict( b=5, t=50, pad=4 ) ) st.plotly_chart(fig) else: st.write("No YouTube URL or file upload detected")