Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -44,6 +44,7 @@ def load_models():
|
|
44 |
'severity_post_tta.keras',
|
45 |
custom_objects={'focal_loss_fixed': focal_loss_fixed()}
|
46 |
)
|
|
|
47 |
rf_model = joblib.load('ensemble_rf_model.pkl')
|
48 |
xgb_model = joblib.load('ensemble_xgb_model.pkl')
|
49 |
lr_model = joblib.load('wildfire_logistic_model_synthetic.joblib')
|
@@ -54,32 +55,33 @@ vgg_model, xception_model, rf_model, xgb_model, lr_model = load_models()
|
|
54 |
# --- RULES & TEMPLATES ---
|
55 |
target_map = {0: 'mild', 1: 'moderate', 2: 'severe'}
|
56 |
trend_map = {1: 'increase', 0: 'same', -1: 'decrease'}
|
57 |
-
# severity transition rules
|
58 |
task_rules = {
|
59 |
'mild': {'decrease':'mild','same':'mild','increase':'moderate'},
|
60 |
'moderate':{'decrease':'mild','same':'moderate','increase':'severe'},
|
61 |
'severe': {'decrease':'moderate','same':'severe','increase':'severe'}
|
62 |
}
|
63 |
-
# static recommendation templates per severity
|
64 |
templates = {
|
65 |
'mild': (
|
66 |
"**1. Immediate actions:** Monitor fire; deploy spot crews.\n"
|
67 |
"**2. Evacuation:** No mass evacuation; notify nearby communities.\n"
|
68 |
"**3. Short-term containment:** Establish fire lines.\n"
|
69 |
"**4. Long-term prevention:** Controlled underburning; vegetation management.\n"
|
70 |
-
"**5. Education:** Inform public on firewatch and reporting."
|
|
|
71 |
'moderate': (
|
72 |
"**1. Immediate actions:** Dispatch engines and aerial support.\n"
|
73 |
"**2. Evacuation:** Prepare evacuation zones; advise voluntary evacuation.\n"
|
74 |
"**3. Short-term containment:** Build fire breaks; water drops.\n"
|
75 |
"**4. Long-term prevention:** Fuel reduction programs.\n"
|
76 |
-
"**5. Education:** Community drills and awareness campaigns."
|
|
|
77 |
'severe': (
|
78 |
"**1. Immediate actions:** Full suppression with air tankers.\n"
|
79 |
"**2. Evacuation:** Mandatory evacuation; open shelters.\n"
|
80 |
"**3. Short-term containment:** Fire retardant lines; backfires.\n"
|
81 |
"**4. Long-term prevention:** Reforestation; infrastructure hardening.\n"
|
82 |
-
"**5. Education:** Emergency response training; risk communication."
|
|
|
83 |
}
|
84 |
|
85 |
# --- PIPELINE FUNCTIONS ---
|
@@ -106,8 +108,7 @@ def fetch_weather_trend(lat, lon):
|
|
106 |
url = API_URL.format(lat=lat, lon=lon,
|
107 |
start=start.strftime('%Y-%m-%d'),
|
108 |
end=end.strftime('%Y-%m-%d'))
|
109 |
-
|
110 |
-
df = pd.DataFrame(data)
|
111 |
for c in ['precipitation_sum','temperature_2m_max','temperature_2m_min',
|
112 |
'relative_humidity_2m_max','relative_humidity_2m_min','windspeed_10m_max']:
|
113 |
df[c] = pd.to_numeric(df.get(c,[]), errors='coerce')
|
@@ -131,9 +132,10 @@ def generate_recommendations(original_severity, weather_trend):
|
|
131 |
# determine projected severity
|
132 |
proj = task_rules[original_severity][weather_trend]
|
133 |
rec = templates[proj]
|
134 |
-
|
|
|
135 |
**Trend:** {weather_trend.title()}
|
136 |
-
**Projected:** {proj.title()}\n\n"
|
137 |
return header + rec
|
138 |
|
139 |
# --- GRADIO INTERFACE ---
|
|
|
44 |
'severity_post_tta.keras',
|
45 |
custom_objects={'focal_loss_fixed': focal_loss_fixed()}
|
46 |
)
|
47 |
+
# Ensemble and trend models
|
48 |
rf_model = joblib.load('ensemble_rf_model.pkl')
|
49 |
xgb_model = joblib.load('ensemble_xgb_model.pkl')
|
50 |
lr_model = joblib.load('wildfire_logistic_model_synthetic.joblib')
|
|
|
55 |
# --- RULES & TEMPLATES ---
|
56 |
target_map = {0: 'mild', 1: 'moderate', 2: 'severe'}
|
57 |
trend_map = {1: 'increase', 0: 'same', -1: 'decrease'}
|
|
|
58 |
task_rules = {
|
59 |
'mild': {'decrease':'mild','same':'mild','increase':'moderate'},
|
60 |
'moderate':{'decrease':'mild','same':'moderate','increase':'severe'},
|
61 |
'severe': {'decrease':'moderate','same':'severe','increase':'severe'}
|
62 |
}
|
|
|
63 |
templates = {
|
64 |
'mild': (
|
65 |
"**1. Immediate actions:** Monitor fire; deploy spot crews.\n"
|
66 |
"**2. Evacuation:** No mass evacuation; notify nearby communities.\n"
|
67 |
"**3. Short-term containment:** Establish fire lines.\n"
|
68 |
"**4. Long-term prevention:** Controlled underburning; vegetation management.\n"
|
69 |
+
"**5. Education:** Inform public on firewatch and reporting."
|
70 |
+
),
|
71 |
'moderate': (
|
72 |
"**1. Immediate actions:** Dispatch engines and aerial support.\n"
|
73 |
"**2. Evacuation:** Prepare evacuation zones; advise voluntary evacuation.\n"
|
74 |
"**3. Short-term containment:** Build fire breaks; water drops.\n"
|
75 |
"**4. Long-term prevention:** Fuel reduction programs.\n"
|
76 |
+
"**5. Education:** Community drills and awareness campaigns."
|
77 |
+
),
|
78 |
'severe': (
|
79 |
"**1. Immediate actions:** Full suppression with air tankers.\n"
|
80 |
"**2. Evacuation:** Mandatory evacuation; open shelters.\n"
|
81 |
"**3. Short-term containment:** Fire retardant lines; backfires.\n"
|
82 |
"**4. Long-term prevention:** Reforestation; infrastructure hardening.\n"
|
83 |
+
"**5. Education:** Emergency response training; risk communication."
|
84 |
+
)
|
85 |
}
|
86 |
|
87 |
# --- PIPELINE FUNCTIONS ---
|
|
|
108 |
url = API_URL.format(lat=lat, lon=lon,
|
109 |
start=start.strftime('%Y-%m-%d'),
|
110 |
end=end.strftime('%Y-%m-%d'))
|
111 |
+
df = pd.DataFrame(requests.get(url).json().get('daily', {}))
|
|
|
112 |
for c in ['precipitation_sum','temperature_2m_max','temperature_2m_min',
|
113 |
'relative_humidity_2m_max','relative_humidity_2m_min','windspeed_10m_max']:
|
114 |
df[c] = pd.to_numeric(df.get(c,[]), errors='coerce')
|
|
|
132 |
# determine projected severity
|
133 |
proj = task_rules[original_severity][weather_trend]
|
134 |
rec = templates[proj]
|
135 |
+
# proper multi-line header
|
136 |
+
header = f"""**Original:** {original_severity.title()}
|
137 |
**Trend:** {weather_trend.title()}
|
138 |
+
**Projected:** {proj.title()}\n\n"""
|
139 |
return header + rec
|
140 |
|
141 |
# --- GRADIO INTERFACE ---
|