Spaces:
Sleeping
Sleeping
File size: 10,400 Bytes
520323c 23cd211 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
from dotenv import load_dotenv
import os
import googlemaps
import re
import gradio as gr
from datetime import datetime
import requests
from geopy.geocoders import Nominatim
from datetime import datetime, date, timedelta
import pandas as pd
import json
from pathlib import Path
dotenv_path = os.getenv('DOTENV_PATH', None)
if dotenv_path:
load_dotenv(dotenv_path)
else:
load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")
if not api_key:
raise EnvironmentError("GOOGLE_API_KEY not found in environment variables.")
gmaps = googlemaps.Client(key=api_key)
def get_directions(
source: str,
destination: str,
mode: str,
departure_time: str):
"""
Returns a Markdown-formatted string with step-by-step directions and a static map URL.
Args:
source: Starting address or place name.
destination: Ending address or place name.
mode: Transport mode: driving, walking, bicycling, or transit.
departure_time: Trip start time in "YYYY-MM-DD HH:MM" format.
waypoints: Optional comma-separated list of waypoints.
Returns:
A single Markdown string summarizing the route or an error message.
"""
# Parse departure time
try:
dt = datetime.strptime(departure_time.strip(), "%Y-%m-%d %H:%M")
except ValueError:
return (
"**⚠️ Invalid time format.** Please enter departure time as YYYY-MM-DD HH:MM."
)
# Build waypoints list
wp_list = None
# Call Directions API
directions = gmaps.directions(
origin=source,
destination=destination,
mode=mode.lower(),
departure_time=dt,
waypoints=wp_list,
optimize_waypoints=True,
)
if not directions:
return "**No route found.**"
# Use first route & leg
route = directions[0]
leg = route["legs"][0]
# Build summary
summary = f"**Trip from '{leg['start_address']}' to '{leg['end_address']}'**\n"
summary += f"- Total distance: {leg['distance']['text']}\n"
summary += f"- Estimated duration: {leg['duration']['text']}\n\n"
summary += "### Step-by-Step Directions:\n"
for i, step in enumerate(leg["steps"], start=1):
travel_mode = step.get("travel_mode", "").upper()
if travel_mode == "TRANSIT":
details = step.get("transit_details", {})
line = details.get("line", {})
vehicle = line.get("vehicle", {}).get("type", "Transit")
line_name = line.get("short_name") or line.get("name", "")
dep = details.get("departure_stop", {}).get("name", "")
arr = details.get("arrival_stop", {}).get("name", "")
stops = details.get("num_stops", "")
instr = f"Take {vehicle} {line_name} from {dep} to {arr} ({stops} stops)"
dist = step.get("distance", {}).get("text", "")
dur = details.get("departure_time",{}).get("text","")
else:
raw = step.get("html_instructions", "")
instr = re.sub(r"<[^>]+>", "", raw)
dist = step.get("distance", {}).get("text", "")
dur = step.get("departure_time",{}).get("text","")
summary += f"{i}. {instr} ({dist},{dur})\n"
# Static map snapshot
poly = route.get("overview_polyline", {}).get("points")
if poly:
static_map_url = (
"https://maps.googleapis.com/maps/api/staticmap"
f"?size=600x400&path=enc:{poly}&key={api_key}"
)
summary += "\n---\n"
summary += ("Here’s a **static map snapshot** of this route:\n" + static_map_url)
return summary
WEATHER_CODES = {
0: "Clear sky", 1: "Mainly clear", 2: "Partly cloudy", 3: "Overcast",
45: "Fog", 48: "Depositing rime fog", 51: "Light drizzle", 53: "Moderate drizzle", 55: "Dense drizzle",
61: "Slight rain", 63: "Moderate rain", 65: "Heavy rain", 71: "Slight snowfall", 73: "Moderate snowfall",
75: "Heavy snowfall", 80: "Slight rain showers", 81: "Moderate rain showers", 82: "Violent rain showers",
95: "Thunderstorm", 96: "Thunderstorm with hail"
}
def get_weather_forecast_range(location_name, start_date, end_date):
try:
if isinstance(start_date, str):
start_date = datetime.strptime(start_date, "%Y-%m-%d").date()
if isinstance(end_date, str):
end_date = datetime.strptime(end_date, "%Y-%m-%d").date()
today = date.today()
days_ahead = (end_date - today).days
if days_ahead > 15:
return {"error": "Weather data only available up to 15 days from today."}
geolocator = Nominatim(user_agent="weather_api")
location = geolocator.geocode(location_name)
if not location:
return {"error": f"Could not find coordinates for '{location_name}'."}
lat, lon = location.latitude, location.longitude
include_hourly = days_ahead <= 6
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": lat,
"longitude": lon,
"daily": "sunrise,sunset,uv_index_max,temperature_2m_max,temperature_2m_min,weather_code",
"temperature_unit": "celsius",
"windspeed_unit": "kmh",
"timeformat": "iso8601",
"timezone": "auto",
"start_date": start_date.isoformat(),
"end_date": end_date.isoformat()
}
if include_hourly:
params["hourly"] = "temperature_2m,weather_code,uv_index,visibility"
response = requests.get(url, params=params)
if response.status_code != 200:
return {"error": f"API error {response.status_code}: {response.text}"}
raw = response.json()
forecasts = []
for idx, d in enumerate(raw["daily"]["time"]):
day_result = {
"date": d,
"sunrise": raw["daily"]["sunrise"][idx].split("T")[1],
"sunset": raw["daily"]["sunset"][idx].split("T")[1],
"uv_max": round(raw["daily"]["uv_index_max"][idx], 1),
"temp_min": round(raw["daily"]["temperature_2m_min"][idx]),
"temp_max": round(raw["daily"]["temperature_2m_max"][idx]),
"weather": WEATHER_CODES.get(int(raw["daily"]["weather_code"][idx]), "Unknown")
}
if include_hourly and "hourly" in raw:
hourly_df = pd.DataFrame({
"time": raw["hourly"]["time"],
"temp": raw["hourly"]["temperature_2m"],
"code": raw["hourly"]["weather_code"],
"uv": raw["hourly"]["uv_index"],
"visibility": [v / 1000 for v in raw["hourly"]["visibility"]]
})
hourly_df["time"] = pd.to_datetime(hourly_df["time"])
target_date = datetime.strptime(d, "%Y-%m-%d").date()
df_day = hourly_df[hourly_df["time"].dt.date == target_date]
df_day["weather"] = df_day["code"].apply(lambda c: WEATHER_CODES.get(int(c), "Unknown"))
day_result["hourly"] = [
{
"time": t.strftime("%Y-%m-%d %H:%M"),
"temp": f"{round(temp)}°C",
"weather": w,
"uv": round(uv, 1),
"visibility": f"{round(vis, 1)} km"
}
for t, temp, w, uv, vis in zip(
df_day["time"], df_day["temp"], df_day["weather"],
df_day["uv"], df_day["visibility"]
)
]
else:
day_result["note"] = "Hourly weather data is only available for the next 7 days."
forecasts.append(day_result)
return forecasts
except Exception as e:
return {"error": str(e)}
def get_weather_forecast_range_ui(location_name, start_date, end_date):
result = get_weather_forecast_range(location_name, start_date, end_date)
if "error" in result:
return f"**⚠️ Error:** {result['error']}"
# Save JSON silently
save_dir = Path("logs")
save_dir.mkdir(exist_ok=True)
save_path = save_dir / f"weather_{location_name}_{start_date}_to_{end_date}.json"
with open(save_path, "w", encoding="utf-8") as f:
json.dump(result, f, indent=2)
# Generate Markdown summary
md = f"## Weather Forecast for {location_name}\n\n"
for day in result:
md += f"### 📅 {day['date']}\n"
md += f"- 🌅 Sunrise: {day['sunrise']}\n"
md += f"- 🌇 Sunset: {day['sunset']}\n"
md += f"- 🌡️ Temp: {day['temp_min']}°C – {day['temp_max']}°C\n"
md += f"- 🌤️ Weather: {day['weather']}\n"
md += f"- ☀️ UV Index Max: {day['uv_max']}\n"
if "hourly" in day:
md += f"**Hourly Forecast:**\n"
for h in day["hourly"]:
md += f" - {h['time']}: {h['temp']}, {h['weather']}, UV {h['uv']}, Visibility {h['visibility']}\n"
elif "note" in day:
md += f"_{day['note']}_\n"
md += "\n"
return md
demo = gr.TabbedInterface(
[
gr.Interface(
fn=get_directions,
inputs=[
gr.Textbox(label="Source Location", placeholder="e.g. New York City"),
gr.Textbox(label="Destination Location", placeholder="e.g. Los Angeles"),
gr.Radio(["driving", "walking", "bicycling", "transit"], label="Travel Mode", value="driving"),
gr.Textbox(label="Departure Time (YYYY-MM-DD HH:MM)", placeholder="e.g. 2025-06-08 14:30"),
],
outputs=gr.Markdown(label="Directions Summary"),
api_name="get direction"
),
gr.Interface(
fn=get_weather_forecast_range_ui,
inputs=[
gr.Textbox(label="Location Name", placeholder="Enter a city or place name"),
gr.Textbox(label="Start Date (YYYY-MM-DD)", value=date.today()),
gr.Textbox(label="End Date (YYYY-MM-DD)", value=date.today() + timedelta(days=6)),
],
outputs=gr.Markdown(label="Readable Forecast"),
api_name="get weather forecast"
)
],
tab_names=["Google Maps Directions", "Weather Forecast"]
)
if __name__ == "__main__":
demo.launch(mcp_server=True) |