molehh commited on
Commit
1210579
·
1 Parent(s): 127e5ab

modified in app

Browse files
Files changed (2) hide show
  1. app.py +22 -46
  2. 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
- # 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
@@ -28,47 +22,29 @@ def analyze_sentiment(text):
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.")
 
 
 
 
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