Spaces:
Runtime error
Runtime error
import streamlit as st | |
st.set_page_config( | |
page_title="Social Media Sentiment Analyzer", page_icon="π", layout="wide" | |
) | |
import pandas as pd | |
import helper_functions as hf | |
import plotly.express as px | |
import plotly.io as pio | |
import plotly | |
# Whenever the search button is clicked, the search_callback function is called | |
def search_callback_twitter(): | |
if platform == "Twitter": | |
if len(st.session_state.search_term_twitter) == 0: | |
st.error("Please enter a search term") | |
return | |
try: | |
st.session_state.df = hf.get_tweets(st.session_state.search_term_twitter, st.session_state.num_tweets) | |
st.session_state.df = hf.get_sentiment(st.session_state.df) | |
except: | |
st.error("Please enter a valid search term") | |
return | |
def search_callback_youtube(): | |
if platform == "Youtube": | |
if len(st.session_state.search_term_youtube) == 0: | |
st.error("Please enter a valid url") | |
return | |
try: | |
st.session_state.df = hf.get_youtube_comments(st.session_state.search_term_youtube, st.session_state.num_comments) | |
st.session_state.df = hf.get_sentiment_youtube(st.session_state.df) | |
except: | |
st.error("Please enter a valid url") | |
return | |
def twitter_form(): | |
with st.form(key="search_form"): | |
st.subheader("Search Parameters") | |
st.text_input("Enter a User handle (like _@elonmusk_), Hashtag (like _#Bitcoin_) or Topic (like _climate change_)", key="search_term_twitter") | |
st.slider("Number of tweets", min_value=100, max_value=500, key="num_tweets") | |
st.form_submit_button(label="Search", on_click=search_callback_twitter) | |
st.markdown( | |
"Note: it may take a while to load the results, especially with large number of tweets" | |
) | |
def youtube_form(): | |
with st.form(key="search_form"): | |
st.subheader("Search Parameters") | |
st.text_input("Enter a Video link to analyse comments", key="search_term_youtube") | |
st.slider("Number of Comments", min_value=100, max_value=500, key="num_comments") | |
st.form_submit_button(label="Search", on_click=search_callback_youtube) | |
st.markdown( | |
"Note: it may take a while to load the results, especially with large number of comments" | |
) | |
with st.sidebar: | |
st.title("Social Media Sentiment Analyzer") | |
#st.subheader("Choose your platform") | |
platform = st.radio( | |
"Choose your platform π", | |
["Twitter", "Youtube"], | |
# key="visibility", | |
# label_visibility=st.session_state.visibility, | |
# disabled=st.session_state.disabled, | |
horizontal=True, | |
) | |
if platform == "Twitter": | |
twitter_form() | |
if platform == "Youtube": | |
youtube_form() | |
st.markdown( | |
"<div style='position: fixed; bottom: 0;'>Created by Taaha Bajwa</div>", | |
unsafe_allow_html=True, | |
) | |
if "df" in st.session_state: | |
def make_dashboard(tweet_df, bar_color, wc_color): | |
# first row | |
col1, col2, col3 = st.columns([28, 34, 38]) | |
with col1: | |
sentiment_plot = hf.plot_sentiment(tweet_df) | |
sentiment_plot.update_layout(height=350, title_x=0.5) | |
st.plotly_chart(sentiment_plot, theme=None, use_container_width=True) | |
with col2: | |
top_unigram = hf.get_top_n_gram(tweet_df, ngram_range=(1, 1), n=10) | |
unigram_plot = hf.plot_n_gram( | |
top_unigram, title="Top 10 Occuring Words", color=bar_color | |
) | |
unigram_plot.update_layout(height=350) | |
st.plotly_chart(unigram_plot, theme=None, use_container_width=True) | |
with col3: | |
top_bigram = hf.get_top_n_gram(tweet_df, ngram_range=(2, 2), n=10) | |
bigram_plot = hf.plot_n_gram( | |
top_bigram, title="Top 10 Occuring Bigrams", color=bar_color | |
) | |
bigram_plot.update_layout(height=350) | |
st.plotly_chart(bigram_plot, theme=None, use_container_width=True) | |
# second row | |
col1, col2 = st.columns([60, 40]) | |
with col1: | |
def sentiment_color(sentiment): | |
if sentiment == "Positive": | |
return "background-color: #54A24B; color: white" | |
elif sentiment == "Negative": | |
return "background-color: #FF7F0E" | |
else: | |
return "background-color: #1F77B4" | |
st.dataframe( | |
tweet_df[["Sentiment", "Tweet"]].style.applymap( | |
sentiment_color, subset=["Sentiment"] | |
), | |
height=350, | |
) | |
with col2: | |
wordcloud = hf.plot_wordcloud(tweet_df, colormap=wc_color) | |
st.pyplot(wordcloud) | |
def make_dashboard_youtube(tweet_df, bar_color, wc_color): | |
tweet_df = tweet_df.rename(columns={"Comment": "Tweet"}) | |
# first row | |
col1, col2, col3 = st.columns([28, 34, 38]) | |
with col1: | |
sentiment_plot = hf.plot_sentiment(tweet_df) | |
sentiment_plot.update_layout(height=350, title_x=0.5) | |
st.plotly_chart(sentiment_plot, theme=None, use_container_width=True) | |
with col2: | |
top_unigram = hf.get_top_n_gram(tweet_df, ngram_range=(1, 1), n=10) | |
unigram_plot = hf.plot_n_gram( | |
top_unigram, title="Top 10 Occuring Words", color=bar_color | |
) | |
unigram_plot.update_layout(height=350) | |
st.plotly_chart(unigram_plot, theme=None, use_container_width=True) | |
with col3: | |
top_bigram = hf.get_top_n_gram(tweet_df, ngram_range=(2, 2), n=10) | |
bigram_plot = hf.plot_n_gram( | |
top_bigram, title="Top 10 Occuring Bigrams", color=bar_color | |
) | |
bigram_plot.update_layout(height=350) | |
st.plotly_chart(bigram_plot, theme=None, use_container_width=True) | |
# second row | |
col1, col2 = st.columns([60, 40]) | |
with col1: | |
def sentiment_color(sentiment): | |
if sentiment == "Positive": | |
return "background-color: #54A24B; color: white" | |
elif sentiment == "Negative": | |
return "background-color: #FF7F0E" | |
else: | |
return "background-color: #1F77B4" | |
tweet_df_temp = tweet_df[["Sentiment", "Tweet"]] | |
tweet_df_temp = tweet_df_temp.rename(columns={"Tweet": "Comment"}) | |
st.dataframe( | |
tweet_df_temp[["Sentiment", "Comment"]].style.applymap( | |
sentiment_color, subset=["Sentiment"] | |
), | |
height=350, | |
) | |
with col2: | |
wordcloud = hf.plot_wordcloud(tweet_df, colormap=wc_color, mask_url='static/yt_mask.png') | |
try: | |
st.pyplot(wordcloud) | |
except: | |
st.write("Wordcloud not available for this search term") | |
adjust_tab_font = """ | |
<style> | |
button[data-baseweb="tab"] > div[data-testid="stMarkdownContainer"] > p { | |
font-size: 20px; | |
} | |
</style> | |
""" | |
st.write(adjust_tab_font, unsafe_allow_html=True) | |
if platform == "Twitter" and st.session_state.search_term_twitter != "": | |
try: | |
tab1, tab2, tab3, tab4 = st.tabs(["All", "Positive π", "Negative βΉοΈ", "Neutral π"]) | |
with tab1: | |
tweet_df = st.session_state.df | |
if tweet_df.shape[0] > 0: | |
make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues") | |
else: | |
st.write("No tweets to display.") | |
with tab2: | |
tweet_df = st.session_state.df.query("Sentiment == 'Positive'") | |
if tweet_df.shape[0] > 0: | |
make_dashboard(tweet_df, bar_color="#54A24B", wc_color="Greens") | |
else: | |
st.write("No tweets to display.") | |
with tab3: | |
tweet_df = st.session_state.df.query("Sentiment == 'Negative'") | |
if tweet_df.shape[0] > 0: | |
make_dashboard(tweet_df, bar_color="#FF7F0E", wc_color="Oranges") | |
else: | |
st.write("No tweets to display.") | |
with tab4: | |
tweet_df = st.session_state.df.query("Sentiment == 'Neutral'") | |
if tweet_df.shape[0] > 0: | |
make_dashboard(tweet_df, bar_color="#1F77B4", wc_color="Blues") | |
else: | |
st.write("No tweets to display.") | |
except: | |
st.error("No plots to display.") | |
elif platform == "Youtube" and st.session_state.search_term_youtube != "": | |
try: | |
tab1, tab2, tab3, tab4 = st.tabs(["All", "Positive π", "Negative βΉοΈ", "Neutral π"]) | |
with tab1: | |
tweet_df = st.session_state.df | |
if tweet_df.shape[0] > 0: | |
make_dashboard_youtube(tweet_df, bar_color="#1F77B4", wc_color="Blues") | |
else: | |
st.write("No comments found.") | |
with tab2: | |
tweet_df = st.session_state.df.query("Sentiment == 'Positive'") | |
if tweet_df.shape[0] > 0: | |
make_dashboard_youtube(tweet_df, bar_color="#54A24B", wc_color="Greens") | |
else: | |
st.write("No positive comments found.") | |
with tab3: | |
tweet_df = st.session_state.df.query("Sentiment == 'Negative'") | |
if tweet_df.shape[0] > 0: | |
make_dashboard_youtube(tweet_df, bar_color="#FF7F0E", wc_color="Oranges") | |
else: | |
st.write("No negative comments found.") | |
with tab4: | |
tweet_df = st.session_state.df.query("Sentiment == 'Neutral'") | |
if tweet_df.shape[0] > 0: | |
make_dashboard_youtube(tweet_df, bar_color="#1F77B4", wc_color="Blues") | |
else: | |
st.write("No neutral comments found.") | |
except: | |
st.error("No plots to display.") |