Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,13 @@ import os
|
|
| 7 |
from PIL import Image
|
| 8 |
import io
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def download_image(url):
|
| 11 |
"""Download image from URL and return as PIL Image."""
|
| 12 |
try:
|
|
@@ -73,9 +80,19 @@ def get_radar_images():
|
|
| 73 |
print(f"Error getting radar images: {str(e)}")
|
| 74 |
return None
|
| 75 |
|
| 76 |
-
def create_map():
|
| 77 |
-
"""Create a folium map centered on
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
m.add_child(folium.ClickForLatLng()) # Enable click events
|
| 80 |
return m._repr_html_()
|
| 81 |
|
|
@@ -96,8 +113,20 @@ def update_weather(lat, lon):
|
|
| 96 |
return forecast, None, None, None, create_map()
|
| 97 |
|
| 98 |
# Create updated map with marker
|
| 99 |
-
m = folium.Map(location=[lat, lon], zoom_start=
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
m.add_child(folium.ClickForLatLng()) # Enable click events
|
| 102 |
|
| 103 |
return (
|
|
@@ -112,25 +141,35 @@ def update_weather(lat, lon):
|
|
| 112 |
except Exception as e:
|
| 113 |
return f"Error: {str(e)}", None, None, None, create_map()
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Create Gradio interface
|
| 116 |
-
with gr.Blocks(title="
|
| 117 |
-
gr.Markdown("#
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
with gr.Column(scale=1):
|
| 121 |
lat_input = gr.Number(
|
| 122 |
label="Latitude",
|
| 123 |
-
value=
|
| 124 |
minimum=-90,
|
| 125 |
maximum=90
|
| 126 |
)
|
| 127 |
lon_input = gr.Number(
|
| 128 |
label="Longitude",
|
| 129 |
-
value=-
|
| 130 |
minimum=-180,
|
| 131 |
maximum=180
|
| 132 |
)
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
with gr.Column(scale=2):
|
| 136 |
map_display = gr.HTML(create_map())
|
|
@@ -139,7 +178,7 @@ with gr.Blocks(title="NOAA Weather Forecast and Radar") as demo:
|
|
| 139 |
forecast_output = gr.Textbox(
|
| 140 |
label="Forecast",
|
| 141 |
lines=12,
|
| 142 |
-
placeholder="
|
| 143 |
)
|
| 144 |
|
| 145 |
with gr.Row():
|
|
@@ -178,11 +217,35 @@ with gr.Blocks(title="NOAA Weather Forecast and Radar") as demo:
|
|
| 178 |
]
|
| 179 |
)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
gr.Markdown("""
|
| 182 |
## Instructions
|
| 183 |
-
1.
|
| 184 |
-
2.
|
| 185 |
-
3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
**Legend:**
|
| 188 |
- Current Radar: Shows current precipitation
|
|
@@ -190,7 +253,7 @@ with gr.Blocks(title="NOAA Weather Forecast and Radar") as demo:
|
|
| 190 |
- 12-Hour Forecast: Shows quantitative precipitation forecast for next 12 hours
|
| 191 |
|
| 192 |
**Note**: This app uses the NOAA Weather API and may have occasional delays or service interruptions.
|
| 193 |
-
|
| 194 |
""")
|
| 195 |
|
| 196 |
# For Hugging Face Spaces, we need to export the demo
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
import io
|
| 9 |
|
| 10 |
+
# Montana Mountain Peaks coordinates
|
| 11 |
+
MONTANA_PEAKS = {
|
| 12 |
+
"Lone Peak (Big Sky)": (45.27806, -111.45028), # 45°16′41″N 111°27′01″W
|
| 13 |
+
"Sacajawea Peak": (45.89583, -110.96861), # 45°53′45″N 110°58′7″W
|
| 14 |
+
"Pioneer Mountain": (45.231835, -111.450505) # 45°13′55″N 111°27′2″W
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
def download_image(url):
|
| 18 |
"""Download image from URL and return as PIL Image."""
|
| 19 |
try:
|
|
|
|
| 80 |
print(f"Error getting radar images: {str(e)}")
|
| 81 |
return None
|
| 82 |
|
| 83 |
+
def create_map(markers=None):
|
| 84 |
+
"""Create a folium map centered on Montana with optional markers."""
|
| 85 |
+
# Center on Montana
|
| 86 |
+
m = folium.Map(location=[45.5, -111.0], zoom_start=7)
|
| 87 |
+
|
| 88 |
+
# Add markers for Montana peaks
|
| 89 |
+
for peak_name, coords in MONTANA_PEAKS.items():
|
| 90 |
+
folium.Marker(
|
| 91 |
+
coords,
|
| 92 |
+
popup=f"{peak_name}<br>Lat: {coords[0]:.4f}, Lon: {coords[1]:.4f}",
|
| 93 |
+
tooltip=peak_name
|
| 94 |
+
).add_to(m)
|
| 95 |
+
|
| 96 |
m.add_child(folium.ClickForLatLng()) # Enable click events
|
| 97 |
return m._repr_html_()
|
| 98 |
|
|
|
|
| 113 |
return forecast, None, None, None, create_map()
|
| 114 |
|
| 115 |
# Create updated map with marker
|
| 116 |
+
m = folium.Map(location=[lat, lon], zoom_start=9)
|
| 117 |
+
|
| 118 |
+
# Add all Montana peaks
|
| 119 |
+
for peak_name, coords in MONTANA_PEAKS.items():
|
| 120 |
+
folium.Marker(
|
| 121 |
+
coords,
|
| 122 |
+
popup=f"{peak_name}<br>Lat: {coords[0]:.4f}, Lon: {coords[1]:.4f}",
|
| 123 |
+
tooltip=peak_name
|
| 124 |
+
).add_to(m)
|
| 125 |
+
|
| 126 |
+
# Add current location marker if not a peak
|
| 127 |
+
if (lat, lon) not in MONTANA_PEAKS.values():
|
| 128 |
+
folium.Marker([lat, lon], popup=f"Selected Location<br>Lat: {lat:.4f}, Lon: {lon:.4f}").add_to(m)
|
| 129 |
+
|
| 130 |
m.add_child(folium.ClickForLatLng()) # Enable click events
|
| 131 |
|
| 132 |
return (
|
|
|
|
| 141 |
except Exception as e:
|
| 142 |
return f"Error: {str(e)}", None, None, None, create_map()
|
| 143 |
|
| 144 |
+
def peak_button_click(peak_name):
|
| 145 |
+
"""Handle click events for Montana peak buttons."""
|
| 146 |
+
coords = MONTANA_PEAKS[peak_name]
|
| 147 |
+
return coords[0], coords[1]
|
| 148 |
+
|
| 149 |
# Create Gradio interface
|
| 150 |
+
with gr.Blocks(title="Montana Mountain Weather") as demo:
|
| 151 |
+
gr.Markdown("# Montana Mountain Weather")
|
| 152 |
|
| 153 |
with gr.Row():
|
| 154 |
with gr.Column(scale=1):
|
| 155 |
lat_input = gr.Number(
|
| 156 |
label="Latitude",
|
| 157 |
+
value=45.5,
|
| 158 |
minimum=-90,
|
| 159 |
maximum=90
|
| 160 |
)
|
| 161 |
lon_input = gr.Number(
|
| 162 |
label="Longitude",
|
| 163 |
+
value=-111.0,
|
| 164 |
minimum=-180,
|
| 165 |
maximum=180
|
| 166 |
)
|
| 167 |
+
|
| 168 |
+
# Quick access buttons for Montana peaks
|
| 169 |
+
gr.Markdown("### Quick Access - Montana Peaks")
|
| 170 |
+
peak_buttons = {name: gr.Button(f"📍 {name}") for name in MONTANA_PEAKS.keys()}
|
| 171 |
+
|
| 172 |
+
submit_btn = gr.Button("Get Weather", variant="primary")
|
| 173 |
|
| 174 |
with gr.Column(scale=2):
|
| 175 |
map_display = gr.HTML(create_map())
|
|
|
|
| 178 |
forecast_output = gr.Textbox(
|
| 179 |
label="Forecast",
|
| 180 |
lines=12,
|
| 181 |
+
placeholder="Select a location or mountain peak to see the forecast..."
|
| 182 |
)
|
| 183 |
|
| 184 |
with gr.Row():
|
|
|
|
| 217 |
]
|
| 218 |
)
|
| 219 |
|
| 220 |
+
# Handle peak button clicks
|
| 221 |
+
for peak_name, button in peak_buttons.items():
|
| 222 |
+
button.click(
|
| 223 |
+
fn=peak_button_click,
|
| 224 |
+
inputs=[],
|
| 225 |
+
outputs=[lat_input, lon_input],
|
| 226 |
+
kwargs={"peak_name": peak_name}
|
| 227 |
+
).then(
|
| 228 |
+
fn=update_weather,
|
| 229 |
+
inputs=[lat_input, lon_input],
|
| 230 |
+
outputs=[
|
| 231 |
+
forecast_output,
|
| 232 |
+
current_radar,
|
| 233 |
+
forecast_6hr,
|
| 234 |
+
forecast_12hr,
|
| 235 |
+
map_display
|
| 236 |
+
]
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
gr.Markdown("""
|
| 240 |
## Instructions
|
| 241 |
+
1. Use the quick access buttons to check specific Montana peaks
|
| 242 |
+
2. Or enter coordinates manually / click on the map
|
| 243 |
+
3. Click "Get Weather" to see the forecast and radar
|
| 244 |
+
|
| 245 |
+
**Montana Peaks Included:**
|
| 246 |
+
- Lone Peak (Big Sky): 45°16′41″N 111°27′01″W
|
| 247 |
+
- Sacajawea Peak: 45°53′45″N 110°58′7″W
|
| 248 |
+
- Pioneer Mountain: 45°13′55″N 111°27′2″W
|
| 249 |
|
| 250 |
**Legend:**
|
| 251 |
- Current Radar: Shows current precipitation
|
|
|
|
| 253 |
- 12-Hour Forecast: Shows quantitative precipitation forecast for next 12 hours
|
| 254 |
|
| 255 |
**Note**: This app uses the NOAA Weather API and may have occasional delays or service interruptions.
|
| 256 |
+
Mountain weather can change rapidly - always check multiple sources for safety.
|
| 257 |
""")
|
| 258 |
|
| 259 |
# For Hugging Face Spaces, we need to export the demo
|