Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1071,219 +1071,297 @@ def show_main_app():
|
|
| 1071 |
"""Main DilBot application"""
|
| 1072 |
username = st.session_state.username
|
| 1073 |
|
| 1074 |
-
st.set_page_config(page_title="DilBot - Emotional AI", page_icon="🧠")
|
| 1075 |
-
|
|
|
|
| 1076 |
st.markdown("""
|
| 1077 |
<style>
|
| 1078 |
/* Global Styles & Background */
|
| 1079 |
.stApp {
|
| 1080 |
-
background
|
| 1081 |
-
color: #
|
| 1082 |
-
font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; /* Modern, clean font */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1083 |
}
|
| 1084 |
|
|
|
|
| 1085 |
/* Header & Titles */
|
| 1086 |
h1, h2, h3, h4, h5, h6 {
|
| 1087 |
-
color: #
|
| 1088 |
-
margin-top: 1.
|
| 1089 |
-
margin-bottom: 0.
|
|
|
|
| 1090 |
}
|
| 1091 |
h1 {
|
| 1092 |
-
font-size: 2.
|
| 1093 |
-
font-weight:
|
| 1094 |
-
color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1095 |
}
|
| 1096 |
h2 {
|
| 1097 |
-
font-size:
|
| 1098 |
-
font-weight:
|
| 1099 |
-
border-bottom:
|
| 1100 |
-
padding-bottom: 0.
|
| 1101 |
-
margin-bottom:
|
|
|
|
| 1102 |
}
|
| 1103 |
h3 {
|
| 1104 |
-
font-size: 1.
|
| 1105 |
font-weight: 600;
|
| 1106 |
-
color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1107 |
}
|
| 1108 |
|
|
|
|
| 1109 |
/* Metrics (Total Conversations, Most Common Emotion, Avg. Confidence) */
|
| 1110 |
[data-testid="stMetric"] {
|
| 1111 |
background-color: #ffffff;
|
| 1112 |
-
border: 1px solid #
|
| 1113 |
-
border-radius:
|
| 1114 |
-
padding:
|
| 1115 |
-
box-shadow: 0
|
| 1116 |
text-align: center;
|
| 1117 |
-
margin-bottom:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1118 |
}
|
| 1119 |
[data-testid="stMetricLabel"] {
|
| 1120 |
-
font-size:
|
| 1121 |
-
color: #
|
| 1122 |
-
margin-bottom:
|
|
|
|
| 1123 |
}
|
| 1124 |
[data-testid="stMetricValue"] {
|
| 1125 |
-
font-size: 2.
|
| 1126 |
-
font-weight:
|
| 1127 |
-
color: #
|
| 1128 |
}
|
| 1129 |
|
| 1130 |
/* Buttons */
|
| 1131 |
.stButton>button {
|
| 1132 |
-
background-color: #
|
| 1133 |
color: white;
|
| 1134 |
border: none;
|
| 1135 |
-
border-radius:
|
| 1136 |
-
padding:
|
| 1137 |
-
font-size: 1em;
|
| 1138 |
font-weight: bold;
|
| 1139 |
-
transition: background-color 0.3s ease, transform 0.2s ease;
|
| 1140 |
-
box-shadow: 0
|
| 1141 |
cursor: pointer;
|
|
|
|
| 1142 |
}
|
| 1143 |
.stButton>button:hover {
|
| 1144 |
-
background-color: #
|
| 1145 |
-
transform: translateY(-
|
|
|
|
| 1146 |
}
|
| 1147 |
.stButton>button:active {
|
| 1148 |
-
background-color: #
|
| 1149 |
transform: translateY(0);
|
|
|
|
| 1150 |
}
|
| 1151 |
-
/*
|
| 1152 |
-
[
|
| 1153 |
background-color: #dc3545; /* Red for logout */
|
|
|
|
| 1154 |
}
|
| 1155 |
-
[
|
| 1156 |
background-color: #c82333;
|
|
|
|
| 1157 |
}
|
| 1158 |
|
| 1159 |
|
| 1160 |
/* Text Inputs and Text Areas */
|
| 1161 |
.stTextInput>div>div>input, .stTextArea>div>div>textarea {
|
| 1162 |
-
border-radius:
|
| 1163 |
-
border: 1px solid #ced4da;
|
| 1164 |
-
padding:
|
| 1165 |
-
font-size:
|
| 1166 |
-
color: #
|
| 1167 |
-
|
|
|
|
| 1168 |
transition: border-color 0.3s ease, box-shadow 0.3s ease;
|
| 1169 |
}
|
| 1170 |
.stTextInput>div>div>input:focus, .stTextArea>div>div>textarea:focus {
|
| 1171 |
-
border-color: #
|
| 1172 |
-
box-shadow: 0 0 0 0.
|
| 1173 |
outline: none;
|
| 1174 |
}
|
| 1175 |
.stTextInput>label, .stTextArea>label {
|
| 1176 |
font-weight: 600;
|
| 1177 |
-
color: #
|
| 1178 |
-
margin-bottom:
|
| 1179 |
}
|
| 1180 |
|
| 1181 |
/* Selectbox (Dropdown) */
|
| 1182 |
.stSelectbox>div>div>div {
|
| 1183 |
-
border-radius:
|
| 1184 |
border: 1px solid #ced4da;
|
| 1185 |
-
padding:
|
| 1186 |
-
font-size:
|
| 1187 |
-
color: #
|
| 1188 |
-
|
|
|
|
| 1189 |
}
|
| 1190 |
.stSelectbox>label {
|
| 1191 |
font-weight: 600;
|
| 1192 |
-
color: #
|
|
|
|
| 1193 |
}
|
| 1194 |
|
| 1195 |
/* File Uploader */
|
| 1196 |
[data-testid="stFileUploaderDropzone"] {
|
| 1197 |
-
border-radius:
|
| 1198 |
-
border: 2px dashed #a0a8b4;
|
| 1199 |
background-color: #ffffff;
|
| 1200 |
-
padding:
|
| 1201 |
transition: border-color 0.3s ease, background-color 0.3s ease;
|
| 1202 |
-
margin-bottom:
|
| 1203 |
}
|
| 1204 |
[data-testid="stFileUploaderDropzone"]:hover {
|
| 1205 |
-
border-color: #
|
| 1206 |
-
background-color: #
|
| 1207 |
}
|
| 1208 |
/* Hide the redundant 'Drag and drop file here' text */
|
| 1209 |
[data-testid="stFileUploaderDropzone"] p:first-child {
|
| 1210 |
display: none;
|
| 1211 |
}
|
| 1212 |
[data-testid="stFileUploaderFileName"] {
|
| 1213 |
-
font-size: 0.
|
| 1214 |
-
color: #
|
| 1215 |
-
margin-top:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1216 |
}
|
| 1217 |
|
| 1218 |
|
| 1219 |
/* Information, Success, Error, Warning Boxes */
|
| 1220 |
[data-testid="stAlert"] {
|
| 1221 |
-
border-radius:
|
| 1222 |
-
padding:
|
| 1223 |
-
margin-bottom:
|
| 1224 |
-
font-size:
|
| 1225 |
-
line-height: 1.
|
| 1226 |
-
|
| 1227 |
-
|
| 1228 |
-
align-items: center;
|
| 1229 |
}
|
| 1230 |
-
/* Specific styles for each type */
|
| 1231 |
[data-testid="stAlert"] .streamlit-warning {
|
| 1232 |
-
background-color: #
|
| 1233 |
-
color: #664d03;
|
| 1234 |
-
border-left: 5px solid #ffc107; /* Yellow border */
|
| 1235 |
}
|
| 1236 |
[data-testid="stAlert"] .streamlit-success {
|
| 1237 |
-
background-color: #
|
| 1238 |
-
color: #0f5132;
|
| 1239 |
-
border-left: 5px solid #198754; /* Green border */
|
| 1240 |
}
|
| 1241 |
[data-testid="stAlert"] .streamlit-error {
|
| 1242 |
-
background-color: #
|
| 1243 |
-
color: #842029;
|
| 1244 |
-
border-left: 5px solid #dc3545; /* Red border */
|
| 1245 |
}
|
| 1246 |
[data-testid="stAlert"] .streamlit-info {
|
| 1247 |
-
background-color: #
|
| 1248 |
-
color: #055160;
|
| 1249 |
-
border-left: 5px solid #0dcaf0; /* Cyan border */
|
| 1250 |
}
|
| 1251 |
|
| 1252 |
|
| 1253 |
-
/*
|
| 1254 |
-
.
|
| 1255 |
-
|
| 1256 |
-
|
| 1257 |
-
margin-
|
| 1258 |
-
margin-bottom: 1rem;
|
| 1259 |
}
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
|
| 1263 |
-
border-
|
| 1264 |
-
padding:
|
| 1265 |
-
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
|
| 1269 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
| 1270 |
-
line-height: 1.6;
|
| 1271 |
}
|
| 1272 |
-
|
| 1273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1274 |
}
|
| 1275 |
|
| 1276 |
/* Quote for you (st.info) */
|
| 1277 |
-
.stInfo {
|
| 1278 |
background-color: #e6f7ff; /* Lighter blue for quote box */
|
| 1279 |
-
border-left: 6px solid #
|
| 1280 |
color: #004085;
|
| 1281 |
-
border-radius:
|
| 1282 |
-
padding:
|
| 1283 |
-
margin-top:
|
| 1284 |
font-style: italic;
|
|
|
|
|
|
|
| 1285 |
}
|
| 1286 |
-
.stInfo span[data-baseweb="tag"] {
|
| 1287 |
font-weight: bold;
|
| 1288 |
color: #004085;
|
| 1289 |
}
|
|
@@ -1296,91 +1374,99 @@ def show_main_app():
|
|
| 1296 |
}
|
| 1297 |
|
| 1298 |
/* Expander styling (for conversations) */
|
| 1299 |
-
.streamlit-expanderContent {
|
| 1300 |
-
background-color: #f8f9fa; /* Light background for content */
|
| 1301 |
-
border-left: 3px solid #ced4da;
|
| 1302 |
-
padding: 10px 15px;
|
| 1303 |
-
border-bottom-left-radius: 8px;
|
| 1304 |
-
border-bottom-right-radius: 8px;
|
| 1305 |
-
margin-top: -10px; /* Adjust to sit right under header */
|
| 1306 |
-
}
|
| 1307 |
.streamlit-expanderHeader {
|
| 1308 |
background-color: #ffffff;
|
| 1309 |
border: 1px solid #e0e0e0;
|
| 1310 |
-
border-radius:
|
| 1311 |
-
padding:
|
| 1312 |
-
margin-bottom: 0.
|
| 1313 |
cursor: pointer;
|
| 1314 |
-
transition: background-color 0.2s ease;
|
| 1315 |
-
box-shadow: 0
|
| 1316 |
}
|
| 1317 |
.streamlit-expanderHeader:hover {
|
| 1318 |
-
background-color: #
|
|
|
|
| 1319 |
}
|
| 1320 |
-
.streamlit-expanderHeader > div > div > p {
|
| 1321 |
-
font-weight:
|
| 1322 |
-
color: #
|
|
|
|
| 1323 |
}
|
| 1324 |
-
.
|
| 1325 |
-
|
| 1326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1327 |
}
|
| 1328 |
|
| 1329 |
|
| 1330 |
/* Horizontal rule */
|
| 1331 |
hr {
|
| 1332 |
-
border-top:
|
| 1333 |
-
margin-top:
|
| 1334 |
-
margin-bottom:
|
| 1335 |
}
|
| 1336 |
|
| 1337 |
/* Caption (footer) */
|
| 1338 |
-
.
|
| 1339 |
-
color: #
|
| 1340 |
-
font-size: 0.
|
| 1341 |
-
margin-top:
|
| 1342 |
-
display: block;
|
| 1343 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1344 |
}
|
| 1345 |
|
| 1346 |
</style>
|
| 1347 |
""", unsafe_allow_html=True)
|
| 1348 |
# --- END CUSTOM CSS ---
|
| 1349 |
|
| 1350 |
-
|
| 1351 |
-
|
| 1352 |
col1, col2 = st.columns([4, 1])
|
| 1353 |
with col1:
|
| 1354 |
-
st.
|
| 1355 |
-
st.markdown("<p
|
| 1356 |
with col2:
|
| 1357 |
-
|
|
|
|
| 1358 |
st.session_state.authenticated = False
|
| 1359 |
st.session_state.username = None
|
| 1360 |
st.rerun()
|
| 1361 |
|
| 1362 |
-
#
|
| 1363 |
-
|
| 1364 |
-
|
| 1365 |
-
"Motivation": ["Believe in yourself and all that you are.", "Tough times never last, but tough people do.", "The only way to do great work is to love what you do."],
|
| 1366 |
-
"Healing": ["Every wound has its own time to heal.", "It's okay to take your time to feel better.", "Healing is not linear, and that's perfectly okay."],
|
| 1367 |
-
"Relationships": ["The best relationships are built on trust.", "Love is not about possession but appreciation.", "Healthy relationships require both people to show up authentically."]
|
| 1368 |
-
}
|
| 1369 |
-
|
| 1370 |
-
# UI for quote selection and file upload
|
| 1371 |
-
# Use st.container for better visual grouping
|
| 1372 |
-
st.markdown("### 📝 Input Your Thoughts") # New subheader for input section
|
| 1373 |
-
with st.container(border=True): # New feature in Streamlit for container with border
|
| 1374 |
col1, col2 = st.columns(2)
|
| 1375 |
with col1:
|
| 1376 |
-
|
|
|
|
|
|
|
| 1377 |
with col2:
|
| 1378 |
-
st.markdown("
|
| 1379 |
uploaded_quotes = st.file_uploader("Upload your own quotes (.txt)", type=["txt"], key="quote_uploader")
|
| 1380 |
uploaded_audio = st.file_uploader("Upload a voice message (.wav)", type=["wav"], key="audio_uploader")
|
| 1381 |
|
| 1382 |
-
# Voice transcription button
|
| 1383 |
-
if uploaded_audio and st.button("🎙️ Transcribe Voice Message", key="transcribe_btn"):
|
| 1384 |
with st.spinner("Transcribing your voice..."):
|
| 1385 |
transcribed = transcribe_audio_file(uploaded_audio)
|
| 1386 |
if transcribed.startswith("Error:"):
|
|
@@ -1388,8 +1474,8 @@ def show_main_app():
|
|
| 1388 |
else:
|
| 1389 |
st.session_state.transcribed_text = transcribed
|
| 1390 |
st.success("✅ Voice transcribed successfully!")
|
| 1391 |
-
|
| 1392 |
-
# Handle vectorstore
|
| 1393 |
current_quotes = []
|
| 1394 |
vectorstore = None
|
| 1395 |
|
|
@@ -1406,37 +1492,33 @@ def show_main_app():
|
|
| 1406 |
vectorstore = build_user_vectorstore(username, default_quotes)
|
| 1407 |
current_quotes = default_quotes
|
| 1408 |
|
| 1409 |
-
#
|
| 1410 |
-
|
| 1411 |
-
with st.spinner("Transcribing your voice..."):
|
| 1412 |
-
transcribed = transcribe_audio_file(uploaded_audio)
|
| 1413 |
-
if transcribed.startswith("Error:"):
|
| 1414 |
-
st.error(transcribed)
|
| 1415 |
-
else:
|
| 1416 |
-
st.session_state.transcribed_text = transcribed
|
| 1417 |
-
st.success("✅ Voice transcribed successfully!")
|
| 1418 |
-
|
| 1419 |
-
# Input area
|
| 1420 |
-
st.markdown("##### 💬 What's on your mind?")
|
| 1421 |
user_input = st.text_area(
|
| 1422 |
-
"Share your thoughts, feelings, or experiences...",
|
| 1423 |
value=st.session_state.transcribed_text,
|
| 1424 |
-
height=
|
| 1425 |
-
placeholder="Type here or use your transcribed voice message..."
|
|
|
|
| 1426 |
)
|
|
|
|
|
|
|
| 1427 |
|
| 1428 |
final_input = user_input.strip() or st.session_state.transcribed_text.strip()
|
| 1429 |
|
| 1430 |
# Main interaction button
|
|
|
|
|
|
|
|
|
|
| 1431 |
if st.button("🧠 Talk to DilBot", type="primary", use_container_width=True):
|
| 1432 |
if not final_input:
|
| 1433 |
st.warning("⚠️ Please enter something to share or upload a voice message.")
|
| 1434 |
else:
|
| 1435 |
with st.spinner("DilBot is thinking and feeling..."):
|
| 1436 |
-
# Emotion detection
|
| 1437 |
emotion, score = detect_emotion(final_input)
|
| 1438 |
|
| 1439 |
-
# Get AI response
|
| 1440 |
prompt_template = PromptTemplate(
|
| 1441 |
input_variables=["context", "user_input", "username"],
|
| 1442 |
template="""You are DilBot, an empathetic emotional support AI companion for {username}.
|
|
@@ -1448,28 +1530,28 @@ User's message:
|
|
| 1448 |
Respond as DilBot with warmth, empathy, and understanding. Keep it conversational and supportive."""
|
| 1449 |
)
|
| 1450 |
|
| 1451 |
-
# Get similar quotes
|
| 1452 |
similar_docs = vectorstore.similarity_search(final_input, k=2)
|
| 1453 |
context = "\n".join([doc.page_content for doc in similar_docs])
|
| 1454 |
|
| 1455 |
-
# Generate response
|
| 1456 |
groq_llm = ChatGroq(api_key=GROQ_API_KEY, model="llama3-70b-8192")
|
| 1457 |
chain = LLMChain(llm=groq_llm, prompt=prompt_template)
|
| 1458 |
response = chain.run(context=context, user_input=final_input, username=username)
|
| 1459 |
|
| 1460 |
-
# Save to user's journal
|
| 1461 |
save_user_journal(username, final_input, emotion, score, response)
|
| 1462 |
|
| 1463 |
-
# Display results
|
| 1464 |
-
st.markdown("
|
| 1465 |
with st.container(border=True): # Container for the conversation output
|
| 1466 |
-
# User's input presented in a
|
| 1467 |
-
st.markdown(f"<div class='user-message'>**You:** {final_input}</div>", unsafe_allow_html=True)
|
| 1468 |
st.success(f"**Emotion Detected:** {emotion.capitalize()} ({round(score*100)}% confidence)")
|
| 1469 |
|
| 1470 |
if is_crisis(final_input):
|
| 1471 |
st.error("🚨 Crisis detected! Please reach out to a mental health professional immediately. "
|
| 1472 |
-
"You are not alone. Consider contacting a helpline like the National Suicide Prevention Lifeline (988 in the US) or a local emergency service.")
|
| 1473 |
|
| 1474 |
if current_quotes:
|
| 1475 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
@@ -1480,25 +1562,46 @@ Respond as DilBot with warmth, empathy, and understanding. Keep it conversationa
|
|
| 1480 |
selected_quote = current_quotes[best_match]
|
| 1481 |
st.info(f"💭 **Quote for you:** *{selected_quote}*")
|
| 1482 |
|
| 1483 |
-
# DilBot's response presented in a
|
| 1484 |
-
st.markdown("
|
| 1485 |
-
st.markdown(f"> {response}") # Using your existing markdown blockquote
|
| 1486 |
|
| 1487 |
speak(response, username)
|
| 1488 |
st.session_state.transcribed_text = ""
|
| 1489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1490 |
# User's personal dashboard
|
| 1491 |
st.markdown("---")
|
| 1492 |
st.header("📊 Your Personal Dashboard")
|
| 1493 |
|
| 1494 |
-
# Load user's journal
|
| 1495 |
journal_data = load_user_journal(username)
|
| 1496 |
|
| 1497 |
if journal_data:
|
| 1498 |
-
#
|
| 1499 |
-
st.subheader("📈 Your
|
| 1500 |
with st.container(border=True):
|
| 1501 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1502 |
df_data = []
|
| 1503 |
for entry in journal_data:
|
| 1504 |
df_data.append({
|
|
@@ -1507,18 +1610,24 @@ Respond as DilBot with warmth, empathy, and understanding. Keep it conversationa
|
|
| 1507 |
"confidence": entry["confidence"]
|
| 1508 |
})
|
| 1509 |
if df_data:
|
| 1510 |
-
|
| 1511 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1512 |
y=alt.Y('count():Q', title='Frequency'),
|
| 1513 |
-
color=alt.Color('emotion:N', title='Emotion'),
|
| 1514 |
tooltip=['date:N', 'emotion:N', 'count():Q']
|
| 1515 |
).properties(
|
| 1516 |
-
width=600,
|
| 1517 |
-
height=
|
| 1518 |
title="Your Emotional Journey Over Time"
|
| 1519 |
).interactive() # Make chart interactive for zoom/pan
|
| 1520 |
st.altair_chart(chart, use_container_width=True)
|
| 1521 |
-
|
|
|
|
|
|
|
| 1522 |
# Recent conversations
|
| 1523 |
st.subheader("💬 Recent Conversations")
|
| 1524 |
recent_entries = journal_data[-5:] if len(journal_data) >= 5 else journal_data
|
|
@@ -1526,31 +1635,17 @@ Respond as DilBot with warmth, empathy, and understanding. Keep it conversationa
|
|
| 1526 |
with st.container(border=True): # Wrap recent conversations in a container
|
| 1527 |
if recent_entries:
|
| 1528 |
for i, entry in enumerate(reversed(recent_entries)):
|
| 1529 |
-
with st.expander(f"📅 {entry['date']} - {entry['emotion'].capitalize()} ({entry['confidence']}%)"):
|
| 1530 |
st.markdown(f"**You said:** {entry['user_input']}")
|
| 1531 |
st.markdown(f"**DilBot replied:** {entry['response']}")
|
| 1532 |
else:
|
| 1533 |
st.info("No recent conversations yet. Start talking to DilBot!")
|
| 1534 |
-
|
| 1535 |
-
# Statistics
|
| 1536 |
-
st.subheader("📊 Your Emotional Statistics")
|
| 1537 |
-
col1, col2, col3 = st.columns(3)
|
| 1538 |
-
|
| 1539 |
-
with col1:
|
| 1540 |
-
st.metric("Total Conversations", len(journal_data))
|
| 1541 |
-
with col2:
|
| 1542 |
-
emotions = [entry['emotion'] for entry in journal_data]
|
| 1543 |
-
most_common = max(set(emotions), key=emotions.count) if emotions else "None"
|
| 1544 |
-
st.metric("Most Common Emotion", most_common.capitalize())
|
| 1545 |
-
with col3:
|
| 1546 |
-
if journal_data:
|
| 1547 |
-
avg_confidence = sum(entry['confidence'] for entry in journal_data) / len(journal_data)
|
| 1548 |
-
st.metric("Avg. Confidence", f"{avg_confidence:.1f}%")
|
| 1549 |
else:
|
| 1550 |
-
st.info("🌟 Start your first conversation with DilBot to see your personal dashboard!")
|
| 1551 |
|
| 1552 |
st.markdown("---")
|
| 1553 |
-
st.
|
| 1554 |
|
| 1555 |
# Main app logic
|
| 1556 |
def main():
|
|
|
|
| 1071 |
"""Main DilBot application"""
|
| 1072 |
username = st.session_state.username
|
| 1073 |
|
| 1074 |
+
st.set_page_config(page_title="DilBot - Emotional AI", page_icon="🧠", layout="wide") # Changed to 'wide' for more space
|
| 1075 |
+
|
| 1076 |
+
# --- ENHANCED CUSTOM CSS FOR MAIN APP ---
|
| 1077 |
st.markdown("""
|
| 1078 |
<style>
|
| 1079 |
/* Global Styles & Background */
|
| 1080 |
.stApp {
|
| 1081 |
+
background: linear-gradient(to bottom right, #f8f9fa, #e9ecef); /* Softer, subtle gradient */
|
| 1082 |
+
color: #343a40; /* Darker default text for better contrast */
|
| 1083 |
+
font-family: 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; /* Modern, clean font stack */
|
| 1084 |
+
-webkit-font-smoothing: antialiased; /* Smoother font rendering */
|
| 1085 |
+
padding-top: 2rem; /* Add some padding at the top */
|
| 1086 |
+
}
|
| 1087 |
+
|
| 1088 |
+
/* Streamlit's main block container for content centering and width */
|
| 1089 |
+
.main .block-container {
|
| 1090 |
+
max-width: 1200px; /* Max width for content */
|
| 1091 |
+
padding-top: 2rem;
|
| 1092 |
+
padding-bottom: 2rem;
|
| 1093 |
+
padding-left: 1rem;
|
| 1094 |
+
padding-right: 1rem;
|
| 1095 |
+
}
|
| 1096 |
+
|
| 1097 |
+
/* Hide Streamlit's header/footer if you want a fully custom layout */
|
| 1098 |
+
.stApp > header {
|
| 1099 |
+
display: none;
|
| 1100 |
+
}
|
| 1101 |
+
.stApp > footer {
|
| 1102 |
+
display: none;
|
| 1103 |
}
|
| 1104 |
|
| 1105 |
+
|
| 1106 |
/* Header & Titles */
|
| 1107 |
h1, h2, h3, h4, h5, h6 {
|
| 1108 |
+
color: #212529; /* Very dark grey for strong headings */
|
| 1109 |
+
margin-top: 1.8rem; /* More space above headings */
|
| 1110 |
+
margin-bottom: 0.9rem; /* More space below headings */
|
| 1111 |
+
font-weight: 700;
|
| 1112 |
}
|
| 1113 |
h1 {
|
| 1114 |
+
font-size: 2.8em;
|
| 1115 |
+
font-weight: 800; /* Extra bold */
|
| 1116 |
+
color: #5d6dbe; /* A slightly darker, richer blue for main title */
|
| 1117 |
+
letter-spacing: -0.8px;
|
| 1118 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.05); /* Subtle text shadow */
|
| 1119 |
+
}
|
| 1120 |
+
.header-tagline { /* Custom class for the welcome tagline */
|
| 1121 |
+
font-size: 1.2em;
|
| 1122 |
+
color: #6c757d; /* Softer grey */
|
| 1123 |
+
margin-top: -0.5rem; /* Pull it closer to the main title */
|
| 1124 |
+
margin-bottom: 2.5rem; /* More space below tagline */
|
| 1125 |
+
font-weight: 400;
|
| 1126 |
}
|
| 1127 |
h2 {
|
| 1128 |
+
font-size: 2em;
|
| 1129 |
+
font-weight: 700;
|
| 1130 |
+
border-bottom: 2px solid #e9ecef; /* Thicker, softer line */
|
| 1131 |
+
padding-bottom: 0.7rem;
|
| 1132 |
+
margin-bottom: 2rem; /* More space below subheader */
|
| 1133 |
+
color: #343a40;
|
| 1134 |
}
|
| 1135 |
h3 {
|
| 1136 |
+
font-size: 1.6em;
|
| 1137 |
font-weight: 600;
|
| 1138 |
+
color: #495057;
|
| 1139 |
+
margin-top: 2rem;
|
| 1140 |
+
margin-bottom: 1.2rem;
|
| 1141 |
+
}
|
| 1142 |
+
h5 { /* For 'Custom Quotes & Voice Input' */
|
| 1143 |
+
font-size: 1.1em;
|
| 1144 |
+
font-weight: 600;
|
| 1145 |
+
color: #495057;
|
| 1146 |
+
margin-bottom: 1rem;
|
| 1147 |
}
|
| 1148 |
|
| 1149 |
+
|
| 1150 |
/* Metrics (Total Conversations, Most Common Emotion, Avg. Confidence) */
|
| 1151 |
[data-testid="stMetric"] {
|
| 1152 |
background-color: #ffffff;
|
| 1153 |
+
border: 1px solid #dee2e6; /* Softer border */
|
| 1154 |
+
border-radius: 12px; /* More rounded */
|
| 1155 |
+
padding: 20px; /* More padding */
|
| 1156 |
+
box-shadow: 0 8px 20px rgba(0,0,0,0.08); /* Stronger, softer shadow */
|
| 1157 |
text-align: center;
|
| 1158 |
+
margin-bottom: 1.5rem; /* More space below metrics */
|
| 1159 |
+
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
|
| 1160 |
+
}
|
| 1161 |
+
[data-testid="stMetric"]:hover {
|
| 1162 |
+
transform: translateY(-5px); /* Lift effect on hover */
|
| 1163 |
+
box-shadow: 0 12px 30px rgba(0,0,0,0.12); /* Enhanced shadow on hover */
|
| 1164 |
}
|
| 1165 |
[data-testid="stMetricLabel"] {
|
| 1166 |
+
font-size: 1em; /* Slightly larger label */
|
| 1167 |
+
color: #6c757d;
|
| 1168 |
+
margin-bottom: 8px; /* More space below label */
|
| 1169 |
+
font-weight: 500;
|
| 1170 |
}
|
| 1171 |
[data-testid="stMetricValue"] {
|
| 1172 |
+
font-size: 2.8em; /* Larger value */
|
| 1173 |
+
font-weight: 800; /* Bolder value */
|
| 1174 |
+
color: #5d6dbe; /* Match primary color */
|
| 1175 |
}
|
| 1176 |
|
| 1177 |
/* Buttons */
|
| 1178 |
.stButton>button {
|
| 1179 |
+
background-color: #5d6dbe; /* DilBot's primary blue/purple */
|
| 1180 |
color: white;
|
| 1181 |
border: none;
|
| 1182 |
+
border-radius: 10px; /* More rounded */
|
| 1183 |
+
padding: 12px 25px;
|
| 1184 |
+
font-size: 1.1em;
|
| 1185 |
font-weight: bold;
|
| 1186 |
+
transition: background-color 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
|
| 1187 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
| 1188 |
cursor: pointer;
|
| 1189 |
+
letter-spacing: 0.5px;
|
| 1190 |
}
|
| 1191 |
.stButton>button:hover {
|
| 1192 |
+
background-color: #4a5c9d; /* Darker shade on hover */
|
| 1193 |
+
transform: translateY(-3px); /* More pronounced lift effect */
|
| 1194 |
+
box-shadow: 0 8px 20px rgba(0,0,0,0.15);
|
| 1195 |
}
|
| 1196 |
.stButton>button:active {
|
| 1197 |
+
background-color: #3b4b80;
|
| 1198 |
transform: translateY(0);
|
| 1199 |
+
box-shadow: 0 3px 10px rgba(0,0,0,0.1);
|
| 1200 |
}
|
| 1201 |
+
/* Logout button specific style */
|
| 1202 |
+
[key="logout_btn"] > button {
|
| 1203 |
background-color: #dc3545; /* Red for logout */
|
| 1204 |
+
box-shadow: 0 5px 15px rgba(220,53,69,0.2);
|
| 1205 |
}
|
| 1206 |
+
[key="logout_btn"] > button:hover {
|
| 1207 |
background-color: #c82333;
|
| 1208 |
+
box-shadow: 0 8px 20px rgba(220,53,69,0.3);
|
| 1209 |
}
|
| 1210 |
|
| 1211 |
|
| 1212 |
/* Text Inputs and Text Areas */
|
| 1213 |
.stTextInput>div>div>input, .stTextArea>div>div>textarea {
|
| 1214 |
+
border-radius: 10px;
|
| 1215 |
+
border: 1px solid #ced4da;
|
| 1216 |
+
padding: 14px 18px; /* More padding */
|
| 1217 |
+
font-size: 1.05em;
|
| 1218 |
+
color: #343a40;
|
| 1219 |
+
background-color: #fcfcfc; /* Off-white background */
|
| 1220 |
+
box-shadow: inset 0 2px 5px rgba(0,0,0,0.03); /* Subtle inner shadow */
|
| 1221 |
transition: border-color 0.3s ease, box-shadow 0.3s ease;
|
| 1222 |
}
|
| 1223 |
.stTextInput>div>div>input:focus, .stTextArea>div>div>textarea:focus {
|
| 1224 |
+
border-color: #5d6dbe; /* Highlight on focus */
|
| 1225 |
+
box-shadow: 0 0 0 0.25rem rgba(93,109,190,0.25); /* Glow effect */
|
| 1226 |
outline: none;
|
| 1227 |
}
|
| 1228 |
.stTextInput>label, .stTextArea>label {
|
| 1229 |
font-weight: 600;
|
| 1230 |
+
color: #495057;
|
| 1231 |
+
margin-bottom: 0.6rem; /* More space below label */
|
| 1232 |
}
|
| 1233 |
|
| 1234 |
/* Selectbox (Dropdown) */
|
| 1235 |
.stSelectbox>div>div>div {
|
| 1236 |
+
border-radius: 10px;
|
| 1237 |
border: 1px solid #ced4da;
|
| 1238 |
+
padding: 12px 15px; /* More padding */
|
| 1239 |
+
font-size: 1.05em;
|
| 1240 |
+
color: #343a40;
|
| 1241 |
+
background-color: #fcfcfc;
|
| 1242 |
+
box-shadow: inset 0 2px 5px rgba(0,0,0,0.03);
|
| 1243 |
}
|
| 1244 |
.stSelectbox>label {
|
| 1245 |
font-weight: 600;
|
| 1246 |
+
color: #495057;
|
| 1247 |
+
margin-bottom: 0.6rem;
|
| 1248 |
}
|
| 1249 |
|
| 1250 |
/* File Uploader */
|
| 1251 |
[data-testid="stFileUploaderDropzone"] {
|
| 1252 |
+
border-radius: 12px; /* More rounded */
|
| 1253 |
+
border: 2px dashed #a0a8b4;
|
| 1254 |
background-color: #ffffff;
|
| 1255 |
+
padding: 25px; /* More padding */
|
| 1256 |
transition: border-color 0.3s ease, background-color 0.3s ease;
|
| 1257 |
+
margin-bottom: 1.5rem;
|
| 1258 |
}
|
| 1259 |
[data-testid="stFileUploaderDropzone"]:hover {
|
| 1260 |
+
border-color: #5d6dbe; /* Highlight on hover */
|
| 1261 |
+
background-color: #f7f9fb;
|
| 1262 |
}
|
| 1263 |
/* Hide the redundant 'Drag and drop file here' text */
|
| 1264 |
[data-testid="stFileUploaderDropzone"] p:first-child {
|
| 1265 |
display: none;
|
| 1266 |
}
|
| 1267 |
[data-testid="stFileUploaderFileName"] {
|
| 1268 |
+
font-size: 0.95em;
|
| 1269 |
+
color: #495057;
|
| 1270 |
+
margin-top: 10px;
|
| 1271 |
+
word-break: break-all; /* Ensure long filenames wrap */
|
| 1272 |
+
}
|
| 1273 |
+
[data-testid="stFileUploaderFile"] { /* Style for the actual uploaded file visual */
|
| 1274 |
+
background-color: #e9ecef;
|
| 1275 |
+
border-radius: 8px;
|
| 1276 |
+
padding: 8px 12px;
|
| 1277 |
+
margin-top: 10px;
|
| 1278 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
|
| 1279 |
}
|
| 1280 |
|
| 1281 |
|
| 1282 |
/* Information, Success, Error, Warning Boxes */
|
| 1283 |
[data-testid="stAlert"] {
|
| 1284 |
+
border-radius: 10px;
|
| 1285 |
+
padding: 18px 25px; /* More padding */
|
| 1286 |
+
margin-bottom: 1.8rem; /* More space below */
|
| 1287 |
+
font-size: 1.05em; /* Slightly larger text */
|
| 1288 |
+
line-height: 1.6;
|
| 1289 |
+
text-align: left;
|
| 1290 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.05); /* Subtle shadow for alerts */
|
|
|
|
| 1291 |
}
|
| 1292 |
+
/* Specific styles for each type with slightly richer colors */
|
| 1293 |
[data-testid="stAlert"] .streamlit-warning {
|
| 1294 |
+
background-color: #fef7e0; color: #7a5f00; border-left: 6px solid #ffcc00;
|
|
|
|
|
|
|
| 1295 |
}
|
| 1296 |
[data-testid="stAlert"] .streamlit-success {
|
| 1297 |
+
background-color: #e6ffe6; color: #0a6b0a; border-left: 6px solid #4CAF50;
|
|
|
|
|
|
|
| 1298 |
}
|
| 1299 |
[data-testid="stAlert"] .streamlit-error {
|
| 1300 |
+
background-color: #ffe6e6; color: #8c0a0a; border-left: 6px solid #e74c3c;
|
|
|
|
|
|
|
| 1301 |
}
|
| 1302 |
[data-testid="stAlert"] .streamlit-info {
|
| 1303 |
+
background-color: #e6f7ff; color: #0a4d6e; border-left: 6px solid #3498db;
|
|
|
|
|
|
|
| 1304 |
}
|
| 1305 |
|
| 1306 |
|
| 1307 |
+
/* Chat Message Bubbles */
|
| 1308 |
+
.user-message-container {
|
| 1309 |
+
display: flex;
|
| 1310 |
+
justify-content: flex-end; /* Align to the right */
|
| 1311 |
+
margin-bottom: 15px;
|
|
|
|
| 1312 |
}
|
| 1313 |
+
.user-message {
|
| 1314 |
+
background-color: #e0e6f6; /* Light blue-grey for user */
|
| 1315 |
+
color: #343a40;
|
| 1316 |
+
border-radius: 18px 18px 5px 18px; /* Rounded corners, pointed at bottom-left */
|
| 1317 |
+
padding: 12px 18px;
|
| 1318 |
+
max-width: 75%; /* Limit width of message */
|
| 1319 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.08);
|
| 1320 |
+
line-height: 1.5;
|
| 1321 |
+
text-align: left;
|
|
|
|
|
|
|
| 1322 |
}
|
| 1323 |
+
|
| 1324 |
+
.bot-message-container {
|
| 1325 |
+
display: flex;
|
| 1326 |
+
justify-content: flex-start; /* Align to the left */
|
| 1327 |
+
margin-bottom: 15px;
|
| 1328 |
+
}
|
| 1329 |
+
.bot-message {
|
| 1330 |
+
background-color: #ffffff; /* White for bot */
|
| 1331 |
+
color: #343a40;
|
| 1332 |
+
border-radius: 18px 18px 18px 5px; /* Rounded corners, pointed at bottom-right */
|
| 1333 |
+
padding: 12px 18px;
|
| 1334 |
+
max-width: 75%;
|
| 1335 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.08);
|
| 1336 |
+
line-height: 1.5;
|
| 1337 |
+
text-align: left;
|
| 1338 |
+
}
|
| 1339 |
+
|
| 1340 |
+
.chat-separator {
|
| 1341 |
+
border-bottom: 1px dashed #ced4da; /* Dashed separator for conversation turns */
|
| 1342 |
+
margin: 25px 0;
|
| 1343 |
+
}
|
| 1344 |
+
.chat-title {
|
| 1345 |
+
font-size: 1.8em;
|
| 1346 |
+
font-weight: 700;
|
| 1347 |
+
color: #5d6dbe;
|
| 1348 |
+
text-align: center;
|
| 1349 |
+
margin-bottom: 2rem;
|
| 1350 |
}
|
| 1351 |
|
| 1352 |
/* Quote for you (st.info) */
|
| 1353 |
+
.stInfo {
|
| 1354 |
background-color: #e6f7ff; /* Lighter blue for quote box */
|
| 1355 |
+
border-left: 6px solid #64b5f6; /* Accent border, slightly brighter blue */
|
| 1356 |
color: #004085;
|
| 1357 |
+
border-radius: 10px;
|
| 1358 |
+
padding: 18px 25px;
|
| 1359 |
+
margin-top: 2rem;
|
| 1360 |
font-style: italic;
|
| 1361 |
+
font-size: 1.05em;
|
| 1362 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
|
| 1363 |
}
|
| 1364 |
+
.stInfo span[data-baseweb="tag"] {
|
| 1365 |
font-weight: bold;
|
| 1366 |
color: #004085;
|
| 1367 |
}
|
|
|
|
| 1374 |
}
|
| 1375 |
|
| 1376 |
/* Expander styling (for conversations) */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1377 |
.streamlit-expanderHeader {
|
| 1378 |
background-color: #ffffff;
|
| 1379 |
border: 1px solid #e0e0e0;
|
| 1380 |
+
border-radius: 10px; /* More rounded */
|
| 1381 |
+
padding: 12px 18px; /* More padding */
|
| 1382 |
+
margin-bottom: 0.8rem; /* More space */
|
| 1383 |
cursor: pointer;
|
| 1384 |
+
transition: background-color 0.2s ease, box-shadow 0.2s ease;
|
| 1385 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.05);
|
| 1386 |
}
|
| 1387 |
.streamlit-expanderHeader:hover {
|
| 1388 |
+
background-color: #f7f9fb;
|
| 1389 |
+
box-shadow: 0 6px 15px rgba(0,0,0,0.08);
|
| 1390 |
}
|
| 1391 |
+
.streamlit-expanderHeader > div > div > p {
|
| 1392 |
+
font-weight: 600; /* Bolder text */
|
| 1393 |
+
color: #343a40;
|
| 1394 |
+
font-size: 1.05em;
|
| 1395 |
}
|
| 1396 |
+
.streamlit-expanderContent {
|
| 1397 |
+
background-color: #f8f9fa;
|
| 1398 |
+
border-left: 4px solid #ced4da; /* Thicker border */
|
| 1399 |
+
padding: 15px 20px; /* More padding */
|
| 1400 |
+
border-bottom-left-radius: 10px;
|
| 1401 |
+
border-bottom-right-radius: 10px;
|
| 1402 |
+
margin-top: -10px;
|
| 1403 |
+
box-shadow: inset 0 2px 5px rgba(0,0,0,0.03); /* Inner shadow */
|
| 1404 |
+
line-height: 1.6;
|
| 1405 |
}
|
| 1406 |
|
| 1407 |
|
| 1408 |
/* Horizontal rule */
|
| 1409 |
hr {
|
| 1410 |
+
border-top: 2px solid #e9ecef; /* Thicker, softer rule */
|
| 1411 |
+
margin-top: 3rem;
|
| 1412 |
+
margin-bottom: 3rem;
|
| 1413 |
}
|
| 1414 |
|
| 1415 |
/* Caption (footer) */
|
| 1416 |
+
.footer-caption { /* Custom class for footer */
|
| 1417 |
+
color: #868e96;
|
| 1418 |
+
font-size: 0.9em;
|
| 1419 |
+
margin-top: 3rem;
|
| 1420 |
+
display: block;
|
| 1421 |
text-align: center;
|
| 1422 |
+
letter-spacing: 0.2px;
|
| 1423 |
+
}
|
| 1424 |
+
|
| 1425 |
+
/* Container for sections */
|
| 1426 |
+
.stContainer {
|
| 1427 |
+
border-radius: 12px;
|
| 1428 |
+
box-shadow: 0 8px 20px rgba(0,0,0,0.08);
|
| 1429 |
+
border: 1px solid #e0e0e0;
|
| 1430 |
+
padding: 2rem;
|
| 1431 |
+
margin-bottom: 2.5rem;
|
| 1432 |
+
background-color: #ffffff;
|
| 1433 |
+
}
|
| 1434 |
+
.stContainer.has-border { /* Style for containers with border=True */
|
| 1435 |
+
border: 1px solid #e0e0e0;
|
| 1436 |
}
|
| 1437 |
|
| 1438 |
</style>
|
| 1439 |
""", unsafe_allow_html=True)
|
| 1440 |
# --- END CUSTOM CSS ---
|
| 1441 |
|
| 1442 |
+
# --- Streamlit UI Components ---
|
| 1443 |
+
# Header with logout button
|
| 1444 |
col1, col2 = st.columns([4, 1])
|
| 1445 |
with col1:
|
| 1446 |
+
st.markdown(f"<h1>🧠 DilBot</h1>", unsafe_allow_html=True)
|
| 1447 |
+
st.markdown(f"<p class='header-tagline'>Welcome back, {username}! Your personal emotional AI companion</p>", unsafe_allow_html=True)
|
| 1448 |
with col2:
|
| 1449 |
+
st.markdown("<div style='height: 40px;'></div>", unsafe_allow_html=True) # Spacer
|
| 1450 |
+
if st.button("Logout", key="logout_btn", use_container_width=True):
|
| 1451 |
st.session_state.authenticated = False
|
| 1452 |
st.session_state.username = None
|
| 1453 |
st.rerun()
|
| 1454 |
|
| 1455 |
+
# Input section wrapped in a styled container
|
| 1456 |
+
st.markdown("<h3>📝 Input Your Thoughts</h3>", unsafe_allow_html=True)
|
| 1457 |
+
with st.container(border=True): # Streamlit's built-in container with border
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1458 |
col1, col2 = st.columns(2)
|
| 1459 |
with col1:
|
| 1460 |
+
st.markdown("<label class='stSelectbox>label' style='margin-bottom:0.6rem;padding-left:5px;'>🎯 Choose a quote theme:</label>", unsafe_allow_html=True) # Custom label for selectbox
|
| 1461 |
+
selected_category = st.selectbox("Choose a quote theme", list(quote_categories.keys()), label_visibility="collapsed") # Hide default label
|
| 1462 |
+
|
| 1463 |
with col2:
|
| 1464 |
+
st.markdown("<h5>📁 Custom Quotes & Voice Input</h5>", unsafe_allow_html=True)
|
| 1465 |
uploaded_quotes = st.file_uploader("Upload your own quotes (.txt)", type=["txt"], key="quote_uploader")
|
| 1466 |
uploaded_audio = st.file_uploader("Upload a voice message (.wav)", type=["wav"], key="audio_uploader")
|
| 1467 |
|
| 1468 |
+
# Voice transcription button
|
| 1469 |
+
if uploaded_audio and st.button("🎙️ Transcribe Voice Message", key="transcribe_btn", use_container_width=True):
|
| 1470 |
with st.spinner("Transcribing your voice..."):
|
| 1471 |
transcribed = transcribe_audio_file(uploaded_audio)
|
| 1472 |
if transcribed.startswith("Error:"):
|
|
|
|
| 1474 |
else:
|
| 1475 |
st.session_state.transcribed_text = transcribed
|
| 1476 |
st.success("✅ Voice transcribed successfully!")
|
| 1477 |
+
|
| 1478 |
+
# Handle vectorstore (ORIGINAL LOGIC - NO CHANGE)
|
| 1479 |
current_quotes = []
|
| 1480 |
vectorstore = None
|
| 1481 |
|
|
|
|
| 1492 |
vectorstore = build_user_vectorstore(username, default_quotes)
|
| 1493 |
current_quotes = default_quotes
|
| 1494 |
|
| 1495 |
+
# Input area for user message
|
| 1496 |
+
st.markdown("<h3>💬 What's on your mind?</h3>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1497 |
user_input = st.text_area(
|
| 1498 |
+
"Share your thoughts, feelings, or experiences...",
|
| 1499 |
value=st.session_state.transcribed_text,
|
| 1500 |
+
height=180, # Increased height for more typing space
|
| 1501 |
+
placeholder="Type here or use your transcribed voice message...",
|
| 1502 |
+
label_visibility="collapsed" # Hide default label
|
| 1503 |
)
|
| 1504 |
+
st.markdown("<label class='stTextArea>label' style='margin-bottom:0.6rem;padding-left:5px;'>Share your thoughts, feelings, or experiences...</label>", unsafe_allow_html=True) # Custom label
|
| 1505 |
+
|
| 1506 |
|
| 1507 |
final_input = user_input.strip() or st.session_state.transcribed_text.strip()
|
| 1508 |
|
| 1509 |
# Main interaction button
|
| 1510 |
+
# IMPORTANT: The "Talk to DilBot" button was appearing twice in your original code.
|
| 1511 |
+
# I'm assuming you only want one, and it should trigger the logic.
|
| 1512 |
+
# The one below is the one that correctly triggers the logic.
|
| 1513 |
if st.button("🧠 Talk to DilBot", type="primary", use_container_width=True):
|
| 1514 |
if not final_input:
|
| 1515 |
st.warning("⚠️ Please enter something to share or upload a voice message.")
|
| 1516 |
else:
|
| 1517 |
with st.spinner("DilBot is thinking and feeling..."):
|
| 1518 |
+
# Emotion detection (ORIGINAL LOGIC - NO CHANGE)
|
| 1519 |
emotion, score = detect_emotion(final_input)
|
| 1520 |
|
| 1521 |
+
# Get AI response (ORIGINAL LOGIC - NO CHANGE)
|
| 1522 |
prompt_template = PromptTemplate(
|
| 1523 |
input_variables=["context", "user_input", "username"],
|
| 1524 |
template="""You are DilBot, an empathetic emotional support AI companion for {username}.
|
|
|
|
| 1530 |
Respond as DilBot with warmth, empathy, and understanding. Keep it conversational and supportive."""
|
| 1531 |
)
|
| 1532 |
|
| 1533 |
+
# Get similar quotes (ORIGINAL LOGIC - NO CHANGE)
|
| 1534 |
similar_docs = vectorstore.similarity_search(final_input, k=2)
|
| 1535 |
context = "\n".join([doc.page_content for doc in similar_docs])
|
| 1536 |
|
| 1537 |
+
# Generate response (ORIGINAL LOGIC - NO CHANGE)
|
| 1538 |
groq_llm = ChatGroq(api_key=GROQ_API_KEY, model="llama3-70b-8192")
|
| 1539 |
chain = LLMChain(llm=groq_llm, prompt=prompt_template)
|
| 1540 |
response = chain.run(context=context, user_input=final_input, username=username)
|
| 1541 |
|
| 1542 |
+
# Save to user's journal (ORIGINAL LOGIC - NO CHANGE)
|
| 1543 |
save_user_journal(username, final_input, emotion, score, response)
|
| 1544 |
|
| 1545 |
+
# Display results with new chat bubble styling
|
| 1546 |
+
st.markdown("<h3 class='chat-title'>🤖 DilBot's Conversation:</h3>", unsafe_allow_html=True)
|
| 1547 |
with st.container(border=True): # Container for the conversation output
|
| 1548 |
+
# User's input presented in a chat bubble
|
| 1549 |
+
st.markdown(f"<div class='user-message-container'><div class='user-message'>**You:** {final_input}</div></div>", unsafe_allow_html=True)
|
| 1550 |
st.success(f"**Emotion Detected:** {emotion.capitalize()} ({round(score*100)}% confidence)")
|
| 1551 |
|
| 1552 |
if is_crisis(final_input):
|
| 1553 |
st.error("🚨 Crisis detected! Please reach out to a mental health professional immediately. "
|
| 1554 |
+
"You are not alone. Consider contacting a helpline like the National Suicide Prevention Lifeline (988 in the US) or a local emergency service.")
|
| 1555 |
|
| 1556 |
if current_quotes:
|
| 1557 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
| 1562 |
selected_quote = current_quotes[best_match]
|
| 1563 |
st.info(f"💭 **Quote for you:** *{selected_quote}*")
|
| 1564 |
|
| 1565 |
+
# DilBot's response presented in a chat bubble
|
| 1566 |
+
st.markdown(f"<div class='bot-message-container'><div class='bot-message'>**DilBot:** {response}</div></div>", unsafe_allow_html=True)
|
|
|
|
| 1567 |
|
| 1568 |
speak(response, username)
|
| 1569 |
st.session_state.transcribed_text = ""
|
| 1570 |
+
|
| 1571 |
+
# Add a visual separator after each conversation turn (optional)
|
| 1572 |
+
st.markdown("<div class='chat-separator'></div>", unsafe_allow_html=True)
|
| 1573 |
+
|
| 1574 |
+
|
| 1575 |
# User's personal dashboard
|
| 1576 |
st.markdown("---")
|
| 1577 |
st.header("📊 Your Personal Dashboard")
|
| 1578 |
|
| 1579 |
+
# Load user's journal (ORIGINAL LOGIC - NO CHANGE)
|
| 1580 |
journal_data = load_user_journal(username)
|
| 1581 |
|
| 1582 |
if journal_data:
|
| 1583 |
+
# Statistics
|
| 1584 |
+
st.subheader("📈 Your Emotional Statistics") # Moved statistics to the top of dashboard for prominence
|
| 1585 |
with st.container(border=True):
|
| 1586 |
+
col1, col2, col3 = st.columns(3)
|
| 1587 |
+
with col1:
|
| 1588 |
+
st.metric("Total Conversations", len(journal_data))
|
| 1589 |
+
with col2:
|
| 1590 |
+
emotions = [entry['emotion'] for entry in journal_data]
|
| 1591 |
+
most_common = max(set(emotions), key=emotions.count) if emotions else "None"
|
| 1592 |
+
st.metric("Most Common Emotion", most_common.capitalize())
|
| 1593 |
+
with col3:
|
| 1594 |
+
if journal_data:
|
| 1595 |
+
avg_confidence = sum(entry['confidence'] for entry in journal_data) / len(journal_data)
|
| 1596 |
+
st.metric("Avg. Confidence", f"{avg_confidence:.1f}%")
|
| 1597 |
+
else:
|
| 1598 |
+
st.metric("Avg. Confidence", "N/A") # Handle case with no journal data for avg. confidence
|
| 1599 |
+
|
| 1600 |
+
|
| 1601 |
+
# Mood tracker
|
| 1602 |
+
st.subheader("📊 Your Daily Mood Tracker")
|
| 1603 |
+
with st.container(border=True): # Wrap chart in a container
|
| 1604 |
+
# Prepare data for chart (ORIGINAL LOGIC - NO CHANGE)
|
| 1605 |
df_data = []
|
| 1606 |
for entry in journal_data:
|
| 1607 |
df_data.append({
|
|
|
|
| 1610 |
"confidence": entry["confidence"]
|
| 1611 |
})
|
| 1612 |
if df_data:
|
| 1613 |
+
# Use pandas DataFrame for better Altair integration
|
| 1614 |
+
import pandas as pd
|
| 1615 |
+
df_chart = pd.DataFrame(df_data)
|
| 1616 |
+
|
| 1617 |
+
chart = alt.Chart(df_chart).mark_bar().encode(
|
| 1618 |
+
x=alt.X('date:N', title='Date', sort=None), # Sort by date ensures correct order
|
| 1619 |
y=alt.Y('count():Q', title='Frequency'),
|
| 1620 |
+
color=alt.Color('emotion:N', title='Emotion', scale=alt.Scale(range=['#4CAF50', '#FFC107', '#E74C3C', '#3498DB', '#9B59B6', '#1ABC9C'])), # Custom colors
|
| 1621 |
tooltip=['date:N', 'emotion:N', 'count():Q']
|
| 1622 |
).properties(
|
| 1623 |
+
# width=600, # Use use_container_width instead
|
| 1624 |
+
height=350, # Slightly increased height
|
| 1625 |
title="Your Emotional Journey Over Time"
|
| 1626 |
).interactive() # Make chart interactive for zoom/pan
|
| 1627 |
st.altair_chart(chart, use_container_width=True)
|
| 1628 |
+
else:
|
| 1629 |
+
st.info("No emotional data to display yet. Start interacting with DilBot!")
|
| 1630 |
+
|
| 1631 |
# Recent conversations
|
| 1632 |
st.subheader("💬 Recent Conversations")
|
| 1633 |
recent_entries = journal_data[-5:] if len(journal_data) >= 5 else journal_data
|
|
|
|
| 1635 |
with st.container(border=True): # Wrap recent conversations in a container
|
| 1636 |
if recent_entries:
|
| 1637 |
for i, entry in enumerate(reversed(recent_entries)):
|
| 1638 |
+
with st.expander(f"📅 {entry['date']} - {entry['emotion'].capitalize()} ({round(entry['confidence'] * 100)}%)"): # Round confidence for display
|
| 1639 |
st.markdown(f"**You said:** {entry['user_input']}")
|
| 1640 |
st.markdown(f"**DilBot replied:** {entry['response']}")
|
| 1641 |
else:
|
| 1642 |
st.info("No recent conversations yet. Start talking to DilBot!")
|
| 1643 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1644 |
else:
|
| 1645 |
+
st.info("🌟 Start your first conversation with DilBot to see your personal dashboard and insights!")
|
| 1646 |
|
| 1647 |
st.markdown("---")
|
| 1648 |
+
st.markdown("<p class='footer-caption'>Built by Members of CSG Hackathon Team | Your data is stored privately and securely</p>", unsafe_allow_html=True)
|
| 1649 |
|
| 1650 |
# Main app logic
|
| 1651 |
def main():
|