Spaces:
Sleeping
Sleeping
| 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 | |
| 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) | |