diff --git a/components/chat_interface.py b/components/chat_interface.py index 462ad45..874a329 100644 --- a/components/chat_interface.py +++ b/components/chat_interface.py @@ -2,6 +2,7 @@ import streamlit.components.v1 as components from datetime import datetime from core.utils import get_current_time, get_ai_response, save_conversations +import requests # Inject JS to get user's local time zone def set_user_time_in_session(): @@ -117,11 +118,25 @@ def format_memory(convo_history, max_turns=10): "time": get_current_time() }) + except ValueError as e: + st.error("I'm having trouble understanding your message. Could you please rephrase it?") + active_convo["messages"].append({ + "sender": "bot", + "message": "I'm having trouble understanding your message. Could you please rephrase it?", + "time": get_current_time() + }) + except requests.RequestException as e: + st.error("Network connection issue. Please check your internet connection.") + active_convo["messages"].append({ + "sender": "bot", + "message": "I'm having trouble connecting to my services. Please check your internet connection and try again.", + "time": get_current_time() + }) except Exception as e: - st.error(f"An error occurred: {e}") + st.error(f"An unexpected error occurred. Please try again.") active_convo["messages"].append({ "sender": "bot", - "message": "I’m having trouble responding right now. Please try again in a moment.", + "message": "I'm having trouble responding right now. Please try again in a moment.", "time": get_current_time() }) diff --git a/components/emergency_page.py b/components/emergency_page.py index e02a5c1..f52136e 100644 --- a/components/emergency_page.py +++ b/components/emergency_page.py @@ -2,6 +2,8 @@ from geopy.geocoders import Nominatim import urllib.parse from .sidebar import GLOBAL_RESOURCES +import geopy.exc +import requests def render_emergency_page(): @@ -41,8 +43,20 @@ def render_emergency_page(): st.error( f"Could not find a location for '{location_query}'. Please try again.") st.session_state.pop('location_info', None) + except geopy.exc.GeocoderTimedOut: + st.error("Location search timed out. Please try again with a more specific location.") + st.session_state.pop('location_info', None) + except geopy.exc.GeocoderUnavailable: + st.error("Location service is currently unavailable. Please try again later.") + st.session_state.pop('location_info', None) + except geopy.exc.GeocoderQuotaExceeded: + st.error("Location service quota exceeded. Please try again later.") + st.session_state.pop('location_info', None) + except requests.RequestException as e: + st.error("Network error while searching for location. Please check your internet connection.") + st.session_state.pop('location_info', None) except Exception as e: - st.error(f"An error occurred during search: {e}") + st.error(f"An unexpected error occurred during search. Please try again.") st.session_state.pop('location_info', None) else: st.warning("Please enter a location to search.") diff --git a/core/config.py b/core/config.py index c14aeb9..a7fa361 100644 --- a/core/config.py +++ b/core/config.py @@ -1,6 +1,7 @@ import streamlit as st import google.generativeai as genai from pathlib import Path +import requests # ---------- Logo and Page Config ---------- logo_path = str(Path(__file__).resolve().parent.parent / "TalkHealLogo.png") @@ -77,8 +78,20 @@ def generate_response(user_input, model): {"role": "user", "parts": [user_input]} ]) return response.text + except ValueError as e: + st.error("❌ Invalid input or model configuration issue. Please check your input.") + return None + except google.generativeai.types.BlockedPromptException as e: + st.error("❌ Content policy violation. Please rephrase your message.") + return None + except google.generativeai.types.GenerationException as e: + st.error("❌ Failed to generate response. Please try again.") + return None + except requests.RequestException as e: + st.error("❌ Network connection issue. Please check your internet connection.") + return None except Exception as e: - st.error(f"❌ Failed to generate response: {e}") + st.error(f"❌ Unexpected error occurred: {e}") return None # ---------- MAIN CHAT INTERFACE ---------- diff --git a/core/utils.py b/core/utils.py index 50bf8f6..97f3a0a 100644 --- a/core/utils.py +++ b/core/utils.py @@ -4,6 +4,7 @@ import json import os import requests +import google.generativeai def get_current_time(): """Returns the user's local time formatted as HH:MM AM/PM.""" @@ -82,7 +83,22 @@ def get_ai_response(user_message, model): # Clean the response to remove any HTML or unwanted formatting cleaned_response = clean_ai_response(response.text) return cleaned_response + except ValueError as e: + # Handle invalid input or model configuration issues + return "I'm having trouble understanding your message. Could you please rephrase it?" + except google.generativeai.types.BlockedPromptException as e: + # Handle content policy violations + return "I understand you're going through something difficult. Let's focus on how you're feeling and what might help you feel better." + except google.generativeai.types.GenerationException as e: + # Handle generation errors + return "I'm having trouble generating a response right now. Please try again in a moment." + except requests.RequestException as e: + # Handle network/API connection issues + return "I'm having trouble connecting to my services. Please check your internet connection and try again." except Exception as e: + # Log unexpected errors for debugging (you can add logging here) + # import logging + # logging.error(f"Unexpected error in get_ai_response: {e}") return "I'm here to listen and support you. Sometimes I have trouble connecting, but I want you to know that your feelings are valid and you're not alone. Would you like to share more about what you're experiencing?" def cached_user_ip():