Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| # Page config | |
| st.set_page_config( | |
| page_title="Sentiment Analysis", | |
| page_icon="π¬", | |
| layout="centered" | |
| ) | |
| # Custom styles | |
| st.markdown(""" | |
| <style> | |
| .stTextArea textarea { | |
| height: 50px; | |
| } | |
| .result-box { | |
| padding: 0.5rem; | |
| border-radius: 0.5rem; | |
| margin-top: 0.5rem; | |
| text-align: center; | |
| font-size: 1.3rem; | |
| } | |
| .positive { background-color: #d4edda; color: #155724; } | |
| .negative { background-color: #f8d7da; color: #721c24; } | |
| .neutral { background-color: #fff3cd; color: #856404; } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # App title and intro | |
| st.title("π¬ Sentiment Analysis App") | |
| st.markdown("Predict sentiment using simple Python logic β no ML, just rule-based π") | |
| # Input section | |
| st.subheader("π Enter your text") | |
| user_input = st.text_area("", placeholder="Type or paste your sentence here...") | |
| # Keywords | |
| positive_words = ["good", "great", "happy", "excellent", "amazing", "love", "awesome", "fantastic", "positive", "nice"] | |
| negative_words = ["bad", "sad", "terrible", "horrible", "hate", "awful", "worst", "angry", "negative", "poor"] | |
| # Sentiment analysis function | |
| def analyze_sentiment(text): | |
| text = text.lower() | |
| pos_count = sum(word in text for word in positive_words) | |
| neg_count = sum(word in text for word in negative_words) | |
| if pos_count > neg_count: | |
| return "π Positive", "positive" | |
| elif neg_count > pos_count: | |
| return "βΉοΈ Negative", "negative" | |
| else: | |
| return "π Neutral", "neutral" | |
| # Button & result | |
| if st.button("π Analyze Sentiment"): | |
| if user_input.strip(): | |
| sentiment, sentiment_class = analyze_sentiment(user_input) | |
| st.markdown(f'<div class="result-box {sentiment_class}">{sentiment}</div>', unsafe_allow_html=True) | |
| else: | |
| st.warning("β οΈ Please enter some text before analyzing.") | |
| # Footer | |
| st.markdown("---") | |
| st.markdown("Made with β€οΈ using Streamlit | Rule-based approach") | |