Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import folium
|
4 |
-
from datetime import datetime
|
5 |
import tempfile
|
6 |
import os
|
7 |
|
@@ -11,12 +11,12 @@ def get_noaa_forecast(lat, lon):
|
|
11 |
|
12 |
try:
|
13 |
# Get the forecast URL for the coordinates
|
14 |
-
response = requests.get(points_url)
|
15 |
response.raise_for_status()
|
16 |
forecast_url = response.json()['properties']['forecast']
|
17 |
|
18 |
# Get the actual forecast
|
19 |
-
forecast_response = requests.get(forecast_url)
|
20 |
forecast_response.raise_for_status()
|
21 |
forecast_data = forecast_response.json()
|
22 |
|
@@ -29,83 +29,149 @@ def get_noaa_forecast(lat, lon):
|
|
29 |
forecast_text += f"Wind: {period['windSpeed']} {period['windDirection']}\n"
|
30 |
forecast_text += f"{period['detailedForecast']}\n"
|
31 |
|
32 |
-
return forecast_text
|
33 |
except requests.exceptions.RequestException as e:
|
34 |
return f"Error fetching forecast: {str(e)}"
|
35 |
|
36 |
-
def
|
37 |
-
"""Get
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
def create_map():
|
46 |
"""Create a folium map centered on the US."""
|
47 |
m = folium.Map(location=[39.8283, -98.5795], zoom_start=4)
|
|
|
48 |
return m._repr_html_()
|
49 |
|
50 |
-
def update_weather(lat, lon
|
51 |
"""Update weather information based on coordinates."""
|
52 |
-
|
53 |
-
|
54 |
-
lat
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
# Create Gradio interface
|
67 |
-
with gr.Blocks() as demo:
|
68 |
gr.Markdown("# NOAA Weather Forecast and Radar")
|
69 |
|
70 |
with gr.Row():
|
71 |
-
with gr.Column():
|
72 |
-
lat_input = gr.Number(
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
submit_btn = gr.Button("Get Weather")
|
75 |
|
76 |
-
with gr.Column():
|
77 |
map_display = gr.HTML(create_map())
|
78 |
-
map_click_coords = gr.State(None)
|
79 |
|
80 |
with gr.Row():
|
81 |
-
forecast_output = gr.Textbox(
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Handle submit button click
|
85 |
submit_btn.click(
|
86 |
fn=update_weather,
|
87 |
-
inputs=[lat_input, lon_input
|
88 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
)
|
90 |
|
91 |
-
# Handle map clicks
|
92 |
-
# Note: This would require additional JavaScript to capture click events
|
93 |
-
# In a full implementation, you'd need to add custom JS to capture map clicks
|
94 |
-
|
95 |
gr.Markdown("""
|
96 |
## Instructions
|
97 |
-
1. Enter coordinates manually or click on the map
|
98 |
2. Click "Get Weather" to see the forecast and radar
|
99 |
-
3.
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
Note
|
102 |
-
|
103 |
-
- Error handling for invalid coordinates
|
104 |
-
- Rate limiting for API requests
|
105 |
-
- Better map interaction
|
106 |
""")
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
demo.launch()
|
111 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import folium
|
4 |
+
from datetime import datetime, timedelta
|
5 |
import tempfile
|
6 |
import os
|
7 |
|
|
|
11 |
|
12 |
try:
|
13 |
# Get the forecast URL for the coordinates
|
14 |
+
response = requests.get(points_url, timeout=10)
|
15 |
response.raise_for_status()
|
16 |
forecast_url = response.json()['properties']['forecast']
|
17 |
|
18 |
# Get the actual forecast
|
19 |
+
forecast_response = requests.get(forecast_url, timeout=10)
|
20 |
forecast_response.raise_for_status()
|
21 |
forecast_data = forecast_response.json()
|
22 |
|
|
|
29 |
forecast_text += f"Wind: {period['windSpeed']} {period['windDirection']}\n"
|
30 |
forecast_text += f"{period['detailedForecast']}\n"
|
31 |
|
32 |
+
return forecast_text.strip()
|
33 |
except requests.exceptions.RequestException as e:
|
34 |
return f"Error fetching forecast: {str(e)}"
|
35 |
|
36 |
+
def get_radar_images():
|
37 |
+
"""Get current radar and forecast radar images."""
|
38 |
+
try:
|
39 |
+
# Current radar
|
40 |
+
current_radar = "https://radar.weather.gov/ridge/standard/CONUS_0.gif"
|
41 |
+
|
42 |
+
# Forecast radar - using NDFD precipitation forecast images
|
43 |
+
# These are probability of precipitation forecasts for different time periods
|
44 |
+
now = datetime.utcnow()
|
45 |
+
forecast_date = now.strftime("%Y%m%d")
|
46 |
+
|
47 |
+
# 6-hour and 12-hour precipitation probability forecasts
|
48 |
+
forecast_6hr = f"https://digital.weather.gov/images/conus/MaxPcpn6_{forecast_date}00.png"
|
49 |
+
forecast_12hr = f"https://digital.weather.gov/images/conus/MaxPcpn12_{forecast_date}00.png"
|
50 |
+
|
51 |
+
return {
|
52 |
+
"current": current_radar,
|
53 |
+
"forecast_6hr": forecast_6hr,
|
54 |
+
"forecast_12hr": forecast_12hr
|
55 |
+
}
|
56 |
+
except Exception as e:
|
57 |
+
return None
|
58 |
|
59 |
def create_map():
|
60 |
"""Create a folium map centered on the US."""
|
61 |
m = folium.Map(location=[39.8283, -98.5795], zoom_start=4)
|
62 |
+
m.add_child(folium.ClickForLatLng()) # Enable click events
|
63 |
return m._repr_html_()
|
64 |
|
65 |
+
def update_weather(lat, lon):
|
66 |
"""Update weather information based on coordinates."""
|
67 |
+
try:
|
68 |
+
# Validate coordinates
|
69 |
+
lat = float(lat)
|
70 |
+
lon = float(lon)
|
71 |
+
if not (-90 <= lat <= 90 and -180 <= lon <= 180):
|
72 |
+
return "Invalid coordinates. Latitude must be between -90 and 90, longitude between -180 and 180.", None, None, None, create_map()
|
73 |
+
|
74 |
+
# Get forecast and radar
|
75 |
+
forecast = get_noaa_forecast(lat, lon)
|
76 |
+
radar_images = get_radar_images()
|
77 |
+
|
78 |
+
if radar_images is None:
|
79 |
+
return forecast, None, None, None, create_map()
|
80 |
+
|
81 |
+
# Create updated map with marker
|
82 |
+
m = folium.Map(location=[lat, lon], zoom_start=8)
|
83 |
+
folium.Marker([lat, lon], popup=f"Lat: {lat:.4f}, Lon: {lon:.4f}").add_to(m)
|
84 |
+
m.add_child(folium.ClickForLatLng()) # Enable click events
|
85 |
+
|
86 |
+
return (
|
87 |
+
forecast,
|
88 |
+
radar_images["current"],
|
89 |
+
radar_images["forecast_6hr"],
|
90 |
+
radar_images["forecast_12hr"],
|
91 |
+
m._repr_html_()
|
92 |
+
)
|
93 |
+
except ValueError as e:
|
94 |
+
return f"Error: Invalid coordinate format - {str(e)}", None, None, None, create_map()
|
95 |
+
except Exception as e:
|
96 |
+
return f"Error: {str(e)}", None, None, None, create_map()
|
97 |
|
98 |
# Create Gradio interface
|
99 |
+
with gr.Blocks(title="NOAA Weather Forecast and Radar") as demo:
|
100 |
gr.Markdown("# NOAA Weather Forecast and Radar")
|
101 |
|
102 |
with gr.Row():
|
103 |
+
with gr.Column(scale=1):
|
104 |
+
lat_input = gr.Number(
|
105 |
+
label="Latitude",
|
106 |
+
value=39.8283,
|
107 |
+
minimum=-90,
|
108 |
+
maximum=90
|
109 |
+
)
|
110 |
+
lon_input = gr.Number(
|
111 |
+
label="Longitude",
|
112 |
+
value=-98.5795,
|
113 |
+
minimum=-180,
|
114 |
+
maximum=180
|
115 |
+
)
|
116 |
submit_btn = gr.Button("Get Weather")
|
117 |
|
118 |
+
with gr.Column(scale=2):
|
119 |
map_display = gr.HTML(create_map())
|
|
|
120 |
|
121 |
with gr.Row():
|
122 |
+
forecast_output = gr.Textbox(
|
123 |
+
label="Forecast",
|
124 |
+
lines=12,
|
125 |
+
placeholder="Enter coordinates or click 'Get Weather' to see the forecast..."
|
126 |
+
)
|
127 |
+
|
128 |
+
with gr.Row():
|
129 |
+
with gr.Column():
|
130 |
+
current_radar = gr.Image(
|
131 |
+
label="Current Radar",
|
132 |
+
show_label=True,
|
133 |
+
container=True
|
134 |
+
)
|
135 |
+
with gr.Column():
|
136 |
+
forecast_6hr = gr.Image(
|
137 |
+
label="6-Hour Precipitation Forecast",
|
138 |
+
show_label=True,
|
139 |
+
container=True
|
140 |
+
)
|
141 |
+
with gr.Column():
|
142 |
+
forecast_12hr = gr.Image(
|
143 |
+
label="12-Hour Precipitation Forecast",
|
144 |
+
show_label=True,
|
145 |
+
container=True
|
146 |
+
)
|
147 |
|
148 |
# Handle submit button click
|
149 |
submit_btn.click(
|
150 |
fn=update_weather,
|
151 |
+
inputs=[lat_input, lon_input],
|
152 |
+
outputs=[
|
153 |
+
forecast_output,
|
154 |
+
current_radar,
|
155 |
+
forecast_6hr,
|
156 |
+
forecast_12hr,
|
157 |
+
map_display
|
158 |
+
]
|
159 |
)
|
160 |
|
|
|
|
|
|
|
|
|
161 |
gr.Markdown("""
|
162 |
## Instructions
|
163 |
+
1. Enter coordinates manually or click on the map to select a location
|
164 |
2. Click "Get Weather" to see the forecast and radar
|
165 |
+
3. View current radar and precipitation forecasts
|
166 |
+
|
167 |
+
**Legend:**
|
168 |
+
- Current Radar: Shows current precipitation
|
169 |
+
- 6-Hour Forecast: Shows probability of precipitation over next 6 hours
|
170 |
+
- 12-Hour Forecast: Shows probability of precipitation over next 12 hours
|
171 |
|
172 |
+
**Note**: This app uses the NOAA Weather API and may have occasional delays or service interruptions.
|
173 |
+
Data is provided by the National Weather Service (weather.gov).
|
|
|
|
|
|
|
174 |
""")
|
175 |
|
176 |
+
# For Hugging Face Spaces, we need to export the demo
|
177 |
+
demo.queue(concurrency_count=3).launch()
|
|
|
|