-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (58 loc) · 2.42 KB
/
Copy pathapp.py
File metadata and controls
70 lines (58 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import subprocess
import sys
subprocess.run([sys.executable, "-m", "pip", "install", "nltk", "wordcloud", "matplotlib"], capture_output=True)
import streamlit as st
import nltk
import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import matplotlib.pyplot as plt
import io
st.set_page_config(page_title="Sentiment Analyzer", page_icon="🎭")
st.title("🎭 Sentiment Analyzer")
analyzer = SentimentIntensityAnalyzer()
# Input options
option = st.radio("Input method", ["Type Text", "Upload .txt File"])
if option == "Upload .txt File":
file = st.file_uploader("Upload a .txt file", type=["txt"])
text = file.read().decode("utf-8") if file else ""
else:
text = st.text_area("Enter text (one sentence per line)", height=200)
if st.button("Analyze") and text.strip():
sentences = [s.strip() for s in text.strip().split("\n") if s.strip()]
results = []
for sentence in sentences:
score = analyzer.polarity_scores(sentence)
compound = score['compound']
if compound >= 0.05:
label, color = "POSITIVE 😊", "green"
elif compound <= -0.05:
label, color = "NEGATIVE 😞", "red"
else:
label, color = "NEUTRAL 😐", "gray"
results.append({"Sentence": sentence, "Sentiment": label, "Score": round(compound, 2), "Color": color})
for r in results:
st.markdown(f":{r['Color']}[**{r['Sentiment']}** — *{r['Sentence']}*] (Score: {r['Score']})")
st.divider()
# Bar chart
st.subheader("📊 Overall Breakdown")
scores = analyzer.polarity_scores(" ".join(sentences))
chart_data = pd.DataFrame({
"Sentiment": ["Positive", "Negative", "Neutral"],
"Score": [scores['pos'], scores['neg'], scores['neu']]
})
st.bar_chart(chart_data.set_index("Sentiment"))
# Word cloud
st.subheader("☁️ Word Cloud")
wc = WordCloud(width=800, height=300, background_color="white").generate(text)
fig, ax = plt.subplots()
ax.imshow(wc, interpolation="bilinear")
ax.axis("off")
st.pyplot(fig)
# Download CSV
st.divider()
df = pd.DataFrame([{"Sentence": r["Sentence"], "Sentiment": r["Sentiment"], "Score": r["Score"]} for r in results])
csv = df.to_csv(index=False).encode("utf-8")
st.download_button("⬇️ Download Results as CSV", csv, "results.csv", "text/csv")