CoolFace
Apppublic

Samran13/Sentiments

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py147 linesDownload Raw Back to root
1import streamlit as st
2from transformers import pipeline
3
4# Custom styling with dark and shining colors
5st.markdown(
6    """
7    <style>
8        body {
9            background-image: url('https://source.unsplash.com/1600x900/?dark,neon');
10            background-size: cover;
11            background-position: center;
12            background-attachment: fixed;
13            color: #ffffff;
14        }
15        .stApp {
16            max-width: 700px;
17            margin: auto;
18            padding: 2rem;
19            border-radius: 15px;
20            background: rgba(0, 0, 0, 0.8);
21            box-shadow: 0px 6px 20px rgba(0, 255, 255, 0.5);
22        }
23        .title {
24            color: #00FFFF;
25            font-size: 3rem;
26            font-weight: bold;
27            text-align: center;
28            margin-bottom: 20px;
29            text-shadow: 0 0 10px #00FFFF;
30        }
31        .text-area {
32            border-radius: 10px;
33            padding: 15px;
34            font-size: 1.1rem;
35            background-color: #222;
36            color: #00FFFF;
37            border: 1px solid #00FFFF;
38        }
39        .button {
40            display: flex;
41            justify-content: center;
42            margin-top: 20px;
43        }
44        .result-box {
45            padding: 20px;
46            border-radius: 10px;
47            margin-top: 20px;
48            font-size: 1.2rem;
49            font-weight: bold;
50            text-align: center;
51            text-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
52        }
53        .positive {
54            background-color: #0f5132;
55            color: #d1e7dd;
56            box-shadow: 0 0 10px #0f5132;
57        }
58        .negative {
59            background-color: #58151c;
60            color: #f8d7da;
61            box-shadow: 0 0 10px #721C24;
62        }
63        .neutral {
64            background-color: #664d03;
65            color: #fff3cd;
66            box-shadow: 0 0 10px #856404;
67        }
68        .mixed {
69            background-color: #3b3b98;
70            color: #dcd6f7;
71            box-shadow: 0 0 10px #3b3b98;
72        }
73        .very_positive {
74            background-color: #006400;
75            color: #90ee90;
76            box-shadow: 0 0 10px #008000;
77        }
78        .very_negative {
79            background-color: #8b0000;
80            color: #ffcccb;
81            box-shadow: 0 0 10px #ff0000;
82        }
83        .angry {
84            background-color: #ff4500;
85            color: #fff5ee;
86            box-shadow: 0 0 10px #ff4500;
87        }
88        .emotional {
89            background-color: #800080;
90            color: #e6ccff;
91            box-shadow: 0 0 10px #800080;
92        }
93        .more_happy {
94            background-color: #ffd700;
95            color: #fffacd;
96            box-shadow: 0 0 10px #ffd700;
97        }
98    </style>
99    """,
100    unsafe_allow_html=True
101)
102
103# Set the title of the app
104st.markdown('<p class="title">Sentiment Analysis App</p>', unsafe_allow_html=True)
105
106# Load the pre-trained sentiment analysis model
107model = pipeline("sentiment-analysis")
108
109# Add a text input box for user input
110user_input = st.text_area("Enter your text here:", height=150, help="Type a sentence or paragraph to analyze its sentiment.")
111
112# Add a button to trigger the analysis
113if st.button("Analyze Sentiment", use_container_width=True):
114    if user_input:
115        # Perform sentiment analysis
116        result = model(user_input)[0]
117        sentiment = result['label']
118        confidence = result['score']
119        
120        # Determine color class
121        if sentiment == "POSITIVE":
122            color_class = "positive"
123        elif sentiment == "NEGATIVE":
124            color_class = "negative"
125        elif sentiment == "MIXED":
126            color_class = "mixed"
127        elif sentiment == "VERY_POSITIVE":
128            color_class = "very_positive"
129        elif sentiment == "VERY_NEGATIVE":
130            color_class = "very_negative"
131        elif "angry" in user_input.lower():
132            color_class = "angry"
133            sentiment = "ANGRY"
134        elif "cry" in user_input.lower() or "emotional" in user_input.lower():
135            color_class = "emotional"
136            sentiment = "EMOTIONAL"
137        elif "happy" in user_input.lower() or "excited" in user_input.lower():
138            color_class = "more_happy"
139            sentiment = "MORE HAPPY"
140        else:
141            color_class = "neutral"
142        
143        # Display the results with styling
144        st.markdown(f'<div class="result-box {color_class}"><b>Sentiment:</b> {sentiment} <br><b>Confidence:</b> {confidence:.2f}</div>', unsafe_allow_html=True)
145    else:
146        st.warning("Please enter some text to analyze.")
147