anamjafar6's picture
Update app.py
3b4c717 verified
import streamlit as st
from transformers import pipeline
# Page settings
st.set_page_config(page_title="Sentiment Analysis App", page_icon="πŸ’¬", layout="centered")
# Custom CSS for styling
st.markdown("""
<style>
.main-container {
background-color: #f0f2f6;
padding: 20px;
border-radius: 15px;
max-width: 600px;
margin: auto;
box-shadow: 2px 2px 15px rgba(0,0,0,0.1);
}
.title {
text-align: center;
color: #4CAF50;
font-size: 2.5em;
margin-bottom: 10px;
}
.subtitle {
text-align: center;
color: #555;
margin-bottom: 30px;
}
.result-box {
background-color: #ffffff;
padding: 15px;
border-radius: 10px;
text-align: center;
font-size: 1.2em;
margin-top: 20px;
box-shadow: 1px 1px 10px rgba(0,0,0,0.05);
}
.positive {
color: #388e3c;
font-weight: bold;
}
.negative {
color: #d32f2f;
font-weight: bold;
}
.neutral {
color: #1976d2;
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
# Load model
@st.cache_resource
def load_model():
return pipeline("sentiment-analysis")
nlp = load_model()
# Main container
with st.container():
st.markdown("<div class='main-container'>", unsafe_allow_html=True)
st.markdown("<div class='title'>πŸ’¬ Sentiment Analysis</div>", unsafe_allow_html=True)
st.markdown("<div class='subtitle'>Discover the sentiment behind your words!</div>", unsafe_allow_html=True)
text = st.text_area("Enter your text here:", height=150, placeholder="Type your sentence...")
if st.button("✨ Analyze Sentiment"):
if not text.strip():
st.warning("Please enter some text to analyze.")
else:
with st.spinner("Analyzing..."):
result = nlp(text)
label = result[0]['label']
score = result[0]['score']
st.markdown("<div class='result-box'>", unsafe_allow_html=True)
if label == "POSITIVE":
st.markdown(f"<span class='positive'>πŸ™‚ Positive Sentiment</span><br>Confidence: {score:.2f}", unsafe_allow_html=True)
elif label == "NEGATIVE":
st.markdown(f"<span class='negative'>☹️ Negative Sentiment</span><br>Confidence: {score:.2f}", unsafe_allow_html=True)
else:
st.markdown(f"<span class='neutral'>😐 Neutral Sentiment</span><br>Confidence: {score:.2f}", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)