File size: 6,215 Bytes
e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 30e8063 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 edc8718 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa dcb6a9f f1f805a dcb6a9f e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 e8a2daa 68f2622 |
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 |
import streamlit as st
import openai
import pandas as pd
import plotly.express as px
from datetime import datetime
import os
import random
# --- GPT-4 API Configuration ---
openai.api_key = os.getenv("Open_AI_GPT_4o")
# --- Page Configuration ---
st.set_page_config(page_title="Emma - Mental Health Companion", page_icon="π", layout="wide")
# --- Session State Initialization ---
if 'user_profile' not in st.session_state:
st.session_state.user_profile = None
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'current_page' not in st.session_state:
st.session_state.current_page = 'home'
# --- Helper Functions ---
def get_gpt4_response(prompt):
try:
response = openai.ChatCompletion.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are Emma, a helpful assistant."},
{"role": "user", "content": prompt}
]
)
return response["choices"][0]["message"]["content"]
except Exception as e:
st.error("Error with GPT-4 API. Please check your API key or connection.")
return "I'm having trouble responding at the moment. Please try again later."
# --- Onboarding and User Profile ---
def save_user_profile(profile_data):
"""Save user profile to session state"""
st.session_state.user_profile = profile_data
def onboarding_page():
if st.session_state.user_profile is None:
st.title("Welcome to Emma π")
st.write("Your personal mental health companion")
with st.form("onboarding_form"):
name = st.text_input("What should I call you?")
challenges = st.multiselect(
"What challenges would you like support with?",
["Anxiety", "Depression", "Stress", "Sleep Issues", "Relationship Issues", "Work-Life Balance"]
)
approach_style = st.slider("Preferred approach style (0: Reassuring, 10: Solution-Oriented):", 0, 10, 5)
tone_style = st.slider("Preferred tone (0: Lighthearted, 10: Serious):", 0, 10, 5)
submitted = st.form_submit_button("Start My Journey")
if submitted:
profile_data = {
"name": name,
"challenges": challenges,
"preferences": {
"approach_style": approach_style,
"tone_style": tone_style
},
"created_at": str(datetime.now())
}
save_user_profile(profile_data)
st.success("Profile created successfully! Let's begin your journey.")
st.rerun()
else:
st.title(f"Welcome back, {st.session_state.user_profile['name']}! π")
if st.button("Continue to Chat"):
st.session_state.current_page = "chat"
# --- Chat Interface ---
def chat_page():
st.title("Chat with Emma π")
for message in st.session_state.chat_history:
with st.chat_message(message["role"]):
st.write(message["content"])
prompt = st.chat_input("Type your message here...")
if prompt:
st.session_state.chat_history.append({"role": "user", "content": prompt})
emma_response = get_gpt4_response(prompt)
st.session_state.chat_history.append({"role": "assistant", "content": emma_response})
st.session_state["needs_rerun"] = True # Set flag to trigger rerun
# Check for rerun
if st.session_state.get("needs_rerun", False):
st.session_state["needs_rerun"] = False
st.set_query_params() # Use st.set_query_params to trigger a rerun
# --- Progress Dashboard ---
def progress_dashboard():
st.title("Your Progress Dashboard π")
# Sample mood data
dates = pd.date_range(start="2024-01-01", end="2024-01-07", freq="D")
moods = [random.randint(1, 5) for _ in range(len(dates))]
df = pd.DataFrame({
"Date": dates,
"Mood": moods
})
fig = px.line(df, x="Date", y="Mood", title="Your Mood Timeline")
st.plotly_chart(fig)
# --- Support Tools ---
def support_tools():
st.title("Support Tools & Resources π οΈ")
st.subheader("Guided Meditation")
st.write("Select a meditation type to begin.")
meditation_type = st.selectbox("Meditation Type", ["Stress Relief", "Mindfulness", "Sleep"])
if st.button("Play Meditation"):
st.write(f"Playing {meditation_type} meditation π΅")
# --- Settings Page ---
def settings_page():
st.title("Settings βοΈ")
if st.session_state.user_profile:
st.subheader("Therapist Preferences")
new_approach = st.slider("Approach Style", 0, 10, st.session_state.user_profile["preferences"]["approach_style"])
new_tone = st.slider("Tone Style", 0, 10, st.session_state.user_profile["preferences"]["tone_style"])
if st.button("Update Preferences"):
st.session_state.user_profile["preferences"].update({
"approach_style": new_approach,
"tone_style": new_tone
})
st.success("Preferences updated!")
# --- Page Navigation ---
def navigation():
with st.sidebar:
st.title("Emma π")
if st.button("Home"):
st.session_state.current_page = 'home'
if st.button("Chat"):
st.session_state.current_page = 'chat'
if st.button("Progress Dashboard"):
st.session_state.current_page = 'progress'
if st.button("Support Tools"):
st.session_state.current_page = 'tools'
if st.button("Settings"):
st.session_state.current_page = 'settings'
# --- Main App Logic ---
def main():
navigation()
# Load pages based on navigation
if st.session_state.current_page == "home":
onboarding_page()
elif st.session_state.current_page == "chat":
chat_page()
elif st.session_state.current_page == "progress":
progress_dashboard()
elif st.session_state.current_page == "tools":
support_tools()
elif st.session_state.current_page == "settings":
settings_page()
if __name__ == "__main__":
main()
|