molehh's picture
changes in app
f2c8ae1
raw
history blame
2.96 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from textblob import TextBlob
import matplotlib.pyplot as plt
def load_data(uploaded_file):
# Load Excel file, supports both .xlsx and .xls
try:
df = pd.read_excel(uploaded_file) # Automatically detects file format
df.columns = df.columns.str.strip().str.lower() # Normalize column names
return df
except Exception as e:
st.error(f"Error loading file: {e}")
return None
def analyze_sentiment(text):
polarity = TextBlob(str(text)).sentiment.polarity
if polarity >= 0.6:
return "Very Positive"
elif polarity >= 0.2:
return "Positive"
elif polarity > -0.2:
return "Neutral"
elif polarity > -0.6:
return "Negative"
else:
return "Very Negative"
st.title("Sentiment Analysis with Pie Chart")
# File uploader supports .xlsx and .xls
uploaded_file = st.file_uploader("Upload an Excel file with text data", type=["xlsx", "xls"])
if uploaded_file is not None:
df = load_data(uploaded_file)
if df is not None:
st.write("Columns in your file:", df.columns.tolist())
# Allow the user to select a column if 'text' is not already present
if "text" not in df.columns:
selected_column = st.selectbox("Select the column to use as text data:", df.columns)
if st.button("Confirm Selection"):
df.rename(columns={selected_column: "text"}, inplace=True)
st.success(f"Column '{selected_column}' renamed to 'text'.")
if "text" in df.columns: # Check again if 'text' column is present after renaming
df["Sentiment"] = df["text"].apply(analyze_sentiment)
st.write("Here is a preview of the data:")
st.write(df.head())
sentiment_counts = df["Sentiment"].value_counts()
fig, ax = plt.subplots()
ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct="%1.1f%%", colors=["green", "lightgreen", "gray", "orange", "red"])
ax.set_title("Sentiment Distribution")
st.pyplot(fig)
# Export processed data to Excel file (.xlsx) without explicitly using xlsxwriter
output_file = "sentiment_results.xlsx"
df.to_excel(output_file, index=False, sheet_name="Sentiment Analysis")
# Download button for Excel file
with open(output_file, "rb") as f:
st.download_button(
"Download Sentiment Data (Excel)",
f,
"sentiment_results.xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
else:
st.warning("Please select a column to rename as 'text' and proceed.")
else:
st.write("Please upload an Excel file to get started.")