Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,89 @@
|
|
| 1 |
-
#app.py
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 4 |
from PIL import Image
|
| 5 |
import torch
|
| 6 |
-
from sentence_transformers import SentenceTransformer
|
| 7 |
-
import faiss
|
| 8 |
-
import pandas as pd
|
| 9 |
-
import os
|
| 10 |
-
from pathlib import Path
|
| 11 |
import json
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
DAMAGE_TYPES = {
|
| 14 |
0: {'name': 'spalling', 'risk': 'High'},
|
| 15 |
1: {'name': 'reinforcement_corrosion', 'risk': 'Critical'},
|
|
@@ -19,81 +93,14 @@ DAMAGE_TYPES = {
|
|
| 19 |
}
|
| 20 |
|
| 21 |
@st.cache_resource
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
"google/vit-base-patch16-224",
|
| 25 |
num_labels=len(DAMAGE_TYPES),
|
| 26 |
ignore_mismatched_sizes=True
|
| 27 |
)
|
| 28 |
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
| 29 |
-
|
| 30 |
-
return vision_model, processor, embedding_model
|
| 31 |
-
|
| 32 |
-
class DamageKnowledgeBase:
|
| 33 |
-
def __init__(self, embedding_model):
|
| 34 |
-
self.embedding_model = embedding_model
|
| 35 |
-
self.load_knowledge_base()
|
| 36 |
-
|
| 37 |
-
def load_knowledge_base(self):
|
| 38 |
-
# Load dataset metadata and embeddings
|
| 39 |
-
knowledge_path = Path("data/knowledge_base.json")
|
| 40 |
-
if knowledge_path.exists():
|
| 41 |
-
with open(knowledge_path, 'r') as f:
|
| 42 |
-
self.kb_data = json.load(f)
|
| 43 |
-
|
| 44 |
-
# Initialize FAISS index
|
| 45 |
-
embeddings = torch.load("data/embeddings.pt")
|
| 46 |
-
self.index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 47 |
-
self.index.add(embeddings.numpy())
|
| 48 |
-
else:
|
| 49 |
-
self.initialize_knowledge_base()
|
| 50 |
-
|
| 51 |
-
def initialize_knowledge_base(self):
|
| 52 |
-
# Sample knowledge base structure
|
| 53 |
-
self.kb_data = {
|
| 54 |
-
'spalling': [
|
| 55 |
-
{
|
| 56 |
-
'description': 'Severe concrete spalling on column surface',
|
| 57 |
-
'severity': 'High',
|
| 58 |
-
'repair_method': 'Remove damaged concrete, clean reinforcement, apply repair mortar',
|
| 59 |
-
'estimated_cost': 'High',
|
| 60 |
-
'timeframe': '2-3 weeks',
|
| 61 |
-
'similar_cases': ['case_123', 'case_456']
|
| 62 |
-
}
|
| 63 |
-
],
|
| 64 |
-
# Add more damage types...
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
# Create embeddings
|
| 68 |
-
texts = []
|
| 69 |
-
for damage_type, cases in self.kb_data.items():
|
| 70 |
-
for case in cases:
|
| 71 |
-
texts.append(f"{damage_type} {case['description']} {case['repair_method']}")
|
| 72 |
-
|
| 73 |
-
embeddings = self.embedding_model.encode(texts)
|
| 74 |
-
self.index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 75 |
-
self.index.add(embeddings)
|
| 76 |
-
|
| 77 |
-
# Save for future use
|
| 78 |
-
os.makedirs("data", exist_ok=True)
|
| 79 |
-
with open("data/knowledge_base.json", 'w') as f:
|
| 80 |
-
json.dump(self.kb_data, f)
|
| 81 |
-
torch.save(torch.tensor(embeddings), "data/embeddings.pt")
|
| 82 |
-
|
| 83 |
-
def query(self, damage_type, confidence):
|
| 84 |
-
query = f"damage type: {damage_type}"
|
| 85 |
-
query_embedding = self.embedding_model.encode([query])
|
| 86 |
-
D, I = self.index.search(query_embedding, k=3)
|
| 87 |
-
|
| 88 |
-
similar_cases = []
|
| 89 |
-
for idx in I[0]:
|
| 90 |
-
for damage, cases in self.kb_data.items():
|
| 91 |
-
for case in cases:
|
| 92 |
-
case_text = f"{damage} {case['description']} {case['repair_method']}"
|
| 93 |
-
if len(similar_cases) < 3:
|
| 94 |
-
similar_cases.append(case)
|
| 95 |
-
|
| 96 |
-
return similar_cases
|
| 97 |
|
| 98 |
def analyze_damage(image, model, processor):
|
| 99 |
image = image.convert('RGB')
|
|
@@ -103,48 +110,108 @@ def analyze_damage(image, model, processor):
|
|
| 103 |
return probs
|
| 104 |
|
| 105 |
def main():
|
| 106 |
-
st.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
if uploaded_file:
|
| 114 |
image = Image.open(uploaded_file)
|
| 115 |
-
st.image(image, caption="Uploaded Structure", use_column_width=True)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
for idx, prob in enumerate(predictions):
|
| 126 |
confidence = float(prob) * 100
|
| 127 |
if confidence > 15:
|
| 128 |
damage_type = DAMAGE_TYPES[idx]['name']
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
st.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
st.
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
main()
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
# Embedded knowledge base
|
| 8 |
+
KNOWLEDGE_BASE = {
|
| 9 |
+
"spalling": [
|
| 10 |
+
{
|
| 11 |
+
"severity": "High",
|
| 12 |
+
"description": "Severe concrete spalling with exposed reinforcement",
|
| 13 |
+
"repair_method": ["Remove damaged concrete", "Clean exposed reinforcement", "Apply rust inhibitor", "Apply bonding agent", "Patch with repair mortar"],
|
| 14 |
+
"estimated_cost": "High ($5,000-$10,000)",
|
| 15 |
+
"timeframe": "2-3 weeks",
|
| 16 |
+
"location": "Column/Beam",
|
| 17 |
+
"required_expertise": "Structural Engineer",
|
| 18 |
+
"immediate_action": "Area isolation and temporary support if structural",
|
| 19 |
+
"prevention": "Regular maintenance, waterproofing, proper concrete cover"
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"severity": "Medium",
|
| 23 |
+
"description": "Surface spalling without exposed reinforcement",
|
| 24 |
+
"repair_method": ["Remove loose concrete", "Clean surface", "Apply repair mortar", "Surface treatment"],
|
| 25 |
+
"estimated_cost": "Medium ($2,000-$5,000)",
|
| 26 |
+
"timeframe": "1-2 weeks",
|
| 27 |
+
"location": "Non-structural elements",
|
| 28 |
+
"required_expertise": "Concrete Repair Specialist",
|
| 29 |
+
"immediate_action": "Mark affected areas and monitor",
|
| 30 |
+
"prevention": "Surface coating, regular inspections"
|
| 31 |
+
}
|
| 32 |
+
],
|
| 33 |
+
"reinforcement_corrosion": [
|
| 34 |
+
{
|
| 35 |
+
"severity": "Critical",
|
| 36 |
+
"description": "Advanced corrosion with significant section loss",
|
| 37 |
+
"repair_method": ["Install temporary support", "Remove damaged concrete", "Replace/supplement reinforcement", "Apply corrosion inhibitor", "Reconstruct concrete cover"],
|
| 38 |
+
"estimated_cost": "Very High ($15,000+)",
|
| 39 |
+
"timeframe": "3-4 weeks",
|
| 40 |
+
"location": "Primary structural elements",
|
| 41 |
+
"required_expertise": "Structural Engineer, Specialist Contractor",
|
| 42 |
+
"immediate_action": "Immediate area evacuation and temporary support",
|
| 43 |
+
"prevention": "Waterproofing, crack sealing, chloride protection"
|
| 44 |
+
}
|
| 45 |
+
],
|
| 46 |
+
"structural_crack": [
|
| 47 |
+
{
|
| 48 |
+
"severity": "High",
|
| 49 |
+
"description": "Load-bearing element cracks >3mm",
|
| 50 |
+
"repair_method": ["Structural assessment", "Crack injection with epoxy", "External reinforcement if needed", "Monitor crack progression"],
|
| 51 |
+
"estimated_cost": "High ($8,000-$15,000)",
|
| 52 |
+
"timeframe": "2-3 weeks",
|
| 53 |
+
"location": "Load-bearing walls/beams",
|
| 54 |
+
"required_expertise": "Structural Engineer",
|
| 55 |
+
"immediate_action": "Install crack gauges, temporary support",
|
| 56 |
+
"prevention": "proper design, load management, movement joints"
|
| 57 |
+
}
|
| 58 |
+
],
|
| 59 |
+
"dampness": [
|
| 60 |
+
{
|
| 61 |
+
"severity": "Medium",
|
| 62 |
+
"description": "Persistent moisture penetration",
|
| 63 |
+
"repair_method": ["Identify water source", "Install drainage system", "Apply waterproofing membrane", "Improve ventilation"],
|
| 64 |
+
"estimated_cost": "Medium ($3,000-$7,000)",
|
| 65 |
+
"timeframe": "1-2 weeks",
|
| 66 |
+
"location": "Walls, floors",
|
| 67 |
+
"required_expertise": "Waterproofing Specialist",
|
| 68 |
+
"immediate_action": "Improve ventilation, dehumidification",
|
| 69 |
+
"prevention": "Proper drainage, regular maintenance of water systems"
|
| 70 |
+
}
|
| 71 |
+
],
|
| 72 |
+
"no_damage": [
|
| 73 |
+
{
|
| 74 |
+
"severity": "Low",
|
| 75 |
+
"description": "No visible structural issues",
|
| 76 |
+
"repair_method": ["Regular inspection", "Preventive maintenance", "Document condition"],
|
| 77 |
+
"estimated_cost": "Low ($500-$1,000)",
|
| 78 |
+
"timeframe": "1-2 days",
|
| 79 |
+
"location": "General structure",
|
| 80 |
+
"required_expertise": "Building Inspector",
|
| 81 |
+
"immediate_action": "Continue regular maintenance",
|
| 82 |
+
"prevention": "Maintain inspection schedule"
|
| 83 |
+
}
|
| 84 |
+
]
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
DAMAGE_TYPES = {
|
| 88 |
0: {'name': 'spalling', 'risk': 'High'},
|
| 89 |
1: {'name': 'reinforcement_corrosion', 'risk': 'Critical'},
|
|
|
|
| 93 |
}
|
| 94 |
|
| 95 |
@st.cache_resource
|
| 96 |
+
def load_model():
|
| 97 |
+
model = ViTForImageClassification.from_pretrained(
|
| 98 |
"google/vit-base-patch16-224",
|
| 99 |
num_labels=len(DAMAGE_TYPES),
|
| 100 |
ignore_mismatched_sizes=True
|
| 101 |
)
|
| 102 |
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
| 103 |
+
return model, processor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
def analyze_damage(image, model, processor):
|
| 106 |
image = image.convert('RGB')
|
|
|
|
| 110 |
return probs
|
| 111 |
|
| 112 |
def main():
|
| 113 |
+
st.set_page_config(
|
| 114 |
+
page_title="Structural Damage Analyzer",
|
| 115 |
+
page_icon="🏗️",
|
| 116 |
+
layout="wide",
|
| 117 |
+
initial_sidebar_state="expanded"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Custom CSS
|
| 121 |
+
st.markdown("""
|
| 122 |
+
<style>
|
| 123 |
+
.main {
|
| 124 |
+
padding: 2rem;
|
| 125 |
+
}
|
| 126 |
+
.stProgress > div > div > div > div {
|
| 127 |
+
background-image: linear-gradient(to right, #ff6b6b, #f06595);
|
| 128 |
+
}
|
| 129 |
+
.damage-card {
|
| 130 |
+
padding: 1.5rem;
|
| 131 |
+
border-radius: 0.5rem;
|
| 132 |
+
background: #f8f9fa;
|
| 133 |
+
margin-bottom: 1rem;
|
| 134 |
+
}
|
| 135 |
+
</style>
|
| 136 |
+
""", unsafe_allow_html=True)
|
| 137 |
+
|
| 138 |
+
col1, col2, col3 = st.columns([1, 3, 1])
|
| 139 |
+
with col2:
|
| 140 |
+
st.title("🏗️ Structural Damage Analyzer")
|
| 141 |
+
st.markdown("### Upload a photo of structural damage for instant analysis")
|
| 142 |
+
|
| 143 |
+
model, processor = load_model()
|
| 144 |
|
| 145 |
+
upload_col1, upload_col2, upload_col3 = st.columns([1, 2, 1])
|
| 146 |
+
with upload_col2:
|
| 147 |
+
uploaded_file = st.file_uploader("Choose an image file", type=['jpg', 'jpeg', 'png'])
|
| 148 |
+
|
| 149 |
if uploaded_file:
|
| 150 |
image = Image.open(uploaded_file)
|
|
|
|
| 151 |
|
| 152 |
+
analysis_col1, analysis_col2 = st.columns(2)
|
| 153 |
+
|
| 154 |
+
with analysis_col1:
|
| 155 |
+
st.image(image, caption="Uploaded Structure", use_column_width=True)
|
| 156 |
+
|
| 157 |
+
with analysis_col2:
|
| 158 |
+
with st.spinner("🔍 Analyzing structural damage..."):
|
| 159 |
+
predictions = analyze_damage(image, model, processor)
|
| 160 |
+
|
| 161 |
+
st.markdown("### 📊 Damage Assessment Results")
|
| 162 |
+
|
| 163 |
for idx, prob in enumerate(predictions):
|
| 164 |
confidence = float(prob) * 100
|
| 165 |
if confidence > 15:
|
| 166 |
damage_type = DAMAGE_TYPES[idx]['name']
|
| 167 |
+
cases = KNOWLEDGE_BASE[damage_type]
|
| 168 |
+
|
| 169 |
+
st.markdown(f"""
|
| 170 |
+
<div class="damage-card">
|
| 171 |
+
<h4>{damage_type.replace('_', ' ').title()} Detected</h4>
|
| 172 |
+
</div>
|
| 173 |
+
""", unsafe_allow_html=True)
|
| 174 |
+
|
| 175 |
+
col1, col2 = st.columns([3, 1])
|
| 176 |
+
with col1:
|
| 177 |
+
st.progress(confidence / 100)
|
| 178 |
+
with col2:
|
| 179 |
+
st.write(f"Confidence: {confidence:.1f}%")
|
| 180 |
|
| 181 |
+
tabs = st.tabs(["Details", "Repair Methods", "Recommendations"])
|
| 182 |
+
|
| 183 |
+
with tabs[0]:
|
| 184 |
+
for case in cases:
|
| 185 |
+
st.markdown(f"""
|
| 186 |
+
- **Severity:** {case['severity']}
|
| 187 |
+
- **Description:** {case['description']}
|
| 188 |
+
- **Location:** {case['location']}
|
| 189 |
+
- **Required Expertise:** {case['required_expertise']}
|
| 190 |
+
""")
|
| 191 |
+
|
| 192 |
+
with tabs[1]:
|
| 193 |
+
st.markdown("#### Repair Steps")
|
| 194 |
+
for step in cases[0]['repair_method']:
|
| 195 |
+
st.markdown(f"- {step}")
|
| 196 |
+
st.markdown(f"""
|
| 197 |
+
- **Estimated Cost:** {cases[0]['estimated_cost']}
|
| 198 |
+
- **Timeframe:** {cases[0]['timeframe']}
|
| 199 |
+
""")
|
| 200 |
+
|
| 201 |
+
with tabs[2]:
|
| 202 |
+
st.markdown("#### Immediate Actions")
|
| 203 |
+
st.warning(cases[0]['immediate_action'])
|
| 204 |
+
|
| 205 |
+
st.markdown("#### Prevention Measures")
|
| 206 |
+
st.info(cases[0]['prevention'])
|
| 207 |
+
|
| 208 |
+
# Footer
|
| 209 |
+
st.markdown("---")
|
| 210 |
+
st.markdown("""
|
| 211 |
+
<div style='text-align: center'>
|
| 212 |
+
<p>🏗️ Structural Damage Analysis Tool | Built with Streamlit</p>
|
| 213 |
+
</div>
|
| 214 |
+
""", unsafe_allow_html=True)
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
main()
|