Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,64 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
st.
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
positive_words = ["good", "great", "happy", "excellent", "amazing", "love", "awesome", "fantastic", "positive", "nice"]
|
| 12 |
negative_words = ["bad", "sad", "terrible", "horrible", "hate", "awful", "worst", "angry", "negative", "poor"]
|
| 13 |
|
|
|
|
| 14 |
def analyze_sentiment(text):
|
| 15 |
text = text.lower()
|
| 16 |
pos_count = sum(word in text for word in positive_words)
|
| 17 |
neg_count = sum(word in text for word in negative_words)
|
| 18 |
|
| 19 |
if pos_count > neg_count:
|
| 20 |
-
return "π Positive"
|
| 21 |
elif neg_count > pos_count:
|
| 22 |
-
return "βΉοΈ Negative"
|
| 23 |
else:
|
| 24 |
-
return "π Neutral"
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
if st.button("Analyze"):
|
| 28 |
if user_input.strip():
|
| 29 |
-
|
| 30 |
-
st.
|
| 31 |
-
st.success(result)
|
| 32 |
else:
|
| 33 |
-
st.warning("Please enter some text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
# Page config
|
| 4 |
+
st.set_page_config(
|
| 5 |
+
page_title="Sentiment Analysis",
|
| 6 |
+
page_icon="π¬",
|
| 7 |
+
layout="centered"
|
| 8 |
+
)
|
| 9 |
|
| 10 |
+
# Custom styles
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
.stTextArea textarea {
|
| 14 |
+
height: 150px;
|
| 15 |
+
}
|
| 16 |
+
.result-box {
|
| 17 |
+
padding: 1rem;
|
| 18 |
+
border-radius: 0.5rem;
|
| 19 |
+
margin-top: 1rem;
|
| 20 |
+
text-align: center;
|
| 21 |
+
font-size: 1.3rem;
|
| 22 |
+
}
|
| 23 |
+
.positive { background-color: #d4edda; color: #155724; }
|
| 24 |
+
.negative { background-color: #f8d7da; color: #721c24; }
|
| 25 |
+
.neutral { background-color: #fff3cd; color: #856404; }
|
| 26 |
+
</style>
|
| 27 |
+
""", unsafe_allow_html=True)
|
| 28 |
|
| 29 |
+
# App title and intro
|
| 30 |
+
st.title("π¬ Sentiment Analysis App")
|
| 31 |
+
st.markdown("Predict sentiment using simple Python logic β no ML, just rule-based π")
|
| 32 |
+
|
| 33 |
+
# Input section
|
| 34 |
+
st.subheader("π Enter your text")
|
| 35 |
+
user_input = st.text_area("", placeholder="Type or paste your sentence here...")
|
| 36 |
+
|
| 37 |
+
# Keywords
|
| 38 |
positive_words = ["good", "great", "happy", "excellent", "amazing", "love", "awesome", "fantastic", "positive", "nice"]
|
| 39 |
negative_words = ["bad", "sad", "terrible", "horrible", "hate", "awful", "worst", "angry", "negative", "poor"]
|
| 40 |
|
| 41 |
+
# Sentiment analysis function
|
| 42 |
def analyze_sentiment(text):
|
| 43 |
text = text.lower()
|
| 44 |
pos_count = sum(word in text for word in positive_words)
|
| 45 |
neg_count = sum(word in text for word in negative_words)
|
| 46 |
|
| 47 |
if pos_count > neg_count:
|
| 48 |
+
return "π Positive", "positive"
|
| 49 |
elif neg_count > pos_count:
|
| 50 |
+
return "βΉοΈ Negative", "negative"
|
| 51 |
else:
|
| 52 |
+
return "π Neutral", "neutral"
|
| 53 |
|
| 54 |
+
# Button & result
|
| 55 |
+
if st.button("π Analyze Sentiment"):
|
| 56 |
if user_input.strip():
|
| 57 |
+
sentiment, sentiment_class = analyze_sentiment(user_input)
|
| 58 |
+
st.markdown(f'<div class="result-box {sentiment_class}">{sentiment}</div>', unsafe_allow_html=True)
|
|
|
|
| 59 |
else:
|
| 60 |
+
st.warning("β οΈ Please enter some text before analyzing.")
|
| 61 |
+
|
| 62 |
+
# Footer
|
| 63 |
+
st.markdown("---")
|
| 64 |
+
st.markdown("Made with β€οΈ using Streamlit | Rule-based approach")
|