Update app.py
Browse files
app.py
CHANGED
|
@@ -4,26 +4,10 @@ from PIL import Image
|
|
| 4 |
import torch
|
| 5 |
import time
|
| 6 |
import gc
|
| 7 |
-
from knowledge_base import KNOWLEDGE_BASE, DAMAGE_TYPES
|
| 8 |
-
from rag_utils import RAGSystem
|
| 9 |
-
import os
|
| 10 |
|
| 11 |
# Constants
|
| 12 |
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
| 13 |
MAX_IMAGE_SIZE = 1024 # Maximum dimension for images
|
| 14 |
-
MODEL_NAME = "google/vit-base-patch16-224"
|
| 15 |
-
CACHE_DIR = "/tmp/model_cache" # HF Spaces compatible cache directory
|
| 16 |
-
|
| 17 |
-
# Ensure cache directory exists
|
| 18 |
-
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 19 |
-
|
| 20 |
-
# Initialize session state for caching
|
| 21 |
-
if 'model' not in st.session_state:
|
| 22 |
-
st.session_state.model = None
|
| 23 |
-
if 'processor' not in st.session_state:
|
| 24 |
-
st.session_state.processor = None
|
| 25 |
-
if 'rag_system' not in st.session_state:
|
| 26 |
-
st.session_state.rag_system = None
|
| 27 |
|
| 28 |
def cleanup_memory():
|
| 29 |
"""Clean up memory and GPU cache"""
|
|
@@ -31,243 +15,143 @@ def cleanup_memory():
|
|
| 31 |
if torch.cuda.is_available():
|
| 32 |
torch.cuda.empty_cache()
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
@st.cache_resource(show_spinner="Loading AI model...")
|
| 35 |
def load_model():
|
| 36 |
-
"""Load and cache the model and processor
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
processor = ViTImageProcessor.from_pretrained(
|
| 40 |
-
|
| 41 |
-
cache_dir=CACHE_DIR,
|
| 42 |
-
local_files_only=False
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Determine device - prefer CPU on Hugging Face Spaces
|
| 46 |
-
device = "cpu" # Default to CPU for stability
|
| 47 |
-
|
| 48 |
-
# Load model with specific configuration
|
| 49 |
model = ViTForImageClassification.from_pretrained(
|
| 50 |
-
|
| 51 |
num_labels=len(DAMAGE_TYPES),
|
| 52 |
ignore_mismatched_sizes=True,
|
| 53 |
-
cache_dir=CACHE_DIR,
|
| 54 |
-
local_files_only=False
|
| 55 |
).to(device)
|
| 56 |
-
|
| 57 |
-
model.eval() # Set to evaluation mode
|
| 58 |
return model, processor
|
| 59 |
except Exception as e:
|
| 60 |
st.error(f"Error loading model: {str(e)}")
|
| 61 |
-
st.info("Attempting to reload model... Please wait.")
|
| 62 |
-
cleanup_memory()
|
| 63 |
return None, None
|
| 64 |
|
| 65 |
-
def
|
| 66 |
-
"""
|
| 67 |
-
if
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
except Exception as e:
|
| 72 |
-
st.error(f"Error initializing RAG system: {str(e)}")
|
| 73 |
-
st.session_state.rag_system = None
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
"""
|
| 77 |
try:
|
| 78 |
-
|
| 79 |
-
if image.mode != 'RGB':
|
| 80 |
-
image = image.convert('RGB')
|
| 81 |
-
|
| 82 |
-
# Resize if needed
|
| 83 |
if max(image.size) > MAX_IMAGE_SIZE:
|
| 84 |
ratio = MAX_IMAGE_SIZE / max(image.size)
|
| 85 |
new_size = tuple([int(dim * ratio) for dim in image.size])
|
| 86 |
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
| 87 |
-
|
| 88 |
return image
|
| 89 |
except Exception as e:
|
| 90 |
st.error(f"Error processing image: {str(e)}")
|
| 91 |
return None
|
| 92 |
|
| 93 |
def analyze_damage(image, model, processor):
|
| 94 |
-
"""Analyze structural damage
|
| 95 |
try:
|
| 96 |
device = next(model.parameters()).device
|
| 97 |
with torch.no_grad():
|
| 98 |
-
|
| 99 |
inputs = processor(images=image, return_tensors="pt")
|
| 100 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 101 |
-
|
| 102 |
-
# Run inference
|
| 103 |
outputs = model(**inputs)
|
| 104 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
| 105 |
-
|
| 106 |
-
# Clean up
|
| 107 |
cleanup_memory()
|
| 108 |
return probs.cpu()
|
| 109 |
except RuntimeError as e:
|
| 110 |
if "out of memory" in str(e):
|
| 111 |
cleanup_memory()
|
| 112 |
-
st.error("
|
| 113 |
-
# Retry with smaller image
|
| 114 |
-
image = image.resize((224, 224), Image.Resampling.LANCZOS)
|
| 115 |
-
return analyze_damage(image, model, processor)
|
| 116 |
else:
|
| 117 |
-
st.error(f"Error
|
| 118 |
-
return None
|
| 119 |
-
except Exception as e:
|
| 120 |
-
st.error(f"Unexpected error: {str(e)}")
|
| 121 |
return None
|
| 122 |
|
| 123 |
-
def display_analysis_results(predictions, analysis_time):
|
| 124 |
-
"""Display analysis results with enhanced visualization and error handling"""
|
| 125 |
-
try:
|
| 126 |
-
st.markdown("### 📊 Analysis Results")
|
| 127 |
-
st.markdown(f"*Analysis completed in {analysis_time:.2f} seconds*")
|
| 128 |
-
|
| 129 |
-
detected = False
|
| 130 |
-
for idx, prob in enumerate(predictions):
|
| 131 |
-
confidence = float(prob) * 100
|
| 132 |
-
if confidence > 15: # Threshold for displaying results
|
| 133 |
-
detected = True
|
| 134 |
-
damage_type = DAMAGE_TYPES[idx]['name']
|
| 135 |
-
risk_level = DAMAGE_TYPES[idx]['risk']
|
| 136 |
-
|
| 137 |
-
# Create expander with color-coded header
|
| 138 |
-
with st.expander(
|
| 139 |
-
f"🔍 {damage_type.replace('_', ' ').title()} - {confidence:.1f}% ({risk_level})",
|
| 140 |
-
expanded=True
|
| 141 |
-
):
|
| 142 |
-
# Display confidence bar
|
| 143 |
-
st.progress(confidence / 100)
|
| 144 |
-
|
| 145 |
-
# Create tabs for organized information
|
| 146 |
-
details_tab, repair_tab, action_tab = st.tabs([
|
| 147 |
-
"📋 Details", "🔧 Repair Plan", "⚠️ Actions Needed"
|
| 148 |
-
])
|
| 149 |
-
|
| 150 |
-
with details_tab:
|
| 151 |
-
display_damage_details(damage_type, confidence)
|
| 152 |
-
|
| 153 |
-
with repair_tab:
|
| 154 |
-
display_repair_plan(damage_type)
|
| 155 |
-
|
| 156 |
-
with action_tab:
|
| 157 |
-
display_action_items(damage_type)
|
| 158 |
-
|
| 159 |
-
# Display enhanced analysis if RAG system is available
|
| 160 |
-
if st.session_state.rag_system:
|
| 161 |
-
display_enhanced_analysis(damage_type, confidence)
|
| 162 |
-
|
| 163 |
-
if not detected:
|
| 164 |
-
st.success("No significant structural damage detected. Regular maintenance recommended.")
|
| 165 |
-
|
| 166 |
-
except Exception as e:
|
| 167 |
-
st.error(f"Error displaying results: {str(e)}")
|
| 168 |
-
|
| 169 |
def main():
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
layout="wide",
|
| 177 |
-
initial_sidebar_state="expanded"
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
-
# Custom CSS
|
| 181 |
-
st.markdown(get_custom_css(), unsafe_allow_html=True)
|
| 182 |
-
|
| 183 |
-
# Header
|
| 184 |
-
display_header()
|
| 185 |
-
|
| 186 |
-
# Initialize systems
|
| 187 |
-
if st.session_state.model is None or st.session_state.processor is None:
|
| 188 |
-
with st.spinner("Initializing AI model..."):
|
| 189 |
-
model, processor = load_model()
|
| 190 |
-
if model is None:
|
| 191 |
-
st.error("Failed to initialize model. Please refresh the page.")
|
| 192 |
-
return
|
| 193 |
-
st.session_state.model = model
|
| 194 |
-
st.session_state.processor = processor
|
| 195 |
-
|
| 196 |
-
init_rag_system()
|
| 197 |
-
|
| 198 |
-
# File upload section
|
| 199 |
-
uploaded_file = st.file_uploader(
|
| 200 |
-
"Upload structural image for analysis",
|
| 201 |
-
type=['jpg', 'jpeg', 'png'],
|
| 202 |
-
help="Maximum file size: 5MB"
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
if uploaded_file:
|
| 206 |
-
process_uploaded_file(uploaded_file)
|
| 207 |
-
|
| 208 |
-
# Footer
|
| 209 |
-
display_footer()
|
| 210 |
-
|
| 211 |
-
except Exception as e:
|
| 212 |
-
st.error(f"Application error: {str(e)}")
|
| 213 |
-
st.info("Please refresh the page and try again.")
|
| 214 |
-
cleanup_memory()
|
| 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 |
if predictions is not None:
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
| 250 |
|
| 251 |
-
|
| 252 |
-
""
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
padding: 1rem;
|
| 263 |
-
border-radius: 0.5rem;
|
| 264 |
-
background: var(--background-color, #ffffff);
|
| 265 |
-
margin-bottom: 1rem;
|
| 266 |
-
border: 1px solid var(--border-color, #e0e0e0);
|
| 267 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 268 |
-
}
|
| 269 |
-
</style>
|
| 270 |
-
"""
|
| 271 |
|
| 272 |
if __name__ == "__main__":
|
| 273 |
main()
|
|
|
|
| 4 |
import torch
|
| 5 |
import time
|
| 6 |
import gc
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Constants
|
| 9 |
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
| 10 |
MAX_IMAGE_SIZE = 1024 # Maximum dimension for images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def cleanup_memory():
|
| 13 |
"""Clean up memory and GPU cache"""
|
|
|
|
| 15 |
if torch.cuda.is_available():
|
| 16 |
torch.cuda.empty_cache()
|
| 17 |
|
| 18 |
+
def init_session_state():
|
| 19 |
+
"""Initialize session state variables"""
|
| 20 |
+
if 'history' not in st.session_state:
|
| 21 |
+
st.session_state.history = []
|
| 22 |
+
if 'dark_mode' not in st.session_state:
|
| 23 |
+
st.session_state.dark_mode = False
|
| 24 |
+
|
| 25 |
@st.cache_resource(show_spinner="Loading AI model...")
|
| 26 |
def load_model():
|
| 27 |
+
"""Load and cache the model and processor"""
|
| 28 |
try:
|
| 29 |
+
model_name = "google/vit-base-patch16-224"
|
| 30 |
+
processor = ViTImageProcessor.from_pretrained(model_name)
|
| 31 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
model = ViTForImageClassification.from_pretrained(
|
| 33 |
+
model_name,
|
| 34 |
num_labels=len(DAMAGE_TYPES),
|
| 35 |
ignore_mismatched_sizes=True,
|
|
|
|
|
|
|
| 36 |
).to(device)
|
| 37 |
+
model.eval()
|
|
|
|
| 38 |
return model, processor
|
| 39 |
except Exception as e:
|
| 40 |
st.error(f"Error loading model: {str(e)}")
|
|
|
|
|
|
|
| 41 |
return None, None
|
| 42 |
|
| 43 |
+
def validate_image(image):
|
| 44 |
+
"""Validate image size and format"""
|
| 45 |
+
if image.size[0] * image.size[1] > 1024 * 1024:
|
| 46 |
+
st.warning("Large image detected. The image will be resized for better performance.")
|
| 47 |
+
if image.format not in ['JPEG', 'PNG']:
|
| 48 |
+
st.warning("Image format not optimal. Consider using JPEG or PNG for better performance.")
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
def preprocess_image(uploaded_file):
|
| 51 |
+
"""Preprocess and validate uploaded image"""
|
| 52 |
try:
|
| 53 |
+
image = Image.open(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if max(image.size) > MAX_IMAGE_SIZE:
|
| 55 |
ratio = MAX_IMAGE_SIZE / max(image.size)
|
| 56 |
new_size = tuple([int(dim * ratio) for dim in image.size])
|
| 57 |
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
|
|
|
| 58 |
return image
|
| 59 |
except Exception as e:
|
| 60 |
st.error(f"Error processing image: {str(e)}")
|
| 61 |
return None
|
| 62 |
|
| 63 |
def analyze_damage(image, model, processor):
|
| 64 |
+
"""Analyze structural damage in the image"""
|
| 65 |
try:
|
| 66 |
device = next(model.parameters()).device
|
| 67 |
with torch.no_grad():
|
| 68 |
+
image = image.convert('RGB')
|
| 69 |
inputs = processor(images=image, return_tensors="pt")
|
| 70 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
|
|
|
|
|
|
| 71 |
outputs = model(**inputs)
|
| 72 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
|
|
|
|
|
|
| 73 |
cleanup_memory()
|
| 74 |
return probs.cpu()
|
| 75 |
except RuntimeError as e:
|
| 76 |
if "out of memory" in str(e):
|
| 77 |
cleanup_memory()
|
| 78 |
+
st.error("Out of memory. Please try with a smaller image.")
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
+
st.error(f"Error analyzing image: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 81 |
return None
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def main():
|
| 84 |
+
st.set_page_config(
|
| 85 |
+
page_title="Structural Damage Analyzer Pro",
|
| 86 |
+
page_icon="🏗️",
|
| 87 |
+
layout="wide",
|
| 88 |
+
initial_sidebar_state="expanded"
|
| 89 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# Initialize session state
|
| 92 |
+
init_session_state()
|
| 93 |
+
|
| 94 |
+
# Display header
|
| 95 |
+
st.markdown(
|
| 96 |
+
"""
|
| 97 |
+
<div style='text-align: center; padding: 1rem;'>
|
| 98 |
+
<h1>🏗️ Structural Damage Analyzer Pro</h1>
|
| 99 |
+
<p style='font-size: 1.2rem;'>Advanced AI-powered structural damage assessment tool</p>
|
| 100 |
+
</div>
|
| 101 |
+
""",
|
| 102 |
+
unsafe_allow_html=True
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Load model
|
| 106 |
+
model, processor = load_model()
|
| 107 |
+
if model is None:
|
| 108 |
+
st.error("Failed to load model. Please refresh the page.")
|
| 109 |
+
return
|
| 110 |
+
|
| 111 |
+
# File upload
|
| 112 |
+
uploaded_file = st.file_uploader(
|
| 113 |
+
"Upload an image for analysis",
|
| 114 |
+
type=['jpg', 'jpeg', 'png'],
|
| 115 |
+
help="Supported formats: JPG, JPEG, PNG"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
if uploaded_file:
|
| 119 |
+
try:
|
| 120 |
+
if uploaded_file.size > MAX_FILE_SIZE:
|
| 121 |
+
st.error("File size too large. Please upload an image smaller than 5MB.")
|
| 122 |
+
return
|
| 123 |
+
|
| 124 |
+
image = preprocess_image(uploaded_file)
|
| 125 |
+
if image is None:
|
| 126 |
+
return
|
| 127 |
+
|
| 128 |
+
validate_image(image)
|
| 129 |
+
|
| 130 |
+
# Display image and analyze
|
| 131 |
+
st.image(image, caption="Uploaded Structure", use_column_width=True)
|
| 132 |
+
|
| 133 |
+
with st.spinner("🔍 Analyzing damage..."):
|
| 134 |
+
predictions = analyze_damage(image, model, processor)
|
| 135 |
if predictions is not None:
|
| 136 |
+
st.success("Analysis complete!")
|
| 137 |
+
# Add analysis display logic here based on your DAMAGE_TYPES
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
cleanup_memory()
|
| 141 |
+
st.error(f"Error processing image: {str(e)}")
|
| 142 |
+
st.info("Please try uploading a different image.")
|
| 143 |
|
| 144 |
+
# Footer
|
| 145 |
+
st.markdown("---")
|
| 146 |
+
st.markdown(
|
| 147 |
+
"""
|
| 148 |
+
<div style='text-align: center'>
|
| 149 |
+
<p>🏗️ Structural Damage Analyzer Pro | Built with Streamlit & Transformers</p>
|
| 150 |
+
<p style='font-size: 0.8rem;'>For professional use only. Always consult with a structural engineer.</p>
|
| 151 |
+
</div>
|
| 152 |
+
""",
|
| 153 |
+
unsafe_allow_html=True
|
| 154 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
main()
|