Spaces:
Runtime error
Runtime error
PulkitT01 commited on
Commit ·
b7e84d4
1
Parent(s): 4a02599
Update so gradio calls forecast function, not ui. Eliminate ui function
Browse files
app.py
CHANGED
|
@@ -20,16 +20,16 @@ WEATHER_CODES = {
|
|
| 20 |
|
| 21 |
def get_weather_forecast_range(location_name, start_date, end_date):
|
| 22 |
"""
|
| 23 |
-
Fetches weather forecast
|
|
|
|
| 24 |
|
| 25 |
Parameters:
|
| 26 |
location_name (str): Name of the location (e.g., city or landmark).\n
|
| 27 |
-
start_date (str or date): Start date of the forecast
|
| 28 |
-
end_date (str or date): End date of the forecast
|
| 29 |
|
| 30 |
Returns:
|
| 31 |
-
|
| 32 |
-
or a dictionary with an "error" key if the process fails.\n
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
if isinstance(start_date, str):
|
|
@@ -41,12 +41,12 @@ def get_weather_forecast_range(location_name, start_date, end_date):
|
|
| 41 |
days_ahead = (end_date - today).days
|
| 42 |
|
| 43 |
if days_ahead > 15:
|
| 44 |
-
return
|
| 45 |
|
| 46 |
geolocator = Nominatim(user_agent="weather_api")
|
| 47 |
location = geolocator.geocode(location_name)
|
| 48 |
if not location:
|
| 49 |
-
return
|
| 50 |
lat, lon = location.latitude, location.longitude
|
| 51 |
|
| 52 |
include_hourly = days_ahead <= 6
|
|
@@ -69,7 +69,7 @@ def get_weather_forecast_range(location_name, start_date, end_date):
|
|
| 69 |
|
| 70 |
response = requests.get(url, params=params)
|
| 71 |
if response.status_code != 200:
|
| 72 |
-
return
|
| 73 |
raw = response.json()
|
| 74 |
|
| 75 |
forecasts = []
|
|
@@ -117,54 +117,35 @@ def get_weather_forecast_range(location_name, start_date, end_date):
|
|
| 117 |
|
| 118 |
forecasts.append(day_result)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
"""
|
| 137 |
-
result = get_weather_forecast_range(location_name, start_date, end_date)
|
| 138 |
-
|
| 139 |
-
if "error" in result:
|
| 140 |
-
return f"**⚠️ Error:** {result['error']}"
|
| 141 |
-
|
| 142 |
-
# Save JSON silently
|
| 143 |
-
save_dir = Path("logs")
|
| 144 |
-
save_dir.mkdir(exist_ok=True)
|
| 145 |
-
save_path = save_dir / f"weather_{location_name}_{start_date}_to_{end_date}.json"
|
| 146 |
-
with open(save_path, "w", encoding="utf-8") as f:
|
| 147 |
-
json.dump(result, f, indent=2)
|
| 148 |
-
|
| 149 |
-
# Generate Markdown summary
|
| 150 |
-
md = f"## Weather Forecast for {location_name}\n\n"
|
| 151 |
-
for day in result:
|
| 152 |
-
md += f"### 📅 {day['date']}\n"
|
| 153 |
-
md += f"- 🌅 Sunrise: {day['sunrise']}\n"
|
| 154 |
-
md += f"- 🌇 Sunset: {day['sunset']}\n"
|
| 155 |
-
md += f"- 🌡️ Temp: {day['temp_min']}°C – {day['temp_max']}°C\n"
|
| 156 |
-
md += f"- 🌤️ Weather: {day['weather']}\n"
|
| 157 |
-
md += f"- ☀️ UV Index Max: {day['uv_max']}\n"
|
| 158 |
-
|
| 159 |
-
if "hourly" in day:
|
| 160 |
-
md += f"**Hourly Forecast:**\n"
|
| 161 |
-
for h in day["hourly"]:
|
| 162 |
-
md += f" - {h['time']}: {h['temp']}, {h['weather']}, UV {h['uv']}, Visibility {h['visibility']}\n"
|
| 163 |
-
elif "note" in day:
|
| 164 |
-
md += f"_{day['note']}_\n"
|
| 165 |
-
md += "\n"
|
| 166 |
-
|
| 167 |
-
return md
|
| 168 |
|
| 169 |
with gr.Blocks(title="TravAI Weather Forecast", theme=gr.themes.Soft(primary_hue="indigo")) as demo:
|
| 170 |
gr.Markdown("""
|
|
@@ -182,7 +163,7 @@ with gr.Blocks(title="TravAI Weather Forecast", theme=gr.themes.Soft(primary_hue
|
|
| 182 |
|
| 183 |
output = gr.Markdown(label="📜 Forecast Output")
|
| 184 |
|
| 185 |
-
submit_btn.click(fn=
|
| 186 |
inputs=[location, start, end],
|
| 187 |
outputs=output)
|
| 188 |
|
|
|
|
| 20 |
|
| 21 |
def get_weather_forecast_range(location_name, start_date, end_date):
|
| 22 |
"""
|
| 23 |
+
Fetches weather forecast for a given location and date range, returns a Markdown summary.
|
| 24 |
+
Also saves the forecast as a JSON file under the 'logs' directory.
|
| 25 |
|
| 26 |
Parameters:
|
| 27 |
location_name (str): Name of the location (e.g., city or landmark).\n
|
| 28 |
+
start_date (str or date): Start date of the forecast in 'YYYY-MM-DD' format or as a date object.\n
|
| 29 |
+
end_date (str or date): End date of the forecast in 'YYYY-MM-DD' format or as a date object.\n
|
| 30 |
|
| 31 |
Returns:
|
| 32 |
+
str: Markdown-formatted string with weather details or an error message.\n
|
|
|
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
if isinstance(start_date, str):
|
|
|
|
| 41 |
days_ahead = (end_date - today).days
|
| 42 |
|
| 43 |
if days_ahead > 15:
|
| 44 |
+
return f"**⚠️ Error:** Weather data only available up to 15 days from today."
|
| 45 |
|
| 46 |
geolocator = Nominatim(user_agent="weather_api")
|
| 47 |
location = geolocator.geocode(location_name)
|
| 48 |
if not location:
|
| 49 |
+
return f"**⚠️ Error:** Could not find coordinates for '{location_name}'."
|
| 50 |
lat, lon = location.latitude, location.longitude
|
| 51 |
|
| 52 |
include_hourly = days_ahead <= 6
|
|
|
|
| 69 |
|
| 70 |
response = requests.get(url, params=params)
|
| 71 |
if response.status_code != 200:
|
| 72 |
+
return f"**⚠️ Error:** API error {response.status_code}: {response.text}"
|
| 73 |
raw = response.json()
|
| 74 |
|
| 75 |
forecasts = []
|
|
|
|
| 117 |
|
| 118 |
forecasts.append(day_result)
|
| 119 |
|
| 120 |
+
# Save the forecast JSON
|
| 121 |
+
save_dir = Path("logs")
|
| 122 |
+
save_dir.mkdir(exist_ok=True)
|
| 123 |
+
save_path = save_dir / f"weather_{location_name}_{start_date}_to_{end_date}.json"
|
| 124 |
+
with open(save_path, "w", encoding="utf-8") as f:
|
| 125 |
+
json.dump(forecasts, f, indent=2)
|
| 126 |
+
|
| 127 |
+
# Generate Markdown output
|
| 128 |
+
md = f"## Weather Forecast for {location_name}\n\n"
|
| 129 |
+
for day in forecasts:
|
| 130 |
+
md += f"### 📅 {day['date']}\n"
|
| 131 |
+
md += f"- 🌅 Sunrise: {day['sunrise']}\n"
|
| 132 |
+
md += f"- 🌇 Sunset: {day['sunset']}\n"
|
| 133 |
+
md += f"- 🌡️ Temp: {day['temp_min']}°C – {day['temp_max']}°C\n"
|
| 134 |
+
md += f"- 🌤️ Weather: {day['weather']}\n"
|
| 135 |
+
md += f"- ☀️ UV Index Max: {day['uv_max']}\n"
|
| 136 |
+
|
| 137 |
+
if "hourly" in day:
|
| 138 |
+
md += f"**Hourly Forecast:**\n"
|
| 139 |
+
for h in day["hourly"]:
|
| 140 |
+
md += f" - {h['time']}: {h['temp']}, {h['weather']}, UV {h['uv']}, Visibility {h['visibility']}\n"
|
| 141 |
+
elif "note" in day:
|
| 142 |
+
md += f"_{day['note']}_\n"
|
| 143 |
+
md += "\n"
|
| 144 |
+
|
| 145 |
+
return md
|
| 146 |
|
| 147 |
+
except Exception as e:
|
| 148 |
+
return f"**⚠️ Error:** {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
with gr.Blocks(title="TravAI Weather Forecast", theme=gr.themes.Soft(primary_hue="indigo")) as demo:
|
| 151 |
gr.Markdown("""
|
|
|
|
| 163 |
|
| 164 |
output = gr.Markdown(label="📜 Forecast Output")
|
| 165 |
|
| 166 |
+
submit_btn.click(fn=get_weather_forecast_range,
|
| 167 |
inputs=[location, start, end],
|
| 168 |
outputs=output)
|
| 169 |
|