Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 73 additions & 128 deletions TalkHeal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# --- 0. IMPORTS ---
import uuid # standard library import
import streamlit as st
from auth.auth_utils import init_db
from components.login_page import show_login_page

emoji-mood-selector
# --- 1. PAGE CONFIG FIRST ---
from core.config import PAGE_CONFIG
st.set_page_config(**PAGE_CONFIG)
st.set_page_config(page_title="TalkHeal", page_icon="💬", layout="wide")

# --- DB Initialization ---
Expand Down Expand Up @@ -49,39 +54,56 @@
from core.config import configure_gemini, PAGE_CONFIG
from core.utils import get_current_time, create_new_conversation
from css.styles import apply_custom_css

from core.config import configure_gemini, get_tone_prompt, get_selected_mood
from core.utils import save_conversations, load_conversations, get_current_time, create_new_conversation

from components.header import render_header
from components.sidebar import render_sidebar
from components.chat_interface import render_chat_interface, handle_chat_input
from components.mood_dashboard import render_mood_dashboard
from components.focus_session import render_focus_session

# --- 1. PAGE CONFIG & CSS ---
apply_custom_css()
from components.emergency_page import render_emergency_page
from components.profile import apply_global_font_size

# --- 1. INITIALIZE SESSION STATE ---
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "conversations" not in st.session_state:
st.session_state.conversations = load_conversations()
if "active_conversation" not in st.session_state:
st.session_state.active_conversation = -1
# if "show_emergency_page" not in st.session_state:
# st.session_state.show_emergency_page = False
if "show_focus_session" not in st.session_state:
st.session_state.show_focus_session = False
if "show_mood_dashboard" not in st.session_state:
st.session_state.show_mood_dashboard = False
if "sidebar_state" not in st.session_state:
st.session_state.sidebar_state = "expanded"
if "mental_disorders" not in st.session_state:
st.session_state.mental_disorders = [
# --- 2. SESSION STATE INITIALIZATION ---
default_values = {
"conversations": [],
"active_conversation": -1,
"send_chat_message": False,
"pre_filled_chat_input": "",
"selected_mood_context": "",
"current_mood_val": "",
"chat_history": [],
"show_emergency_page": False,
"show_focus_session": False,
"show_mood_dashboard": False,
"sidebar_state": "expanded",
"mental_disorders": [
"Anxiety", "Depression", "Bipolar Disorder", "PTSD",
"OCD", "ADHD", "Eating Disorders", "Schizophrenia"
]
}

for key, value in default_values.items():
if key not in st.session_state:
st.session_state[key] = value

"Depression & Mood Disorders", "Anxiety & Panic Disorders", "Bipolar Disorder",
"PTSD & Trauma", "OCD & Related Disorders", "Eating Disorders",
"Substance Use Disorders", "ADHD & Neurodevelopmental", "Personality Disorders",
"Sleep Disorders"
]
if "selected_tone" not in st.session_state:
st.session_state.selected_tone = "Compassionate Listener"
],
"selected_tone": "Compassionate Listener",
"selected_mood": "🙂"
}

for key, value in default_values.items():
if key not in st.session_state:
st.session_state[key] = value

# --- 2. SET PAGE CONFIG ---
apply_global_font_size()
Expand All @@ -99,132 +121,54 @@
"Mindfulness Guide": "You are a mindfulness guide — calm, slow, and grounding — focused on breathing, presence, and awareness."
}

with st.sidebar:
st.header("🧠 Choose Your AI Tone")
selected_tone = st.selectbox(
"Select a personality tone:",
options=list(TONE_OPTIONS.keys()),
index=0
)
st.session_state.selected_tone = selected_tone
}

# --- 5. DEFINE FUNCTION TO GET TONE PROMPT ---
def get_tone_prompt():
return TONE_OPTIONS.get(st.session_state.get("selected_tone", "Compassionate Listener"), TONE_OPTIONS["Compassionate Listener"])
for key, val in default_values.items():
if key not in st.session_state:
st.session_state[key] = val

# --- 6. RENDER SIDEBAR ---
# --- 3. GEMINI MODEL SETUP ---
model = configure_gemini()

# --- 4. SIDEBAR ---
render_sidebar()

# --- 7. PAGE ROUTING ---
# --- 5. MAIN LOGIC CONTAINER ---
main_area = st.container()

# --- 6. Load or Start Conversation ---
if not st.session_state.conversations:
saved_conversations = load_conversations()
if saved_conversations:
st.session_state.conversations = saved_conversations
saved = load_conversations()
if saved:
st.session_state.conversations = saved
if st.session_state.active_conversation == -1:
st.session_state.active_conversation = 0
else:
create_new_conversation()
st.session_state.active_conversation = 0
st.rerun()

# --- 8. RENDER PAGE ---
# if st.session_state.get("show_emergency_page"):
# with main_area:
# render_emergency_page()
# else:
if st.session_state.get("show_focus_session"):
with main_area:
# --- 7. ROUTING ---
with main_area:
if st.session_state.get("show_emergency_page"):
render_emergency_page()
elif st.session_state.get("show_focus_session"):
render_focus_session()
elif st.session_state.get("show_mood_dashboard"):
with main_area:
elif st.session_state.get("show_mood_dashboard"):
render_mood_dashboard()
else:
with main_area:
else:
render_header()
st.markdown(f"""
<div style="text-align: center; margin: 20px 0;">
<h3>🗣️ Current Chatbot Tone: <strong>{st.session_state['selected_tone']}</strong></h3>
</div>
""", unsafe_allow_html=True)

