-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTalkHeal.py
More file actions
187 lines (168 loc) Β· 7.9 KB
/
TalkHeal.py
File metadata and controls
187 lines (168 loc) Β· 7.9 KB
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
import streamlit as st
import cv2
import google.generativeai as genai
# --- LOCAL IMPORTS ---
from auth.auth_utils import init_db
from components.login_page import show_login_page
from hand_gesture_recognition_mediapipe.inference import gesture_mode
from core.utils import save_conversations, load_conversations, get_current_time, create_new_conversation
from core.config import configure_gemini
from css.styles import apply_custom_css
from components.header import render_header
from components.sidebar import render_sidebar
from components.chat_interface import render_chat_interface, handle_chat_input, render_session_controls
from components.mood_dashboard import render_mood_dashboard
from components.emergency_page import render_emergency_page
from components.focus_session import render_focus_session
from components.profile import apply_global_font_size
# --- PAGE CONFIG ---
st.set_page_config(page_title="TalkHeal", page_icon="π¬", layout="wide")
# --- HIDE SIDEBAR NAV ---
st.markdown("<style>div[data-testid='stSidebarNav'] {display: none;}</style>", unsafe_allow_html=True)
# --- DB INIT ---
if "db_initialized" not in st.session_state:
init_db()
st.session_state["db_initialized"] = True
# --- AUTH STATE ---
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if "show_signup" not in st.session_state:
st.session_state.show_signup = False
# --- LOGIN PAGE ---
if not st.session_state.authenticated:
show_login_page()
st.stop()
# --- TOP RIGHT BUTTONS ---
if st.session_state.get("authenticated", False):
col_spacer, col_theme, col_emergency, col_about, col_logout = st.columns([0.7, 0.1, 0.35, 0.2, 0.2])
with col_spacer: pass
with col_theme:
is_dark = st.session_state.get('dark_mode', False)
if st.button("π" if is_dark else "βοΈ", key="top_theme_toggle", use_container_width=True):
st.session_state.dark_mode = not is_dark
st.session_state.theme_changed = True
st.rerun()
with col_emergency:
if st.button("π¨ Emergency Help", key="emergency_main_btn", use_container_width=True, type="secondary"):
st.session_state.show_emergency_page = True
st.rerun()
with col_about:
if st.button("βΉοΈ About", key="about_btn", use_container_width=True):
st.switch_page("pages/About.py")
with col_logout:
if st.button("Logout", key="logout_btn", use_container_width=True):
for key in ["authenticated", "user_email", "user_name", "show_signup"]:
st.session_state.pop(key, None)
st.rerun()
# --- SESSION STATE INIT ---
defaults = {
"chat_history": [],
"conversations": load_conversations(),
"active_conversation": -1,
"show_emergency_page": False,
"show_focus_session": False,
"show_mood_dashboard": False,
"sidebar_state": "expanded",
"mental_disorders": [
"Depression & Mood Disorders", "Anxiety & Panic Disorders", "Bipolar Disorder",
"PTSD & Trauma", "OCD & Related Disorders", "Eating Disorders",
"Substance Use Disorders", "ADHD & Neurodevelopmental", "Personality Disorders",
"Sleep Disorders"
],
"selected_tone": "Compassionate Listener",
"gesture_mode": False
}
for k, v in defaults.items():
if k not in st.session_state:
st.session_state[k] = v
# --- CONFIG & STYLES ---
apply_global_font_size()
apply_custom_css()
model = configure_gemini()
# --- AI TONES ---
TONE_OPTIONS = {
"Compassionate Listener": "You are a compassionate listener β soft, empathetic, patient β like a therapist who listens without judgment.",
"Motivating Coach": "You are a motivating coach β energetic, encouraging, and action-focused β helping the user push through rough days.",
"Wise Friend": "You are a wise friend β thoughtful, poetic, and reflective β giving soulful responses and timeless advice.",
"Neutral Therapist": "You are a neutral therapist β balanced, logical, and non-intrusive β asking guiding questions using CBT techniques.",
"Mindfulness Guide": "You are a mindfulness guide β calm, slow, and grounding β focused on breathing, presence, and awareness."
}
def get_tone_prompt():
return TONE_OPTIONS.get(st.session_state.get("selected_tone"), TONE_OPTIONS["Compassionate Listener"])
# --- RENDER SIDEBAR ---
render_sidebar()
# --- MAIN CONTAINER ---
main_area = st.container()
# --- HANDLE CONVERSATIONS ---
if not st.session_state.conversations:
saved_conversations = load_conversations()
if saved_conversations:
st.session_state.conversations = saved_conversations
st.session_state.active_conversation = 0
else:
create_new_conversation()
st.session_state.active_conversation = 0
st.rerun()
# --- FEATURE CARDS ---
def render_feature_cards():
cols = st.columns(6)
features = [
("π§ββοΈ Start Yoga", "pages/Yoga.py", "π§ββοΈ", "Yoga & Meditation"),
("π¬οΈ Start Breathing", "pages/Breathing_Exercise.py", "π¬οΈ", "Breathing Exercises"),
("π Open Journal", "pages/Journaling.py", "π", "Personal Journaling"),
("π¨ββοΈ Find Specialists", "pages/doctor_spec.py", "π¨ββοΈ", "Doctor Specialist"),
("π οΈ Explore Tools", "pages/selfHelpTools.py", "π οΈ", "Self-Help Tools"),
("πΏ Open Wellness Hub", "pages/WellnessResourceHub.py", "πΏ", "Wellness Hub")
]
for col, (btn_text, page, icon, title) in zip(cols, features):
with col:
st.markdown(f"<div class='feature-card'><div class='card-icon'>{icon}</div><h3>{title}</h3></div>", unsafe_allow_html=True)
if st.button(btn_text, use_container_width=True):
st.switch_page(page)
# --- ROUTING ---
if st.session_state.show_emergency_page:
with main_area: render_emergency_page()
elif st.session_state.show_focus_session:
with main_area: render_focus_session()
elif st.session_state.show_mood_dashboard:
with main_area: render_mood_dashboard()
else:
with main_area:
mode = st.radio("Choose Mode", ["π¬ Chat Mode", "π Gesture Mode"], horizontal=True)
if mode == "π¬ Chat Mode":
render_feature_cards()
with st.expander("π§ Customize Your AI Companion"):
selected_tone = st.selectbox("Select AI personality:", list(TONE_OPTIONS.keys()),
index=list(TONE_OPTIONS.keys()).index(st.session_state.selected_tone))
if selected_tone != st.session_state.selected_tone:
st.session_state.selected_tone = selected_tone
st.rerun()
st.info(f"**Current Style**: {TONE_OPTIONS[selected_tone]}")
st.markdown("### π How are you feeling today?")
mood_options = ['Very Sad π’', 'Sad π', 'Neutral π', 'Happy π', 'Very Happy π']
mood = st.slider("Select your current mood", 1, 5, 3, 1)
tips = {
1: "π€ It's okay to feel this way. Try some deep breathing.",
2: "π Write down your thoughts in your journal.",
3: "πΆββοΈ Take a short walk or stretch.",
4: "β¨ You're happy! Share something positive.",
5: "π You're shining! Spread positivity."
}
col_m, col_t = st.columns([1, 2])
with col_m: st.markdown(f"**Current mood**: {mood_options[mood-1]}")
with col_t: st.info(tips.get(mood))
render_chat_interface()
handle_chat_input(model, system_prompt=get_tone_prompt())
render_session_controls()
else:
gesture_mode()
# --- AUTO SCROLL ---
st.markdown("""
<script>
function scrollToBottom() {
var chatContainer = document.querySelector('.chat-container');
if (chatContainer) { chatContainer.scrollTop = chatContainer.scrollHeight; }
}
setTimeout(scrollToBottom, 100);
</script>
""", unsafe_allow_html=True)