|
import streamlit as st |
|
import torch |
|
from transformers import ViTForImageClassification, ViTImageProcessor |
|
from PIL import Image |
|
import numpy as np |
|
from langchain.vectorstores import FAISS |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.document_loaders import TextLoader |
|
from langchain.text_splitter import CharacterTextSplitter |
|
import io |
|
import json |
|
|
|
|
|
model_name = "google/vit-base-patch16-224-in21k" |
|
processor = ViTImageProcessor.from_pretrained(model_name) |
|
model = ViTForImageClassification.from_pretrained(model_name) |
|
|
|
|
|
damage_types = [ |
|
"spalling", |
|
"reinforcement_corrosion", |
|
"flexural_crack", |
|
"structural_crack", |
|
"dampness", |
|
"impact_failure" |
|
] |
|
|
|
|
|
embeddings = HuggingFaceEmbeddings() |
|
knowledge_base = FAISS.load_local("knowledge_base", embeddings) |
|
|
|
def process_image(image): |
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
|
|
outputs = model(**inputs) |
|
probs = torch.nn.functional.softmax(outputs.logits, dim=-1) |
|
|
|
|
|
top_probs, top_indices = torch.topk(probs, len(damage_types)) |
|
|
|
return { |
|
damage_types[idx]: float(prob) |
|
for idx, prob in zip(top_indices[0], top_probs[0]) |
|
} |
|
|
|
def get_recommendations(damage_type): |
|
|
|
docs = knowledge_base.similarity_search( |
|
f"Remedial measures for {damage_type} in building structures", |
|
k=3 |
|
) |
|
return [doc.page_content for doc in docs] |
|
|
|
|
|
st.title("Structural Damage Assessment Tool") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload structural image", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file: |
|
|
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
with st.spinner("Analyzing image..."): |
|
predictions = process_image(image) |
|
|
|
|
|
st.subheader("Damage Assessment") |
|
for damage_type, probability in predictions.items(): |
|
st.progress(probability) |
|
st.write(f"{damage_type.replace('_', ' ').title()}: {probability:.2%}") |
|
|
|
|
|
if probability > 0.5: |
|
st.subheader(f"Recommendations for {damage_type.replace('_', ' ').title()}") |
|
recommendations = get_recommendations(damage_type) |
|
for i, rec in enumerate(recommendations, 1): |
|
st.write(f"{i}. {rec}") |
|
|
|
|
|
st.download_button( |
|
"Download Report", |
|
json.dumps(predictions, indent=2), |
|
"assessment_report.json", |
|
"application/json" |
|
) |