Skip to content
Merged
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
81 changes: 80 additions & 1 deletion components/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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.",
Expand Down Expand Up @@ -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"):
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ geopy
requests
Pillow
streamlit-lottie
bcrypt
bcrypt
streamlit_js_eval