molehh's picture
modified in app
1210579
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from textblob import TextBlob
def load_data(uploaded_file):
df = pd.read_excel(uploaded_file)
return df
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")
uploaded_file = st.file_uploader("Upload an Excel file with text data", type=["xlsx"])
if uploaded_file is not None:
df = load_data(uploaded_file)
if "text" not in df.columns:
st.error("Error: The file must contain a 'text' column.")
else:
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)
csv = df.to_csv(index=False)
st.download_button("Download Sentiment Data", csv, "sentiment_results.csv", "text/csv")
else:
st.write("Please upload an Excel file to get started.")