| sys.path.append(os.path.abspath(os.path.dirname(__file__))) |
| import sys |
| import os |
| import json |
| import logging |
| import uuid |
| import streamlit as st |
| from src.chatbot import generate_response_with_history, COUNTRY_HELPLINES, get_helpline_for_country |
|
|
| |
| sys.path.append(os.path.abspath("src")) |
| logging.basicConfig(filename="safeguard_logs.txt", level=logging.INFO) |
| METRICS_FILE = "user_metrics.json" |
|
|
| |
| CUSTOM_CSS = f""" |
| <style> |
| .stApp {{ |
| background-color: #101920; |
| color: #BAC7D8; |
| }} |
| .stTextInput, .stSelectbox, .stButton {{ |
| background-color: #1A242E !important; |
| border-color: #D75D72 !important; |
| }} |
| h1, h2, h3 {{ |
| color: #D75D72 !important; |
| }} |
| .stMarkdown {{ |
| color: #BAC7D8; |
| }} |
| .stAlert {{ |
| background-color: #1A242E !important; |
| }} |
| </style> |
| """ |
| st.markdown(CUSTOM_CSS, unsafe_allow_html=True) |
|
|
| |
| def init_session(): |
| if "metrics" not in st.session_state: |
| st.session_state.metrics = load_metrics() |
| if "session_id" not in st.session_state: |
| st.session_state.session_id = str(uuid.uuid4())[:8] |
| if "chat_history" not in st.session_state: |
| st.session_state.chat_history = [] |
|
|
| |
| def load_metrics(): |
| if os.path.exists(METRICS_FILE): |
| try: |
| with open(METRICS_FILE, "r") as f: |
| return json.load(f) |
| except Exception as e: |
| st.error(f"Error loading metrics: {e}") |
| return {"sessions": [], "feedback": {"thumbs_up": 0, "thumbs_down": 0}, "harmful_content": []} |
|
|
| def save_metrics(): |
| try: |
| with open(METRICS_FILE, "w") as f: |
| json.dump(st.session_state.metrics, f, indent=4) |
| except Exception as e: |
| st.error(f"Error saving metrics: {e}") |
|
|
| |
| def render_chat_message(role, content): |
| with st.chat_message(role): |
| st.markdown(f"**{'You' if role == 'user' else 'Bot'}:** {content}") |
|
|
| def render_helpline(): |
| with st.sidebar: |
| st.subheader("🌍 Country Support") |
| selected_country = st.selectbox( |
| "Select your country:", |
| options=list(COUNTRY_HELPLINES.keys()), |
| index=0 |
| ) |
| st.markdown(f"**📞 {selected_country} Helpline:** \n{get_helpline_for_country(selected_country)}") |
|
|
| |
| def main(): |
| st.title("JoyCloud Mental Health Assistant 🧠") |
| st.markdown("---") |
| |
| |
| with st.sidebar: |
| st.markdown("### Session Info") |
| st.write(f"Session ID: `{st.session_state.session_id}`") |
| st.markdown("---") |
| st.markdown("**Disclaimer:** \nThis is a prototype chatbot. For emergencies, contact a professional.") |
| |
| |
| render_helpline() |
| |
| |
| for msg in st.session_state.chat_history: |
| render_chat_message(msg["role"], msg["content"]) |
| |
| |
| prompt = st.chat_input("Type your message...") |
| if prompt: |
| try: |
| |
| st.session_state.chat_history.append({"role": "user", "content": prompt}) |
| render_chat_message("user", prompt) |
| |
| |
| with st.spinner("Thinking..."): |
| response = generate_response_with_history( |
| st.session_state.chat_history, |
| st.session_state.selected_country |
| ) |
| |
| |
| st.session_state.chat_history.append({"role": "bot", "content": response}) |
| render_chat_message("bot", response) |
| |
| except Exception as e: |
| st.error(f"Error generating response: {e}") |
| logging.error(f"Session {st.session_state.session_id}: {str(e)}") |
| |
| |
| st.markdown("---") |
| col1, col2 = st.columns(2) |
| with col1: |
| if st.button("👍 Helpful", use_container_width=True, type="primary"): |
| st.session_state.metrics["feedback"]["thumbs_up"] += 1 |
| save_metrics() |
| st.toast("Thank you for your feedback!") |
| with col2: |
| if st.button("👎 Needs Improvement", use_container_width=True, type="secondary"): |
| st.session_state.metrics["feedback"]["thumbs_down"] += 1 |
| save_metrics() |
| st.toast("We'll use this to improve!") |
|
|
| |
| if __name__ == "__main__": |
| init_session() |
| main() |