Spaces:
Sleeping
Sleeping
Rename pages/Image Analysis.py to pages/image_analysis.py
Browse files
pages/{Image Analysis.py → image_analysis.py}
RENAMED
|
@@ -2,9 +2,6 @@ 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():
|
|
@@ -58,47 +55,9 @@ PRECAUTIONS = {
|
|
| 58 |
"Call emergency services immediately.",
|
| 59 |
"Help others only if it's safe to do so."
|
| 60 |
],
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
"Stay
|
| 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 |
|
|
@@ -109,20 +68,18 @@ def get_precautions(label: str):
|
|
| 109 |
"Monitor verified alerts."
|
| 110 |
])
|
| 111 |
|
| 112 |
-
# ----------------- UI
|
|
|
|
| 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):",
|
|
@@ -132,25 +89,20 @@ with col2:
|
|
| 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)
|
|
@@ -158,21 +110,19 @@ elif uploaded_file is not None:
|
|
| 158 |
results = classifier(img)
|
| 159 |
predicted_label = results[0]['label']
|
| 160 |
confidence = results[0]['score']
|
| 161 |
-
|
| 162 |
-
disaster_label = mapped
|
| 163 |
source = "model"
|
| 164 |
-
|
| 165 |
st.info(f"Model prediction: **{disaster_label}** (raw: {predicted_label}, confidence: {confidence:.2%})")
|
| 166 |
|
| 167 |
-
#
|
| 168 |
if disaster_label is None:
|
| 169 |
-
st.warning("Please upload an image or
|
| 170 |
elif disaster_label == "no disaster":
|
| 171 |
-
st.info("Model says: **no disaster detected**.
|
| 172 |
else:
|
| 173 |
title = "User-selected disaster" if source == "manual" else "Model-detected disaster"
|
| 174 |
st.subheader(f"{title}: **{disaster_label}**")
|
| 175 |
-
if confidence
|
| 176 |
st.caption(f"Confidence: {confidence:.2%}")
|
| 177 |
st.markdown("### Precautionary Measures")
|
| 178 |
for i, tip in enumerate(get_precautions(disaster_label), 1):
|
|
@@ -180,4 +130,4 @@ else:
|
|
| 180 |
|
| 181 |
st.markdown("---")
|
| 182 |
if st.button("⬅ Back to Chat Assistant"):
|
| 183 |
-
st.switch_page("
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
# ----------------- Model -----------------
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
|
|
|
| 55 |
"Call emergency services immediately.",
|
| 56 |
"Help others only if it's safe to do so."
|
| 57 |
],
|
| 58 |
+
"no disaster": [
|
| 59 |
+
"No immediate action needed.",
|
| 60 |
+
"Stay alert and monitor updates if conditions change."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
]
|
| 62 |
}
|
| 63 |
|
|
|
|
| 68 |
"Monitor verified alerts."
|
| 69 |
])
|
| 70 |
|
| 71 |
+
# ----------------- UI -----------------
|
| 72 |
+
st.set_page_config(page_title="Image Analysis", page_icon="🖼️")
|
| 73 |
st.markdown("""
|
| 74 |
<h1 style='text-align: center; background-color: #008080; color: white; padding: 1rem; border-radius: 12px;'>
|
| 75 |
📷 Disaster Image Analysis
|
| 76 |
</h1>
|
| 77 |
""", unsafe_allow_html=True)
|
| 78 |
|
|
|
|
|
|
|
| 79 |
col1, col2 = st.columns(2)
|
| 80 |
|
| 81 |
with col1:
|
| 82 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
|
|
|
| 83 |
with col2:
|
| 84 |
manual_disaster = st.selectbox(
|
| 85 |
"Manually select a disaster (optional):",
|
|
|
|
| 89 |
"flood",
|
| 90 |
"landslide",
|
| 91 |
"earthquake",
|
| 92 |
+
"building_collapse"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
]
|
| 94 |
)
|
| 95 |
|
|
|
|
| 96 |
disaster_label = None
|
| 97 |
confidence = None
|
| 98 |
source = None
|
| 99 |
|
| 100 |
+
# Manual override
|
| 101 |
if manual_disaster != "None":
|
| 102 |
disaster_label = manual_disaster
|
| 103 |
source = "manual"
|
| 104 |
|
| 105 |
+
# Otherwise, use model
|
| 106 |
elif uploaded_file is not None:
|
| 107 |
img = Image.open(uploaded_file).convert("RGB")
|
| 108 |
st.image(img, caption="Uploaded Image", use_column_width=True)
|
|
|
|
| 110 |
results = classifier(img)
|
| 111 |
predicted_label = results[0]['label']
|
| 112 |
confidence = results[0]['score']
|
| 113 |
+
disaster_label = LABEL_MAP.get(predicted_label, "unknown")
|
|
|
|
| 114 |
source = "model"
|
|
|
|
| 115 |
st.info(f"Model prediction: **{disaster_label}** (raw: {predicted_label}, confidence: {confidence:.2%})")
|
| 116 |
|
| 117 |
+
# Output
|
| 118 |
if disaster_label is None:
|
| 119 |
+
st.warning("Please upload an image or select a disaster manually.")
|
| 120 |
elif disaster_label == "no disaster":
|
| 121 |
+
st.info("Model says: **no disaster detected**.")
|
| 122 |
else:
|
| 123 |
title = "User-selected disaster" if source == "manual" else "Model-detected disaster"
|
| 124 |
st.subheader(f"{title}: **{disaster_label}**")
|
| 125 |
+
if confidence and source == "model":
|
| 126 |
st.caption(f"Confidence: {confidence:.2%}")
|
| 127 |
st.markdown("### Precautionary Measures")
|
| 128 |
for i, tip in enumerate(get_precautions(disaster_label), 1):
|
|
|
|
| 130 |
|
| 131 |
st.markdown("---")
|
| 132 |
if st.button("⬅ Back to Chat Assistant"):
|
| 133 |
+
st.switch_page("app")
|