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 """ """ def display_header(): st.markdown( """
Advanced AI-powered Construction Defect tool
🏗️ Smart Construction Defect Analyzer | Built with Streamlit & Transformers
For professional use only. Always consult with a structural engineer.