sharktide commited on
Commit
5412e32
·
verified ·
1 Parent(s): 32745b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -1
app.py CHANGED
@@ -102,6 +102,30 @@ def predict_pluvial_flood(rain, imp, drain, urban, conv, use_trust, rainfall_uni
102
 
103
  return f"{verdict} ({final:.2f})"
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  def generate_plot(axis, use_trustnet):
106
  sweep_values = np.linspace({
107
  "temperature": (280, 320),
@@ -243,10 +267,45 @@ def generate_pluvial_plot(axis, use_trust):
243
  ax.grid(True)
244
  return fig
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  with gr.Blocks(theme=gr.themes.Default(), css=".tab-nav-button { font-size: 1.1rem !important; padding: 0.8em; } ") as demo:
247
  gr.Markdown("# ClimateNet - A family of tabular classification models to predict natural disasters")
248
 
249
- with gr.Tab("🔥Wildfires"):
250
  with gr.Row():
251
  with gr.Column():
252
  with gr.Row():
@@ -429,6 +488,68 @@ with gr.Blocks(theme=gr.themes.Default(), css=".tab-nav-button { font-size: 1.1r
429
  outputs=[pv_output, pv_plot]
430
  )
431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  app = FastAPI()
433
 
434
  app.add_middleware(
 
102
 
103
  return f"{verdict} ({final:.2f})"
104
 
105
+ def predict_flash_flood(rainfall, slope, drainage, saturation, convergence, use_trust):
106
+ input_data = {
107
+ "rainfall_intensity": rainfall,
108
+ "slope": slope,
109
+ "drainage_density": drainage,
110
+ "soil_saturation": saturation,
111
+ "convergence_index": convergence
112
+ }
113
+ input_df = pd.DataFrame([input_data])
114
+ base_pred = FlashFloodNet.predict(input_df)[0][0]
115
+ if use_trust:
116
+ trust_score = FlashFloodTrustNet.predict(FlashFloodScaler.transform(input_df))[0][0]
117
+ adjusted = np.clip(base_pred * trust_score, 0, 1)
118
+ else:
119
+ adjusted = base_pred
120
+
121
+ if adjusted > 0.55:
122
+ return f"🌩️ FLASH FLOOD LIKELY ({adjusted:.2f})"
123
+ elif 0.40 < adjusted <= 0.55:
124
+ return f"⚠️ Flash Flood Possible ({adjusted:.2f})"
125
+ else:
126
+ return f"🛡️ Flash Flood Unlikely ({adjusted:.2f})"
127
+
128
+
129
  def generate_plot(axis, use_trustnet):
130
  sweep_values = np.linspace({
131
  "temperature": (280, 320),
 
267
  ax.grid(True)
268
  return fig
269
 
270
+ def generate_flash_plot(axis, use_trust):
271
+ sweep_values = np.linspace(
272
+ {"rainfall_intensity": 0, "slope": 0, "drainage_density": 1.0,
273
+ "soil_saturation": 0.3, "convergence_index": 0.0}[axis],
274
+ {"rainfall_intensity": 150, "slope": 30, "drainage_density": 5.0,
275
+ "soil_saturation": 1.0, "convergence_index": 1.0}[axis],
276
+ 100
277
+ )
278
+ base_input = {
279
+ "rainfall_intensity": 90,
280
+ "slope": 15,
281
+ "drainage_density": 3.0,
282
+ "soil_saturation": 0.7,
283
+ "convergence_index": 0.5
284
+ }
285
+
286
+ sweep_df = pd.DataFrame([{**base_input, axis: val} for val in sweep_values])
287
+ raw_probs = FlashFloodNet.predict(sweep_df).flatten()
288
+ if use_trust:
289
+ trust_mods = FlashFloodTrustNet.predict(FlashFloodScaler.transform(sweep_df)).flatten()
290
+ adjusted_probs = np.clip(raw_probs * trust_mods, 0, 1)
291
+ else:
292
+ adjusted_probs = raw_probs
293
+
294
+ fig, ax = plt.subplots()
295
+ ax.plot(sweep_values, raw_probs, "--", color="gray", label="Base Model")
296
+ if use_trust:
297
+ ax.plot(sweep_values, adjusted_probs, color="darkcyan", label="With FlashFloodTrustNet")
298
+ ax.set_xlabel(axis.replace("_", " ").title())
299
+ ax.set_ylabel("Flash Flood Probability")
300
+ ax.set_title(f"Flash Flood Probability vs. {axis.replace('_', ' ').title()}")
301
+ ax.legend()
302
+ ax.grid(True)
303
+ return fig
304
+
305
  with gr.Blocks(theme=gr.themes.Default(), css=".tab-nav-button { font-size: 1.1rem !important; padding: 0.8em; } ") as demo:
306
  gr.Markdown("# ClimateNet - A family of tabular classification models to predict natural disasters")
307
 
308
+ with gr.Tab("🔥 Wildfires"):
309
  with gr.Row():
310
  with gr.Column():
311
  with gr.Row():
 
488
  outputs=[pv_output, pv_plot]
489
  )
490
 
491
+ with gr.Tab("🌩️ Flash Floods"):
492
+ with gr.Row():
493
+ with gr.Column():
494
+ with gr.Row():
495
+ rainfall_intensity = gr.Slider(0, 150, value=12, label="Rainfall Intensity (mm/hr)")
496
+ rainfall_unit_dropdown = gr.Dropdown(["mm/hr", "in/hr"], value="mm/hr", label="", scale=0.2)
497
+ with gr.Row():
498
+ slope_input = gr.Slider(0, 30, value=15, label="Slope (°)")
499
+ gr.Dropdown(["°"], label="", scale=0.1)
500
+
501
+ with gr.Row():
502
+ drainage_input = gr.Slider(1.0, 5.0, value=3.0, label="Drainage Density")
503
+ gr.Dropdown(["L/A"], value="L/A", label="", scale=0.2)
504
+
505
+ with gr.Row():
506
+ saturation_input = gr.Slider(0.3, 1.0, value=0.7, label="Soil Saturation")
507
+ gr.Dropdown(["VWC"], value="VWC", label="", scale=0.2)
508
+
509
+ with gr.Row():
510
+ convergence_input = gr.Slider(0.0, 1.0, value=0.5, label="Convergence Index")
511
+ gr.Dropdown(["CI"], value="CI", label="", scale=0.2)
512
+
513
+ rainfall_unit_dropdown.change(update_rain_slider, inputs=rainfall_unit_dropdown, outputs=rainfall_intensity)
514
+
515
+ use_trust_flash = gr.Checkbox(label="Use FlashFloodTrustNet", value=True)
516
+
517
+ flash_sweep_axis = gr.Radio(
518
+ ["rainfall_intensity", "slope", "drainage_density", "soil_saturation", "convergence_index"],
519
+ label="Sweep Axis", value="rainfall_intensity"
520
+ )
521
+
522
+ flash_predict_btn = gr.Button("Predict")
523
+
524
+ with gr.Column():
525
+ with gr.Accordion("ℹ️ Feature Definitions", open=False):
526
+ gr.Markdown("""
527
+ **Rainfall Intensity:** Measured in mm/hr or in/hr.
528
+
529
+ **Slope:** Terrain gradient in degrees.
530
+
531
+ **Drainage Density:** Total stream length per unit area.
532
+
533
+ **Soil Saturation:** Volumetric water content — higher values = wetter ground.
534
+
535
+ **Convergence Index:** Measures topographical tendency to channel runoff.
536
+ """)
537
+ flash_output = gr.Textbox(label="Flash Flood Risk Verdict")
538
+ flash_plot = gr.Plot(label="Trust Modulation Plot")
539
+
540
+ flash_predict_btn.click(
541
+ fn=lambda r, ru, s, d, ss, c, trust, axis: (
542
+ predict_flash_flood(convert_rainfall_intensity(r, ru), s, d, ss, c, trust),
543
+ generate_flash_plot(axis, trust)
544
+ ),
545
+ inputs=[
546
+ rainfall_intensity, rainfall_unit_dropdown,
547
+ slope_input, drainage_input, saturation_input,
548
+ convergence_input, use_trust_flash, flash_sweep_axis
549
+ ],
550
+ outputs=[flash_output, flash_plot]
551
+ )
552
+
553
  app = FastAPI()
554
 
555
  app.add_middleware(