Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from textblob import TextBlob | |
| def main(): | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="Basic Sentiment Analysis", | |
| page_icon="π", | |
| layout="centered" | |
| ) | |
| # Custom CSS for styling | |
| st.markdown(""" | |
| <style> | |
| .main { | |
| background-color: #f8f9fa; | |
| } | |
| .stTextInput > div > div > input { | |
| border-radius: 15px; | |
| padding: 10px; | |
| } | |
| .reportview-container .main .block-container { | |
| padding: 2rem; | |
| } | |
| .result-box { | |
| padding: 20px; | |
| border-radius: 10px; | |
| margin-top: 20px; | |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
| } | |
| .positive { | |
| background-color: #d4edda; | |
| color: #155724; | |
| } | |
| .neutral { | |
| background-color: #e2e3e5; | |
| color: #383d41; | |
| } | |
| .negative { | |
| background-color: #f8d7da; | |
| color: #721c24; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Header | |
| st.title("Basic Sentiment Analysis") | |
| st.markdown("Enter text below to analyze its sentiment (without using pre-trained models).") | |
| # Text input | |
| user_input = st.text_area("Enter your text:", height=100, | |
| placeholder="Type something like 'I love this!' or 'This is terrible.'") | |
| if st.button("Analyze Sentiment"): | |
| if user_input: | |
| # Analyze sentiment using TextBlob | |
| analysis = TextBlob(user_input) | |
| polarity = analysis.sentiment.polarity | |
| # Determine sentiment category | |
| if polarity > 0.2: | |
| sentiment = "Positive π" | |
| emotion_class = "positive" | |
| elif polarity < -0.2: | |
| sentiment = "Negative π" | |
| emotion_class = "negative" | |
| else: | |
| sentiment = "Neutral π" | |
| emotion_class = "neutral" | |
| # Display results | |
| st.markdown(f"<div class='result-box {emotion_class}'>", unsafe_allow_html=True) | |
| st.subheader("Sentiment Analysis Results:") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.metric("Sentiment", sentiment) | |
| with col2: | |
| st.metric("Polarity Score", round(polarity, 3)) | |
| st.progress((polarity + 1) / 2) | |
| st.markdown(""" | |
| **Polarity Scale:** | |
| -1.0 (Very Negative) ββ 0.0 (Neutral) ββ +1.0 (Very Positive) | |
| """) | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| # Additional analysis | |
| with st.expander("Detailed Analysis:"): | |
| st.write(f"- **Subjectivity:** {'Subjective' if analysis.sentiment.subjectivity > 0.5 else 'Objective'} " | |
| f"(Score: {round(analysis.sentiment.subjectivity, 3)})") | |
| st.write("- **Word Count:**", len(analysis.words)) | |
| else: | |
| st.warning("Please enter some text to analyze.") | |
| if __name__ == "__main__": | |
| main() | |