Spaces:
Sleeping
Sleeping
File size: 10,835 Bytes
875374f be3853b 07dccfc be3853b 07dccfc 3935af7 07dccfc ba70128 c4e745f 07dccfc 3935af7 07dccfc ba70128 07dccfc 3935af7 07dccfc 3935af7 07dccfc c4e745f ba70128 c4e745f ba70128 c4e745f ba70128 07dccfc ad08169 67bb397 07dccfc 67bb397 ad08169 67bb397 07dccfc ad08169 07dccfc 67bb397 3935af7 67bb397 07dccfc ad08169 f64bc90 67bb397 3935af7 67bb397 3935af7 f64bc90 07dccfc be3853b 969ff00 be3853b d483d4c be3853b d483d4c be3853b 3935af7 be3853b ad08169 be3853b ba70128 3935af7 be3853b ba70128 3935af7 be3853b 67bb397 c4e745f 3935af7 be3853b c4e745f 3935af7 be3853b 3935af7 be3853b 3935af7 be3853b 07dccfc be3853b 07dccfc be3853b ad08169 be3853b 3935af7 be3853b c4e745f 67bb397 be3853b 07dccfc c4e745f f64bc90 07dccfc 3935af7 be3853b ad08169 67bb397 ad08169 67bb397 be3853b 07dccfc be3853b 40422b6 be3853b | 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | import streamlit as st
from textblob import TextBlob
import json
import os
import matplotlib.pyplot as plt
# === UI/UX Modifications ===
def set_background_and_styles():
st.markdown(
"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;700&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Merriweather:wght@300;400;700&display=swap');
.stApp {
background-image: url("https://i.pinimg.com/1200x/f0/ca/71/f0ca7163efb2ecc676426ae5ba1c1723.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
font-family: 'Montserrat', sans-serif;
color: white; /* Make all general text white */
}
/* Changed all header colors to white, also targeting labels directly */
h1, h2, h3, h4, h5, h6, .stMarkdown, label { /* Added .stMarkdown and label for white text */
font-family: 'Merriweather', serif;
color: white;
}
/* Specific target for the text area label, just in case */
.stTextArea > label {
color: white !important;
}
.stButton>button {
background-color: #4682B4; /* Steel Blue */
color: white;
border-radius: 8px;
border: none;
padding: 10px 20px;
font-size: 16px;
font-weight: bold;
transition: all 0.2s ease-in-out;
}
.stButton>button:hover {
background-color: #5F9EA0; /* Cadet Blue */
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.stTextInput>div>div>input, .stTextArea>div>div>textarea {
border-radius: 8px;
border: 1px solid #B0C4DE; /* LightSteelBlue */
padding: 10px;
background-color: rgba(255, 255, 255, 0.85);
color: #333; /* Input text color should remain dark for readability */
}
.stSelectbox>div>div>div {
border-radius: 8px;
border: 1px solid #B0C4DE;
background-color: rgba(255, 255, 255, 0.85);
color: #333; /* Selectbox text color should remain dark for readability */
}
/* Modified stSuccess and stInfo for white text on darker transparent black background */
.stSuccess {
border-left: 5px solid #28a745;
background-color: rgba(0, 0, 0, 0.6); /* Transparent black */
color: white; /* Make text white */
border-radius: 8px;
padding: 10px;
margin-bottom: 10px;
}
.stInfo {
border-left: 5px solid #17a2b8;
background-color: rgba(0, 0, 0, 0.6); /* Transparent black */
color: white; /* Make text white */
border-radius: 8px;
padding: 10px;
margin-bottom: 10px;
}
.stWarning {
border-left: 5px solid #ffc107;
background-color: rgba(255, 255, 255, 0.9); /* Keep existing light for warning */
color: #333; /* Keep dark for warning */
border-radius: 8px;
padding: 10px;
margin-bottom: 10px;
}
.stError {
border-left: 5px solid #dc3545;
background-color: rgba(255, 255, 255, 0.9); /* Keep existing light for error */
color: #333; /* Keep dark for error */
border-radius: 8px;
padding: 10px;
margin-bottom: 10px;
}
/* Custom container for content with blur background */
.main-content-container {
background-color: rgba(255, 255, 255, 0.7); /* Slightly transparent white */
backdrop-filter: blur(5px); /* Blur effect */
border-radius: 15px;
padding: 30px;
margin: 20px auto; /* Centering the container */
max-width: 550px; /* Significantly less wide */
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
/* Wider layout (adjusted for much less wide) */
.st-emotion-cache-fg4lbf {
max-width: 600px !important; /* Adjusted for less wide */
padding-left: 0 !important;
padding-right: 0 !important;
}
.st-emotion-cache-1d391kg {
padding-top: 2rem;
padding-bottom: 2rem;
}
.block-container { /* Ensure content inside main-content-container respects its max-width */
padding-left: 1rem;
padding-right: 1rem;
color: white; /* Ensure text within block-container is white */
}
/* Center plots */
.st-emotion-cache-1pxazr6 { /* Specific Streamlit container for pyplot */
display: flex;
justify-content: center;
}
/* Hide the top grey bar */
header.st-emotion-cache-1gh8zsi {
display: none !important;
}
div.st-emotion-cache-fis6y8 {
padding-top: 0 !important;
}
div.st-emotion-cache-z5inrg {
display: none !important;
}
</style>
""",
unsafe_allow_html=True
)
# === Load Quotes ===
QUOTES_PATH = os.path.join(os.path.dirname(__file__), "quotes.json")
# Create a dummy quotes.json if it doesn't exist for the script to run locally without error
if not os.path.exists(QUOTES_PATH):
dummy_quotes = {
"positive": ["You are doing great!", "Keep shining!", "Believe in yourself!"],
"negative": ["It's okay to feel down, brighter days are ahead.", "Take a deep breath.", "This too shall pass."],
"neutral": ["Observe your thoughts.", "Find your center.", "Acknowledge your feelings."]
}
with open(QUOTES_PATH, "w") as f:
json.dump(dummy_quotes, f, indent=4)
with open(QUOTES_PATH, "r") as f:
quotes_data = json.load(f)
# === Emotion Detection via TextBlob ===
def detect_emotion(text):
blob = TextBlob(text)
polarity = blob.sentiment.polarity
if polarity > 0.2:
return "positive", polarity
elif polarity < -0.2:
return "negative", polarity
else:
return "neutral", polarity
# === Quotes Retrieval ===
def get_quote(emotion):
if isinstance(quotes_data, dict):
# Return a random quote from the list for the given emotion
import random
return [random.choice(quotes_data.get(emotion, ["Stay strong, you're doing great!"]))]
else:
return ["Stay strong, you're doing great!"]
# === Page Layout ===
st.set_page_config(page_title="DilBot - Emotionally Intelligent AI Companion", layout="centered")
set_background_and_styles()
# This div will act as the centered container
st.markdown('<div class="main-content-container">', unsafe_allow_html=True)
# Motivational quote above title
st.markdown("<p style='text-align: center; font-style: italic; color: white;'>\"The only way to do great work is to love what you do.\"</p>", unsafe_allow_html=True)
st.title("DilBot: Your Emotional Companion")
st.markdown("---")
# === Input Section ===
# Label color is now white via CSS
user_input = st.text_area("How are you feeling today?", height=150, placeholder="Type your thoughts here...")
if st.button("Analyze"):
if user_input.strip():
# Detect Emotion
emotion, polarity = detect_emotion(user_input)
confidence = abs(polarity) * 100
# Display Emotion - now white text on transparent black background
st.subheader("Detected Emotion:")
st.success(f"**{emotion.capitalize()}** with a confidence of **{confidence:.2f}%**")
# Show a personal quote - now white text on transparent black background
st.markdown("### DilBot's Message:")
quote = get_quote(emotion)[0]
st.info(f"_{quote}_")
# === Personal Dashboard ===
st.markdown("### Personal Dashboard")
# Bar & Doughnut Chart
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3), dpi=100)
labels = ['Positive', 'Neutral', 'Negative']
display_values = [0, 0, 0]
if emotion == 'positive':
display_values[0] = confidence
remaining = 100 - confidence
display_values[1] = remaining / 2
display_values[2] = remaining / 2
elif emotion == 'negative':
display_values[2] = confidence
remaining = 100 - confidence
display_values[0] = remaining / 2
display_values[1] = remaining / 2
else: # neutral
display_values[1] = confidence
remaining = 100 - confidence
display_values[0] = remaining / 2
display_values[2] = remaining / 2
# Ensure values sum to 100 for the pie chart
total_sum = sum(display_values)
if total_sum != 0:
display_values = [v * 100 / total_sum for v in display_values]
colors = ['#00cc99', '#b0bec5', '#ff6f61']
# Set chart backgrounds to white
ax1.set_facecolor('white')
ax2.set_facecolor('white')
fig.patch.set_facecolor('white') # Entire figure background white
# Bar Chart
ax1.bar(labels, display_values, color=colors)
ax1.set_title("Confidence by Emotion", color='black')
ax1.set_ylabel("Percentage", color='black')
ax1.tick_params(axis='x', colors='black')
ax1.tick_params(axis='y', colors='black')
ax1.spines['bottom'].set_color('black')
ax1.spines['left'].set_color('black')
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
# Doughnut Chart
if sum(display_values) > 0:
wedges, texts, autotexts = ax2.pie(
display_values, labels=labels, autopct='%1.1f%%',
colors=colors, startangle=90, wedgeprops=dict(width=0.4)
)
ax2.set_title("Emotion Distribution", color='black')
for text_obj in texts:
text_obj.set_color('black')
for autotext_obj in autotexts:
autotext_obj.set_color('black')
else:
ax2.text(0.5, 0.5, 'No data', horizontalalignment='center', verticalalignment='center', transform=ax2.transAxes, color='black')
# Use st.columns to center the plot
col_left, col_center, col_right = st.columns([0.5, 6, 0.5])
with col_center:
st.pyplot(fig)
plt.close(fig)
else:
st.warning("Please write something to analyze.")
st.markdown('</div>', unsafe_allow_html=True) # Close main-content-container |