Spaces:
Runtime error
Runtime error
add pv-floodnet to app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ def predict_fire(temp, temp_unit, humidity, wind, wind_unit, veg, elev, elev_uni
|
|
| 31 |
elif final > 0.43 and final < 0.50:
|
| 32 |
verdict = "⚠️ Fire Possible"
|
| 33 |
else:
|
| 34 |
-
verdict = "
|
| 35 |
return f"{verdict} ({final:.2f})"
|
| 36 |
|
| 37 |
def predict_flood(rainfall_val, rainfall_unit, water_level_val, elevation_val, elev_unit,
|
|
@@ -70,7 +70,36 @@ def predict_flood(rainfall_val, rainfall_unit, water_level_val, elevation_val, e
|
|
| 70 |
elif final > 0.43 and final < 0.50:
|
| 71 |
verdict = "⚠️ FV-Flood Possible"
|
| 72 |
else:
|
| 73 |
-
verdict = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
return f"{verdict} ({final:.2f})"
|
| 75 |
|
| 76 |
def generate_plot(axis, use_trustnet):
|
|
@@ -173,12 +202,51 @@ def generate_flood_plot(axis, use_trustnet):
|
|
| 173 |
ax.legend()
|
| 174 |
return fig
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
# Launch the app
|
| 178 |
with gr.Blocks(theme=gr.themes.Default(), css=".tab-nav-button { font-size: 1.1rem !important; padding: 0.8em; } ") as demo:
|
| 179 |
gr.Markdown("# ClimateNet - A family of tabular classification models to predict natural disasters")
|
| 180 |
|
| 181 |
-
with gr.Tab("🔥
|
| 182 |
with gr.Row():
|
| 183 |
with gr.Column():
|
| 184 |
with gr.Row():
|
|
@@ -243,7 +311,7 @@ with gr.Blocks(theme=gr.themes.Default(), css=".tab-nav-button { font-size: 1.1r
|
|
| 243 |
outputs=[output, plot_output]
|
| 244 |
)
|
| 245 |
|
| 246 |
-
with gr.Tab("
|
| 247 |
with gr.Row():
|
| 248 |
with gr.Column():
|
| 249 |
with gr.Row():
|
|
@@ -310,6 +378,55 @@ with gr.Blocks(theme=gr.themes.Default(), css=".tab-nav-button { font-size: 1.1r
|
|
| 310 |
],
|
| 311 |
outputs=[flood_output, flood_plot]
|
| 312 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
app = FastAPI()
|
| 315 |
|
|
|
|
| 31 |
elif final > 0.43 and final < 0.50:
|
| 32 |
verdict = "⚠️ Fire Possible"
|
| 33 |
else:
|
| 34 |
+
verdict = "🛡️ Fire Unlikely"
|
| 35 |
return f"{verdict} ({final:.2f})"
|
| 36 |
|
| 37 |
def predict_flood(rainfall_val, rainfall_unit, water_level_val, elevation_val, elev_unit,
|
|
|
|
| 70 |
elif final > 0.43 and final < 0.50:
|
| 71 |
verdict = "⚠️ FV-Flood Possible"
|
| 72 |
else:
|
| 73 |
+
verdict = "🛡️ FV-Flood Unlikely"
|
| 74 |
+
return f"{verdict} ({final:.2f})"
|
| 75 |
+
|
| 76 |
+
def predict_pluvial_flood(rain, imp, drain, urban, conv, use_trust, rainfall_unit):
|
| 77 |
+
print(rainfall_unit)
|
| 78 |
+
rain = convert_rainfall_intensity(rain, rainfall_unit)
|
| 79 |
+
print(rain)
|
| 80 |
+
input_data = {
|
| 81 |
+
"rainfall_intensity": rain,
|
| 82 |
+
"impervious_ratio": imp,
|
| 83 |
+
"drainage_density": drain,
|
| 84 |
+
"urbanization_index": urban,
|
| 85 |
+
"convergence_index": conv
|
| 86 |
+
}
|
| 87 |
+
input_df = pd.DataFrame([input_data])
|
| 88 |
+
base_prob = PV_FloodNet.predict(input_df)[0][0]
|
| 89 |
+
|
| 90 |
+
if use_trust:
|
| 91 |
+
trust_score = PV_FloodTrustNet.predict(PV_FloodScaler.transform(input_df))[0][0]
|
| 92 |
+
final = np.clip(base_prob * trust_score, 0, 1)
|
| 93 |
+
else:
|
| 94 |
+
final = base_prob
|
| 95 |
+
|
| 96 |
+
if final > 0.52:
|
| 97 |
+
verdict = "🌧️ PV-FLOOD LIKELY"
|
| 98 |
+
elif 0.45 < final <= 0.52:
|
| 99 |
+
verdict = "⚠️ PV-Flood Possible"
|
| 100 |
+
else:
|
| 101 |
+
verdict = "🛡️ PV-Flood Unlikely"
|
| 102 |
+
|
| 103 |
return f"{verdict} ({final:.2f})"
|
| 104 |
|
| 105 |
def generate_plot(axis, use_trustnet):
|
|
|
|
| 202 |
ax.legend()
|
| 203 |
return fig
|
| 204 |
|
| 205 |
+
def generate_pluvial_plot(axis, use_trust):
|
| 206 |
+
sweep_range = {
|
| 207 |
+
"rainfall_intensity": (0, 160),
|
| 208 |
+
"impervious_ratio": (0.0, 1.0),
|
| 209 |
+
"drainage_density": (1.0, 5.0),
|
| 210 |
+
"urbanization_index": (0.0, 1.0),
|
| 211 |
+
"convergence_index": (0.0, 1.0)
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
sweep_values = np.linspace(*sweep_range[axis], 100)
|
| 215 |
+
base_input = {
|
| 216 |
+
"rainfall_intensity": 60.0,
|
| 217 |
+
"impervious_ratio": 0.5,
|
| 218 |
+
"drainage_density": 2.5,
|
| 219 |
+
"urbanization_index": 0.6,
|
| 220 |
+
"convergence_index": 0.5
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
sweep_df = pd.DataFrame([
|
| 224 |
+
{**base_input, axis: val} for val in sweep_values
|
| 225 |
+
])
|
| 226 |
+
|
| 227 |
+
base_probs = PV_FloodNet.predict(sweep_df).flatten()
|
| 228 |
+
if use_trust:
|
| 229 |
+
trust_mods = PV_FloodTrustNet.predict(PV_FloodScaler.transform(sweep_df)).flatten()
|
| 230 |
+
adjusted = np.clip(base_probs * trust_mods, 0, 1)
|
| 231 |
+
else:
|
| 232 |
+
adjusted = base_probs
|
| 233 |
+
|
| 234 |
+
fig, ax = plt.subplots()
|
| 235 |
+
ax.plot(sweep_values, base_probs, "--", color="gray", label="Base Model")
|
| 236 |
+
if use_trust:
|
| 237 |
+
ax.plot(sweep_values, adjusted, color="royalblue", label="With PV-FloodTrustNet")
|
| 238 |
+
|
| 239 |
+
ax.set_xlabel(axis.replace("_", " ").title())
|
| 240 |
+
ax.set_ylabel("PV Flood Probability")
|
| 241 |
+
ax.set_title(f"PV Flood Probability vs. {axis.replace('_', ' ').title()}")
|
| 242 |
+
ax.legend()
|
| 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():
|
|
|
|
| 311 |
outputs=[output, plot_output]
|
| 312 |
)
|
| 313 |
|
| 314 |
+
with gr.Tab("🌊 Fluvial Floods"):
|
| 315 |
with gr.Row():
|
| 316 |
with gr.Column():
|
| 317 |
with gr.Row():
|
|
|
|
| 378 |
],
|
| 379 |
outputs=[flood_output, flood_plot]
|
| 380 |
)
|
| 381 |
+
with gr.Tab("🌧️ Pluvial Floods"):
|
| 382 |
+
with gr.Row():
|
| 383 |
+
with gr.Column():
|
| 384 |
+
with gr.Row():
|
| 385 |
+
rain_input = gr.Slider(0, 150, value=12, label="Rainfall Intensity (mm/hr)")
|
| 386 |
+
rain_unit_dropdown = gr.Dropdown(["mm/hr", "in/hr"], value="mm/hr", label="", scale=0.2)
|
| 387 |
+
with gr.Row():
|
| 388 |
+
imp_input = gr.Slider(0.0, 1.0, value=0.5, label="Impervious Ratio")
|
| 389 |
+
gr.Dropdown(["ISR"], value="ISR", label="", scale=0.2)
|
| 390 |
+
with gr.Row():
|
| 391 |
+
drain_input = gr.Slider(1.0, 5.0, value=2.5, label="Drainage Density")
|
| 392 |
+
gr.Dropdown(["tL/tA"], value="tL/tA", label="", scale=0.2)
|
| 393 |
+
with gr.Row():
|
| 394 |
+
urban_input = gr.Slider(0.0, 1.0, value=0.6, label="Urbanization Index")
|
| 395 |
+
gr.Dropdown(["uP/tP"], value="uP/tP", label="", scale=0.2)
|
| 396 |
+
with gr.Row():
|
| 397 |
+
conv_input = gr.Slider(0.0, 1.0, value=0.5, label="Convergence Index")
|
| 398 |
+
gr.Dropdown(["CI"], value="CI", label="", scale=0.2)
|
| 399 |
+
use_trust_pv = gr.Checkbox(label="Use PV-FloodTrustNet", value=True)
|
| 400 |
+
pv_sweep_axis = gr.Radio(
|
| 401 |
+
["rainfall_intensity", "impervious_ratio", "drainage_density", "urbanization_index", "convergence_index"],
|
| 402 |
+
label="Sweep Axis", value="rainfall_intensity"
|
| 403 |
+
)
|
| 404 |
+
pv_predict_btn = gr.Button("Predict")
|
| 405 |
+
|
| 406 |
+
with gr.Column():
|
| 407 |
+
with gr.Accordion("ℹ️ Feature Definitions", open=False):
|
| 408 |
+
gr.Markdown("""
|
| 409 |
+
**Rainfall Intensity:** Recent precipitation rate, typically measured in mm/hr.
|
| 410 |
+
|
| 411 |
+
**Impervious Ratio:** Proportion of surface area that cannot absorb water.
|
| 412 |
+
|
| 413 |
+
**Drainage Density:** Drainage channel length per unit area.
|
| 414 |
+
|
| 415 |
+
**Urbanization Index:** Estimate of built-up density and infrastructure pressure.
|
| 416 |
+
|
| 417 |
+
**Convergence Index:** Terrain feature promoting water pooling or runoff directionality.
|
| 418 |
+
""")
|
| 419 |
+
pv_output = gr.Textbox(label="PV-Flood Risk Verdict")
|
| 420 |
+
pv_plot = gr.Plot(label="Trust Modulation Plot")
|
| 421 |
+
|
| 422 |
+
pv_predict_btn.click(
|
| 423 |
+
fn=lambda r, ra, i, d, u, c, trust, axis: (
|
| 424 |
+
predict_pluvial_flood(r, i, d, u, c, trust, ra),
|
| 425 |
+
generate_pluvial_plot(axis, trust)
|
| 426 |
+
),
|
| 427 |
+
inputs=[rain_input, rain_unit_dropdown, imp_input, drain_input, urban_input, conv_input, use_trust_pv, pv_sweep_axis],
|
| 428 |
+
outputs=[pv_output, pv_plot]
|
| 429 |
+
)
|
| 430 |
|
| 431 |
app = FastAPI()
|
| 432 |
|