Spaces:
Build error
Build error
from variables import * | |
import plotly_express as px | |
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator | |
import matplotlib.pyplot as plt | |
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
import textwrap | |
st.set_option('deprecation.showPyplotGlobalUse', False) | |
#st.set_page_config(page_title="Earnings Sentiment Analysis", page_icon="π") | |
st.sidebar.header("Sentiment Analysis Visualization") | |
st.markdown("## Sentiment Analysis and Density Graphs") | |
max_word = st.sidebar.slider(label= "WordCloud Max Words", min_value=20, max_value=500, value=50) | |
max_font = st.sidebar.slider(label = "WordCloud Max Font", min_value=50, max_value=350, value=50) | |
stopwords = set(STOPWORDS) | |
stopwords.update(['us', 'one', 'will', 'said', 'now', 'well', 'man', 'may', | |
'little', 'say', 'must', 'way', 'long', 'yet', 'mean', | |
'put', 'seem', 'asked', 'made', 'half', 'much', | |
'certainly', 'might', 'came','RT','amp']) | |
def cloud(text, max_word, max_font, random): | |
'''Generate Word Cloud''' | |
wc = WordCloud(background_color="white", colormap="hot", max_words=max_word, | |
stopwords=stopwords, max_font_size=max_font, random_state=random).generate(text) | |
return wc | |
try: | |
if 'tdf' in st.session_state: | |
df = st.session_state['tdf'] | |
# df['creation_date'] = pd.to_datetime(df['creation_date'], | |
# format='%Y-%m-%d %H:%M:%S-%Z', | |
# errors='coerce').dt.date | |
with st.comtainer(): | |
st.subheader('Sentiment Scatter Plot') | |
## Display negative sentence locations | |
ht = df.tweet.apply(lambda txt: '<br>'.join(textwrap.wrap(txt, width=70))) | |
fig = px.scatter(df, y='sentiment', x='creation_time', color='topic', size='sentiment_confidence', hover_data=[ht,'topic'], \ | |
color_discrete_map={"Bearish":"firebrick","Neutral":"navajowhite","Bullish":"darkgreen"}, \ | |
title='Sentiment Score Distribution') | |
fig.update_layout( | |
showlegend=False, | |
autosize=True, | |
width=1000, | |
height=500, | |
margin=dict( | |
b=5, | |
t=50, | |
pad=2 | |
) | |
) | |
st.plotly_chart(fig) | |
with st.container(): | |
st.subheader('Topic Distribution Scatter Plot') | |
## Display negative sentence locations | |
ht = df.tweet.apply(lambda txt: '<br>'.join(textwrap.wrap(txt, width=70))) | |
fig = px.scatter(df, y='topic', x='creation_time', color='sentiment', size='topic_confidence', hover_data=[ht,'sentiment'],\ | |
title='Topic Score Distribution') | |
fig.update_layout( | |
showlegend=False, | |
autosize=True, | |
width=1000, | |
height=500, | |
margin=dict( | |
b=5, | |
t=50, | |
pad=2 | |
) | |
) | |
st.plotly_chart(fig) | |
with st.container(): | |
st.subheader('Sentiment WordCloud') | |
cleaned_tweets = "".join(df['tweet'].tolist()) | |
wc = cloud(cleaned_tweets, max_word, max_font, 35) | |
plt.imshow(wc, interpolation='bilinear') | |
plt.axis("off") | |
plt.show() | |
st.pyplot() | |
except (AttributeError, KeyError) as e: | |
print(e) | |
st.error('Tweets Error, please navigate to Home page and refresh tweet stream', icon="π¨") |