|
6 | 6 | from core.config import PAGE_CONFIG
|
7 | 7 | st.set_page_config(**PAGE_CONFIG)
|
8 | 8 |
|
| 9 | +import google.generativeai as genai |
| 10 | +import streamlit as st |
| 11 | + |
| 12 | +# --- API Key Setup --- |
9 | 13 | GEMINI_API_KEY = st.secrets["GEMINI_API_KEY"]
|
10 | 14 |
|
11 |
| -# --- 2. CONTINUED IMPORTS --- |
12 |
| -from core.utils import save_conversations, load_conversations, get_current_time, create_new_conversation |
13 |
| -from core.config import configure_gemini, get_tone_prompt, get_selected_mood |
| 15 | +# --- Initialize Database --- |
| 16 | +from core.db import init_db, create_user, get_user_by_username, create_chat, add_message, get_messages |
| 17 | +init_db() |
| 18 | + |
| 19 | +# --- Imports --- |
| 20 | +from core.utils import save_conversations, load_conversations, get_current_time, create_new_conversation, get_ai_response |
| 21 | +from core.config import configure_gemini, PAGE_CONFIG, get_tone_prompt, get_selected_mood |
| 22 | + |
14 | 23 | from css.styles import apply_custom_css
|
15 | 24 | from components.header import render_header
|
16 | 25 | from components.sidebar import render_sidebar
|
17 |
| -from components.chat_interface import render_chat_interface, handle_chat_input |
| 26 | +from components.chat_interface import render_chat_interface |
18 | 27 | from components.emergency_page import render_emergency_page
|
| 28 | +# --- Database Setup --- |
| 29 | +from core.db import init_db, get_messages, add_message, create_chat |
| 30 | +from core.db import get_user_by_username, create_user |
| 31 | +init_db() |
19 | 32 |
|
20 |
| -# --- 3. SESSION STATE INITIALIZATION --- |
| 33 | +# --- Session State Initialization --- |
21 | 34 | if "chat_history" not in st.session_state:
|
22 | 35 | st.session_state.chat_history = []
|
23 | 36 | if "conversations" not in st.session_state:
|
24 | 37 | st.session_state.conversations = load_conversations()
|
25 | 38 | if "active_conversation" not in st.session_state:
|
26 | 39 | st.session_state.active_conversation = -1
|
| 40 | + |
27 | 41 | if "show_emergency_page" not in st.session_state:
|
28 | 42 | st.session_state.show_emergency_page = False
|
29 | 43 | if "sidebar_state" not in st.session_state:
|
|
40 | 54 | if "selected_mood" not in st.session_state:
|
41 | 55 | st.session_state.selected_mood = "🙂" # Default emoji mood
|
42 | 56 |
|
| 57 | +if "user_id" not in st.session_state: |
| 58 | + st.session_state.user_id = None |
| 59 | +if "active_chat_id" not in st.session_state: |
| 60 | + st.session_state.active_chat_id = None |
| 61 | + |
| 62 | + |
43 | 63 | # --- 4. STYLES & GEMINI SETUP ---
|
44 | 64 | apply_custom_css()
|
45 | 65 | model = configure_gemini()
|
46 | 66 |
|
47 |
| -# --- 5. SIDEBAR --- |
| 67 | +# --- 4. TONE SELECTION DROPDOWN & EMOJI MOOD IN SIDEBAR --- |
| 68 | +TONE_OPTIONS = { |
| 69 | + "Compassionate Listener": "You are a compassionate listener — soft, empathetic, patient — like a therapist who listens without judgment.", |
| 70 | + "Motivating Coach": "You are a motivating coach — energetic, encouraging, and action-focused — helping the user push through rough days.", |
| 71 | + "Wise Friend": "You are a wise friend — thoughtful, poetic, and reflective — giving soulful responses and timeless advice.", |
| 72 | + "Neutral Therapist": "You are a neutral therapist — balanced, logical, and non-intrusive — asking guiding questions using CBT techniques.", |
| 73 | + "Mindfulness Guide": "You are a mindfulness guide — calm, slow, and grounding — focused on breathing, presence, and awareness." |
| 74 | +} |
| 75 | + |
| 76 | +def get_tone_prompt(): |
| 77 | + return TONE_OPTIONS.get(st.session_state.get("selected_tone", "Compassionate Listener"), TONE_OPTIONS["Compassionate Listener"]) |
| 78 | + |
| 79 | +# --- 5. RENDER SIDEBAR --- |
| 80 | +def render_sidebar(): |
| 81 | + from core.db import get_chats_for_user, create_chat |
| 82 | + |
| 83 | + with st.sidebar: |
| 84 | + st.header("🧠 Choose Your AI Tone") |
| 85 | + selected_tone = st.selectbox( |
| 86 | + "Select a personality tone:", |
| 87 | + options=list(TONE_OPTIONS.keys()), |
| 88 | + index=0 |
| 89 | + ) |
| 90 | + st.session_state.selected_tone = selected_tone |
| 91 | + |
| 92 | + # Emoji Mood Selector (if implemented in sidebar) |
| 93 | + MOOD_OPTIONS = { |
| 94 | + "😊": "Happy", |
| 95 | + "😢": "Sad", |
| 96 | + "😡": "Angry", |
| 97 | + "😨": "Anxious", |
| 98 | + "😌": "Calm", |
| 99 | + "😕": "Confused", |
| 100 | + "🥱": "Tired", |
| 101 | + "😶": "Numb" |
| 102 | + } |
| 103 | + st.markdown("### 🌤️ How are you feeling?") |
| 104 | + selected_mood = st.radio( |
| 105 | + "Select your current mood:", |
| 106 | + options=list(MOOD_OPTIONS.keys()), |
| 107 | + horizontal=True |
| 108 | + ) |
| 109 | + st.session_state.selected_mood = selected_mood |
| 110 | + |
| 111 | + st.write(f"Current Mood: {MOOD_OPTIONS.get(selected_mood, 'Happy')}") |
| 112 | + |
| 113 | + # Chat list from DB |
| 114 | + if "user_id" in st.session_state and st.session_state.user_id: |
| 115 | + chats = get_chats_for_user(st.session_state.user_id) |
| 116 | + for chat in chats: |
| 117 | + if st.button(chat["title"], key=f"chat_{chat['id']}"): |
| 118 | + st.session_state.active_chat_id = chat["id"] |
| 119 | + st.rerun() |
| 120 | + if st.button("➕ New Chat"): |
| 121 | + chat_id = create_chat(st.session_state.user_id, "Untitled Chat") |
| 122 | + st.session_state.active_chat_id = chat_id |
| 123 | + st.rerun() |
| 124 | + |
| 125 | +# --- 6. CALL SIDEBAR --- |
48 | 126 | render_sidebar()
|
49 | 127 |
|
50 | 128 | # --- 6. MAIN PAGE ROUTING LOGIC ---
|
51 | 129 | main_area = st.container()
|
52 |
| - |
53 |
| -# Load conversations or start a new one |
| 130 | +# --- 8. LOAD CONVERSATIONS OR START A NEW ONE --- |
54 | 131 | if not st.session_state.conversations:
|
55 | 132 | saved_convos = load_conversations()
|
56 | 133 | if saved_convos:
|
|
62 | 139 | st.session_state.active_conversation = 0
|
63 | 140 | st.rerun()
|
64 | 141 |
|
65 |
| -# --- 7. MAIN VIEW DISPLAY --- |
66 | 142 | if st.session_state.get("show_emergency_page"):
|
67 | 143 | with main_area:
|
68 | 144 | render_emergency_page()
|
69 | 145 | else:
|
70 | 146 | with main_area:
|
71 | 147 | render_header()
|
72 |
| - st.subheader(f"🗣️ Current Chatbot Tone: **{st.session_state['selected_tone']}**") |
73 |
| - st.markdown(f"**🧠 Mood Selected:** {get_selected_mood()}") |
74 |
| - render_chat_interface() |
75 |
| - handle_chat_input(model, system_prompt=get_tone_prompt()) |
| 148 | +# --- 9. CHAT INTERFACE --- |
| 149 | +st.subheader(f"🗣️ Current Chatbot Tone: **{st.session_state['selected_tone']}**") |
| 150 | +st.markdown(f"**🧠 Mood Selected:** {get_selected_mood()}") |
| 151 | +render_chat_interface() |
| 152 | +handle_chat_input(model, system_prompt=get_tone_prompt()) |
76 | 153 |
|
77 |
| -# --- 8. AUTO SCROLL SCRIPT --- |
78 | 154 | st.markdown("""
|
79 | 155 | <script>
|
80 | 156 | function scrollToBottom() {
|
|
0 commit comments