|
|
|
import streamlit as st |
|
import pandas as pd |
|
from youtube import get_video_comments |
|
from predict import predict_sentiments |
|
|
|
|
|
st.set_page_config( |
|
page_title="Youtube Sentiment Analysis", |
|
layout="centered" |
|
) |
|
|
|
|
|
|
|
def get_video(video_id): |
|
|
|
if not video_id: |
|
return {"error": "video_id is required"} |
|
|
|
comments = get_video_comments(video_id) |
|
predictions = predict_sentiments(comments) |
|
|
|
positive = predictions.count("Positive") |
|
negative = predictions.count("Negative") |
|
|
|
|
|
summary = { |
|
"num_comments": len(comments), |
|
"positive": positive, |
|
"negative": negative, |
|
"rating": (positive / len(comments)) * 100 if len(comments) > 0 else 0 |
|
|
|
} |
|
|
|
return {"predictions": predictions, "comments": comments, "summary": summary} |
|
|
|
|
|
|
|
def highlight_sentiment(row): |
|
|
|
if row['Sentiment'] == 'Positive': |
|
return ['background-color: #d1e7dd; color: #0f5132;'] * len(row) |
|
elif row['Sentiment'] == 'Negative': |
|
return ['background-color: #f5c6cb; color: #842b2e;'] * len(row) |
|
|
|
|
|
|
|
st.title("YOUTUBE SENTIMENT ANALYSIS") |
|
|
|
|
|
video_url = st.text_input("**Enter A YouTube Video Link**") |
|
|
|
|
|
st.markdown("**Examples of Valid YouTube Links:**") |
|
st.markdown("- https://www.youtube.com/watch?v=b5k8bkWYyPQ") |
|
st.markdown("- https://www.youtube.com/watch?v=b5k8bkWYyPQ&t=30s") |
|
st.markdown("- https://youtu.be/b5k8bkWYyPQ?si=gABWBqKdjo_um6nk") |
|
|
|
|
|
if st.button("Analyze"): |
|
if video_url: |
|
try: |
|
video_id = None |
|
|
|
if "youtube.com/watch?v=" in video_url: |
|
video_id = video_url.split("v=")[1].split("&")[0] |
|
elif "youtu.be" in video_url: |
|
video_id = video_url.split('/')[-1].split('?')[0] |
|
else: |
|
raise ValueError("Invalid YouTube link format!!!") |
|
|
|
data = get_video(video_id) |
|
|
|
if "error" in data: |
|
st.warning("Please enter a valid YouTube link!!!") |
|
else: |
|
summary = data['summary'] |
|
comments = list( |
|
zip(data['comments'], data['predictions'])) |
|
|
|
|
|
st.header("Summary") |
|
st.markdown(f"**Number of Comments:** {summary['num_comments']}") |
|
st.markdown(f"**Positive:** {summary['positive']}") |
|
st.markdown(f"**Negative:** {summary['negative']}") |
|
st.markdown(f"**Rating:** {summary['rating']:.2f}%") |
|
|
|
|
|
st.subheader("Comments") |
|
comments_df = pd.DataFrame(comments, columns=['Comment', 'Sentiment']) |
|
|
|
|
|
styled_comments_df = comments_df.style.apply(highlight_sentiment, axis=1) |
|
|
|
st.dataframe(styled_comments_df, width=700, use_container_width=True) |
|
|
|
except ValueError as ve: |
|
st.error(str(ve)) |
|
except (Exception,) as e: |
|
|
|
st.warning("An error occurred while processing your request. Please try again later!!!") |
|
else: |
|
st.warning("Please enter a valid YouTube link!!!") |
|
|