File size: 4,041 Bytes
dfbb37f 3ab6a7b dfbb37f fb94850 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
import streamlit as st
st.set_page_config(page_title="YouTube Sentiment Analyzer π¬", layout="wide")
import pandas as pd
from multilingual_sentiment_model import (
extract_video_id, get_video_title, get_comments,
analyze_sentiment, plot_pie_chart, get_overall_sentiment
)
st.markdown("""
<style>
.video-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 5px; }
.video-row button { margin-left: 10px; }
</style>
""", unsafe_allow_html=True)
# Header
st.markdown("""
<h2 style='text-align: center; margin-bottom: 0;'>π¬ YouTube Sentiment Analyzer</h2>
<p style='text-align: center; font-size: 16px; margin-top: 0;'>Analyze viewer sentiments β Positive, Neutral or Negative</p>
<hr style="margin-top: 0; margin-bottom: 1.5rem;">
""", unsafe_allow_html=True)
# demo videos
example_videos = {
"https://www.youtube.com/watch?v=JGwWNGJdvx8": "Ed Sheeran - Shape of You",
"https://www.youtube.com/watch?v=dvgZkm1xWPE": "Coldplay - Viva La Vida",
"https://www.youtube.com/watch?v=YQHsXMglC9A": "Adele - Hello",
"https://www.youtube.com/watch?v=09R8_2nJtjg": "Maroon 5 - Sugar",
"https://www.youtube.com/watch?v=7wtfhZwyrcc": "Imagine Dragons - Believer"
}
col1, col2 = st.columns([1.2, 1.8])
with col1:
st.markdown("### π₯ Input")
if "url" not in st.session_state:
st.session_state["url"] = ""
url = st.text_input("YouTube Video URL", value=st.session_state["url"], key="youtube_url")
num_comments = st.slider("Number of Comments ", 10, 50, value=20, step=5)
analyze_btn = st.button("π Analyze")
st.markdown("##### π Example YouTube Videos")
st.text("Copy the links if you don't have any")
for link, title in example_videos.items():
# st.markdown(f"β‘οΈ **{title}**: {link}")
st.markdown(f"{link}")
with col2:
if analyze_btn:
video_id = extract_video_id(url)
if not video_id:
st.error("β Invalid YouTube URL.")
else:
with st.spinner("Fetching video title, comments and analyzing sentiment..."):
video_title = get_video_title(video_id)
comments, error = get_comments(video_id, max_results=num_comments)
if error:
st.error(f"β {error}")
elif not comments:
st.warning("β οΈ No comments found for this video.")
else:
st.markdown(f"### π₯ {video_title}", unsafe_allow_html=True)
results, counts = analyze_sentiment(comments)
sentiment_summary = get_overall_sentiment(counts)
pie_chart = plot_pie_chart(counts, video_title)
# Sentiment badge
color = '#66bb6a' if 'Positive' in sentiment_summary else '#ef5350' if 'Negative' in sentiment_summary else "#2cb1f4"
st.markdown(
f"<h4 style='color: {color}; text-align: center;'>{sentiment_summary}</h4>",
unsafe_allow_html=True
)
left, center, right = st.columns([1, 2, 1])
with center:
st.plotly_chart(pie_chart, use_container_width=False)
st.markdown("##### π¬ Top 5 Comments")
df_sample = pd.DataFrame(results).head(5)
st.table(df_sample[['Comment', 'Sentiment']])
csv = df_sample.to_csv(index=False).encode('utf-8')
st.download_button(
label="π₯ Download Top Comments as CSV",
data=csv,
file_name='top_comments.csv',
mime='text/csv'
)
st.markdown("<hr>", unsafe_allow_html=True)
st.markdown(
"<p style='text-align: center; font-size: 12px; color: gray;'>Β© 2025 YouTube Sentiment Analyzer | Built with β€οΈ using Streamlit</p>",
unsafe_allow_html=True
) |