Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from geopy.geocoders import Nominatim
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# API keys
|
| 10 |
+
GOOGLE_MAPS_API_KEY = os.getenv("GOOGLE_MAPS_API_KEY")
|
| 11 |
+
OPENWEATHER_API_KEY = os.getenv("OPENWEATHER_API_KEY")
|
| 12 |
+
NEWS_API_KEY = os.getenv("NEWS_API_KEY")
|
| 13 |
+
|
| 14 |
+
geolocator = Nominatim(user_agent="commute_planner")
|
| 15 |
+
|
| 16 |
+
st.title("🚗 Commute Planner with Live Weather, AQI & Traffic Updates")
|
| 17 |
+
|
| 18 |
+
source = st.text_input("Enter your current location:")
|
| 19 |
+
destination = st.text_input("Enter your destination:")
|
| 20 |
+
|
| 21 |
+
if st.button("Plan My Commute") and source and destination:
|
| 22 |
+
with st.spinner("Getting route info..."):
|
| 23 |
+
def get_coordinates(location):
|
| 24 |
+
try:
|
| 25 |
+
loc = geolocator.geocode(location)
|
| 26 |
+
return (loc.latitude, loc.longitude)
|
| 27 |
+
except:
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
src_coords = get_coordinates(source)
|
| 31 |
+
dest_coords = get_coordinates(destination)
|
| 32 |
+
|
| 33 |
+
if not src_coords or not dest_coords:
|
| 34 |
+
st.error("Could not get coordinates. Please check the location.")
|
| 35 |
+
else:
|
| 36 |
+
# 1. Estimated travel time using Google Maps Directions API
|
| 37 |
+
directions_url = "https://maps.googleapis.com/maps/api/directions/json"
|
| 38 |
+
directions_params = {
|
| 39 |
+
"origin": source,
|
| 40 |
+
"destination": destination,
|
| 41 |
+
"key": GOOGLE_MAPS_API_KEY,
|
| 42 |
+
}
|
| 43 |
+
route = requests.get(directions_url, params=directions_params).json()
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
duration = route["routes"][0]["legs"][0]["duration"]["text"]
|
| 47 |
+
st.success(f"Estimated Commute Time: {duration}")
|
| 48 |
+
except:
|
| 49 |
+
st.warning("Could not retrieve travel time.")
|
| 50 |
+
|
| 51 |
+
# 2. Weather and AQI using OpenWeatherMap
|
| 52 |
+
weather_url = "https://api.openweathermap.org/data/2.5/weather"
|
| 53 |
+
weather_params = {
|
| 54 |
+
"lat": src_coords[0],
|
| 55 |
+
"lon": src_coords[1],
|
| 56 |
+
"appid": OPENWEATHER_API_KEY,
|
| 57 |
+
"units": "metric"
|
| 58 |
+
}
|
| 59 |
+
weather_data = requests.get(weather_url, params=weather_params).json()
|
| 60 |
+
temp = weather_data.get("main", {}).get("temp", "N/A")
|
| 61 |
+
st.info(f"🌡️ Current Temperature: {temp}°C")
|
| 62 |
+
|
| 63 |
+
# AQI
|
| 64 |
+
aqi_url = "http://api.openweathermap.org/data/2.5/air_pollution"
|
| 65 |
+
aqi_params = {
|
| 66 |
+
"lat": src_coords[0],
|
| 67 |
+
"lon": src_coords[1],
|
| 68 |
+
"appid": OPENWEATHER_API_KEY
|
| 69 |
+
}
|
| 70 |
+
aqi_data = requests.get(aqi_url, params=aqi_params).json()
|
| 71 |
+
aqi = aqi_data.get("list", [{}])[0].get("main", {}).get("aqi", "N/A")
|
| 72 |
+
aqi_status = {
|
| 73 |
+
1: "Good",
|
| 74 |
+
2: "Fair",
|
| 75 |
+
3: "Moderate",
|
| 76 |
+
4: "Poor",
|
| 77 |
+
5: "Very Poor"
|
| 78 |
+
}
|
| 79 |
+
st.info(f"🌀 Air Quality Index: {aqi} - {aqi_status.get(aqi, 'Unknown')}")
|
| 80 |
+
|
| 81 |
+
# 3. News or traffic updates (using NewsAPI)
|
| 82 |
+
st.subheader("📰 Related Traffic News")
|
| 83 |
+
news_url = "https://newsapi.org/v2/everything"
|
| 84 |
+
query = f"{source} {destination} traffic OR accident OR construction"
|
| 85 |
+
news_params = {
|
| 86 |
+
"q": query,
|
| 87 |
+
"apiKey": NEWS_API_KEY,
|
| 88 |
+
"sortBy": "publishedAt",
|
| 89 |
+
"language": "en",
|
| 90 |
+
"pageSize": 5,
|
| 91 |
+
}
|
| 92 |
+
news_data = requests.get(news_url, params=news_params).json()
|
| 93 |
+
articles = news_data.get("articles", [])
|
| 94 |
+
if articles:
|
| 95 |
+
for article in articles:
|
| 96 |
+
st.markdown(f"[{article['title']}]({article['url']})")
|
| 97 |
+
else:
|
| 98 |
+
st.write("No recent traffic-related news found for your route.")
|