diff --git a/components/sidebar.py b/components/sidebar.py index e684be2..462617e 100644 --- a/components/sidebar.py +++ b/components/sidebar.py @@ -4,7 +4,8 @@ from core.utils import create_new_conversation, get_current_time from core.theme import get_current_theme, toggle_theme, set_palette, PALETTES from components.profile import initialize_profile_state, render_profile_section - +from streamlit_js_eval import streamlit_js_eval +import requests # --- Structured Emergency Resources --- GLOBAL_RESOURCES = [ @@ -20,6 +21,69 @@ "url": "https://www.childhelplineinternational.org/"} ] + +def get_country_from_coords(lat, lon): + try: + url = f"https://geocode.maps.co/reverse?lat={lat}&lon={lon}" + resp = requests.get(url, timeout=5) + if resp.status_code == 200: + data = resp.json() + return data.get("address", {}).get("country_code", "").upper() + except: + pass + return None + +def get_user_country(): + # 1. Try to get user's actual browser location (via JS) + coords = streamlit_js_eval( + js_expressions=""" + new Promise((resolve, reject) => { + navigator.geolocation.getCurrentPosition( + position => resolve({ + latitude: position.coords.latitude, + longitude: position.coords.longitude + }), + error => resolve(null) + ); + }); + """, + key="get_coords" + ) + + if coords and "latitude" in coords and "longitude" in coords: + country = get_country_from_coords(coords["latitude"], coords["longitude"]) + if country: + return country + + # 2. Fallback to IP-based location using ipapi.co (no key required) + try: + resp = requests.get("https://ipapi.co/json/", timeout=3) + if resp.status_code == 200: + return resp.json().get("country_code", "").upper() + except: + pass + + return None # final fallback if everything fails + +country_helplines = { + "US": [ + "National Suicide Prevention Lifeline: 988", + "Crisis Text Line: Text HOME to 741741", + "SAMHSA National Helpline: 1-800-662-4357" + ], + "IN": [ + "AASRA: 9152987821", + "Sneha Foundation: 044-24640050" + ], + "GB": [ + "Samaritans: 116 123" + ], + "AU": [ + "Lifeline: 13 11 14" + ] +} +IASP_LINK = "https://findahelpline.com/" + mental_health_resources_full = { "Depression & Mood Disorders": { "description": "Information on understanding and coping with depression, persistent depressive disorder, and other mood-related challenges.", @@ -320,6 +384,21 @@ def render_sidebar(): for resource in GLOBAL_RESOURCES: st.markdown( f"**{resource['name']}**: {resource['desc']} [Visit Website]({resource['url']})") + + # Provide localized helplines based on user's country + user_country = get_user_country() + country_label = user_country if user_country else "your country" + st.markdown("### 🚨 Emergency Help") + if user_country and user_country in country_helplines: + st.markdown(f"**Helplines for {country_label}:**") + for line in country_helplines[user_country]: + st.markdown(f"• {line}") + else: + st.markdown( + f"Couldn't detect a local helpline for {country_label}. [Find help worldwide via IASP]({IASP_LINK})" + ) + + st.markdown("---") # Theme toggle in sidebar with st.expander("🎨 Theme Settings"): diff --git a/requirements.txt b/requirements.txt index 13d53ac..0660064 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ geopy requests Pillow streamlit-lottie -bcrypt \ No newline at end of file +bcrypt +streamlit_js_eval \ No newline at end of file