Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,22 @@ class EnhancedOceanClimateAgent:
|
|
| 15 |
self.anomaly_threshold = 2.0
|
| 16 |
self.critical_temp_change = 1.5
|
| 17 |
|
| 18 |
-
# API endpoints
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
self.noaa_base_url = "https://api.tidesandcurrents.noaa.gov/api/prod/datagetter"
|
| 20 |
self.noaa_stations_url = "https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations.json"
|
| 21 |
|
|
@@ -191,7 +206,7 @@ class EnhancedOceanClimateAgent:
|
|
| 191 |
analysis['water_level_trend'] = wl_trend * 24 # per day
|
| 192 |
|
| 193 |
if abs(wl_trend * 24) > 5: # >5cm per day change
|
| 194 |
-
alerts.append(f"
|
| 195 |
|
| 196 |
# Temperature analysis
|
| 197 |
if 'water_temp' in data.columns:
|
|
@@ -199,7 +214,7 @@ class EnhancedOceanClimateAgent:
|
|
| 199 |
analysis['water_temp_trend'] = temp_trend * 24 # per day
|
| 200 |
|
| 201 |
if temp_trend * 24 > 0.5: # >0.5Β°C per day
|
| 202 |
-
alerts.append(f"
|
| 203 |
|
| 204 |
# Anomaly detection
|
| 205 |
for col in ['water_level', 'water_temp', 'wind_speed']:
|
|
@@ -209,7 +224,7 @@ class EnhancedOceanClimateAgent:
|
|
| 209 |
analysis[f'{col}_anomaly_frequency'] = anomaly_pct
|
| 210 |
|
| 211 |
if anomaly_pct > 10:
|
| 212 |
-
alerts.append(f"
|
| 213 |
|
| 214 |
if not alerts:
|
| 215 |
alerts.append(f"β
No significant anomalies detected at {station_name}")
|
|
@@ -229,20 +244,20 @@ def analyze_real_ocean_data(station_name, days_back, anomaly_sensitivity, use_re
|
|
| 229 |
raw_data, status_msg = agent.get_comprehensive_station_data(station_name, days_back)
|
| 230 |
|
| 231 |
if raw_data is None:
|
| 232 |
-
return None, None, None, f"
|
| 233 |
|
| 234 |
# Process the data
|
| 235 |
data = agent.process_noaa_data(raw_data)
|
| 236 |
|
| 237 |
if data is None or data.empty:
|
| 238 |
-
return None, None, None, "
|
| 239 |
|
| 240 |
-
data_source = f"
|
| 241 |
|
| 242 |
else:
|
| 243 |
# Use synthetic data for demonstration
|
| 244 |
data = generate_synthetic_data(days_back)
|
| 245 |
-
data_source = f"
|
| 246 |
|
| 247 |
# Generate analysis and alerts
|
| 248 |
analysis, alerts = agent.generate_climate_analysis(data, station_name)
|
|
@@ -257,9 +272,19 @@ def analyze_real_ocean_data(station_name, days_back, anomaly_sensitivity, use_re
|
|
| 257 |
alerts_text = "\n".join([f"- {alert}" for alert in alerts])
|
| 258 |
|
| 259 |
# Create CSV for download
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
|
|
|
|
|
|
| 263 |
|
| 264 |
def generate_synthetic_data(days):
|
| 265 |
"""Generate synthetic data for demonstration"""
|
|
@@ -407,9 +432,9 @@ def format_analysis_results(analysis, data_source):
|
|
| 407 |
return result
|
| 408 |
|
| 409 |
# Create Gradio interface
|
| 410 |
-
with gr.Blocks(title="
|
| 411 |
gr.Markdown("""
|
| 412 |
-
#
|
| 413 |
### Real-time Analysis with NOAA Data Integration
|
| 414 |
|
| 415 |
This enhanced AI agent can fetch real ocean data from NOAA stations or use synthetic data for demonstration.
|
|
@@ -443,7 +468,7 @@ with gr.Blocks(title="π Enhanced Ocean Climate Monitoring AI Agent", theme=gr
|
|
| 443 |
value=True,
|
| 444 |
info="Uncheck to use synthetic data"
|
| 445 |
)
|
| 446 |
-
analyze_btn = gr.Button("
|
| 447 |
|
| 448 |
with gr.Column(scale=2):
|
| 449 |
gr.Markdown("### Climate Alerts")
|
|
|
|
| 15 |
self.anomaly_threshold = 2.0
|
| 16 |
self.critical_temp_change = 1.5
|
| 17 |
|
| 18 |
+
# API endpoints* Running on local URL: http://0.0.0.0:7860, with SSR β‘ (experimental, to disable set `ssr_mode=False` in `launch()`)
|
| 19 |
+
* To create a public link, set `share=True` in `launch()`.
|
| 20 |
+
Traceback (most recent call last):
|
| 21 |
+
File "/usr/local/lib/python3.10/site-packages/gradio/queueing.py", line 626, in process_events
|
| 22 |
+
response = await route_utils.call_process_api(
|
| 23 |
+
File "/usr/local/lib/python3.10/site-packages/gradio/route_utils.py", line 322, in call_process_api
|
| 24 |
+
output = await app.get_blocks().process_api(
|
| 25 |
+
File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 2230, in process_api
|
| 26 |
+
data = await self.postprocess_data(block_fn, result["prediction"], state)
|
| 27 |
+
File "/usr/local/lib/python3.10/site-packages/gradio/blocks.py", line 2012, in postprocess_data
|
| 28 |
+
prediction_value = block.postprocess(prediction_value)
|
| 29 |
+
File "/usr/local/lib/python3.10/site-packages/gradio/components/file.py", line 227, in postprocess
|
| 30 |
+
size=Path(value).stat().st_size,
|
| 31 |
+
File "/usr/local/lib/python3.10/pathlib.py", line 1097, in stat
|
| 32 |
+
return self._accessor.stat(self, follow_symlinks=follow_symlinks)
|
| 33 |
+
|
| 34 |
self.noaa_base_url = "https://api.tidesandcurrents.noaa.gov/api/prod/datagetter"
|
| 35 |
self.noaa_stations_url = "https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations.json"
|
| 36 |
|
|
|
|
| 206 |
analysis['water_level_trend'] = wl_trend * 24 # per day
|
| 207 |
|
| 208 |
if abs(wl_trend * 24) > 5: # >5cm per day change
|
| 209 |
+
alerts.append(f"Significant water level change: {wl_trend*24:.1f}cm/day at {station_name}")
|
| 210 |
|
| 211 |
# Temperature analysis
|
| 212 |
if 'water_temp' in data.columns:
|
|
|
|
| 214 |
analysis['water_temp_trend'] = temp_trend * 24 # per day
|
| 215 |
|
| 216 |
if temp_trend * 24 > 0.5: # >0.5Β°C per day
|
| 217 |
+
alerts.append(f"Rapid water temperature rise: {temp_trend*24:.2f}Β°C/day at {station_name}")
|
| 218 |
|
| 219 |
# Anomaly detection
|
| 220 |
for col in ['water_level', 'water_temp', 'wind_speed']:
|
|
|
|
| 224 |
analysis[f'{col}_anomaly_frequency'] = anomaly_pct
|
| 225 |
|
| 226 |
if anomaly_pct > 10:
|
| 227 |
+
alerts.append(f"High {col.replace('_', ' ')} anomaly frequency: {anomaly_pct:.1f}% at {station_name}")
|
| 228 |
|
| 229 |
if not alerts:
|
| 230 |
alerts.append(f"β
No significant anomalies detected at {station_name}")
|
|
|
|
| 244 |
raw_data, status_msg = agent.get_comprehensive_station_data(station_name, days_back)
|
| 245 |
|
| 246 |
if raw_data is None:
|
| 247 |
+
return None, None, None, f"Error: {status_msg}", "No alerts - data unavailable", None
|
| 248 |
|
| 249 |
# Process the data
|
| 250 |
data = agent.process_noaa_data(raw_data)
|
| 251 |
|
| 252 |
if data is None or data.empty:
|
| 253 |
+
return None, None, None, "No processable data available", "No alerts - data unavailable", None
|
| 254 |
|
| 255 |
+
data_source = f"Real NOAA data from {station_name} ({status_msg})"
|
| 256 |
|
| 257 |
else:
|
| 258 |
# Use synthetic data for demonstration
|
| 259 |
data = generate_synthetic_data(days_back)
|
| 260 |
+
data_source = f"Synthetic demonstration data ({days_back} days)"
|
| 261 |
|
| 262 |
# Generate analysis and alerts
|
| 263 |
analysis, alerts = agent.generate_climate_analysis(data, station_name)
|
|
|
|
| 272 |
alerts_text = "\n".join([f"- {alert}" for alert in alerts])
|
| 273 |
|
| 274 |
# Create CSV for download
|
| 275 |
+
import tempfile
|
| 276 |
+
|
| 277 |
+
#Create CSV
|
| 278 |
+
def save_csv_temp(data):
|
| 279 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode='w', newline='', encoding='utf-8')
|
| 280 |
+
data.to_csv(tmp.name, index=False)
|
| 281 |
+
tmp.close()
|
| 282 |
+
return tmp.name
|
| 283 |
+
|
| 284 |
|
| 285 |
+
csv_file_path = save_csv_temp(data)
|
| 286 |
+
return fig1, fig2, fig3, analysis_text, alerts_text, csv_file_path
|
| 287 |
+
|
| 288 |
|
| 289 |
def generate_synthetic_data(days):
|
| 290 |
"""Generate synthetic data for demonstration"""
|
|
|
|
| 432 |
return result
|
| 433 |
|
| 434 |
# Create Gradio interface
|
| 435 |
+
with gr.Blocks(title="Enhanced Ocean Climate Monitoring AI Agent", theme=gr.themes.Ocean()) as demo:
|
| 436 |
gr.Markdown("""
|
| 437 |
+
# Enhanced Ocean Climate Monitoring AI Agent
|
| 438 |
### Real-time Analysis with NOAA Data Integration
|
| 439 |
|
| 440 |
This enhanced AI agent can fetch real ocean data from NOAA stations or use synthetic data for demonstration.
|
|
|
|
| 468 |
value=True,
|
| 469 |
info="Uncheck to use synthetic data"
|
| 470 |
)
|
| 471 |
+
analyze_btn = gr.Button("Analyze Ocean Data", variant="primary")
|
| 472 |
|
| 473 |
with gr.Column(scale=2):
|
| 474 |
gr.Markdown("### Climate Alerts")
|