Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,16 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
-
import
|
| 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:
|
|
@@ -22,88 +20,48 @@ if 'chat_history' not in st.session_state:
|
|
| 22 |
if 'current_page' not in st.session_state:
|
| 23 |
st.session_state.current_page = 'home'
|
| 24 |
|
| 25 |
-
# ---
|
| 26 |
-
def
|
| 27 |
-
"""
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# ---
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
|
| 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 |
-
"
|
| 70 |
["Anxiety", "Depression", "Stress", "Sleep Issues", "Relationship Issues", "Work-Life Balance"]
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
st.
|
| 74 |
-
|
| 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())
|
|
@@ -113,49 +71,29 @@ def home_page():
|
|
| 113 |
st.rerun()
|
| 114 |
else:
|
| 115 |
st.title(f"Welcome back, {st.session_state.user_profile['name']}! π")
|
| 116 |
-
st.
|
| 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
|
| 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 |
-
|
| 139 |
-
if prompt
|
| 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.
|
| 153 |
|
| 154 |
# --- Progress Dashboard ---
|
| 155 |
def progress_dashboard():
|
| 156 |
st.title("Your Progress Dashboard π")
|
| 157 |
|
| 158 |
-
# Sample mood 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 |
|
|
@@ -163,130 +101,57 @@ def progress_dashboard():
|
|
| 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 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
st.
|
| 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 |
-
|
| 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 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
if st.button("
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 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
|
| 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 |
-
|
| 288 |
elif st.session_state.current_page == "chat":
|
| 289 |
-
|
| 290 |
elif st.session_state.current_page == "progress":
|
| 291 |
progress_dashboard()
|
| 292 |
elif st.session_state.current_page == "tools":
|
|
@@ -295,4 +160,4 @@ def main():
|
|
| 295 |
settings_page()
|
| 296 |
|
| 297 |
if __name__ == "__main__":
|
| 298 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
from datetime import datetime
|
| 6 |
+
import os
|
|
|
|
| 7 |
import random
|
| 8 |
|
| 9 |
+
# --- GPT-4 API Configuration ---
|
| 10 |
+
openai.api_key = os.getenv("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")
|
| 11 |
+
|
| 12 |
# --- Page Configuration ---
|
| 13 |
+
st.set_page_config(page_title="Emma - Mental Health Companion", page_icon="π", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# --- Session State Initialization ---
|
| 16 |
if 'user_profile' not in st.session_state:
|
|
|
|
| 20 |
if 'current_page' not in st.session_state:
|
| 21 |
st.session_state.current_page = 'home'
|
| 22 |
|
| 23 |
+
# --- Helper Functions ---
|
| 24 |
+
def get_gpt4_response(prompt):
|
| 25 |
+
"""Get response from GPT-4"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
+
response = openai.ChatCompletion.create(
|
| 28 |
+
model="gpt-4",
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": "system", "content": "You are a helpful mental health companion named Emma."},
|
| 31 |
+
{"role": "user", "content": prompt}
|
| 32 |
+
]
|
| 33 |
+
)
|
| 34 |
+
return response['choices'][0]['message']['content']
|
| 35 |
+
except Exception as e:
|
| 36 |
+
st.error("Error with GPT-4 API. Please check your API key or connection.")
|
| 37 |
+
return "I'm having trouble responding at the moment. Please try again later."
|
| 38 |
|
| 39 |
+
# --- Onboarding and User Profile ---
|
| 40 |
+
def save_user_profile(profile_data):
|
| 41 |
+
"""Save user profile to session state"""
|
| 42 |
+
st.session_state.user_profile = profile_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
def onboarding_page():
|
|
|
|
| 45 |
if st.session_state.user_profile is None:
|
| 46 |
st.title("Welcome to Emma π")
|
| 47 |
st.write("Your personal mental health companion")
|
| 48 |
+
|
| 49 |
with st.form("onboarding_form"):
|
|
|
|
| 50 |
name = st.text_input("What should I call you?")
|
|
|
|
|
|
|
| 51 |
challenges = st.multiselect(
|
| 52 |
+
"What challenges would you like support with?",
|
| 53 |
["Anxiety", "Depression", "Stress", "Sleep Issues", "Relationship Issues", "Work-Life Balance"]
|
| 54 |
)
|
| 55 |
+
approach_style = st.slider("Preferred approach style (0: Reassuring, 10: Solution-Oriented):", 0, 10, 5)
|
| 56 |
+
tone_style = st.slider("Preferred tone (0: Lighthearted, 10: Serious):", 0, 10, 5)
|
| 57 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
submitted = st.form_submit_button("Start My Journey")
|
|
|
|
| 59 |
if submitted:
|
| 60 |
profile_data = {
|
| 61 |
"name": name,
|
| 62 |
"challenges": challenges,
|
| 63 |
"preferences": {
|
| 64 |
"approach_style": approach_style,
|
|
|
|
| 65 |
"tone_style": tone_style
|
| 66 |
},
|
| 67 |
"created_at": str(datetime.now())
|
|
|
|
| 71 |
st.rerun()
|
| 72 |
else:
|
| 73 |
st.title(f"Welcome back, {st.session_state.user_profile['name']}! π")
|
| 74 |
+
if st.button("Continue to Chat"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
st.session_state.current_page = "chat"
|
|
|
|
| 76 |
|
| 77 |
# --- Chat Interface ---
|
| 78 |
+
def chat_page():
|
| 79 |
st.title("Chat with Emma π")
|
| 80 |
+
|
|
|
|
| 81 |
for message in st.session_state.chat_history:
|
| 82 |
with st.chat_message(message["role"]):
|
| 83 |
st.write(message["content"])
|
| 84 |
+
|
| 85 |
+
prompt = st.chat_input("Type your message here...")
|
| 86 |
+
if prompt:
|
|
|
|
| 87 |
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 88 |
+
emma_response = get_gpt4_response(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
st.session_state.chat_history.append({"role": "assistant", "content": emma_response})
|
| 90 |
+
st.experimental_rerun()
|
| 91 |
|
| 92 |
# --- Progress Dashboard ---
|
| 93 |
def progress_dashboard():
|
| 94 |
st.title("Your Progress Dashboard π")
|
| 95 |
|
| 96 |
+
# Sample mood data
|
| 97 |
dates = pd.date_range(start="2024-01-01", end="2024-01-07", freq="D")
|
| 98 |
moods = [random.randint(1, 5) for _ in range(len(dates))]
|
| 99 |
|
|
|
|
| 101 |
"Date": dates,
|
| 102 |
"Mood": moods
|
| 103 |
})
|
| 104 |
+
|
|
|
|
| 105 |
fig = px.line(df, x="Date", y="Mood", title="Your Mood Timeline")
|
| 106 |
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# --- Support Tools ---
|
| 109 |
def support_tools():
|
| 110 |
st.title("Support Tools & Resources π οΈ")
|
| 111 |
+
st.subheader("Guided Meditation")
|
| 112 |
+
st.write("Select a meditation type to begin.")
|
| 113 |
+
meditation_type = st.selectbox("Meditation Type", ["Stress Relief", "Mindfulness", "Sleep"])
|
| 114 |
+
if st.button("Play Meditation"):
|
| 115 |
+
st.write(f"Playing {meditation_type} meditation π΅")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# --- Settings Page ---
|
| 118 |
def settings_page():
|
| 119 |
st.title("Settings βοΈ")
|
|
|
|
| 120 |
if st.session_state.user_profile:
|
| 121 |
st.subheader("Therapist Preferences")
|
| 122 |
+
new_approach = st.slider("Approach Style", 0, 10, st.session_state.user_profile["preferences"]["approach_style"])
|
| 123 |
+
new_tone = st.slider("Tone Style", 0, 10, st.session_state.user_profile["preferences"]["tone_style"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
if st.button("Update Preferences"):
|
| 125 |
st.session_state.user_profile["preferences"].update({
|
| 126 |
"approach_style": new_approach,
|
|
|
|
| 127 |
"tone_style": new_tone
|
| 128 |
})
|
| 129 |
+
st.success("Preferences updated!")
|
| 130 |
+
|
| 131 |
+
# --- Page Navigation ---
|
| 132 |
+
def navigation():
|
| 133 |
+
with st.sidebar:
|
| 134 |
+
st.title("Emma π")
|
| 135 |
+
if st.button("Home"):
|
| 136 |
+
st.session_state.current_page = 'home'
|
| 137 |
+
if st.button("Chat"):
|
| 138 |
+
st.session_state.current_page = 'chat'
|
| 139 |
+
if st.button("Progress Dashboard"):
|
| 140 |
+
st.session_state.current_page = 'progress'
|
| 141 |
+
if st.button("Support Tools"):
|
| 142 |
+
st.session_state.current_page = 'tools'
|
| 143 |
+
if st.button("Settings"):
|
| 144 |
+
st.session_state.current_page = 'settings'
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
# --- Main App Logic ---
|
| 147 |
def main():
|
| 148 |
navigation()
|
| 149 |
+
|
| 150 |
+
# Load pages based on navigation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
if st.session_state.current_page == "home":
|
| 152 |
+
onboarding_page()
|
| 153 |
elif st.session_state.current_page == "chat":
|
| 154 |
+
chat_page()
|
| 155 |
elif st.session_state.current_page == "progress":
|
| 156 |
progress_dashboard()
|
| 157 |
elif st.session_state.current_page == "tools":
|
|
|
|
| 160 |
settings_page()
|
| 161 |
|
| 162 |
if __name__ == "__main__":
|
| 163 |
+
main()
|