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
41 changes: 38 additions & 3 deletions TalkHeal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.",
Expand All @@ -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"])
Expand Down Expand Up @@ -114,4 +149,4 @@ def get_tone_prompt():
}
setTimeout(scrollToBottom, 100);
</script>
""", unsafe_allow_html=True)
""", unsafe_allow_html=True)
32 changes: 31 additions & 1 deletion components/chat_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
})

Expand Down
9 changes: 9 additions & 0 deletions components/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ google-generativeai
geopy
requests
Pillow
streamlit-lottie
streamlit-lottie
deep-translator