Spaces:
Running
Running
add streamlit_piechart
Browse files- sms_process_data_main.xlsx +0 -0
- streamlit_piechart +74 -0
sms_process_data_main.xlsx
CHANGED
Binary files a/sms_process_data_main.xlsx and b/sms_process_data_main.xlsx differ
|
|
streamlit_piechart
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from textblob import TextBlob
|
5 |
+
|
6 |
+
def load_data(uploaded_file):
|
7 |
+
# Load Excel file, supports both .xlsx and .xls
|
8 |
+
try:
|
9 |
+
df = pd.read_excel(uploaded_file) # Automatically detects file format
|
10 |
+
df.columns = df.columns.str.strip().str.lower() # Normalize column names
|
11 |
+
return df
|
12 |
+
except Exception as e:
|
13 |
+
st.error(f"Error loading file: {e}")
|
14 |
+
return None
|
15 |
+
|
16 |
+
def analyze_sentiment(text):
|
17 |
+
polarity = TextBlob(str(text)).sentiment.polarity
|
18 |
+
if polarity >= 0.6:
|
19 |
+
return "Very Positive"
|
20 |
+
elif polarity >= 0.2:
|
21 |
+
return "Positive"
|
22 |
+
elif polarity > -0.2:
|
23 |
+
return "Neutral"
|
24 |
+
elif polarity > -0.6:
|
25 |
+
return "Negative"
|
26 |
+
else:
|
27 |
+
return "Very Negative"
|
28 |
+
|
29 |
+
st.title("Sentiment Analysis with Pie Chart")
|
30 |
+
|
31 |
+
# File uploader supports .xlsx and .xls
|
32 |
+
uploaded_file = st.file_uploader("Upload an Excel file with text data", type=["xlsx", "xls"])
|
33 |
+
|
34 |
+
if uploaded_file is not None:
|
35 |
+
df = load_data(uploaded_file)
|
36 |
+
if df is not None:
|
37 |
+
st.write("Columns in your file:", df.columns.tolist())
|
38 |
+
|
39 |
+
# Allow the user to select a column if 'text' is not already present
|
40 |
+
if "text" not in df.columns:
|
41 |
+
selected_column = st.selectbox("Select the column to use as text data:", df.columns)
|
42 |
+
if st.button("Confirm Selection"):
|
43 |
+
df.rename(columns={selected_column: "text"}, inplace=True)
|
44 |
+
st.success(f"Column '{selected_column}' renamed to 'text'.")
|
45 |
+
|
46 |
+
if "text" in df.columns: # Check again if 'text' column is present after renaming
|
47 |
+
df["Sentiment"] = df["text"].apply(analyze_sentiment)
|
48 |
+
|
49 |
+
st.write("Here is a preview of the data:")
|
50 |
+
st.write(df.head())
|
51 |
+
|
52 |
+
sentiment_counts = df["Sentiment"].value_counts()
|
53 |
+
|
54 |
+
fig, ax = plt.subplots()
|
55 |
+
ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct="%1.1f%%", colors=["green", "lightgreen", "gray", "orange", "red"])
|
56 |
+
ax.set_title("Sentiment Distribution")
|
57 |
+
st.pyplot(fig)
|
58 |
+
|
59 |
+
# Export processed data to Excel file (.xlsx) without explicitly using xlsxwriter
|
60 |
+
output_file = "sentiment_results.xlsx"
|
61 |
+
df.to_excel(output_file, index=False, sheet_name="Sentiment Analysis")
|
62 |
+
|
63 |
+
# Download button for Excel file
|
64 |
+
with open(output_file, "rb") as f:
|
65 |
+
st.download_button(
|
66 |
+
"Download Sentiment Data (Excel)",
|
67 |
+
f,
|
68 |
+
"sentiment_results.xlsx",
|
69 |
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
70 |
+
)
|
71 |
+
else:
|
72 |
+
st.warning("Please select a column to rename as 'text' and proceed.")
|
73 |
+
else:
|
74 |
+
st.write("Please upload an Excel file to get started.")
|