Spaces:
Sleeping
Sleeping
File size: 12,909 Bytes
e0fdb55 598b1e5 e0fdb55 598b1e5 3d49530 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 e0fdb55 598b1e5 e0fdb55 598b1e5 e0fdb55 f17ccd4 598b1e5 e0fdb55 598b1e5 3d49530 598b1e5 e0fdb55 598b1e5 f17ccd4 3d49530 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 e0fdb55 598b1e5 80c792f 598b1e5 f17ccd4 598b1e5 e0fdb55 598b1e5 3d49530 f17ccd4 598b1e5 f17ccd4 598b1e5 f17ccd4 3d49530 598b1e5 f17ccd4 598b1e5 e0fdb55 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
import streamlit as st
from transformers import ViTForImageClassification, ViTImageProcessor
from PIL import Image
import torch
import time
import io
import base64
# Cache the model globally
MODEL = None
PROCESSOR = None
# Embedded knowledge base
# Knowledge base
KNOWLEDGE_BASE = {
"spalling": [
{
"severity": "Critical",
"description": "Severe concrete spalling with exposed reinforcement and section loss",
"repair_method": ["Install temporary support", "Remove deteriorated concrete", "Clean and treat reinforcement", "Apply corrosion inhibitor", "Apply bonding agent", "High-strength repair mortar"],
"estimated_cost": "Very High ($15,000+)",
"timeframe": "3-4 weeks",
"location": "Primary structural elements",
"required_expertise": "Structural Engineer + Specialist Contractor",
"immediate_action": "Evacuate area, install temporary support, prevent access",
"prevention": "Regular inspections, waterproofing, chloride protection"
},
{
"severity": "High",
"description": "Surface spalling with visible reinforcement",
"repair_method": ["Remove damaged concrete", "Treat reinforcement", "Apply repair mortar", "Surface treatment"],
"estimated_cost": "High ($8,000-$15,000)",
"timeframe": "2-3 weeks",
"location": "Structural elements",
"required_expertise": "Structural Engineer",
"immediate_action": "Area isolation, temporary support assessment",
"prevention": "Protective coatings, drainage improvement"
}
],
"reinforcement_corrosion": [
{
"severity": "Critical",
"description": "Severe corrosion with >30% section loss",
"repair_method": ["Structural support", "Remove concrete", "Replace reinforcement", "Corrosion protection", "Concrete repair"],
"estimated_cost": "Critical ($20,000+)",
"timeframe": "4-6 weeks",
"location": "Load-bearing elements",
"required_expertise": "Senior Structural Engineer",
"immediate_action": "Immediate evacuation, emergency shoring",
"prevention": "Waterproofing, cathodic protection"
}
],
"structural_crack": [
{
"severity": "High",
"description": "Cracks >5mm in structural elements",
"repair_method": ["Structural analysis", "Epoxy injection", "Carbon fiber reinforcement", "Crack monitoring"],
"estimated_cost": "High ($10,000-$20,000)",
"timeframe": "2-4 weeks",
"location": "Primary structure",
"required_expertise": "Structural Engineer",
"immediate_action": "Install crack monitors, load restriction",
"prevention": "Load management, joint maintenance"
}
],
"dampness": [
{
"severity": "Medium",
"description": "Active water penetration with efflorescence",
"repair_method": ["Water source identification", "Drainage improvement", "Waterproof membrane", "Ventilation"],
"estimated_cost": "Medium ($5,000-$10,000)",
"timeframe": "1-2 weeks",
"location": "Various",
"required_expertise": "Waterproofing Specialist",
"immediate_action": "Dehumidification, efflorescence cleaning",
"prevention": "Proper drainage, vapor barriers"
}
],
"no_damage": [
{
"severity": "Low",
"description": "No significant structural issues",
"repair_method": ["Regular inspection", "Preventive maintenance"],
"estimated_cost": "Low ($500-$2,000)",
"timeframe": "1-2 days",
"location": "General",
"required_expertise": "Building Inspector",
"immediate_action": "Continue monitoring",
"prevention": "Regular maintenance schedule"
}
]
}
DAMAGE_TYPES = {
0: {'name': 'spalling', 'risk': 'High', 'color': '#ff4d4d'},
1: {'name': 'reinforcement_corrosion', 'risk': 'Critical', 'color': '#800000'},
2: {'name': 'structural_crack', 'risk': 'High', 'color': '#ff6b6b'},
3: {'name': 'dampness', 'risk': 'Medium', 'color': '#4dabf7'},
4: {'name': 'no_damage', 'risk': 'Low', 'color': '#40c057'}
}
def init_session_state():
if 'history' not in st.session_state:
st.session_state.history = []
if 'dark_mode' not in st.session_state:
st.session_state.dark_mode = False
@st.cache_resource
def load_model():
try:
model = ViTForImageClassification.from_pretrained(
"google/vit-base-patch16-224",
num_labels=len(DAMAGE_TYPES),
ignore_mismatched_sizes=True
)
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
return model, processor
except Exception as e:
st.error(f"Error loading model: {str(e)}")
return None, None
def analyze_damage(image, model, processor):
try:
image = image.convert('RGB')
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
return probs
except Exception as e:
st.error(f"Error analyzing image: {str(e)}")
return None
def get_custom_css():
return """
<style>
.main {
padding: 2rem;
}
.stProgress > div > div > div > div {
background-image: linear-gradient(to right, var(--progress-color, #ff6b6b), var(--progress-color-end, #f06595));
}
.damage-card {
padding: 1.5rem;
border-radius: 0.5rem;
background: var(--card-bg, #f8f9fa);
margin-bottom: 1rem;
border: 1px solid var(--border-color, #dee2e6);
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.damage-header {
font-size: 1.25rem;
font-weight: bold;
margin-bottom: 1rem;
color: var(--text-color, #212529);
}
.dark-mode {
background-color: #1a1a1a;
color: #ffffff;
}
.dark-mode .damage-card {
background: #2d2d2d;
border-color: #404040;
}
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted #ccc;
}
.tooltip .tooltiptext {
visibility: hidden;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
</style>
"""
def display_header():
st.markdown(
"""
<div style='text-align: center; padding: 1rem;'>
<h1>ποΈ Smart Construction Defect Analyzer</h1>
<p style='font-size: 1.2rem;'>Advanced AI-powered Construction Defect tool</p>
</div>
""",
unsafe_allow_html=True
)
def main():
init_session_state()
st.set_page_config(
page_title="Smart Construction Defect Analyzer",
page_icon="ποΈ",
layout="wide",
initial_sidebar_state="expanded"
)
st.markdown(get_custom_css(), unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.markdown("### βοΈ Settings")
st.session_state.dark_mode = st.toggle("Dark Mode", st.session_state.dark_mode)
st.markdown("### π Analysis History")
if st.session_state.history:
for item in st.session_state.history[-5:]:
st.markdown(f"- {item}")
display_header()
# Load model
global MODEL, PROCESSOR
if MODEL is None or PROCESSOR is None:
with st.spinner("Loading AI model..."):
MODEL, PROCESSOR = load_model()
if MODEL is None:
st.error("Failed to load model. Please refresh the page.")
return
# File upload with drag and drop
uploaded_file = st.file_uploader(
"Drag and drop or click to upload an image",
type=['jpg', 'jpeg', 'png'],
help="Supported formats: JPG, JPEG, PNG"
)
if uploaded_file:
try:
# Image display and analysis
image = Image.open(uploaded_file)
col1, col2 = st.columns([1, 1])
with col1:
st.image(image, caption="Uploaded Structure", use_container_width =True)
with col2:
with st.spinner("π Analyzing damage..."):
start_time = time.time()
predictions = analyze_damage(image, MODEL, PROCESSOR)
analysis_time = time.time() - start_time
if predictions is not None:
st.markdown("### π Analysis Results")
st.markdown(f"*Analysis completed in {analysis_time:.2f} seconds*")
detected = False
for idx, prob in enumerate(predictions):
confidence = float(prob) * 100
if confidence > 15:
detected = True
damage_type = DAMAGE_TYPES[idx]['name']
cases = KNOWLEDGE_BASE[damage_type]
with st.expander(f"{damage_type.replace('_', ' ').title()} - {confidence:.1f}%", expanded=True):
# Progress bar with custom color
st.markdown(
f"""
<style>
.stProgress > div > div > div > div {{
background-color: {DAMAGE_TYPES[idx]['color']} !important;
}}
</style>
""",
unsafe_allow_html=True
)
st.progress(confidence / 100)
tabs = st.tabs(["π Details", "π§ Repairs", "β οΈ Actions"])
with tabs[0]:
for case in cases:
st.markdown(f"""
- **Severity:** {case['severity']}
- **Description:** {case['description']}
- **Location:** {case['location']}
- **Required Expertise:** {case['required_expertise']}
""")
with tabs[1]:
for step in cases[0]['repair_method']:
st.markdown(f"β {step}")
st.info(f"**Estimated Cost:** {cases[0]['estimated_cost']}")
st.info(f"**Timeframe:** {cases[0]['timeframe']}")
with tabs[2]:
st.warning("**Immediate Actions Required:**")
st.markdown(cases[0]['immediate_action'])
st.success("**Prevention Measures:**")
st.markdown(cases[0]['prevention'])
if not detected:
st.info("No significant Construction Defect detected. Regular maintenance recommended.")
# Add to history
st.session_state.history.append(f"Analyzed image: {uploaded_file.name}")
except Exception as e:
st.error(f"Error processing image: {str(e)}")
st.info("Please try uploading a different image.")
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center'>
<p>ποΈ Smart Construction Defect Analyzer | Built with Streamlit & Transformers</p>
<p style='font-size: 0.8rem;'>For professional use only. Always consult with a structural engineer.</p>
</div>
""",
unsafe_allow_html=True
)
if __name__ == "__main__":
main() |