import streamlit as st
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import plotly.graph_objs as go
from wordcloud import WordCloud
from transformers import pipeline
# Load the pre-trained sentiment analysis model
sentiment_model = pipeline("sentiment-analysis", model="siebert/sentiment-roberta-large-english")
# Define the Streamlit app's user interface
# Set page title and favicon
st.set_page_config(page_title="Review Analysis", page_icon=":smiley:")
# Add image and heading
st.image("img.png", use_column_width=True)
file = st.file_uploader("", type=["csv"])
# Define the app's functionality
if file is not None:
# Read the CSV file into a Pandas DataFrame
df = pd.read_csv(file)
st.markdown(f"
{len(df)-1} reviews to analyse!
", unsafe_allow_html=True)
# Write the total number of records
st.markdown(
f' Distribution of Reviews
',
unsafe_allow_html=True
)
# Apply the sentiment analysis model to each review and store the results in a new column
df["Sentiment"] = df["Review"].apply(lambda x: sentiment_model(x)[0]["label"])
# Generate pie chart
# Define custom colors
colors = ['#30C3C4', '#D1DDDE']
# Generate pie chart
sentiment_counts = df["Sentiment"].value_counts()
fig = px.pie(sentiment_counts, values=sentiment_counts.values, names=sentiment_counts.index,
color_discrete_sequence=colors)
st.plotly_chart(fig, use_container_width=True)
# Create word clouds for positive and negative reviews
positive_reviews = " ".join(df[df["Sentiment"] == "POSITIVE"]["Review"].tolist())
negative_reviews = " ".join(df[df["Sentiment"] == "NEGATIVE"]["Review"].tolist())
st.markdown(
f' Causes behind Positive Reviews
',
unsafe_allow_html=True
)
wc = WordCloud(width=800, height=400, background_color="white", colormap="winter").generate(positive_reviews)
st.image(wc.to_array(),use_column_width=True)
st.markdown(
f' Causes behind Negative Reviews
',
unsafe_allow_html=True
)
wc = WordCloud(width=800, height=400, background_color="white", colormap="winter").generate(negative_reviews)
st.image(wc.to_array(),use_column_width=True)
# Display the sentiment of each review as cards
st.markdown(
f' Reviews in depth
',
unsafe_allow_html=True
)
# Add the selectbox to filter sentiments
filter_sentiment = st.selectbox("", ["All", "POSITIVE", "NEGATIVE"])
# Filter the dataframe based on the selected sentiment
if filter_sentiment != "All":
df = df[df['sentiment'] == filter_sentiment]
# Set the max number of rows to display at a time
max_rows = 15
# Create HTML table with no border and centered text
table_html = (df.style
.set_properties(**{'text-align': 'left','font-size': '15px','font-family': 'Verdana'})
.set_table_styles([{'selector': 'th', 'props': [('border', '0px')]},
{'selector': 'td', 'props': [('border', '0px')]}])
.set_table_attributes('style="position: sticky; top: 0;"')
.to_html(index=False, escape=False))
# Wrap the table inside a div with a fixed height and scrollable content
st.write(f'{table_html}
', unsafe_allow_html=True,header=True,sticky_header=True)
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
csv = convert_df(df)
css = """
.css-1irvrzc:hover, .css-1irvrzc:active {
background-color: #4AA6DD !important;
}
"""
# Add some space between the download button and the table
st.write("
", unsafe_allow_html=True)
st.write(f'', unsafe_allow_html=True)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='Review Sentiments.csv',
border_color='#4AA6DD'
)
else:
st.write("Please upload the reviews as a CSV file.")