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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# ----------------- Page Config -----------------
|
| 6 |
+
st.set_page_config(page_title="Image Analysis", page_icon="🖼️")
|
| 7 |
+
|
| 8 |
+
# ----------------- Model -----------------
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
return pipeline("image-classification", model="Luwayy/disaster_images_model")
|
| 12 |
+
|
| 13 |
+
classifier = load_model()
|
| 14 |
+
|
| 15 |
+
# ----------------- Mappings & Precautions -----------------
|
| 16 |
+
LABEL_MAP = {
|
| 17 |
+
"Fire_Disaster": "fire",
|
| 18 |
+
"Water_Disaster": "flood",
|
| 19 |
+
"Land_Disaster": "landslide",
|
| 20 |
+
"Human_Damage": "earthquake",
|
| 21 |
+
"Damaged_Infrastructure": "building_collapse",
|
| 22 |
+
"Non_Damage": "no disaster"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
PRECAUTIONS = {
|
| 26 |
+
"fire": [
|
| 27 |
+
"Evacuate the area immediately.",
|
| 28 |
+
"Use a wet cloth to cover nose and mouth.",
|
| 29 |
+
"Stay low to avoid smoke.",
|
| 30 |
+
"Call emergency services.",
|
| 31 |
+
"Do not use elevators."
|
| 32 |
+
],
|
| 33 |
+
"flood": [
|
| 34 |
+
"Move to higher ground immediately.",
|
| 35 |
+
"Avoid walking or driving through flood waters.",
|
| 36 |
+
"Turn off electricity if safe.",
|
| 37 |
+
"Boil or treat water before drinking.",
|
| 38 |
+
"Listen to official evacuation instructions."
|
| 39 |
+
],
|
| 40 |
+
"landslide": [
|
| 41 |
+
"Evacuate immediately if you hear rumbling or see debris flow.",
|
| 42 |
+
"Stay away from steep slopes and channels.",
|
| 43 |
+
"Follow evacuation orders.",
|
| 44 |
+
"Check for structural damage before re-entering buildings.",
|
| 45 |
+
"Be alert for sudden changes in water flow."
|
| 46 |
+
],
|
| 47 |
+
"earthquake": [
|
| 48 |
+
"Drop, cover, and hold on.",
|
| 49 |
+
"Stay away from windows.",
|
| 50 |
+
"Turn off gas and electricity if possible.",
|
| 51 |
+
"Prepare an emergency kit.",
|
| 52 |
+
"Follow evacuation routes."
|
| 53 |
+
],
|
| 54 |
+
"building_collapse": [
|
| 55 |
+
"Evacuate the structure if safe.",
|
| 56 |
+
"Avoid using elevators.",
|
| 57 |
+
"Stay away from damaged areas.",
|
| 58 |
+
"Call emergency services immediately.",
|
| 59 |
+
"Help others only if it's safe to do so."
|
| 60 |
+
],
|
| 61 |
+
"tsunami": [
|
| 62 |
+
"Move to higher ground immediately.",
|
| 63 |
+
"Stay away from the coastlines.",
|
| 64 |
+
"Follow evacuation routes and warnings.",
|
| 65 |
+
"Do not return until authorities declare it safe.",
|
| 66 |
+
"Avoid rivers and streams connected to the ocean."
|
| 67 |
+
],
|
| 68 |
+
"cyclone": [
|
| 69 |
+
"Stay indoors and away from windows.",
|
| 70 |
+
"Stock emergency supplies and drinking water.",
|
| 71 |
+
"Evacuate if instructed by authorities.",
|
| 72 |
+
"Stay tuned to weather alerts.",
|
| 73 |
+
"Secure loose outdoor items."
|
| 74 |
+
],
|
| 75 |
+
"drought": [
|
| 76 |
+
"Conserve water whenever possible.",
|
| 77 |
+
"Store extra drinking water.",
|
| 78 |
+
"Avoid unnecessary water usage.",
|
| 79 |
+
"Follow water rationing rules if issued.",
|
| 80 |
+
"Use water-efficient appliances."
|
| 81 |
+
],
|
| 82 |
+
"storm": [
|
| 83 |
+
"Stay indoors during severe storms.",
|
| 84 |
+
"Unplug electrical devices.",
|
| 85 |
+
"Avoid flood-prone areas.",
|
| 86 |
+
"Keep emergency supplies ready.",
|
| 87 |
+
"Listen to weather updates."
|
| 88 |
+
],
|
| 89 |
+
"tornado": [
|
| 90 |
+
"Take shelter in a basement or interior room.",
|
| 91 |
+
"Stay away from windows.",
|
| 92 |
+
"Protect your head and neck with sturdy coverings.",
|
| 93 |
+
"Avoid mobile homes or temporary shelters.",
|
| 94 |
+
"Monitor weather alerts closely."
|
| 95 |
+
],
|
| 96 |
+
"hurricane": [
|
| 97 |
+
"Evacuate coastal areas if warned.",
|
| 98 |
+
"Stay in a safe, reinforced building.",
|
| 99 |
+
"Stock up on food, water, and medicine.",
|
| 100 |
+
"Keep flashlights and batteries ready.",
|
| 101 |
+
"Avoid going outside during the hurricane."
|
| 102 |
+
]
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
def get_precautions(label: str):
|
| 106 |
+
return PRECAUTIONS.get(label.lower(), [
|
| 107 |
+
"Contact local authorities and follow official guidance.",
|
| 108 |
+
"Prepare an emergency kit.",
|
| 109 |
+
"Monitor verified alerts."
|
| 110 |
+
])
|
| 111 |
+
|
| 112 |
+
# ----------------- UI Layout -----------------
|
| 113 |
+
st.markdown("""
|
| 114 |
+
<h1 style='text-align: center; background-color: #008080; color: white; padding: 1rem; border-radius: 12px;'>
|
| 115 |
+
📷 Disaster Image Analysis
|
| 116 |
+
</h1>
|
| 117 |
+
""", unsafe_allow_html=True)
|
| 118 |
+
|
| 119 |
+
st.write("Upload an **image** (left) **or** pick a disaster **manually** (right). Manual choice overrides the model.")
|
| 120 |
+
|
| 121 |
+
col1, col2 = st.columns(2)
|
| 122 |
+
|
| 123 |
+
with col1:
|
| 124 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 125 |
+
|
| 126 |
+
with col2:
|
| 127 |
+
manual_disaster = st.selectbox(
|
| 128 |
+
"Manually select a disaster (optional):",
|
| 129 |
+
[
|
| 130 |
+
"None",
|
| 131 |
+
"fire",
|
| 132 |
+
"flood",
|
| 133 |
+
"landslide",
|
| 134 |
+
"earthquake",
|
| 135 |
+
"building_collapse",
|
| 136 |
+
"tsunami",
|
| 137 |
+
"cyclone",
|
| 138 |
+
"drought",
|
| 139 |
+
"storm",
|
| 140 |
+
"tornado",
|
| 141 |
+
"hurricane"
|
| 142 |
+
]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# ----------------- Disaster Detection Logic -----------------
|
| 146 |
+
disaster_label = None
|
| 147 |
+
confidence = None
|
| 148 |
+
source = None
|
| 149 |
+
|
| 150 |
+
if manual_disaster != "None":
|
| 151 |
+
disaster_label = manual_disaster
|
| 152 |
+
source = "manual"
|
| 153 |
+
|
| 154 |
+
elif uploaded_file is not None:
|
| 155 |
+
img = Image.open(uploaded_file).convert("RGB")
|
| 156 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 157 |
+
with st.spinner("Analyzing image..."):
|
| 158 |
+
results = classifier(img)
|
| 159 |
+
predicted_label = results[0]['label']
|
| 160 |
+
confidence = results[0]['score']
|
| 161 |
+
mapped = LABEL_MAP.get(predicted_label, "unknown")
|
| 162 |
+
disaster_label = mapped
|
| 163 |
+
source = "model"
|
| 164 |
+
|
| 165 |
+
st.info(f"Model prediction: **{disaster_label}** (raw: {predicted_label}, confidence: {confidence:.2%})")
|
| 166 |
+
|
| 167 |
+
# ----------------- Output Section -----------------
|
| 168 |
+
if disaster_label is None:
|
| 169 |
+
st.warning("Please upload an image or manually select a disaster type to see precautions.")
|
| 170 |
+
elif disaster_label == "no disaster":
|
| 171 |
+
st.info("Model says: **no disaster detected**. You can still select a disaster type on the right to view tips.")
|
| 172 |
+
else:
|
| 173 |
+
title = "User-selected disaster" if source == "manual" else "Model-detected disaster"
|
| 174 |
+
st.subheader(f"{title}: **{disaster_label}**")
|
| 175 |
+
if confidence is not None and source == "model":
|
| 176 |
+
st.caption(f"Confidence: {confidence:.2%}")
|
| 177 |
+
st.markdown("### Precautionary Measures")
|
| 178 |
+
for i, tip in enumerate(get_precautions(disaster_label), 1):
|
| 179 |
+
st.write(f"{i}. {tip}")
|
| 180 |
+
|
| 181 |
+
st.markdown("---")
|
| 182 |
+
if st.button("⬅ Back to Chat Assistant"):
|
| 183 |
+
st.switch_page("Home")
|