Spaces:
Sleeping
Sleeping
File size: 10,547 Bytes
9c047e2 fbbf258 9e7f401 9c047e2 9e7f401 9c047e2 9e7f401 9c047e2 9e7f401 9c047e2 9e7f401 f32f693 9c047e2 d81cdbb 9e7f401 d81cdbb 9e7f401 d81cdbb 9c047e2 9e7f401 d81cdbb 9e7f401 9c047e2 9e7f401 9c047e2 9e7f401 9c047e2 9e7f401 9c047e2 9e7f401 3317091 9e7f401 8d978a8 9e7f401 9c047e2 9e7f401 9c047e2 9e7f401 9c047e2 f32f693 9e7f401 f32f693 9e7f401 fbbf258 9e7f401 fbbf258 9e7f401 fbbf258 9e7f401 fbbf258 9e7f401 fbbf258 9c047e2 9e7f401 |
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
import gradio as gr
import requests
import folium
import os
import json
from datetime import datetime, timedelta
import pandas as pd
# Get API key from environment
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
def create_map(lat=40.7128, lon=-74.0060):
"""Create a Folium map centered at given coordinates"""
m = folium.Map(
location=[lat, lon],
zoom_start=10,
tiles="OpenStreetMap"
)
# Add a marker for the selected location
folium.Marker(
[lat, lon],
popup=f"Selected Location: {lat:.4f}, {lon:.4f}",
tooltip="Click to select this location",
icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)
return m._repr_html_()
def get_pollen_data(lat, lon, days=5):
"""Fetch pollen data from Google Pollen API"""
if not GOOGLE_API_KEY:
return "Error: Google API key not found. Please set GOOGLE_API_KEY as a secret."
# Google Pollen API endpoint
url = "https://pollen.googleapis.com/v1/forecast:lookup"
# Calculate date range
start_date = datetime.now()
end_date = start_date + timedelta(days=days-1)
params = {
"key": GOOGLE_API_KEY,
"location.longitude": lon,
"location.latitude": lat,
"days": days,
"plantsDescription": True
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
# Debug: Print the structure to understand the API response
print(f"API Response keys: {data.keys()}")
if "dailyInfo" in data and len(data["dailyInfo"]) > 0:
print(f"First day info keys: {data['dailyInfo'][0].keys()}")
print(f"Date structure: {data['dailyInfo'][0].get('date', 'No date field')}")
return data
except requests.exceptions.RequestException as e:
error_msg = f"Error fetching pollen data: {str(e)}"
if hasattr(e, 'response') and e.response is not None:
error_msg += f" (Status: {e.response.status_code})"
return error_msg
except json.JSONDecodeError as e:
return f"Error parsing response: {str(e)}"
except Exception as e:
return f"Unexpected error: {str(e)}"
def format_pollen_data(data):
"""Format pollen data for display"""
if isinstance(data, str): # Error message
return data, None
if "dailyInfo" not in data:
return "No pollen data available for this location.", None
# Create formatted output
output = []
output.append("# πΈ Pollen Forecast Report\n")
# Location info if available
if "regionInfo" in data:
region = data["regionInfo"]
output.append(f"**Location:** {region.get('displayName', 'Unknown')}\n")
# Daily pollen data
daily_data = []
for day_info in data["dailyInfo"]:
# Handle different date formats from the API
date_info = day_info.get("date", {})
if isinstance(date_info, dict):
# If date is a dict, try to extract the date string
if "year" in date_info and "month" in date_info and "day" in date_info:
year = date_info["year"]
month = date_info["month"]
day = date_info["day"]
date_obj = datetime(year, month, day)
else:
# Fallback to today's date if we can't parse
date_obj = datetime.now()
elif isinstance(date_info, str):
# If it's already a string, parse it
try:
date_obj = datetime.strptime(date_info, "%Y-%m-%d")
except ValueError:
date_obj = datetime.now()
else:
# Fallback
date_obj = datetime.now()
formatted_date = date_obj.strftime("%B %d, %Y")
output.append(f"## π
{formatted_date}\n")
if "pollenTypeInfo" in day_info:
pollen_types = day_info["pollenTypeInfo"]
# Create a row for the dataframe
row_data = {"Date": formatted_date}
for pollen in pollen_types:
pollen_type = pollen["code"].replace("_", " ").title()
index_info = pollen.get("indexInfo", {})
index_value = index_info.get("value", "N/A")
category = index_info.get("category", "Unknown")
# Add color coding based on category
color_map = {
"VERY_LOW": "π’",
"LOW": "π‘",
"MEDIUM": "π ",
"HIGH": "π΄",
"VERY_HIGH": "π£"
}
color = color_map.get(category, "βͺ")
output.append(f"**{pollen_type}:** {color} {category} (Index: {index_value})")
row_data[pollen_type] = f"{category} ({index_value})"
# Add plant descriptions if available
if "plantDescription" in pollen:
plants = pollen["plantDescription"]
if "plants" in plants:
plant_list = [plant["displayName"] for plant in plants["plants"][:3]] # Show top 3
output.append(f" - *Main sources: {', '.join(plant_list)}*")
output.append("")
daily_data.append(row_data)
output.append("---\n")
# Create DataFrame for tabular view
df = None
if daily_data:
df = pd.DataFrame(daily_data)
# Add legend
output.append("\n## π Pollen Index Legend")
output.append("π’ Very Low | π‘ Low | π Medium | π΄ High | π£ Very High\n")
# Add tips
output.append("## π‘ Tips")
output.append("- Check pollen levels before outdoor activities")
output.append("- Take allergy medications during high pollen days")
output.append("- Keep windows closed during peak pollen times")
output.append("- Shower and change clothes after being outdoors")
return "\n".join(output), df
def update_location(lat, lon):
"""Update the map and fetch pollen data for new location"""
if lat is None or lon is None:
return create_map(), "Please select a location on the map.", None
# Create new map
new_map = create_map(lat, lon)
# Get pollen data
pollen_data = get_pollen_data(lat, lon)
formatted_data, df = format_pollen_data(pollen_data)
return new_map, formatted_data, df
# Create the Gradio interface
with gr.Blocks(title="πΈ Pollen Forecast Map", theme=gr.themes.Soft()) as app:
gr.HTML("""
<div style="text-align: center; padding: 20px;">
<h1>πΈ Pollen Forecast Map</h1>
<p>Click on the map to select a location and get detailed pollen forecasts powered by Google Pollen API</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.HTML("<h3>π Location Selection</h3>")
# Coordinate inputs
lat_input = gr.Number(
label="Latitude",
value=40.7128,
precision=6,
info="Enter latitude or click on map"
)
lon_input = gr.Number(
label="Longitude",
value=-74.0060,
precision=6,
info="Enter longitude or click on map"
)
update_btn = gr.Button("π Update Location", variant="primary")
# Preset locations
gr.HTML("<h4>π Quick Locations</h4>")
with gr.Row():
nyc_btn = gr.Button("ποΈ NYC", size="sm")
la_btn = gr.Button("π΄ LA", size="sm")
chicago_btn = gr.Button("π¬οΈ Chicago", size="sm")
miami_btn = gr.Button("ποΈ Miami", size="sm")
with gr.Column(scale=2):
# Map display
map_html = gr.HTML(
value=create_map(),
label="Interactive Map"
)
with gr.Row():
with gr.Column():
# Pollen data output
pollen_output = gr.Markdown(
value="Select a location to view pollen forecast.",
label="Pollen Forecast"
)
# Data table
pollen_table = gr.Dataframe(
label="Pollen Data Summary",
visible=False
)
# Event handlers
def set_nyc():
return 40.7128, -74.0060
def set_la():
return 34.0522, -118.2437
def set_chicago():
return 41.8781, -87.6298
def set_miami():
return 25.7617, -80.1918
# Button events
update_btn.click(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
nyc_btn.click(
fn=set_nyc,
outputs=[lat_input, lon_input]
).then(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
la_btn.click(
fn=set_la,
outputs=[lat_input, lon_input]
).then(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
chicago_btn.click(
fn=set_chicago,
outputs=[lat_input, lon_input]
).then(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
miami_btn.click(
fn=set_miami,
outputs=[lat_input, lon_input]
).then(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
# Auto-update when coordinates change
lat_input.change(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
lon_input.change(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
# Load initial data
app.load(
fn=update_location,
inputs=[lat_input, lon_input],
outputs=[map_html, pollen_output, pollen_table]
)
if __name__ == "__main__":
app.launch() |