Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,419 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
4 |
-
from textblob import TextBlob
|
5 |
-
from transformers import pipeline
|
6 |
-
import matplotlib.pyplot as plt
|
7 |
-
import base64
|
8 |
-
import os
|
9 |
-
from wordcloud import WordCloud
|
10 |
-
|
11 |
-
# Function to perform sentiment analysis using Hugging Face model
|
12 |
-
hf_sentiment_analyzer = pipeline(
|
13 |
-
"sentiment-analysis", "Dmyadav2001/Sentimental-Analysis"
|
14 |
-
)
|
15 |
-
|
16 |
-
def analyze_hf_sentiment(text):
|
17 |
-
if len(text) > 512:
|
18 |
-
temp = text[:511]
|
19 |
-
text = temp
|
20 |
-
result = hf_sentiment_analyzer(text)
|
21 |
-
label = result[0]["label"]
|
22 |
-
if label == "LABEL_1":
|
23 |
-
return "Positive"
|
24 |
-
elif label == "LABEL_0":
|
25 |
-
return "Negative"
|
26 |
-
elif label == "LABEL_2":
|
27 |
-
return "Neutral"
|
28 |
-
|
29 |
-
# Function to perform sentiment analysis using VADER
|
30 |
-
def analyze_vader_sentiment(text):
|
31 |
-
analyzer = SentimentIntensityAnalyzer()
|
32 |
-
vader_score = analyzer.polarity_scores(text)["compound"]
|
33 |
-
if vader_score > 0:
|
34 |
-
return "Positive"
|
35 |
-
elif vader_score == 0:
|
36 |
-
return "Neutral"
|
37 |
-
else:
|
38 |
-
return "Negative"
|
39 |
-
|
40 |
-
# Function to perform sentiment analysis using TextBlob
|
41 |
-
def analyze_textblob_sentiment(text):
|
42 |
-
analysis = TextBlob(text)
|
43 |
-
sentiment_score = analysis.sentiment.polarity
|
44 |
-
if sentiment_score > 0:
|
45 |
-
return "Positive"
|
46 |
-
elif sentiment_score == 0:
|
47 |
-
return "Neutral"
|
48 |
-
else:
|
49 |
-
return "Negative"
|
50 |
-
|
51 |
-
# Function to display DataFrame with updated sentiment column
|
52 |
-
def display_dataframe(df):
|
53 |
-
st.write(df)
|
54 |
-
|
55 |
-
# Function to display pie chart for sentiment distribution
|
56 |
-
def display_pie_chart(df, column):
|
57 |
-
sentiment_counts = df[column].value_counts()
|
58 |
-
fig, ax = plt.subplots()
|
59 |
-
ax.pie(
|
60 |
-
sentiment_counts,
|
61 |
-
labels=sentiment_counts.index,
|
62 |
-
autopct="%1.1f%%",
|
63 |
-
startangle=140,
|
64 |
-
)
|
65 |
-
ax.axis("equal")
|
66 |
-
st.pyplot(fig)
|
67 |
-
|
68 |
-
# Add a download button
|
69 |
-
if st.button('Download Pie Chart'):
|
70 |
-
# Save the pie chart as an image file
|
71 |
-
plt.savefig('pie_chart.png')
|
72 |
-
|
73 |
-
# Offer the image file for download
|
74 |
-
st.download_button(label='Download Pie Chart Image', data=open('pie_chart.png', 'rb').read(), file_name='pie_chart.png', mime='image/png')
|
75 |
-
|
76 |
-
# Function to display word cloud
|
77 |
-
def display_wordcloud(text_data):
|
78 |
-
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(
|
79 |
-
text_data
|
80 |
-
)
|
81 |
-
fig, ax = plt.subplots(figsize=(10, 5))
|
82 |
-
ax.imshow(wordcloud, interpolation="bilinear")
|
83 |
-
ax.axis("off")
|
84 |
-
st.pyplot(fig)
|
85 |
-
|
86 |
-
# Add a download button
|
87 |
-
if st.button('Download Word Cloud'):
|
88 |
-
# Save the word cloud as an image file
|
89 |
-
plt.savefig('word_cloud.png')
|
90 |
-
|
91 |
-
# Offer the image file for download
|
92 |
-
st.download_button(label='Download Word Cloud Image', data=open('word_cloud.png', 'rb').read(), file_name='word_cloud.png', mime='image/png')
|
93 |
-
|
94 |
-
# Function to download CSV file
|
95 |
-
def download_csv(df):
|
96 |
-
csv = df.to_csv(index=False)
|
97 |
-
b64 = base64.b64encode(csv.encode()).decode() # B64 encoding
|
98 |
-
href = f'<a href="data:file/csv;base64,{b64}" download="sentiment_analysis_results.csv">Download CSV File</a>'
|
99 |
-
st.markdown(href, unsafe_allow_html=True)
|
100 |
-
|
101 |
-
# Streamlit UI
|
102 |
-
st.set_page_config(page_title="JazbaatMeter", page_icon=":smiley:")
|
103 |
-
st.title("JazbaatMeter")
|
104 |
-
|
105 |
-
# Sidebar
|
106 |
-
st.sidebar.title("Options")
|
107 |
-
input_option = st.sidebar.radio("Select Input Option", ("Free Text", "CSV Files"))
|
108 |
-
selected_model = st.sidebar.radio(
|
109 |
-
"Select Sentiment Analysis Model", ("VADER", "TextBlob", "Hugging Face")
|
110 |
-
)
|
111 |
-
result_option = st.sidebar.radio(
|
112 |
-
"Select Result Display Option",
|
113 |
-
("DataFrame", "Pie Chart", "Bar Chart", "Keyword Frequency", "WordCloud"),
|
114 |
-
)
|
115 |
-
|
116 |
-
# Main content
|
117 |
-
progress_label = st.empty() # Define progress label
|
118 |
-
progress_bar = st.progress(0)
|
119 |
-
progress = 0
|
120 |
-
|
121 |
-
# Directory path to store processed files
|
122 |
-
processed_directory = "processed_files"
|
123 |
-
|
124 |
-
# Ensure the directory exists, if not create it
|
125 |
-
os.makedirs(processed_directory, exist_ok=True)
|
126 |
-
|
127 |
-
# List to store processed filenames
|
128 |
-
processed_files = []
|
129 |
-
|
130 |
-
# Function to get filenames from the processed directory
|
131 |
-
def get_processed_filenames():
|
132 |
-
return [
|
133 |
-
f
|
134 |
-
for f in os.listdir(processed_directory)
|
135 |
-
if os.path.isfile(os.path.join(processed_directory, f))
|
136 |
-
]
|
137 |
-
|
138 |
-
if input_option == "Free Text":
|
139 |
-
st.subheader("Enter review for sentiment analysis:")
|
140 |
-
user_input = st.text_area("", "")
|
141 |
-
if not user_input:
|
142 |
-
st.info("Enter some text above for sentiment analysis.")
|
143 |
-
else:
|
144 |
-
with st.spinner("Analyzing..."):
|
145 |
-
if selected_model == "Hugging Face":
|
146 |
-
result = analyze_hf_sentiment(user_input)
|
147 |
-
elif selected_model == "VADER":
|
148 |
-
result = analyze_vader_sentiment(user_input)
|
149 |
-
elif selected_model == "TextBlob":
|
150 |
-
result = analyze_textblob_sentiment(user_input)
|
151 |
-
st.write("Sentiment:", result)
|
152 |
-
|
153 |
-
if input_option == "CSV Files":
|
154 |
-
st.subheader("Select CSV files for sentiment analysis:")
|
155 |
-
|
156 |
-
# Uploading new file
|
157 |
-
files = st.file_uploader(
|
158 |
-
"Upload New File", type=["csv"], accept_multiple_files=True
|
159 |
-
)
|
160 |
-
if files:
|
161 |
-
# Process uploaded new files
|
162 |
-
for file in files:
|
163 |
-
if file.type != "text/csv":
|
164 |
-
st.warning(
|
165 |
-
"Uploaded file is not a CSV file. Please upload a CSV file only."
|
166 |
-
)
|
167 |
-
else:
|
168 |
-
df = pd.read_csv(file)
|
169 |
-
if "review_text" not in df.columns:
|
170 |
-
st.warning(
|
171 |
-
"Uploaded CSV file doesn't contain 'review_text' column. Please check the CSV file format."
|
172 |
-
)
|
173 |
-
else:
|
174 |
-
total_rows = len(df)
|
175 |
-
|
176 |
-
sentiments_v = []
|
177 |
-
sentiments_tb = []
|
178 |
-
sentiments_hf = []
|
179 |
-
|
180 |
-
for review_text in df["review_text"]:
|
181 |
-
sentiments_v.append(analyze_vader_sentiment(review_text))
|
182 |
-
sentiments_tb.append(analyze_textblob_sentiment(review_text))
|
183 |
-
sentiments_hf.append(analyze_hf_sentiment(review_text))
|
184 |
-
progress += 1
|
185 |
-
progress_label.text(f"{progress}/{total_rows}")
|
186 |
-
progress_bar.progress(min(progress / total_rows, 1.0))
|
187 |
-
|
188 |
-
df["VADER Sentiment"] = sentiments_v
|
189 |
-
df["TextBlob Sentiment"] = sentiments_tb
|
190 |
-
df["HuggingFace Sentiment"] = sentiments_hf
|
191 |
-
|
192 |
-
# Save processed file with modified filename
|
193 |
-
new_filename = os.path.splitext(file.name)[0] + "1.csv"
|
194 |
-
df.to_csv(
|
195 |
-
os.path.join(processed_directory, new_filename), index=False
|
196 |
-
)
|
197 |
-
st.success(f"New file processed and saved as {new_filename}")
|
198 |
-
|
199 |
-
# List of already processed files
|
200 |
-
processed_files = get_processed_filenames()
|
201 |
-
selected_files = st.multiselect("Select from Processed Files", processed_files)
|
202 |
-
|
203 |
-
if not files and not selected_files:
|
204 |
-
st.info(
|
205 |
-
"Upload a new CSV file or select from processed files above for sentiment analysis."
|
206 |
-
)
|
207 |
-
|
208 |
-
all_dfs = []
|
209 |
-
|
210 |
-
# Process already selected files
|
211 |
-
for file_name in selected_files:
|
212 |
-
df = pd.read_csv(os.path.join(processed_directory, file_name))
|
213 |
-
all_dfs.append(df)
|
214 |
-
|
215 |
-
# Results
|
216 |
-
if all_dfs:
|
217 |
-
combined_df = pd.concat(all_dfs, ignore_index=True)
|
218 |
-
if selected_model == "TextBlob":
|
219 |
-
result = "TextBlob Sentiment"
|
220 |
-
combined_df.drop(
|
221 |
-
columns=["VADER Sentiment", "HuggingFace Sentiment"],
|
222 |
-
inplace=True,
|
223 |
-
)
|
224 |
-
elif selected_model == "VADER":
|
225 |
-
result = "VADER Sentiment"
|
226 |
-
combined_df.drop(
|
227 |
-
columns=["TextBlob Sentiment", "HuggingFace Sentiment"],
|
228 |
-
inplace=True,
|
229 |
-
)
|
230 |
-
elif selected_model == "Hugging Face":
|
231 |
-
result = "HuggingFace Sentiment"
|
232 |
-
combined_df.drop(
|
233 |
-
columns=["TextBlob Sentiment", "VADER Sentiment"],
|
234 |
-
inplace=True,
|
235 |
-
)
|
236 |
-
combined_df.rename(columns={result: "Sentiment"}, inplace=True)
|
237 |
-
|
238 |
-
if result_option == "DataFrame":
|
239 |
-
st.subheader("Sentiment Analysis Results")
|
240 |
-
display_dataframe(combined_df)
|
241 |
-
download_csv(combined_df)
|
242 |
-
elif result_option == "Pie Chart":
|
243 |
-
st.subheader("Sentiment Distribution")
|
244 |
-
display_pie_chart(combined_df, "Sentiment")
|
245 |
-
elif result_option == "Bar Chart":
|
246 |
-
# Calculate value counts
|
247 |
-
sentiment_counts = combined_df["Sentiment"].value_counts()
|
248 |
-
# Display bar chart
|
249 |
-
st.bar_chart(sentiment_counts)
|
250 |
-
|
251 |
-
# Add a download button
|
252 |
-
if st.button('Download Sentiment Counts Chart'):
|
253 |
-
# Plot the bar chart
|
254 |
-
fig, ax = plt.subplots()
|
255 |
-
sentiment_counts.plot(kind='bar', ax=ax)
|
256 |
-
plt.xlabel('Sentiment')
|
257 |
-
plt.ylabel('Count')
|
258 |
-
plt.title('Sentiment Counts')
|
259 |
-
plt.xticks(rotation=45, ha='right')
|
260 |
-
plt.tight_layout()
|
261 |
-
|
262 |
-
# Save the bar chart as an image file
|
263 |
-
plt.savefig('sentiment_counts_chart.png')
|
264 |
-
|
265 |
-
# Offer the image file for download
|
266 |
-
st.download_button(label='Download Sentiment Counts Chart Image', data=open('sentiment_counts_chart.png', 'rb').read(), file_name='sentiment_counts_chart.png', mime='image/png')
|
267 |
-
|
268 |
-
elif result_option == "Keyword Frequency":
|
269 |
-
st.subheader("Keyword Frequency")
|
270 |
-
|
271 |
-
# List of keywords
|
272 |
-
keywords = [
|
273 |
-
"delivery",
|
274 |
-
"shipping",
|
275 |
-
"parcel",
|
276 |
-
"package",
|
277 |
-
"tracking",
|
278 |
-
"shipment",
|
279 |
-
"cargo",
|
280 |
-
"freight",
|
281 |
-
"automation",
|
282 |
-
"automated",
|
283 |
-
"robotic",
|
284 |
-
"robots",
|
285 |
-
"AI",
|
286 |
-
"artificial intelligence",
|
287 |
-
"machine learning",
|
288 |
-
"chatbot",
|
289 |
-
"virtual assistant",
|
290 |
-
"customer support",
|
291 |
-
"real-time",
|
292 |
-
"instant",
|
293 |
-
"live update",
|
294 |
-
"status",
|
295 |
-
"IoT",
|
296 |
-
"internet of things",
|
297 |
-
"connected devices",
|
298 |
-
"smart technology",
|
299 |
-
"blockchain",
|
300 |
-
"ledger",
|
301 |
-
"transparency",
|
302 |
-
"security",
|
303 |
-
"sustainability",
|
304 |
-
"eco-friendly",
|
305 |
-
"green logistics",
|
306 |
-
"carbon footprint",
|
307 |
-
"customer service",
|
308 |
-
"support",
|
309 |
-
"experience",
|
310 |
-
"satisfaction",
|
311 |
-
"data analytics",
|
312 |
-
"big data",
|
313 |
-
"analysis",
|
314 |
-
"insights",
|
315 |
-
"cloud computing",
|
316 |
-
"cloud-based",
|
317 |
-
"digital infrastructure",
|
318 |
-
"storage",
|
319 |
-
"5G",
|
320 |
-
"connectivity",
|
321 |
-
"network speed",
|
322 |
-
"wireless",
|
323 |
-
"drone",
|
324 |
-
"aerial delivery",
|
325 |
-
"UAV",
|
326 |
-
"drone shipping",
|
327 |
-
"augmented reality",
|
328 |
-
"AR",
|
329 |
-
"virtual reality",
|
330 |
-
"VR",
|
331 |
-
"3D printing",
|
332 |
-
"additive manufacturing",
|
333 |
-
"custom parts",
|
334 |
-
"prototyping",
|
335 |
-
"inventory management",
|
336 |
-
"stock levels",
|
337 |
-
"warehouse management",
|
338 |
-
"storage solutions",
|
339 |
-
"supply chain",
|
340 |
-
"logistics",
|
341 |
-
"supply network",
|
342 |
-
"distribution",
|
343 |
-
"eco-packaging",
|
344 |
-
"sustainable materials",
|
345 |
-
"recycling",
|
346 |
-
"waste reduction",
|
347 |
-
"digital platform",
|
348 |
-
"e-commerce",
|
349 |
-
"online shopping",
|
350 |
-
"online order",
|
351 |
-
"cybersecurity",
|
352 |
-
"data protection",
|
353 |
-
"privacy",
|
354 |
-
"encryption",
|
355 |
-
"predictive modeling",
|
356 |
-
"forecasting",
|
357 |
-
"demand planning",
|
358 |
-
"trend analysis",
|
359 |
-
"robotics",
|
360 |
-
"automated vehicles",
|
361 |
-
"self-driving cars",
|
362 |
-
"logistics automation",
|
363 |
-
"visibility",
|
364 |
-
"supply chain visibility",
|
365 |
-
"track and trace",
|
366 |
-
"monitoring",
|
367 |
-
"integration",
|
368 |
-
"ERP",
|
369 |
-
"supply chain integration",
|
370 |
-
"software",
|
371 |
-
"optimization",
|
372 |
-
"efficiency",
|
373 |
-
"process improvement",
|
374 |
-
"lean logistics",
|
375 |
-
"personalization",
|
376 |
-
"customization",
|
377 |
-
"tailored services",
|
378 |
-
"personal touch",
|
379 |
-
"ethical sourcing",
|
380 |
-
"fair trade",
|
381 |
-
"labor rights",
|
382 |
-
"ethical business",
|
383 |
-
"user experience",
|
384 |
-
"UX",
|
385 |
-
"customer journey",
|
386 |
-
"service design",
|
387 |
-
"visibility",
|
388 |
-
]
|
389 |
-
text_data = " ".join(combined_df["review_text"])
|
390 |
-
keyword_frequency = (
|
391 |
-
pd.Series(text_data.split()).value_counts().reset_index()
|
392 |
-
)
|
393 |
-
keyword_frequency.columns = ["Keyword", "Frequency"]
|
394 |
-
|
395 |
-
# Filter keyword frequency for specific keywords
|
396 |
-
filtered_keyword_frequency = keyword_frequency[
|
397 |
-
keyword_frequency["Keyword"].isin(keywords)
|
398 |
-
]
|
399 |
-
|
400 |
-
# Display bar chart for filtered keyword frequency
|
401 |
-
st.bar_chart(filtered_keyword_frequency.set_index("Keyword"))
|
402 |
-
|
403 |
-
# Add a download button
|
404 |
-
if st.button('Download Keyword Frequency Chart'):
|
405 |
-
# Plot the bar chart
|
406 |
-
fig, ax = plt.subplots()
|
407 |
-
filtered_keyword_frequency.plot(kind='bar', x='Keyword', y='Frequency', ax=ax)
|
408 |
-
plt.xticks(rotation=45, ha='right')
|
409 |
-
plt.tight_layout()
|
410 |
-
|
411 |
-
# Save the bar chart as an image file
|
412 |
-
plt.savefig('keyword_frequency_chart.png')
|
413 |
-
|
414 |
-
# Offer the image file for download
|
415 |
-
st.download_button(label='Download Keyword Frequency Chart Image', data=open('keyword_frequency_chart.png', 'rb').read(), file_name='keyword_frequency_chart.png', mime='image/png')
|
416 |
-
else:
|
417 |
-
st.subheader("Word Cloud")
|
418 |
-
text_data = " ".join(combined_df["review_text"])
|
419 |
-
display_wordcloud(text_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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