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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import plotly.express as px
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import json
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import random
|
| 8 |
+
|
| 9 |
+
# --- Page Configuration ---
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Emma - Mental Health Companion",
|
| 12 |
+
page_icon="π",
|
| 13 |
+
layout="wide",
|
| 14 |
+
initial_sidebar_state="expanded"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# --- Session State Initialization ---
|
| 18 |
+
if 'user_profile' not in st.session_state:
|
| 19 |
+
st.session_state.user_profile = None
|
| 20 |
+
if 'chat_history' not in st.session_state:
|
| 21 |
+
st.session_state.chat_history = []
|
| 22 |
+
if 'current_page' not in st.session_state:
|
| 23 |
+
st.session_state.current_page = 'home'
|
| 24 |
+
|
| 25 |
+
# --- Utility Functions ---
|
| 26 |
+
def save_user_profile(profile_data):
|
| 27 |
+
"""Save user profile to session state and local storage"""
|
| 28 |
+
st.session_state.user_profile = profile_data
|
| 29 |
+
Path("data").mkdir(exist_ok=True)
|
| 30 |
+
with open('data/user_profile.json', 'w') as f:
|
| 31 |
+
json.dump(profile_data, f)
|
| 32 |
+
|
| 33 |
+
def load_user_profile():
|
| 34 |
+
"""Load user profile from local storage"""
|
| 35 |
+
try:
|
| 36 |
+
with open('data/user_profile.json', 'r') as f:
|
| 37 |
+
return json.load(f)
|
| 38 |
+
except:
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
# --- Navigation ---
|
| 42 |
+
def navigation():
|
| 43 |
+
with st.sidebar:
|
| 44 |
+
st.title("Emma π")
|
| 45 |
+
pages = {
|
| 46 |
+
"Home": "home",
|
| 47 |
+
"Chat Interface": "chat",
|
| 48 |
+
"Progress Dashboard": "progress",
|
| 49 |
+
"Support Tools": "tools",
|
| 50 |
+
"Settings": "settings"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
for page_name, page_id in pages.items():
|
| 54 |
+
if st.button(page_name):
|
| 55 |
+
st.session_state.current_page = page_id
|
| 56 |
+
|
| 57 |
+
# --- Home Page ---
|
| 58 |
+
def home_page():
|
| 59 |
+
if st.session_state.user_profile is None:
|
| 60 |
+
st.title("Welcome to Emma π")
|
| 61 |
+
st.write("Your personal mental health companion")
|
| 62 |
+
|
| 63 |
+
with st.form("onboarding_form"):
|
| 64 |
+
st.subheader("Let's personalize your experience")
|
| 65 |
+
name = st.text_input("What should I call you?")
|
| 66 |
+
|
| 67 |
+
st.subheader("What challenges would you like support with?")
|
| 68 |
+
challenges = st.multiselect(
|
| 69 |
+
"Select all that apply:",
|
| 70 |
+
["Anxiety", "Depression", "Stress", "Sleep Issues", "Relationship Issues", "Work-Life Balance"]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
st.subheader("Customize your therapeutic experience")
|
| 74 |
+
approach_style = st.slider(
|
| 75 |
+
"Preferred approach style:",
|
| 76 |
+
min_value=0,
|
| 77 |
+
max_value=10,
|
| 78 |
+
value=5,
|
| 79 |
+
help="0: More Reassuring, 10: More Solution-Oriented"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
focus_style = st.slider(
|
| 83 |
+
"Focus preference:",
|
| 84 |
+
min_value=0,
|
| 85 |
+
max_value=10,
|
| 86 |
+
value=5,
|
| 87 |
+
help="0: Holistic Well-being, 10: Targeted Issue Resolution"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
tone_style = st.slider(
|
| 91 |
+
"Preferred tone:",
|
| 92 |
+
min_value=0,
|
| 93 |
+
max_value=10,
|
| 94 |
+
value=5,
|
| 95 |
+
help="0: Lighthearted, 10: Serious"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
submitted = st.form_submit_button("Start My Journey")
|
| 99 |
+
|
| 100 |
+
if submitted:
|
| 101 |
+
profile_data = {
|
| 102 |
+
"name": name,
|
| 103 |
+
"challenges": challenges,
|
| 104 |
+
"preferences": {
|
| 105 |
+
"approach_style": approach_style,
|
| 106 |
+
"focus_style": focus_style,
|
| 107 |
+
"tone_style": tone_style
|
| 108 |
+
},
|
| 109 |
+
"created_at": str(datetime.now())
|
| 110 |
+
}
|
| 111 |
+
save_user_profile(profile_data)
|
| 112 |
+
st.success("Profile created successfully! Let's begin your journey.")
|
| 113 |
+
st.rerun()
|
| 114 |
+
else:
|
| 115 |
+
st.title(f"Welcome back, {st.session_state.user_profile['name']}! π")
|
| 116 |
+
st.write("How are you feeling today?")
|
| 117 |
+
|
| 118 |
+
# Quick mood check-in
|
| 119 |
+
mood = st.select_slider(
|
| 120 |
+
"Rate your current mood:",
|
| 121 |
+
options=["π’", "π", "π", "π", "π"],
|
| 122 |
+
value="π"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
if st.button("Start Chat Session"):
|
| 126 |
+
st.session_state.current_page = "chat"
|
| 127 |
+
st.rerun()
|
| 128 |
+
|
| 129 |
+
# --- Chat Interface ---
|
| 130 |
+
def chat_interface():
|
| 131 |
+
st.title("Chat with Emma π")
|
| 132 |
+
|
| 133 |
+
# Display chat history
|
| 134 |
+
for message in st.session_state.chat_history:
|
| 135 |
+
with st.chat_message(message["role"]):
|
| 136 |
+
st.write(message["content"])
|
| 137 |
+
|
| 138 |
+
# Chat input
|
| 139 |
+
if prompt := st.chat_input("Type your message here..."):
|
| 140 |
+
# Add user message to chat history
|
| 141 |
+
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 142 |
+
|
| 143 |
+
# Simulate Emma's response (replace with actual AI integration)
|
| 144 |
+
responses = [
|
| 145 |
+
"I hear you. Can you tell me more about how that makes you feel?",
|
| 146 |
+
"That sounds challenging. Let's explore this together.",
|
| 147 |
+
"I understand this is difficult. What support do you need right now?",
|
| 148 |
+
"You're showing great courage in sharing this. How can I help?"
|
| 149 |
+
]
|
| 150 |
+
emma_response = random.choice(responses)
|
| 151 |
+
st.session_state.chat_history.append({"role": "assistant", "content": emma_response})
|
| 152 |
+
st.rerun()
|
| 153 |
+
|
| 154 |
+
# --- Progress Dashboard ---
|
| 155 |
+
def progress_dashboard():
|
| 156 |
+
st.title("Your Progress Dashboard π")
|
| 157 |
+
|
| 158 |
+
# Sample mood data (replace with actual user data)
|
| 159 |
+
dates = pd.date_range(start="2024-01-01", end="2024-01-07", freq="D")
|
| 160 |
+
moods = [random.randint(1, 5) for _ in range(len(dates))]
|
| 161 |
+
|
| 162 |
+
df = pd.DataFrame({
|
| 163 |
+
"Date": dates,
|
| 164 |
+
"Mood": moods
|
| 165 |
+
})
|
| 166 |
+
|
| 167 |
+
# Mood tracking chart
|
| 168 |
+
fig = px.line(df, x="Date", y="Mood", title="Your Mood Timeline")
|
| 169 |
+
st.plotly_chart(fig)
|
| 170 |
+
|
| 171 |
+
# Weekly insights
|
| 172 |
+
st.subheader("Weekly Insights")
|
| 173 |
+
col1, col2 = st.columns(2)
|
| 174 |
+
|
| 175 |
+
with col1:
|
| 176 |
+
st.metric("Average Mood", f"{sum(moods)/len(moods):.1f}")
|
| 177 |
+
st.metric("Sessions Completed", len(st.session_state.chat_history) // 2)
|
| 178 |
+
|
| 179 |
+
with col2:
|
| 180 |
+
st.metric("Mood Trend", "βοΈ Improving")
|
| 181 |
+
st.metric("Engagement", "Regular")
|
| 182 |
+
|
| 183 |
+
# --- Support Tools ---
|
| 184 |
+
def support_tools():
|
| 185 |
+
st.title("Support Tools & Resources π οΈ")
|
| 186 |
+
|
| 187 |
+
tab1, tab2, tab3 = st.tabs(["Meditation", "Breathing Exercises", "Coping Strategies"])
|
| 188 |
+
|
| 189 |
+
with tab1:
|
| 190 |
+
st.subheader("Guided Meditation")
|
| 191 |
+
st.write("Choose a meditation to begin:")
|
| 192 |
+
meditation_type = st.selectbox(
|
| 193 |
+
"Select meditation type:",
|
| 194 |
+
["Stress Relief", "Mindfulness", "Sleep", "Anxiety Relief"]
|
| 195 |
+
)
|
| 196 |
+
if st.button("Start Meditation"):
|
| 197 |
+
st.write("π΅ Meditation audio would play here")
|
| 198 |
+
|
| 199 |
+
with tab2:
|
| 200 |
+
st.subheader("Breathing Exercises")
|
| 201 |
+
st.write("Try this simple breathing exercise:")
|
| 202 |
+
if st.button("Start Breathing Exercise"):
|
| 203 |
+
st.write("Breathe in... 2... 3... 4...")
|
| 204 |
+
st.write("Hold... 2... 3... 4...")
|
| 205 |
+
st.write("Breathe out... 2... 3... 4...")
|
| 206 |
+
|
| 207 |
+
with tab3:
|
| 208 |
+
st.subheader("Coping Strategies")
|
| 209 |
+
strategies = [
|
| 210 |
+
"Practice mindfulness",
|
| 211 |
+
"Write in a journal",
|
| 212 |
+
"Talk to a friend",
|
| 213 |
+
"Exercise",
|
| 214 |
+
"Practice gratitude"
|
| 215 |
+
]
|
| 216 |
+
st.write("Here are some strategies you can try:")
|
| 217 |
+
for strategy in strategies:
|
| 218 |
+
st.checkbox(strategy)
|
| 219 |
+
|
| 220 |
+
# --- Settings Page ---
|
| 221 |
+
def settings_page():
|
| 222 |
+
st.title("Settings βοΈ")
|
| 223 |
+
|
| 224 |
+
if st.session_state.user_profile:
|
| 225 |
+
st.subheader("Therapist Preferences")
|
| 226 |
+
|
| 227 |
+
# Update preferences
|
| 228 |
+
new_approach = st.slider(
|
| 229 |
+
"Approach Style:",
|
| 230 |
+
min_value=0,
|
| 231 |
+
max_value=10,
|
| 232 |
+
value=st.session_state.user_profile["preferences"]["approach_style"]
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
new_focus = st.slider(
|
| 236 |
+
"Focus Style:",
|
| 237 |
+
min_value=0,
|
| 238 |
+
max_value=10,
|
| 239 |
+
value=st.session_state.user_profile["preferences"]["focus_style"]
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
new_tone = st.slider(
|
| 243 |
+
"Tone Style:",
|
| 244 |
+
min_value=0,
|
| 245 |
+
max_value=10,
|
| 246 |
+
value=st.session_state.user_profile["preferences"]["tone_style"]
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
if st.button("Update Preferences"):
|
| 250 |
+
st.session_state.user_profile["preferences"].update({
|
| 251 |
+
"approach_style": new_approach,
|
| 252 |
+
"focus_style": new_focus,
|
| 253 |
+
"tone_style": new_tone
|
| 254 |
+
})
|
| 255 |
+
save_user_profile(st.session_state.user_profile)
|
| 256 |
+
st.success("Preferences updated successfully!")
|
| 257 |
+
|
| 258 |
+
# Privacy settings
|
| 259 |
+
st.subheader("Privacy Settings")
|
| 260 |
+
if st.button("Download My Data"):
|
| 261 |
+
st.download_button(
|
| 262 |
+
"Click to Download",
|
| 263 |
+
data=json.dumps(st.session_state.user_profile),
|
| 264 |
+
file_name="my_emma_data.json"
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
if st.button("Delete My Data"):
|
| 268 |
+
if st.confirm("Are you sure? This action cannot be undone."):
|
| 269 |
+
st.session_state.user_profile = None
|
| 270 |
+
st.session_state.chat_history = []
|
| 271 |
+
Path("data/user_profile.json").unlink(missing_ok=True)
|
| 272 |
+
st.success("All data deleted successfully!")
|
| 273 |
+
st.rerun()
|
| 274 |
+
|
| 275 |
+
# --- Main App Logic ---
|
| 276 |
+
def main():
|
| 277 |
+
navigation()
|
| 278 |
+
|
| 279 |
+
# Load user profile if exists
|
| 280 |
+
if st.session_state.user_profile is None:
|
| 281 |
+
profile = load_user_profile()
|
| 282 |
+
if profile:
|
| 283 |
+
st.session_state.user_profile = profile
|
| 284 |
+
|
| 285 |
+
# Page routing
|
| 286 |
+
if st.session_state.current_page == "home":
|
| 287 |
+
home_page()
|
| 288 |
+
elif st.session_state.current_page == "chat":
|
| 289 |
+
chat_interface()
|
| 290 |
+
elif st.session_state.current_page == "progress":
|
| 291 |
+
progress_dashboard()
|
| 292 |
+
elif st.session_state.current_page == "tools":
|
| 293 |
+
support_tools()
|
| 294 |
+
elif st.session_state.current_page == "settings":
|
| 295 |
+
settings_page()
|
| 296 |
+
|
| 297 |
+
if __name__ == "__main__":
|
| 298 |
+
main()
|