File size: 4,632 Bytes
ba3c54c
 
 
 
 
 
9e01c0a
ba3c54c
 
36d2023
9e01c0a
 
 
 
ba3c54c
9e01c0a
ba3c54c
34c54ab
9e01c0a
 
 
ba3c54c
9e01c0a
ba3c54c
9e01c0a
ba3c54c
 
9e01c0a
 
 
 
 
ba3c54c
9e01c0a
ba3c54c
 
 
 
9e01c0a
 
 
 
 
ba3c54c
9e01c0a
ba3c54c
 
 
 
9e01c0a
 
 
ba3c54c
9e01c0a
 
 
ba3c54c
9e01c0a
 
 
 
 
 
 
ba3c54c
 
9e01c0a
 
 
 
 
ba3c54c
9e01c0a
 
ba3c54c
 
9e01c0a
34c54ab
 
9e01c0a
 
 
a3a5584
 
 
9e01c0a
a3a5584
9e01c0a
a3a5584
9e01c0a
 
 
 
 
34c54ab
 
 
 
 
 
 
 
 
 
 
 
 
9e01c0a
 
34c54ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e01c0a
34c54ab
ba3c54c
34c54ab
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import streamlit as st
import pandas as pd
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from textblob import TextBlob
from transformers import pipeline
import matplotlib.pyplot as plt
import base64
import os
from wordcloud import WordCloud

# Function to perform sentiment analysis using Hugging Face model
hf_sentiment_analyzer = pipeline(
    "sentiment-analysis", "Dmyadav2001/Sentimental-Analysis"
)

def analyze_hf_sentiment(text):
    if len(text) > 512:
        text = text[:511]
    result = hf_sentiment_analyzer(text)
    label = result[0]["label"]
    if label == "LABEL_1":
        return "Positive"
    elif label == "LABEL_0":
        return "Negative"
    elif label == "LABEL_2":
        return "Neutral"

# Function to perform sentiment analysis using VADER
def analyze_vader_sentiment(text):
    analyzer = SentimentIntensityAnalyzer()
    vader_score = analyzer.polarity_scores(text)["compound"]
    if vader_score > 0:
        return "Positive"
    elif vader_score == 0:
        return "Neutral"
    else:
        return "Negative"

# Function to perform sentiment analysis using TextBlob
def analyze_textblob_sentiment(text):
    analysis = TextBlob(text)
    sentiment_score = analysis.sentiment.polarity
    if sentiment_score > 0:
        return "Positive"
    elif sentiment_score == 0:
        return "Neutral"
    else:
        return "Negative"

# Function to display DataFrame with updated sentiment column
def display_dataframe(df):
    st.write(df)

# Function to display pie chart for sentiment distribution
def display_pie_chart(df, column):
    sentiment_counts = df[column].value_counts()
    fig, ax = plt.subplots()
    ax.pie(
        sentiment_counts,
        labels=sentiment_counts.index,
        autopct="%1.1f%%",
        startangle=140,
    )
    ax.axis("equal")
    st.pyplot(fig)

# Function to display word cloud
def display_wordcloud(text_data):
    wordcloud = WordCloud(width=800, height=400, background_color="white").generate(
        text_data
    )
    fig, ax = plt.subplots(figsize=(10, 5))
    ax.imshow(wordcloud, interpolation="bilinear")
    ax.axis("off")
    st.pyplot(fig)

# Streamlit UI
st.set_page_config(page_title="Sentiment Analysis App", page_icon=":smiley:")
st.title("Sentiment Analysis App")

# Sidebar
st.sidebar.title("Options")
input_option = st.sidebar.selectbox("Select Input Option", ["Free Text", "CSV Files"])
selected_model = st.sidebar.selectbox(
    "Select Sentiment Analysis Model", ["VADER", "TextBlob", "Hugging Face"]
)
result_option = st.sidebar.selectbox(
    "Select Result Display Option",
    ["DataFrame", "Pie Chart", "Bar Chart", "Keyword Frequency", "Word Cloud", "Comparative Sentiment Analysis"],
)

# Main content
if input_option == "Free Text":
    st.subheader("Enter review for sentiment analysis:")
    user_input = st.text_input("", placeholder="Enter your text here")
    if st.button('Analyze'):
        if user_input:
            with st.spinner("Analyzing..."):
                if selected_model == "Hugging Face":
                    result = analyze_hf_sentiment(user_input)
                elif selected_model == "VADER":
                    result = analyze_vader_sentiment(user_input)
                elif selected_model == "TextBlob":
                    result = analyze_textblob_sentiment(user_input)
            st.write("Sentiment:", result)
        else:
            st.error("Please enter some text to analyze.")

if input_option == "CSV Files":
    st.subheader("Upload CSV files for sentiment analysis:")
    uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True)
    if st.button('Start Analysis'):
        if uploaded_files:
            for uploaded_file in uploaded_files:
                df = pd.read_csv(uploaded_file)
                if 'review_text' in df.columns:
                    df['Sentiment'] = df['review_text'].apply(lambda x: analyze_hf_sentiment(x) if selected_model == "Hugging Face" else (analyze_vader_sentiment(x) if selected_model == "VADER" else analyze_textblob_sentiment(x)))
                    if result_option == "DataFrame":
                        display_dataframe(df)
                    elif result_option == "Pie Chart":
                        display_pie_chart(df, 'Sentiment')
                    elif result_option == "Word Cloud":
                        combined_text = ' '.join(df['review_text'])
                        display_wordcloud(combined_text)
                else:
                    st.error("CSV must contain 'review_text' column.")
        else:
            st.error("Please upload a CSV file.")