Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
104 lines (78 loc) · 4.28 KB
/
Copy pathapp.py
File metadata and controls
104 lines (78 loc) · 4.28 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
import streamlit as st
from database import init_db, create_new_session, get_sessions, rename_session, delete_session, save_chat, load_chat_history
from auth import show_auth_page
from chatbot import get_response
# Initialize database
init_db()
st.set_page_config(page_title="Mental Palace", page_icon="💬", layout="wide")
st.sidebar.image("img/logo.jpg", width=200) # Sidebar Logo
st.title("Mental Palace - AI Mental Health Companion")
# **Login/Register System**
show_auth_page()
# **Handle Chat Sessions**
if "authenticated" in st.session_state and st.session_state["authenticated"]:
username = st.session_state["username"]
# **Sidebar Section**
st.sidebar.title(f"💬 {username}'s Chat Sessions")
# **Persona Selection**
st.sidebar.markdown("### 🧠 Choose Your AI Persona")
persona_options = {
"Mental Palace Counselor": "The balanced and supportive AI that provides empathetic yet structured mental health support.",
"Compassionate Listener": "A deeply empathetic AI that focuses on active listening and validation.",
"Motivational Coach": "A high-energy AI that encourages and empowers users to take action for self-improvement.",
"CBT Guide": "A rational AI that helps reframe negative thoughts using cognitive behavioral techniques."
}
selected_persona = st.sidebar.selectbox("Select a Persona:", list(persona_options.keys()), key="persona_select")
st.session_state["selected_persona"] = selected_persona
st.sidebar.markdown(f"📝 *{persona_options[selected_persona]}*")
# Fetch user chat sessions
user_sessions = get_sessions(username)
st.session_state["selected_session"] = None # Force session to remain unselected
# Display available chat sessions
session_options = {session_name: session_id for session_id, session_name in user_sessions}
selected_session_name = st.sidebar.radio("Select a chat:", list(session_options.keys()), key="session_select", index=None)
# Update selected session when user clicks
if selected_session_name:
st.session_state["selected_session"] = session_options[selected_session_name]
# **New Chat Button**
if st.sidebar.button("🆕 New Chat"):
new_session_id, new_session_name = create_new_session(username)
if new_session_id:
st.session_state["selected_session"] = new_session_id
st.rerun()
# Rename session
new_name = st.sidebar.text_input("Rename chat:", selected_session_name if selected_session_name else "")
if st.sidebar.button("Rename") and selected_session_name:
rename_session(st.session_state["selected_session"], new_name)
st.rerun()
# Delete session
if st.sidebar.button("🗑️ Delete Chat") and selected_session_name:
delete_session(st.session_state["selected_session"])
st.session_state["selected_session"] = None
st.rerun()
# **Display Chat Interface OR Welcome Message**
if st.session_state["selected_session"]:
st.write(f"💬 Chat Session: **{selected_session_name}**")
st.write(f"🧠 AI Persona: **{st.session_state['selected_persona']}**") # Show selected persona
# Load chat history
chat_history = load_chat_history(st.session_state["selected_session"])
for message, response in chat_history:
st.chat_message("user", avatar="🌸").write(message)
st.chat_message("assistant", avatar="🤖").write(response)
# User input
user_input = st.chat_input("Type your message here...")
if user_input:
response = get_response(username, user_input, st.session_state["selected_persona"]) # Pass persona
# Display chat
st.chat_message("user", avatar="🌸").write(user_input)
st.chat_message("assistant", avatar="🤖").write(response)
# Save conversation
save_chat(st.session_state["selected_session"], username, user_input, response)
else:
st.markdown(f"### 👋 Welcome, {username}!")
st.markdown("Select an existing chat from the left panel or start a **new chat** to begin.")
# **Logout Button at Bottom of Sidebar**
st.sidebar.markdown("---")
if st.sidebar.button("🚪 Log Out", key="logout_button"):
st.session_state.clear()
st.rerun()