Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import plotly.graph_objs as go
|
6 |
+
from wordcloud import WordCloud
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
# Load the pre-trained sentiment analysis model
|
10 |
+
sentiment_model = pipeline("sentiment-analysis", model="siebert/sentiment-roberta-large-english")
|
11 |
+
|
12 |
+
# Define the Streamlit app's user interface
|
13 |
+
# Set page title and favicon
|
14 |
+
st.set_page_config(page_title="review Analysis", page_icon=":smiley:")
|
15 |
+
|
16 |
+
# Add image and heading
|
17 |
+
st.image("home.png", use_column_width=True)
|
18 |
+
|
19 |
+
file = st.file_uploader("Drop your file here", type=["csv"])
|
20 |
+
|
21 |
+
# Define the app's functionality
|
22 |
+
if file is not None:
|
23 |
+
# Read the CSV file into a Pandas DataFrame
|
24 |
+
df = pd.read_csv(file)
|
25 |
+
|
26 |
+
# Write the total number of records
|
27 |
+
st.markdown(
|
28 |
+
f'<div style="background-color: #234A21; color: #ffffff; padding: 8px; font-size: 30px; font-family: Open-Sans; font-weight: bold; text-align: center;"> {len(df)} reviews to analyse!</div>',
|
29 |
+
unsafe_allow_html=True
|
30 |
+
)
|
31 |
+
|
32 |
+
# Apply the sentiment analysis model to each review and store the results in a new column
|
33 |
+
df["sentiment"] = df["review"].apply(lambda x: sentiment_model(x)[0]["label"])
|
34 |
+
|
35 |
+
# Generate pie chart
|
36 |
+
# Define custom colors
|
37 |
+
colors = ['#FFC845', '#52565E']
|
38 |
+
|
39 |
+
# Generate pie chart
|
40 |
+
sentiment_counts = df["sentiment"].value_counts()
|
41 |
+
fig = px.pie(sentiment_counts, values=sentiment_counts.values, names=sentiment_counts.index,
|
42 |
+
color_discrete_sequence=colors)
|
43 |
+
st.plotly_chart(fig)
|
44 |
+
|
45 |
+
# Create word clouds for positive and negative reviews
|
46 |
+
positive_reviews = " ".join(df[df["sentiment"] == "POSITIVE"]["review"].tolist())
|
47 |
+
negative_reviews = " ".join(df[df["sentiment"] == "NEGATIVE"]["review"].tolist())
|
48 |
+
|
49 |
+
st.markdown(
|
50 |
+
f'<div style="background-color: #234A21; color: #ffffff; padding: 6px; font-size: 20px; font-family: Open-Sans; font-weight: bold; text-align: center; margin-bottom: 250px;"> Word Cloud for Positive Reviews</div>',
|
51 |
+
unsafe_allow_html=True
|
52 |
+
)
|
53 |
+
wc = WordCloud(width=800, height=400, background_color="white", colormap="blues").generate(positive_reviews)
|
54 |
+
st.image(wc.to_array())
|
55 |
+
|
56 |
+
st.markdown(
|
57 |
+
f'<div style="background-color: #234A21; color: #ffffff; padding: 6px; font-size: 20px; font-family: Open-Sans; font-weight: bold; text-align: center; margin-bottom: 250px; margin-top: 50px;"> Word Cloud for Negative Reviews</div>',
|
58 |
+
unsafe_allow_html=True
|
59 |
+
)
|
60 |
+
wc = WordCloud(width=800, height=400, background_color="white", colormap="blues").generate(negative_reviews)
|
61 |
+
st.image(wc.to_array())
|
62 |
+
|
63 |
+
# Display the sentiment of each review as cards
|
64 |
+
st.markdown(
|
65 |
+
f'<div style="background-color: #234A21; color: #ffffff; padding: 6px; font-size: 20px; font-family: Open-Sans; font-weight: bold; text-align: center; margin-bottom: 10px; margin-top: 10px;"> What customers said about us</div>',
|
66 |
+
unsafe_allow_html=True
|
67 |
+
)
|
68 |
+
|
69 |
+
# Define sentiment colors
|
70 |
+
sentiment_colors = {'POSITIVE': 'FFC845', 'NEGATIVE': '52565E'}
|
71 |
+
|
72 |
+
# Map sentiment to colors and apply to review column
|
73 |
+
df['review'] = df['review'].apply(lambda x: f'<span style="color: {sentiment_colors[x]}">{x}</span>')
|
74 |
+
|
75 |
+
# Create HTML table with no border and centered text
|
76 |
+
table_html = (df.style
|
77 |
+
.set_properties(**{'text-align': 'center'})
|
78 |
+
.set_table_styles(
|
79 |
+
[{'selector': 'th', 'props': [('border', '0px')]}, {'selector': 'td', 'props': [('border', '0px')]}])
|
80 |
+
.render())
|
81 |
+
|
82 |
+
# Display the table
|
83 |
+
st.write(table_html, unsafe_allow_html=True)
|
84 |
+
|
85 |
+
|
86 |
+
else:
|
87 |
+
st.write("Please upload a CSV file.")
|