Kabila22 commited on
Commit
d5b1967
·
1 Parent(s): e723dd3

Add Streamlit sentiment analysis app

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -33,19 +33,25 @@ if uploaded_file is not None:
33
  if st.button("Analyze Sentiment"):
34
  df["Sentiment"] = predict_sentiment(df[text_column].astype(str).tolist())
35
 
36
- # Display results
37
- st.write("Sentiment Analysis Results:")
38
- st.dataframe(df[[text_column, "Sentiment"]])
39
-
40
- # Pie chart
41
- sentiment_counts = df["Sentiment"].value_counts()
42
- all_sentiments = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"]
43
- sentiment_counts = sentiment_counts.reindex(all_sentiments, fill_value=0)
44
-
45
- fig, ax = plt.subplots()
46
- ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', colors=["red", "orange", "gray", "lightgreen", "green"])
47
- ax.set_title("Sentiment Distribution")
48
- st.pyplot(fig)
49
-
50
- # Download option
51
- st.download_button("Download Results", df.to_csv(index=False).encode('utf-8'), "sentiment_results.csv", "text/csv")
 
 
 
 
 
 
 
33
  if st.button("Analyze Sentiment"):
34
  df["Sentiment"] = predict_sentiment(df[text_column].astype(str).tolist())
35
 
36
+ # Store results in session state
37
+ st.session_state["df_results"] = df
38
+
39
+ # Display the results only if available in session state
40
+ if "df_results" in st.session_state:
41
+ df = st.session_state["df_results"]
42
+
43
+ st.write("### Sentiment Analysis Results:")
44
+ st.dataframe(df[[text_column, "Sentiment"]])
45
+
46
+ # Pie chart
47
+ sentiment_counts = df["Sentiment"].value_counts()
48
+ all_sentiments = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"]
49
+ sentiment_counts = sentiment_counts.reindex(all_sentiments, fill_value=0)
50
+
51
+ fig, ax = plt.subplots()
52
+ ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', colors=["red", "orange", "gray", "lightgreen", "green"])
53
+ ax.set_title("Sentiment Distribution")
54
+ st.pyplot(fig)
55
+
56
+ # Download option
57
+ st.download_button("Download Results", df.to_csv(index=False).encode('utf-8'), "sentiment_results.csv", "text/csv")