Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,29 +26,37 @@ API_URL = (
|
|
26 |
|
27 |
# --- LOAD MODELS ---
|
28 |
def load_models():
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
def
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
|
|
|
|
|
|
52 |
|
53 |
# --- RULES & TEMPLATES ---
|
54 |
target_map = {0: 'mild', 1: 'moderate', 2: 'severe'}
|
@@ -167,57 +175,53 @@ def pipeline(image):
|
|
167 |
)
|
168 |
|
169 |
# --- GRADIO APP ---
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
background
|
176 |
-
border:
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
background-color: #0072ce !important;
|
191 |
-
color: white !important;
|
192 |
-
border-radius: 6px !important;
|
193 |
-
padding: 8px 16px !important;
|
194 |
-
font-weight: 600 !important;
|
195 |
-
}
|
196 |
-
.gradio-button:hover {
|
197 |
-
background-color: #005bb5 !important;
|
198 |
-
}
|
199 |
-
.gradio-markdown h1, .gradio-markdown h2 {
|
200 |
-
color: #1f2937 !important;
|
201 |
-
margin-bottom: 0.5em !important;
|
202 |
-
}
|
203 |
-
""") as demo:
|
204 |
-
gr.Markdown("# Wildfire Detection & Management Assistant")
|
205 |
-
gr.Markdown("Upload a forest image from Pakistan; the system will detect fire, assess severity, analyze weather trends, and provide in-depth recommendations.")
|
206 |
|
207 |
-
with gr.Row():
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
211 |
severity = gr.Textbox(label="Severity Level", interactive=False)
|
212 |
trend = gr.Textbox(label="Weather Trend", interactive=False)
|
213 |
|
214 |
with gr.Accordion("📋 Detailed Recommendations", open=False):
|
215 |
-
rec_box = gr.Markdown(
|
216 |
|
217 |
-
btn = gr.Button("Analyze")
|
218 |
-
btn.click(
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
if __name__ == "__main__":
|
223 |
-
demo.launch()
|
|
|
26 |
|
27 |
# --- LOAD MODELS ---
|
28 |
def load_models():
|
29 |
+
try:
|
30 |
+
vgg_model = load_model(
|
31 |
+
'vgg16_focal_unfreeze_more.keras',
|
32 |
+
custom_objects={'BinaryFocalCrossentropy': BinaryFocalCrossentropy}
|
33 |
+
)
|
34 |
+
|
35 |
+
def focal_loss_fixed(gamma=2., alpha=.25):
|
36 |
+
import tensorflow.keras.backend as K
|
37 |
+
def loss_fn(y_true, y_pred):
|
38 |
+
eps = K.epsilon()
|
39 |
+
y_pred = K.clip(y_pred, eps, 1. - eps)
|
40 |
+
ce = -y_true * K.log(y_pred)
|
41 |
+
w = alpha * K.pow(1 - y_pred, gamma)
|
42 |
+
return K.mean(w * ce, axis=-1)
|
43 |
+
return loss_fn
|
44 |
+
|
45 |
+
xce_model = load_model(
|
46 |
+
'severity_post_tta.keras',
|
47 |
+
custom_objects={'focal_loss_fixed': focal_loss_fixed()}
|
48 |
+
)
|
49 |
+
rf_model = joblib.load('ensemble_rf_model.pkl')
|
50 |
+
xgb_model = joblib.load('ensemble_xgb_model.pkl')
|
51 |
+
lr_model = joblib.load('wildfire_logistic_model_synthetic.joblib')
|
52 |
+
return vgg_model, xce_model, rf_model, xgb_model, lr_model
|
53 |
+
except Exception as e:
|
54 |
+
raise gr.Error(f"Model loading failed: {str(e)}")
|
55 |
|
56 |
+
try:
|
57 |
+
vgg_model, xception_model, rf_model, xgb_model, lr_model = load_models()
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Initial model loading failed: {str(e)}")
|
60 |
|
61 |
# --- RULES & TEMPLATES ---
|
62 |
target_map = {0: 'mild', 1: 'moderate', 2: 'severe'}
|
|
|
175 |
)
|
176 |
|
177 |
# --- GRADIO APP ---
|
178 |
+
custom_css = """
|
179 |
+
#component-0 { max-width: 800px; margin: 0 auto; }
|
180 |
+
.gradio-container { background: #f0f4f7; }
|
181 |
+
#upload-wildfire-image { min-height: 300px; }
|
182 |
+
.panel {
|
183 |
+
background: white !important;
|
184 |
+
border-radius: 12px !important;
|
185 |
+
padding: 20px !important;
|
186 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
187 |
+
}
|
188 |
+
.status-box {
|
189 |
+
background: #fff3e6 !important;
|
190 |
+
border: 1px solid #ffd8b3 !important;
|
191 |
+
}
|
192 |
+
.dark-red { color: #cc0000 !important; }
|
193 |
+
.green { color: #008000 !important; }
|
194 |
+
"""
|
195 |
+
|
196 |
+
with gr.Blocks(css=custom_css) as demo:
|
197 |
+
gr.Markdown("# 🔥 Wildfire Detection & Management Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
+
with gr.Row(variant="panel"):
|
200 |
+
with gr.Column(scale=2):
|
201 |
+
inp = gr.Image(type="numpy", label="Satellite Image", elem_id="upload-wildfire-image")
|
202 |
+
with gr.Column(scale=1):
|
203 |
+
status = gr.Textbox(label="Fire Status", interactive=False, elem_classes="status-box")
|
204 |
severity = gr.Textbox(label="Severity Level", interactive=False)
|
205 |
trend = gr.Textbox(label="Weather Trend", interactive=False)
|
206 |
|
207 |
with gr.Accordion("📋 Detailed Recommendations", open=False):
|
208 |
+
rec_box = gr.Markdown()
|
209 |
|
210 |
+
btn = gr.Button("Analyze", variant="primary")
|
211 |
+
btn.click(
|
212 |
+
fn=pipeline,
|
213 |
+
inputs=inp,
|
214 |
+
outputs=[status, severity, trend, rec_box],
|
215 |
+
api_name="analyze"
|
216 |
+
)
|
217 |
|
218 |
+
gr.Markdown("---")
|
219 |
+
gr.HTML("<div style='text-align: center; color: #666;'>© 2025 ForestAI Labs</div>")
|
220 |
+
|
221 |
+
def handle_errors(inputs, outputs):
|
222 |
+
for output in outputs:
|
223 |
+
if isinstance(output, Exception):
|
224 |
+
raise gr.Error("Analysis failed. Please check the input and try again.")
|
225 |
|
226 |
if __name__ == "__main__":
|
227 |
+
demo.launch() # Fixed the space typo in launch()
|