# --- Mood Slider with Keyboard Navigation ---
def mood_slider():
slider_html = """
<div>
<label for="mood-slider" class="sr-only">Select your mood</label>
<input type="range" id="mood-slider" min="1" max="5" value="3" step="1"
aria-valuemin="1" aria-valuemax="5" aria-valuenow="3"
onkeydown="handleKeydown(event)" onchange="updateSliderValue(this.value)">
<div id="mood-label">Neutral</div>
<script>
function handleKeydown(event) {
const slider = document.getElementById('mood-slider');
let value = parseInt(slider.value);
if (event.key === 'ArrowLeft' && value > 1) {
value--;
} else if (event.key === 'ArrowRight' && value < 5) {
value++;
}
slider.value = value;
slider.setAttribute('aria-valuenow', value);
updateSliderValue(value);
}
function updateSliderValue(value) {
const labels = ['Very Sad', 'Sad', 'Neutral', 'Happy', 'Very Happy'];
document.getElementById('mood-label').innerText = labels[value - 1];
Streamlit.setComponentValue(value);
}
</script>
<style>
#mood-slider {
width: 100%;
accent-color: #ff69b4; /* Matches the soft pink/magenta UI */
}
#mood-label {
text-align: center;
margin-top: 10px;
font-size: 16px;
color: #333;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
</style>
</div>
"""
mood_value = st.components.v1.html(slider_html, height=100)
return mood_value

# --- Mood Slider ---
st.subheader("😊 Track Your Mood")
mood_options = ['Very Sad', 'Sad', 'Neutral', 'Happy', 'Very Happy']
mood = st.slider(
'Select your mood',
min_value=1, max_value=5, value=3, step=1
)
coping_tips = {
1: "It’s okay to feel this way. Try some deep breathing exercises to find calm.",
2: "Consider writing down your thoughts in the journal to process your feelings.",
3: "A short walk or some light stretching might help you feel balanced.",
4: "Great to hear you’re feeling happy! Share something positive in your journal.",
5: "You’re shining today! Keep spreading that positivity with a kind act."
}
st.write(f"Selected mood: {mood_options[mood-1]}")
st.write(f"Coping tip: {coping_tips.get(mood, 'Let’s explore how you’re feeling.')}")

<div style="text-align: center; margin: 20px 0;">
<h3>🗣️ Current Chatbot Tone: <strong>{st.session_state['selected_tone']}</strong></h3>
<h4>🧠 Mood Selected: <strong>{get_selected_mood()}</strong></h4>
</div>
""", unsafe_allow_html=True)

render_chat_interface()
handle_chat_input(model, system_prompt=get_tone_prompt())
handle_chat_input(model=model, system_prompt=get_tone_prompt())

# --- 9. SCROLL SCRIPT ---
# --- 8. AUTO SCROLL SCRIPT ---
st.markdown("""
<script>
function scrollToBottom() {
Expand All @@ -235,4 +179,5 @@ def mood_slider():
}
setTimeout(scrollToBottom, 100);
</script>
""", unsafe_allow_html=True)
""", unsafe_allow_html=True)

86 changes: 86 additions & 0 deletions TalkHeal.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# --- 0. IMPORTS ---
import streamlit as st
import google.generativeai as genai

# --- 1. FIRST COMMAND: PAGE CONFIG ---
from core.config import PAGE_CONFIG
st.set_page_config(**PAGE_CONFIG)

# --- 2. CONTINUED IMPORTS ---
from core.utils import save_conversations, load_conversations, get_current_time, create_new_conversation
from core.config import configure_gemini, get_tone_prompt, get_selected_mood
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
from components.emergency_page import render_emergency_page

# --- 3. SESSION STATE INITIALIZATION ---
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "conversations" not in st.session_state:
st.session_state.conversations = load_conversations()
if "active_conversation" not in st.session_state:
st.session_state.active_conversation = -1
if "show_emergency_page" not in st.session_state:
st.session_state.show_emergency_page = False
if "sidebar_state" not in st.session_state:
st.session_state.sidebar_state = "expanded"
if "mental_disorders" not in st.session_state:
st.session_state.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"
]
if "selected_tone" not in st.session_state:
st.session_state.selected_tone = "Compassionate Listener"
if "selected_mood" not in st.session_state:
st.session_state.selected_mood = "🙂" # Default emoji mood

# --- 4. STYLES & GEMINI SETUP ---
apply_custom_css()
model = configure_gemini()

# --- 5. SIDEBAR ---
render_sidebar()

# --- 6. MAIN PAGE ROUTING LOGIC ---
main_area = st.container()

# Load conversations or start a new one
if not st.session_state.conversations:
saved_convos = load_conversations()
if saved_convos:
st.session_state.conversations = saved_convos
if st.session_state.active_conversation == -1:
st.session_state.active_conversation = 0
else:
create_new_conversation()
st.session_state.active_conversation = 0
st.rerun()

# --- 7. MAIN VIEW DISPLAY ---
if st.session_state.get("show_emergency_page"):
with main_area:
render_emergency_page()
else:
with main_area:
render_header()
st.subheader(f"🗣️ Current Chatbot Tone: **{st.session_state['selected_tone']}**")
st.markdown(f"**🧠 Mood Selected:** {get_selected_mood()}")
render_chat_interface()
handle_chat_input(model, system_prompt=get_tone_prompt())

# --- 8. AUTO SCROLL SCRIPT ---
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)
5 changes: 5 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import google.generativeai as genai
genai.configure(api_key="AIzaSyAgtTtPjJzTaNSrXaWFkMEJKIpMiFI3QNw")
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Hello!")
print(response.text)
Loading