Spaces:
Sleeping
Sleeping
Update app.py
#1
by
iffazainab
- opened
app.py
CHANGED
|
@@ -9,11 +9,14 @@ from timezonefinder import TimezoneFinder
|
|
| 9 |
import pytz
|
| 10 |
from streamlit_js_eval import get_geolocation
|
| 11 |
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
| 17 |
rescue_contacts = {
|
| 18 |
"Pakistan": {
|
| 19 |
"Ambulance": "115",
|
|
@@ -35,29 +38,31 @@ rescue_contacts = {
|
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
tf = TimezoneFinder()
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return local_dt
|
| 48 |
-
except Exception as e:
|
| 49 |
-
print(f"[⚠] Error loading timezone: {e}")
|
| 50 |
-
else:
|
| 51 |
-
print("[⚠] Timezone not found. Falling back to UTC.")
|
| 52 |
-
|
| 53 |
-
# Fallback
|
| 54 |
-
return datetime.utcnow().replace(tzinfo=pytz.utc)
|
| 55 |
|
| 56 |
-
#
|
|
|
|
|
|
|
| 57 |
if "history" not in st.session_state:
|
| 58 |
st.session_state.history = []
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
st.set_page_config(page_title="AI Disaster Assistant", layout="wide")
|
| 62 |
st.markdown("""
|
| 63 |
<style>
|
|
@@ -88,36 +93,14 @@ language_options = {
|
|
| 88 |
preferred_language = st.selectbox("🌐 Select your preferred language for responses:", list(language_options.keys()))
|
| 89 |
target_lang_code = language_options[preferred_language]
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
#
|
| 93 |
-
#
|
| 94 |
-
# city = ""
|
| 95 |
-
# country = ""
|
| 96 |
-
# local_time = datetime.utcnow()
|
| 97 |
-
|
| 98 |
-
# if location_data and location_data.ok:
|
| 99 |
-
# lat = location_data.latlng[0]
|
| 100 |
-
# lon = location_data.latlng[1]
|
| 101 |
-
# st.success(f"📍 GPS Location: {lat:.4f}, {lon:.4f}")
|
| 102 |
-
# try:
|
| 103 |
-
# geolocator = Nominatim(user_agent="geoapiExercises")
|
| 104 |
-
# location = geolocator.reverse((lat, lon), language='en')
|
| 105 |
-
# city = location.raw['address'].get('city', '') or location.raw['address'].get('town', '')
|
| 106 |
-
# country = location.raw['address'].get('country', '')
|
| 107 |
-
# user_location = f"{city}, {country}"
|
| 108 |
-
# st.markdown(f"📌 You are in *{user_location}*")
|
| 109 |
-
|
| 110 |
-
# local_time = get_local_time(lat, lon)
|
| 111 |
-
|
| 112 |
-
# except Exception as e:
|
| 113 |
-
# st.warning("🌐 Could not resolve full address or local time from GPS.")
|
| 114 |
-
|
| 115 |
location_data = get_geolocation()
|
| 116 |
|
| 117 |
user_location = "Unknown"
|
| 118 |
city = ""
|
| 119 |
country = ""
|
| 120 |
-
local_time = datetime.now()
|
| 121 |
|
| 122 |
if location_data and location_data.get("coords"):
|
| 123 |
lat = location_data["coords"]["latitude"]
|
|
@@ -131,11 +114,13 @@ if location_data and location_data.get("coords"):
|
|
| 131 |
user_location = f"{city}, {country}"
|
| 132 |
st.markdown(f"📌 You are in *{user_location}*")
|
| 133 |
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
except Exception as e:
|
| 137 |
-
st.warning("🌐 Could not resolve full address or
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
# 🚨 Display rescue numbers
|
| 141 |
if country in rescue_contacts:
|
|
@@ -144,11 +129,14 @@ if country in rescue_contacts:
|
|
| 144 |
for service, number in contacts.items():
|
| 145 |
st.markdown(f"<div class='contact-box'>📞 <strong>{service}</strong>: {number}</div>", unsafe_allow_html=True)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
| 147 |
col1, col2 = st.columns([2, 1])
|
| 148 |
|
| 149 |
with col1:
|
| 150 |
st.markdown("### 🧠 Ask your question (any language):")
|
| 151 |
-
user_input = st.text_area(" ", placeholder="e.g. زلزلے کے دوران
|
| 152 |
|
| 153 |
if st.button("🔍 Get Help"):
|
| 154 |
if user_input.strip() != "":
|
|
@@ -173,7 +161,9 @@ with col1:
|
|
| 173 |
translated_answer = GoogleTranslator(source='en', target=target_lang_code).translate(answer_en)
|
| 174 |
st.markdown(f"<div class='answer-box'>{translated_answer}</div>", unsafe_allow_html=True)
|
| 175 |
|
| 176 |
-
|
|
|
|
|
|
|
| 177 |
st.session_state.history.append({"time": timestamp, "question": user_input})
|
| 178 |
else:
|
| 179 |
st.error("❌ Error: " + response.text)
|
|
@@ -195,3 +185,4 @@ with col2:
|
|
| 195 |
if st.button("🧹 Clear History"):
|
| 196 |
st.session_state.history = []
|
| 197 |
st.success("Chat history cleared.")
|
|
|
|
|
|
| 9 |
import pytz
|
| 10 |
from streamlit_js_eval import get_geolocation
|
| 11 |
|
| 12 |
+
# ----------------------------------
|
| 13 |
+
# 🔐 Secrets (recommended)
|
| 14 |
+
# ----------------------------------
|
| 15 |
+
# Put this in .streamlit/secrets.toml:
|
| 16 |
+
# GROQ_API_KEY = "your_key_here"
|
| 17 |
+
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY", "YOUR_FALLBACK_KEY")
|
| 18 |
+
|
| 19 |
+
# 🚨 Rescue numbers dictionary
|
| 20 |
rescue_contacts = {
|
| 21 |
"Pakistan": {
|
| 22 |
"Ambulance": "115",
|
|
|
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
+
# ----------------------------------
|
| 42 |
+
# 🕒 Timezone helpers
|
| 43 |
+
# ----------------------------------
|
| 44 |
+
def get_timezone(lat: float, lon: float):
|
| 45 |
tf = TimezoneFinder()
|
| 46 |
+
tz_name = tf.timezone_at(lat=lat, lng=lon)
|
| 47 |
+
if not tz_name:
|
| 48 |
+
return pytz.utc
|
| 49 |
+
try:
|
| 50 |
+
return pytz.timezone(tz_name)
|
| 51 |
+
except Exception:
|
| 52 |
+
return pytz.utc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# ----------------------------------
|
| 55 |
+
# 🧠 Session setup
|
| 56 |
+
# ----------------------------------
|
| 57 |
if "history" not in st.session_state:
|
| 58 |
st.session_state.history = []
|
| 59 |
|
| 60 |
+
if "tz" not in st.session_state:
|
| 61 |
+
st.session_state.tz = pytz.utc # fallback until we detect it
|
| 62 |
+
|
| 63 |
+
# ----------------------------------
|
| 64 |
+
# 🧭 Page setup
|
| 65 |
+
# ----------------------------------
|
| 66 |
st.set_page_config(page_title="AI Disaster Assistant", layout="wide")
|
| 67 |
st.markdown("""
|
| 68 |
<style>
|
|
|
|
| 93 |
preferred_language = st.selectbox("🌐 Select your preferred language for responses:", list(language_options.keys()))
|
| 94 |
target_lang_code = language_options[preferred_language]
|
| 95 |
|
| 96 |
+
# ----------------------------------
|
| 97 |
+
# 📍 Get Location & Timezone
|
| 98 |
+
# ----------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
location_data = get_geolocation()
|
| 100 |
|
| 101 |
user_location = "Unknown"
|
| 102 |
city = ""
|
| 103 |
country = ""
|
|
|
|
| 104 |
|
| 105 |
if location_data and location_data.get("coords"):
|
| 106 |
lat = location_data["coords"]["latitude"]
|
|
|
|
| 114 |
user_location = f"{city}, {country}"
|
| 115 |
st.markdown(f"📌 You are in *{user_location}*")
|
| 116 |
|
| 117 |
+
# ✅ Store timezone in session state
|
| 118 |
+
st.session_state.tz = get_timezone(lat, lon)
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
+
st.warning("🌐 Could not resolve full address or timezone from GPS.")
|
| 122 |
+
else:
|
| 123 |
+
st.info("📡 Trying to detect your location... (allow location in your browser)")
|
| 124 |
|
| 125 |
# 🚨 Display rescue numbers
|
| 126 |
if country in rescue_contacts:
|
|
|
|
| 129 |
for service, number in contacts.items():
|
| 130 |
st.markdown(f"<div class='contact-box'>📞 <strong>{service}</strong>: {number}</div>", unsafe_allow_html=True)
|
| 131 |
|
| 132 |
+
# ----------------------------------
|
| 133 |
+
# 💬 UI
|
| 134 |
+
# ----------------------------------
|
| 135 |
col1, col2 = st.columns([2, 1])
|
| 136 |
|
| 137 |
with col1:
|
| 138 |
st.markdown("### 🧠 Ask your question (any language):")
|
| 139 |
+
user_input = st.text_area(" ", placeholder="e.g. زلزلے کے دوران مجھے کیا کرنا چاہیے؟ or What to do in a flood?", height=120)
|
| 140 |
|
| 141 |
if st.button("🔍 Get Help"):
|
| 142 |
if user_input.strip() != "":
|
|
|
|
| 161 |
translated_answer = GoogleTranslator(source='en', target=target_lang_code).translate(answer_en)
|
| 162 |
st.markdown(f"<div class='answer-box'>{translated_answer}</div>", unsafe_allow_html=True)
|
| 163 |
|
| 164 |
+
# ✅ Use the CURRENT time with the saved timezone
|
| 165 |
+
now = datetime.now(st.session_state.tz)
|
| 166 |
+
timestamp = now.strftime("%I:%M %p")
|
| 167 |
st.session_state.history.append({"time": timestamp, "question": user_input})
|
| 168 |
else:
|
| 169 |
st.error("❌ Error: " + response.text)
|
|
|
|
| 185 |
if st.button("🧹 Clear History"):
|
| 186 |
st.session_state.history = []
|
| 187 |
st.success("Chat history cleared.")
|
| 188 |
+
|