Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -230,110 +230,3 @@
|
|
| 230 |
# st.session_state.history = []
|
| 231 |
# st.success("Chat history cleared.")
|
| 232 |
|
| 233 |
-
|
| 234 |
-
import streamlit as st
|
| 235 |
-
import openai
|
| 236 |
-
from googletrans import Translator
|
| 237 |
-
from streamlit_js_eval import get_geolocation
|
| 238 |
-
from timezonefinder import TimezoneFinder
|
| 239 |
-
from datetime import datetime
|
| 240 |
-
import pytz
|
| 241 |
-
from geopy.geocoders import Nominatim
|
| 242 |
-
|
| 243 |
-
st.set_page_config(page_title="AI Disaster Assistant π", layout="centered")
|
| 244 |
-
|
| 245 |
-
# Session state setup
|
| 246 |
-
if 'tz' not in st.session_state:
|
| 247 |
-
st.session_state.tz = None
|
| 248 |
-
|
| 249 |
-
st.title("π AI Disaster Safety Assistant")
|
| 250 |
-
st.markdown("Your emergency translator and safety guide, wherever you are.")
|
| 251 |
-
|
| 252 |
-
# Get user's geolocation
|
| 253 |
-
loc = get_geolocation()
|
| 254 |
-
|
| 255 |
-
if loc:
|
| 256 |
-
lat = loc['coords']['latitude']
|
| 257 |
-
lon = loc['coords']['longitude']
|
| 258 |
-
st.success(f"β
GPS Coordinates detected: Latitude {lat}, Longitude {lon}")
|
| 259 |
-
|
| 260 |
-
try:
|
| 261 |
-
# Reverse geocoding
|
| 262 |
-
geolocator = Nominatim(user_agent="geoapiExercises")
|
| 263 |
-
location = geolocator.reverse((lat, lon), language='en')
|
| 264 |
-
if location:
|
| 265 |
-
address = location.raw.get('address', {})
|
| 266 |
-
city = (address.get('city') or address.get('town') or
|
| 267 |
-
address.get('village') or address.get('municipality') or '')
|
| 268 |
-
country = address.get('country', '')
|
| 269 |
-
|
| 270 |
-
if city:
|
| 271 |
-
user_location = f"{city}, {country}"
|
| 272 |
-
elif country:
|
| 273 |
-
user_location = f"{country}"
|
| 274 |
-
else:
|
| 275 |
-
user_location = "Unknown Location"
|
| 276 |
-
|
| 277 |
-
st.markdown(f"π You are in **{user_location}**")
|
| 278 |
-
|
| 279 |
-
# Timezone calculation
|
| 280 |
-
tf = TimezoneFinder()
|
| 281 |
-
timezone_str = tf.timezone_at(lng=lon, lat=lat)
|
| 282 |
-
if timezone_str:
|
| 283 |
-
tz = pytz.timezone(timezone_str)
|
| 284 |
-
local_time = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
|
| 285 |
-
st.session_state.tz = timezone_str
|
| 286 |
-
st.markdown(f"π Local time: `{local_time}`")
|
| 287 |
-
else:
|
| 288 |
-
st.warning("β οΈ Timezone could not be determined.")
|
| 289 |
-
else:
|
| 290 |
-
st.warning("β οΈ Could not find location details for the given coordinates.")
|
| 291 |
-
except Exception as e:
|
| 292 |
-
st.warning(f"π Error during location lookup: {e}")
|
| 293 |
-
else:
|
| 294 |
-
st.warning("π Since I couldn't determine your exact location, translation and timezone might be less accurate.")
|
| 295 |
-
|
| 296 |
-
# Language selector
|
| 297 |
-
language = st.selectbox(
|
| 298 |
-
"π Select your language",
|
| 299 |
-
("English", "Urdu", "Spanish", "French", "Arabic", "Chinese")
|
| 300 |
-
)
|
| 301 |
-
|
| 302 |
-
# Translator setup
|
| 303 |
-
translator = Translator()
|
| 304 |
-
|
| 305 |
-
# Input prompt
|
| 306 |
-
user_input = st.text_area("π¬ Describe your emergency or ask for help:", "")
|
| 307 |
-
|
| 308 |
-
if st.button("π¨ Get Safety Instructions"):
|
| 309 |
-
if user_input:
|
| 310 |
-
prompt = f"Give safety tips and emergency measures based on this situation: {user_input}. Be concise and helpful."
|
| 311 |
-
|
| 312 |
-
openai.api_key = st.secrets.get("GROQ_API_KEY", "your-api-key")
|
| 313 |
-
|
| 314 |
-
try:
|
| 315 |
-
from openai import OpenAI
|
| 316 |
-
client = OpenAI()
|
| 317 |
-
completion = client.chat.completions.create(
|
| 318 |
-
model="llama3-70b-8192",
|
| 319 |
-
messages=[
|
| 320 |
-
{"role": "system", "content": "You are a helpful assistant providing safety tips."},
|
| 321 |
-
{"role": "user", "content": prompt}
|
| 322 |
-
],
|
| 323 |
-
temperature=0.7
|
| 324 |
-
)
|
| 325 |
-
reply = completion.choices[0].message.content
|
| 326 |
-
|
| 327 |
-
# Translate if needed
|
| 328 |
-
if language != "English":
|
| 329 |
-
translated = translator.translate(reply, dest=language.lower())
|
| 330 |
-
st.markdown("### π‘οΈ Safety Tips (Translated):")
|
| 331 |
-
st.write(translated.text)
|
| 332 |
-
else:
|
| 333 |
-
st.markdown("### π‘οΈ Safety Tips:")
|
| 334 |
-
st.write(reply)
|
| 335 |
-
|
| 336 |
-
except Exception as e:
|
| 337 |
-
st.error(f"β Error generating response: {e}")
|
| 338 |
-
else:
|
| 339 |
-
st.warning("Please describe your emergency situation first.")
|
|
|
|
| 230 |
# st.session_state.history = []
|
| 231 |
# st.success("Chat history cleared.")
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|