Spaces:
Sleeping
Sleeping
modified in app
Browse files- app.py +22 -46
- sentiment_results.xlsx +0 -0
app.py
CHANGED
@@ -4,14 +4,8 @@ import matplotlib.pyplot as plt
|
|
4 |
from textblob import TextBlob
|
5 |
|
6 |
def load_data(uploaded_file):
|
7 |
-
|
8 |
-
|
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
|
@@ -28,47 +22,29 @@ def analyze_sentiment(text):
|
|
28 |
|
29 |
st.title("Sentiment Analysis with Pie Chart")
|
30 |
|
31 |
-
|
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
|
37 |
-
st.
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
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 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
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.")
|
|
|
|
|
|
|
|
4 |
from textblob import TextBlob
|
5 |
|
6 |
def load_data(uploaded_file):
|
7 |
+
df = pd.read_excel(uploaded_file)
|
8 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def analyze_sentiment(text):
|
11 |
polarity = TextBlob(str(text)).sentiment.polarity
|
|
|
22 |
|
23 |
st.title("Sentiment Analysis with Pie Chart")
|
24 |
|
25 |
+
uploaded_file = st.file_uploader("Upload an Excel file with text data", type=["xlsx"])
|
|
|
26 |
|
27 |
if uploaded_file is not None:
|
28 |
df = load_data(uploaded_file)
|
29 |
+
if "text" not in df.columns:
|
30 |
+
st.error("Error: The file must contain a 'text' column.")
|
31 |
+
else:
|
32 |
+
df["Sentiment"] = df["text"].apply(analyze_sentiment)
|
33 |
|
34 |
+
st.write("Here is a preview of the data:")
|
35 |
+
st.write(df.head())
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
sentiment_counts = df["Sentiment"].value_counts()
|
38 |
+
|
39 |
+
fig, ax = plt.subplots()
|
40 |
+
ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct="%1.1f%%", colors=["green", "lightgreen", "gray", "orange", "red"])
|
41 |
+
ax.set_title("Sentiment Distribution")
|
42 |
+
st.pyplot(fig)
|
43 |
+
|
44 |
+
csv = df.to_csv(index=False)
|
45 |
+
st.download_button("Download Sentiment Data", csv, "sentiment_results.csv", "text/csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
else:
|
47 |
+
st.write("Please upload an Excel file to get started.")
|
48 |
+
|
49 |
+
|
50 |
+
|
sentiment_results.xlsx
CHANGED
Binary files a/sentiment_results.xlsx and b/sentiment_results.xlsx differ
|
|