Spaces:
Sleeping
Sleeping
File size: 3,250 Bytes
24ba145 053ff25 13afc7c 053ff25 13afc7c 053ff25 13afc7c 053ff25 13afc7c 053ff25 13afc7c 053ff25 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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()
|