File size: 2,908 Bytes
c13c0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from helper import extract_youtube_id, get_all_comments
import streamlit as st
import random
import pandas as pd
import numpy as np

st.header("💬 Youtube Comments Sentiment Analysis")

st.markdown("""
    <style>
      .stTextInput > label > div > p {
        font-size: 23px;
        padding: 0;
        margin: 0;
        font-weight: 600;
      }
    </style>
""", unsafe_allow_html=True)

user_input = st.text_input("Enter a youtube link for sentiment analysis")

sentiment_colors = {
    "Positive": "#28a745",  
    "Neutral": "#ffc107",   
    "Negative": "#dc3545"   
}

if st.button('Submit', type="secondary"):
  sentiments = ["Positive", "Neutral", "Negative"]
  try:
    the_youtube_id = extract_youtube_id(user_input)
    if the_youtube_id:
      with st.spinner("Please wait while we're loading the data..."):
        the_data = get_all_comments(the_youtube_id)
        data = np.random.choice(["Positive", "Neutral", "Negative"], size=len(the_data))

        # Create a DataFrame
        sentiment_data = pd.DataFrame(data, columns=["Sentiment"])
        sentiment_counts = sentiment_data["Sentiment"].value_counts()
        positives = sentiment_counts.get("Positive", 0)
        neutrals = sentiment_counts.get("Neutral", 0)
        negatives = sentiment_counts.get("Negative", 0)
        st.balloons()
        st.markdown(f"""<p style="color: gray;">Total comments: {len(the_data)}</p>""", unsafe_allow_html=True)
        st.markdown(f"""<p style="color: green;">Positives: {positives}</p>""", unsafe_allow_html=True)
        st.markdown(f"""<p style="color: gray;">Neutrals: {neutrals}</p>""", unsafe_allow_html=True)
        st.markdown(f"""<p style="color: red;">Negatives: {negatives}</p>""", unsafe_allow_html=True)
        for index, data in enumerate(the_data):
          sentiment_color = sentiment_colors.get(sentiment_data.iloc[index, 0], "#6c757d")
          comment_html = f"""
              <div style="background-color: #f9f9f9; border: 1px solid #ddd; border-radius: 10px; padding: 20px; margin: 20px auto; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
                <p style="font-size: 18px; line-height: 1.6; color: #333; font-family: 'Arial', sans-serif;">
                  {data["comment"]}
                </p>
                <p style="font-size: 16px; color: gray; margin-top: 15px; font-family: 'Arial', sans-serif; font-weight: bold;">
                  Sentiment Analysis: <span style="color: {sentiment_color}; font-size: 18px; font-weight: bold; padding: 5px 10px; background-color: {sentiment_color + "33"}; border-radius: 5px;">
                    {sentiment_data.iloc[index, 0]}
                  </span>
                </p>
              </div>
            """

          st.markdown(comment_html, unsafe_allow_html=True)
    else:
      st.write("Invalid youtube link.")
  except Exception as e:
    print(e)
    st.write("Invalid youtube link.")