RohitBh's picture
Update app.py
a3a5584 verified
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.")