diff --git a/TalkHeal.py b/TalkHeal.py index 02ba9ea..a9f839c 100644 --- a/TalkHeal.py +++ b/TalkHeal.py @@ -19,6 +19,31 @@ from components.emergency_page import render_emergency_page from components.profile import apply_global_font_size +# ------------- ADDED FOR CHATBOT LANGUAGE --------------- +from deep_translator import GoogleTranslator + + +lang_map = { + "English": "en", + "Hindi": "hi", + "Bengali": "bn", + "Gujarati": "gu", + "Tamil": "ta", + "Telugu": "te", + "Marathi": "mr", + "Punjabi": "pa" +} + + +def translate_chatbot_reply(text): + lang = st.session_state.get("language", "English") + if lang != "English": + try: + return GoogleTranslator.translate(text, dest=lang_map[lang]).text + except Exception: + return text + return text +# ------------------------------------------------------- # --- 1. INITIALIZE SESSION STATE --- if "chat_history" not in st.session_state: @@ -40,16 +65,17 @@ ] if "selected_tone" not in st.session_state: st.session_state.selected_tone = "Compassionate Listener" +if "language" not in st.session_state: + st.session_state.language = "English" # --- 2. SET PAGE CONFIG --- apply_global_font_size() - # --- 3. APPLY STYLES & CONFIGURATIONS --- apply_custom_css() model = configure_gemini() -# --- 4. TONE SELECTION DROPDOWN IN SIDEBAR --- +# --- 4. TONE & LANGUAGE SELECTION DROPDOWN IN SIDEBAR --- 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.", @@ -67,6 +93,15 @@ ) st.session_state.selected_tone = selected_tone + # ------------- LANGUAGE DROPDOWN ADDED HERE ------------- + st.header("🌐 Chatbot Reply Language") + st.session_state.language = st.selectbox( + "Choose reply language for chatbot:", + ["English", "Hindi", "Bengali", "Gujarati", "Tamil", "Telugu", "Marathi", "Punjabi"], + index=0 + ) + # -------------------------------------------------------- + # --- 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"]) @@ -114,4 +149,4 @@ def get_tone_prompt(): } setTimeout(scrollToBottom, 100); -""", unsafe_allow_html=True) \ No newline at end of file +""", unsafe_allow_html=True) diff --git a/components/chat_interface.py b/components/chat_interface.py index 874a329..c55972f 100644 --- a/components/chat_interface.py +++ b/components/chat_interface.py @@ -4,6 +4,32 @@ from core.utils import get_current_time, get_ai_response, save_conversations import requests +# ---- Translation Code Directly Here ---- +from deep_translator import GoogleTranslator + +lang_map = { + "English": "en", + "Hindi": "hi", + "Bengali": "bn", + "Gujarati": "gu", + "Tamil": "ta", + "Telugu": "te", + "Marathi": "mr", + "Punjabi": "pa" +} + +def translate_chatbot_reply(text): + lang = st.session_state.get("language", "English") + if lang != "English": + try: + return GoogleTranslator(source="auto", target=lang_map[lang]).translate(text) + except Exception as e: + print("Translation error:", e) + return text + return text + +# ----------------------------------------- + # Inject JS to get user's local time zone def set_user_time_in_session(): if "user_time_offset" not in st.session_state: @@ -112,9 +138,13 @@ def format_memory(convo_history, max_turns=10): prompt = f"{system_prompt}\n\n{memory}\nUser: {user_input.strip()}\nBot:" ai_response = get_ai_response(prompt, model) + # --------------- TRANSLATE THE BOT REPLY --------------- + ai_response_translated = translate_chatbot_reply(ai_response) + # ------------------------------------------------------- + active_convo["messages"].append({ "sender": "bot", - "message": ai_response, + "message": ai_response_translated, "time": get_current_time() }) diff --git a/components/sidebar.py b/components/sidebar.py index 5c0bafe..66cf87d 100644 --- a/components/sidebar.py +++ b/components/sidebar.py @@ -76,6 +76,15 @@ def render_sidebar(): """Renders the left and right sidebars.""" + + with st.sidebar: + st.header("🌐 Chatbot Reply Language") + st.session_state.language = st.selectbox( + "Choose reply language for chatbot:", + ["English", "Hindi", "Bengali", "Gujarati", "Tamil", "Telugu", "Marathi", "Punjabi"], + index=0 + ) + with st.sidebar: render_profile_section() diff --git a/requirements.txt b/requirements.txt index 7a85788..2987619 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ google-generativeai geopy requests Pillow -streamlit-lottie \ No newline at end of file +streamlit-lottie +deep-translator +