Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,672 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 1 |
+
# Construction Site Safety Analyzer - FIXED VERSION
|
| 2 |
+
# Using Local LLaVA + Llama 3 70B via Groq API
|
| 3 |
+
# Google Colab Implementation with JSON Error Handling
|
| 4 |
+
|
| 5 |
+
# ============================================================================
|
| 6 |
+
# SETUP AND INSTALLATION
|
| 7 |
+
# ============================================================================
|
| 8 |
+
|
| 9 |
+
# Cell 1: Install required packages
|
| 10 |
+
#!pip install transformers torch torchvision Pillow requests opencv-python
|
| 11 |
+
#!pip install groq accelerate bitsandbytes
|
| 12 |
+
#!pip install gradio ipywidgets
|
| 13 |
+
|
| 14 |
+
# Cell 2: Import libraries
|
| 15 |
+
import torch
|
| 16 |
+
import requests
|
| 17 |
+
import json
|
| 18 |
+
import base64
|
| 19 |
+
import re
|
| 20 |
+
from PIL import Image
|
| 21 |
+
import io
|
| 22 |
+
import cv2
|
| 23 |
+
import numpy as np
|
| 24 |
+
from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration
|
| 25 |
+
from groq import Groq
|
| 26 |
import gradio as gr
|
| 27 |
+
from google.colab import files
|
| 28 |
+
import matplotlib.pyplot as plt
|
| 29 |
+
from typing import Dict, List, Optional, Tuple
|
| 30 |
+
import warnings
|
| 31 |
+
warnings.filterwarnings('ignore')
|
| 32 |
|
| 33 |
+
# Cell 3: Configuration and API Setup
|
| 34 |
+
class Config:
|
| 35 |
+
def __init__(self):
|
| 36 |
+
self.groq_api_key = "" # Set your Groq API key here
|
| 37 |
+
self.llava_model_name = "llava-hf/llava-v1.6-mistral-7b-hf"
|
| 38 |
+
self.max_qa_rounds = 5 # Reduced to prevent timeout issues
|
| 39 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 40 |
|
| 41 |
+
def set_groq_key(self, api_key: str):
|
| 42 |
+
self.groq_api_key = api_key
|
| 43 |
|
| 44 |
+
config = Config()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Prompt user for API key
|
| 47 |
+
from getpass import getpass
|
| 48 |
+
groq_key = getpass("Enter your Groq API key: ")
|
| 49 |
+
config.set_groq_key(groq_key)
|
|
|
|
| 50 |
|
| 51 |
+
print(f"Using device: {config.device}")
|
| 52 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 53 |
|
| 54 |
+
# ============================================================================
|
| 55 |
+
# LLAVA MODEL SETUP (LOCAL)
|
| 56 |
+
# ============================================================================
|
| 57 |
|
| 58 |
+
# Cell 4: Load LLaVA Model
|
| 59 |
+
class LocalLLaVA:
|
| 60 |
+
def __init__(self, model_name: str, device: str):
|
| 61 |
+
print("Loading LLaVA model locally...")
|
| 62 |
+
self.device = device
|
| 63 |
+
self.processor = LlavaNextProcessor.from_pretrained(model_name)
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Load model with appropriate settings for Colab
|
| 66 |
+
if device == "cuda":
|
| 67 |
+
self.model = LlavaNextForConditionalGeneration.from_pretrained(
|
| 68 |
+
model_name,
|
| 69 |
+
torch_dtype=torch.float16,
|
| 70 |
+
low_cpu_mem_usage=True,
|
| 71 |
+
load_in_4bit=True, # Use 4-bit quantization to save memory
|
| 72 |
+
device_map="auto"
|
| 73 |
+
)
|
| 74 |
+
else:
|
| 75 |
+
self.model = LlavaNextForConditionalGeneration.from_pretrained(
|
| 76 |
+
model_name,
|
| 77 |
+
torch_dtype=torch.float32,
|
| 78 |
+
low_cpu_mem_usage=True
|
| 79 |
+
)
|
| 80 |
+
self.model.to(device)
|
| 81 |
|
| 82 |
+
print("LLaVA model loaded successfully!")
|
| 83 |
|
| 84 |
+
def analyze_image(self, image: Image.Image, question: str = None) -> str:
|
| 85 |
+
"""Analyze construction site image with optional specific question"""
|
| 86 |
+
|
| 87 |
+
if question is None:
|
| 88 |
+
# Initial comprehensive analysis prompt
|
| 89 |
+
prompt = """[INST] <image>
|
| 90 |
+
You are a construction safety expert analyzing this construction site image.
|
| 91 |
+
Please provide a detailed analysis covering:
|
| 92 |
+
|
| 93 |
+
1. Overall scene description and type of construction work
|
| 94 |
+
2. Workers present and their activities
|
| 95 |
+
3. Heavy machinery and equipment visible
|
| 96 |
+
4. Safety equipment and PPE compliance
|
| 97 |
+
5. Visible hazards and safety concerns
|
| 98 |
+
6. Site organization and conditions
|
| 99 |
+
|
| 100 |
+
Be specific and detailed in your observations. Focus on safety-critical elements.
|
| 101 |
+
[/INST]"""
|
| 102 |
+
else:
|
| 103 |
+
# Specific question prompt
|
| 104 |
+
prompt = f"[INST] <image>\nAs a construction safety expert, please answer this specific question about the construction site image:\n\n{question}\n\nProvide a detailed and specific answer based on what you can observe in the image.[/INST]"
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
# Process inputs
|
| 108 |
+
inputs = self.processor(prompt, image, return_tensors="pt").to(self.device)
|
| 109 |
+
|
| 110 |
+
# Generate response
|
| 111 |
+
with torch.no_grad():
|
| 112 |
+
output = self.model.generate(
|
| 113 |
+
**inputs,
|
| 114 |
+
max_new_tokens=500,
|
| 115 |
+
do_sample=True,
|
| 116 |
+
temperature=0.1,
|
| 117 |
+
pad_token_id=self.processor.tokenizer.eos_token_id
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Decode response
|
| 121 |
+
response = self.processor.decode(output[0], skip_special_tokens=True)
|
| 122 |
+
|
| 123 |
+
# Extract only the generated response (after [/INST])
|
| 124 |
+
if "[/INST]" in response:
|
| 125 |
+
response = response.split("[/INST]")[-1].strip()
|
| 126 |
+
|
| 127 |
+
return response
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"Error in LLaVA analysis: {e}")
|
| 131 |
+
return f"Error analyzing image: {str(e)}"
|
| 132 |
+
|
| 133 |
+
# Initialize LLaVA
|
| 134 |
+
llava_model = LocalLLaVA(config.llava_model_name, config.device)
|
| 135 |
+
|
| 136 |
+
# ============================================================================
|
| 137 |
+
# GROQ LLAMA 3 70B INTEGRATION - FIXED JSON HANDLING
|
| 138 |
+
# ============================================================================
|
| 139 |
+
|
| 140 |
+
# Cell 5: Groq Llama Integration with Error Handling
|
| 141 |
+
class GroqLlamaAnalyzer:
|
| 142 |
+
def __init__(self, api_key: str):
|
| 143 |
+
self.client = Groq(api_key=api_key)
|
| 144 |
+
self.model_name = "llama3-70b-8192"
|
| 145 |
+
|
| 146 |
+
def extract_json_from_text(self, text: str) -> Optional[Dict]:
|
| 147 |
+
"""Extract JSON from text response, handling various formats"""
|
| 148 |
+
try:
|
| 149 |
+
# First, try to parse the entire text as JSON
|
| 150 |
+
return json.loads(text)
|
| 151 |
+
except:
|
| 152 |
+
pass
|
| 153 |
+
|
| 154 |
+
# Look for JSON-like patterns in the text
|
| 155 |
+
json_patterns = [
|
| 156 |
+
r'\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}', # Simple nested JSON
|
| 157 |
+
r'\{.*?\}', # Basic JSON pattern
|
| 158 |
+
]
|
| 159 |
+
|
| 160 |
+
for pattern in json_patterns:
|
| 161 |
+
matches = re.findall(pattern, text, re.DOTALL)
|
| 162 |
+
for match in matches:
|
| 163 |
+
try:
|
| 164 |
+
return json.loads(match)
|
| 165 |
+
except:
|
| 166 |
+
continue
|
| 167 |
+
|
| 168 |
+
return None
|
| 169 |
+
|
| 170 |
+
def generate_question(self, context: str, round_num: int) -> Dict:
|
| 171 |
+
"""Generate dynamic questions based on context analysis"""
|
| 172 |
+
|
| 173 |
+
system_prompt = """You are an expert construction safety analyst. Generate specific questions to gather detailed safety information about construction sites. Always respond in valid JSON format."""
|
| 174 |
+
|
| 175 |
+
user_prompt = f"""Based on the construction site analysis so far (Round {round_num + 1}):
|
| 176 |
+
|
| 177 |
+
{context[:2000]} # Truncate to prevent token limits
|
| 178 |
+
|
| 179 |
+
Generate ONE specific question to identify safety risks, or respond "ANALYSIS_COMPLETE" if sufficient.
|
| 180 |
+
|
| 181 |
+
Respond ONLY in this exact JSON format:
|
| 182 |
+
{{"action": "QUESTION", "question": "your specific safety question", "reasoning": "why this question matters for safety"}}
|
| 183 |
+
|
| 184 |
+
OR
|
| 185 |
+
|
| 186 |
+
{{"action": "ANALYSIS_COMPLETE", "reasoning": "sufficient information gathered"}}"""
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
response = self.client.chat.completions.create(
|
| 190 |
+
model=self.model_name,
|
| 191 |
+
messages=[
|
| 192 |
+
{"role": "system", "content": system_prompt},
|
| 193 |
+
{"role": "user", "content": user_prompt}
|
| 194 |
+
],
|
| 195 |
+
temperature=0.3,
|
| 196 |
+
max_tokens=300
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
response_text = response.choices[0].message.content.strip()
|
| 200 |
+
print(f"Raw Groq response: {response_text}")
|
| 201 |
+
|
| 202 |
+
# Try to extract JSON
|
| 203 |
+
result = self.extract_json_from_text(response_text)
|
| 204 |
+
|
| 205 |
+
if result is None:
|
| 206 |
+
# Fallback: create a question based on round number
|
| 207 |
+
safety_questions = [
|
| 208 |
+
"What personal protective equipment (PPE) are workers wearing or missing?",
|
| 209 |
+
"Are there any fall protection measures in place for workers at height?",
|
| 210 |
+
"What heavy machinery is present and are proper safety protocols being followed?",
|
| 211 |
+
"Are there any visible electrical hazards or unsafe conditions?",
|
| 212 |
+
"Is the work area properly organized and free of debris or obstacles?"
|
| 213 |
+
]
|
| 214 |
+
|
| 215 |
+
if round_num < len(safety_questions):
|
| 216 |
+
result = {
|
| 217 |
+
"action": "QUESTION",
|
| 218 |
+
"question": safety_questions[round_num],
|
| 219 |
+
"reasoning": "Systematic safety assessment"
|
| 220 |
+
}
|
| 221 |
+
else:
|
| 222 |
+
result = {
|
| 223 |
+
"action": "ANALYSIS_COMPLETE",
|
| 224 |
+
"reasoning": "Completed systematic safety review"
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
# Validate result structure
|
| 228 |
+
if "action" not in result:
|
| 229 |
+
result["action"] = "ANALYSIS_COMPLETE"
|
| 230 |
+
if result["action"] == "QUESTION" and "question" not in result:
|
| 231 |
+
result["action"] = "ANALYSIS_COMPLETE"
|
| 232 |
+
|
| 233 |
+
return result
|
| 234 |
+
|
| 235 |
+
except Exception as e:
|
| 236 |
+
print(f"Error generating question: {e}")
|
| 237 |
+
return {
|
| 238 |
+
"action": "ANALYSIS_COMPLETE",
|
| 239 |
+
"reasoning": f"Error occurred: {str(e)}"
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
def final_analysis(self, context: str) -> Dict:
|
| 243 |
+
"""Generate comprehensive safety analysis with improved error handling"""
|
| 244 |
+
|
| 245 |
+
system_prompt = """You are a senior construction safety expert. Analyze the provided information and create a comprehensive safety assessment. You must respond ONLY in valid JSON format."""
|
| 246 |
+
|
| 247 |
+
user_prompt = f"""Based on all construction site information:
|
| 248 |
+
|
| 249 |
+
{context[:3000]} # Truncate to prevent token limits
|
| 250 |
+
|
| 251 |
+
Create a comprehensive safety analysis in this EXACT JSON format:
|
| 252 |
+
{{
|
| 253 |
+
"risk_level": "LOW/MODERATE/HIGH/CRITICAL",
|
| 254 |
+
"confidence_score": "85%",
|
| 255 |
+
"executive_summary": "Brief overview of main safety findings",
|
| 256 |
+
"identified_risks": [
|
| 257 |
+
"Risk 1 with severity level",
|
| 258 |
+
"Risk 2 with severity level"
|
| 259 |
+
],
|
| 260 |
+
"immediate_actions": [
|
| 261 |
+
"Urgent action 1",
|
| 262 |
+
"Urgent action 2"
|
| 263 |
],
|
| 264 |
+
"prevention_methods": [
|
| 265 |
+
"Prevention method 1",
|
| 266 |
+
"Prevention method 2"
|
| 267 |
+
],
|
| 268 |
+
"regulatory_compliance": [
|
| 269 |
+
"Compliance issue 1",
|
| 270 |
+
"Compliance issue 2"
|
| 271 |
+
]
|
| 272 |
+
}}
|
| 273 |
+
|
| 274 |
+
Respond ONLY with valid JSON, no additional text."""
|
| 275 |
+
|
| 276 |
+
try:
|
| 277 |
+
response = self.client.chat.completions.create(
|
| 278 |
+
model=self.model_name,
|
| 279 |
+
messages=[
|
| 280 |
+
{"role": "system", "content": system_prompt},
|
| 281 |
+
{"role": "user", "content": user_prompt}
|
| 282 |
+
],
|
| 283 |
+
temperature=0.2,
|
| 284 |
+
max_tokens=1500
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
response_text = response.choices[0].message.content.strip()
|
| 288 |
+
print(f"Raw final analysis response: {response_text}")
|
| 289 |
+
|
| 290 |
+
# Try to extract JSON
|
| 291 |
+
result = self.extract_json_from_text(response_text)
|
| 292 |
+
|
| 293 |
+
if result is None:
|
| 294 |
+
# Create a fallback analysis structure
|
| 295 |
+
result = {
|
| 296 |
+
"risk_level": "MODERATE",
|
| 297 |
+
"confidence_score": "75%",
|
| 298 |
+
"executive_summary": "Analysis completed with limited data processing capabilities.",
|
| 299 |
+
"identified_risks": ["Unable to fully parse detailed risk assessment"],
|
| 300 |
+
"immediate_actions": ["Conduct manual safety review"],
|
| 301 |
+
"prevention_methods": ["Implement standard safety protocols"],
|
| 302 |
+
"regulatory_compliance": ["Review OSHA compliance standards"]
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
# Ensure all required fields exist
|
| 306 |
+
required_fields = ["risk_level", "confidence_score", "executive_summary",
|
| 307 |
+
"identified_risks", "immediate_actions", "prevention_methods",
|
| 308 |
+
"regulatory_compliance"]
|
| 309 |
+
|
| 310 |
+
for field in required_fields:
|
| 311 |
+
if field not in result:
|
| 312 |
+
result[field] = ["Information not available"] if field.endswith(('_risks', '_actions', '_methods', '_compliance')) else "Not available"
|
| 313 |
+
|
| 314 |
+
return result
|
| 315 |
+
|
| 316 |
+
except Exception as e:
|
| 317 |
+
print(f"Error in final analysis: {e}")
|
| 318 |
+
return {
|
| 319 |
+
"error": str(e),
|
| 320 |
+
"risk_level": "UNKNOWN",
|
| 321 |
+
"confidence_score": "0%",
|
| 322 |
+
"executive_summary": f"Analysis failed due to: {str(e)}",
|
| 323 |
+
"identified_risks": [f"System error: {str(e)}"],
|
| 324 |
+
"immediate_actions": ["Manual review required"],
|
| 325 |
+
"prevention_methods": ["System troubleshooting needed"],
|
| 326 |
+
"regulatory_compliance": ["Unable to assess due to system error"]
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
# Initialize Groq analyzer
|
| 330 |
+
groq_analyzer = GroqLlamaAnalyzer(config.groq_api_key)
|
| 331 |
+
|
| 332 |
+
# ============================================================================
|
| 333 |
+
# MAIN ANALYSIS SYSTEM - IMPROVED ERROR HANDLING
|
| 334 |
+
# ============================================================================
|
| 335 |
+
|
| 336 |
+
# Cell 6: Complete Analysis System with Better Error Handling
|
| 337 |
+
class ConstructionSafetyAnalyzer:
|
| 338 |
+
def __init__(self, llava_model: LocalLLaVA, groq_analyzer: GroqLlamaAnalyzer):
|
| 339 |
+
self.llava = llava_model
|
| 340 |
+
self.groq = groq_analyzer
|
| 341 |
+
self.qa_history = []
|
| 342 |
+
self.analysis_context = ""
|
| 343 |
+
|
| 344 |
+
def analyze_construction_site(self, image_path: str) -> Dict:
|
| 345 |
+
"""Complete construction site safety analysis with improved error handling"""
|
| 346 |
+
|
| 347 |
+
try:
|
| 348 |
+
# Load and display image
|
| 349 |
+
image = Image.open(image_path)
|
| 350 |
+
plt.figure(figsize=(10, 8))
|
| 351 |
+
plt.imshow(image)
|
| 352 |
+
plt.axis('off')
|
| 353 |
+
plt.title("Construction Site Image for Analysis")
|
| 354 |
+
plt.show()
|
| 355 |
+
|
| 356 |
+
print("π Starting Construction Site Safety Analysis...")
|
| 357 |
+
print("=" * 60)
|
| 358 |
+
|
| 359 |
+
# Step 1: Initial LLaVA analysis
|
| 360 |
+
print("π Step 1: Initial Image Analysis with LLaVA...")
|
| 361 |
+
initial_analysis = self.llava.analyze_image(image)
|
| 362 |
+
|
| 363 |
+
print("Initial Analysis:")
|
| 364 |
+
print("-" * 30)
|
| 365 |
+
print(initial_analysis)
|
| 366 |
+
print("\n")
|
| 367 |
+
|
| 368 |
+
# Initialize context
|
| 369 |
+
self.analysis_context = f"Initial Visual Analysis:\n{initial_analysis}\n\n"
|
| 370 |
+
self.qa_history = []
|
| 371 |
+
|
| 372 |
+
# Step 2: Interactive Q&A rounds with error handling
|
| 373 |
+
print("π€ Step 2: Dynamic Question Generation and Analysis...")
|
| 374 |
+
print("=" * 60)
|
| 375 |
+
|
| 376 |
+
round_num = 0
|
| 377 |
+
max_rounds = config.max_qa_rounds
|
| 378 |
+
consecutive_errors = 0
|
| 379 |
+
|
| 380 |
+
while round_num < max_rounds and consecutive_errors < 3:
|
| 381 |
+
print(f"\nπ Round {round_num + 1}:")
|
| 382 |
+
print("-" * 20)
|
| 383 |
+
|
| 384 |
+
try:
|
| 385 |
+
# Generate question with Llama
|
| 386 |
+
print("π§ Llama 3 70B analyzing and generating question...")
|
| 387 |
+
question_result = self.groq.generate_question(self.analysis_context, round_num)
|
| 388 |
+
|
| 389 |
+
if question_result["action"] == "ANALYSIS_COMPLETE":
|
| 390 |
+
print("β
Analysis determined complete.")
|
| 391 |
+
print(f"Reasoning: {question_result.get('reasoning', 'Analysis complete')}")
|
| 392 |
+
break
|
| 393 |
+
|
| 394 |
+
question = question_result.get("question", "")
|
| 395 |
+
reasoning = question_result.get("reasoning", "")
|
| 396 |
+
|
| 397 |
+
if not question:
|
| 398 |
+
print("β οΈ No question generated, moving to final analysis.")
|
| 399 |
+
break
|
| 400 |
+
|
| 401 |
+
print(f"Generated Question: {question}")
|
| 402 |
+
print(f"Reasoning: {reasoning}")
|
| 403 |
+
|
| 404 |
+
# Get answer from LLaVA
|
| 405 |
+
print("ποΈ LLaVA analyzing specific aspect...")
|
| 406 |
+
answer = self.llava.analyze_image(image, question)
|
| 407 |
+
|
| 408 |
+
print(f"LLaVA Response: {answer}")
|
| 409 |
+
|
| 410 |
+
# Store Q&A
|
| 411 |
+
qa_round = {
|
| 412 |
+
"round": round_num + 1,
|
| 413 |
+
"question": question,
|
| 414 |
+
"answer": answer,
|
| 415 |
+
"reasoning": reasoning
|
| 416 |
+
}
|
| 417 |
+
self.qa_history.append(qa_round)
|
| 418 |
+
|
| 419 |
+
# Update context
|
| 420 |
+
self.analysis_context += f"Q{round_num + 1}: {question}\nA{round_num + 1}: {answer}\nReasoning: {reasoning}\n\n"
|
| 421 |
+
|
| 422 |
+
consecutive_errors = 0 # Reset error counter on success
|
| 423 |
+
|
| 424 |
+
except Exception as e:
|
| 425 |
+
print(f"β οΈ Error in round {round_num + 1}: {e}")
|
| 426 |
+
consecutive_errors += 1
|
| 427 |
+
if consecutive_errors >= 3:
|
| 428 |
+
print("π Too many consecutive errors, proceeding to final analysis.")
|
| 429 |
+
break
|
| 430 |
+
|
| 431 |
+
round_num += 1
|
| 432 |
+
|
| 433 |
+
# Step 3: Final comprehensive analysis
|
| 434 |
+
print("\nπ Step 3: Generating Comprehensive Safety Report...")
|
| 435 |
+
print("=" * 60)
|
| 436 |
+
|
| 437 |
+
final_analysis = self.groq.final_analysis(self.analysis_context)
|
| 438 |
+
|
| 439 |
+
return {
|
| 440 |
+
"initial_analysis": initial_analysis,
|
| 441 |
+
"qa_rounds": self.qa_history,
|
| 442 |
+
"final_analysis": final_analysis,
|
| 443 |
+
"total_rounds": len(self.qa_history),
|
| 444 |
+
"status": "completed"
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
except Exception as e:
|
| 448 |
+
print(f"π¨ Critical error in analysis: {e}")
|
| 449 |
+
return {
|
| 450 |
+
"error": str(e),
|
| 451 |
+
"status": "failed",
|
| 452 |
+
"initial_analysis": "Failed to analyze image",
|
| 453 |
+
"qa_rounds": [],
|
| 454 |
+
"final_analysis": {
|
| 455 |
+
"risk_level": "UNKNOWN",
|
| 456 |
+
"confidence_score": "0%",
|
| 457 |
+
"executive_summary": f"Analysis failed: {str(e)}",
|
| 458 |
+
"identified_risks": [f"System error: {str(e)}"],
|
| 459 |
+
"immediate_actions": ["Manual analysis required"],
|
| 460 |
+
"prevention_methods": ["System troubleshooting needed"],
|
| 461 |
+
"regulatory_compliance": ["Unable to assess"]
|
| 462 |
+
},
|
| 463 |
+
"total_rounds": 0
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
def display_results(self, results: Dict):
|
| 467 |
+
"""Display formatted analysis results with error handling"""
|
| 468 |
+
|
| 469 |
+
print("\n" + "=" * 80)
|
| 470 |
+
print("ποΈ CONSTRUCTION SITE SAFETY ANALYSIS REPORT")
|
| 471 |
+
print("=" * 80)
|
| 472 |
+
|
| 473 |
+
if results.get("status") == "failed":
|
| 474 |
+
print(f"\nβ ANALYSIS FAILED")
|
| 475 |
+
print("-" * 40)
|
| 476 |
+
print(f"Error: {results.get('error', 'Unknown error')}")
|
| 477 |
+
return
|
| 478 |
+
|
| 479 |
+
# Executive Summary
|
| 480 |
+
final = results.get("final_analysis", {})
|
| 481 |
+
print(f"\nπ― EXECUTIVE SUMMARY")
|
| 482 |
+
print("-" * 40)
|
| 483 |
+
print(f"Risk Level: {final.get('risk_level', 'Unknown')}")
|
| 484 |
+
print(f"Confidence: {final.get('confidence_score', 'Unknown')}")
|
| 485 |
+
print(f"Summary: {final.get('executive_summary', 'No summary available')}")
|
| 486 |
+
|
| 487 |
+
# Q&A Summary
|
| 488 |
+
print(f"\nπ ANALYSIS PROCESS")
|
| 489 |
+
print("-" * 40)
|
| 490 |
+
print(f"Total Investigation Rounds: {results.get('total_rounds', 0)}")
|
| 491 |
+
|
| 492 |
+
for qa in results.get("qa_rounds", []):
|
| 493 |
+
print(f"\nRound {qa['round']}: {qa['question']}")
|
| 494 |
+
answer_preview = qa['answer'][:100] + "..." if len(qa['answer']) > 100 else qa['answer']
|
| 495 |
+
print(f"Answer: {answer_preview}")
|
| 496 |
+
|
| 497 |
+
# Risk Assessment
|
| 498 |
+
risks = final.get("identified_risks", [])
|
| 499 |
+
if risks and risks != ["Information not available"]:
|
| 500 |
+
print(f"\nβ οΈ IDENTIFIED RISKS")
|
| 501 |
+
print("-" * 40)
|
| 502 |
+
for i, risk in enumerate(risks, 1):
|
| 503 |
+
print(f"{i}. {risk}")
|
| 504 |
+
|
| 505 |
+
# Immediate Actions
|
| 506 |
+
actions = final.get("immediate_actions", [])
|
| 507 |
+
if actions and actions != ["Information not available"]:
|
| 508 |
+
print(f"\nπ¨ IMMEDIATE ACTIONS REQUIRED")
|
| 509 |
+
print("-" * 40)
|
| 510 |
+
for i, action in enumerate(actions, 1):
|
| 511 |
+
print(f"{i}. {action}")
|
| 512 |
+
|
| 513 |
+
# Prevention Methods
|
| 514 |
+
methods = final.get("prevention_methods", [])
|
| 515 |
+
if methods and methods != ["Information not available"]:
|
| 516 |
+
print(f"\nπ‘οΈ PREVENTION METHODS")
|
| 517 |
+
print("-" * 40)
|
| 518 |
+
for i, method in enumerate(methods, 1):
|
| 519 |
+
print(f"{i}. {method}")
|
| 520 |
+
|
| 521 |
+
# Regulatory Compliance
|
| 522 |
+
compliance = final.get("regulatory_compliance", [])
|
| 523 |
+
if compliance and compliance != ["Information not available"]:
|
| 524 |
+
print(f"\nπ REGULATORY COMPLIANCE ISSUES")
|
| 525 |
+
print("-" * 40)
|
| 526 |
+
for i, issue in enumerate(compliance, 1):
|
| 527 |
+
print(f"{i}. {issue}")
|
| 528 |
+
|
| 529 |
+
# Initialize the complete system
|
| 530 |
+
analyzer = ConstructionSafetyAnalyzer(llava_model, groq_analyzer)
|
| 531 |
+
|
| 532 |
+
# ============================================================================
|
| 533 |
+
# IMPROVED GRADIO INTERFACE
|
| 534 |
+
# ============================================================================
|
| 535 |
+
|
| 536 |
+
# Cell 7: Create Improved Gradio Interface
|
| 537 |
+
def create_gradio_interface():
|
| 538 |
+
def analyze_uploaded_image(image):
|
| 539 |
+
if image is None:
|
| 540 |
+
return "Please upload an image first."
|
| 541 |
+
|
| 542 |
+
# Save temporary image
|
| 543 |
+
temp_path = "/tmp/construction_site.jpg"
|
| 544 |
+
image.save(temp_path)
|
| 545 |
+
|
| 546 |
+
try:
|
| 547 |
+
# Run analysis
|
| 548 |
+
results = analyzer.analyze_construction_site(temp_path)
|
| 549 |
+
|
| 550 |
+
if results.get("status") == "failed":
|
| 551 |
+
return f"# β Analysis Failed\n\nError: {results.get('error', 'Unknown error')}\n\nPlease try again or check your API configuration."
|
| 552 |
+
|
| 553 |
+
# Format results for display
|
| 554 |
+
final = results.get("final_analysis", {})
|
| 555 |
+
|
| 556 |
+
report = f"""
|
| 557 |
+
# ποΈ Construction Site Safety Analysis Report
|
| 558 |
+
|
| 559 |
+
## π― Executive Summary
|
| 560 |
+
- **Risk Level**: {final.get('risk_level', 'Unknown')}
|
| 561 |
+
- **Confidence**: {final.get('confidence_score', 'Unknown')}
|
| 562 |
+
- **Summary**: {final.get('executive_summary', 'No summary available')}
|
| 563 |
+
|
| 564 |
+
## π Analysis Process
|
| 565 |
+
- **Total Investigation Rounds**: {results.get('total_rounds', 0)}
|
| 566 |
+
- **Status**: {results.get('status', 'Unknown')}
|
| 567 |
+
|
| 568 |
+
### Question & Answer Rounds:
|
| 569 |
+
"""
|
| 570 |
+
|
| 571 |
+
for qa in results.get("qa_rounds", []):
|
| 572 |
+
report += f"\n**Round {qa['round']}**: {qa['question']}\n"
|
| 573 |
+
report += f"*Answer*: {qa['answer'][:200]}{'...' if len(qa['answer']) > 200 else ''}\n"
|
| 574 |
+
|
| 575 |
+
risks = final.get("identified_risks", [])
|
| 576 |
+
if risks and risks != ["Information not available"]:
|
| 577 |
+
report += "\n## β οΈ Identified Risks\n"
|
| 578 |
+
for i, risk in enumerate(risks, 1):
|
| 579 |
+
report += f"{i}. {risk}\n"
|
| 580 |
+
|
| 581 |
+
actions = final.get("immediate_actions", [])
|
| 582 |
+
if actions and actions != ["Information not available"]:
|
| 583 |
+
report += "\n## π¨ Immediate Actions Required\n"
|
| 584 |
+
for i, action in enumerate(actions, 1):
|
| 585 |
+
report += f"{i}. {action}\n"
|
| 586 |
+
|
| 587 |
+
methods = final.get("prevention_methods", [])
|
| 588 |
+
if methods and methods != ["Information not available"]:
|
| 589 |
+
report += "\n## π‘οΈ Prevention Methods\n"
|
| 590 |
+
for i, method in enumerate(methods, 1):
|
| 591 |
+
report += f"{i}. {method}\n"
|
| 592 |
+
|
| 593 |
+
return report
|
| 594 |
+
|
| 595 |
+
except Exception as e:
|
| 596 |
+
return f"# β Error During Analysis\n\n```\n{str(e)}\n```\n\nPlease check your configuration and try again."
|
| 597 |
+
|
| 598 |
+
# Create Gradio interface
|
| 599 |
+
iface = gr.Interface(
|
| 600 |
+
fn=analyze_uploaded_image,
|
| 601 |
+
inputs=gr.Image(type="pil", label="Upload Construction Site Image"),
|
| 602 |
+
outputs=gr.Markdown(label="Safety Analysis Report"),
|
| 603 |
+
title="ποΈ Construction Site Safety Analyzer (Fixed Version)",
|
| 604 |
+
description="Upload a construction site image for comprehensive safety analysis using LLaVA + Llama 3 70B. This version includes improved error handling and JSON parsing.",
|
| 605 |
+
examples=None
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
return iface
|
| 609 |
+
|
| 610 |
+
# ============================================================================
|
| 611 |
+
# EXAMPLE USAGE AND TESTING
|
| 612 |
+
# ============================================================================
|
| 613 |
+
|
| 614 |
+
# Cell 8: Test the Fixed System
|
| 615 |
+
def test_system():
|
| 616 |
+
"""Test the fixed system with better error handling"""
|
| 617 |
+
print("π§ͺ Testing Fixed Construction Safety Analyzer System...")
|
| 618 |
+
|
| 619 |
+
# Test 1: Check model loading
|
| 620 |
+
print("β
Test 1: Models loaded successfully")
|
| 621 |
+
print(f" - LLaVA model: {llava_model.model.__class__.__name__}")
|
| 622 |
+
print(f" - Groq client: {groq_analyzer.client.__class__.__name__}")
|
| 623 |
+
|
| 624 |
+
# Test 2: Check API connectivity with better error handling
|
| 625 |
+
try:
|
| 626 |
+
test_response = groq_analyzer.client.chat.completions.create(
|
| 627 |
+
model="llama3-70b-8192",
|
| 628 |
+
messages=[{"role": "user", "content": "Hello, this is a test."}],
|
| 629 |
+
max_tokens=10
|
| 630 |
+
)
|
| 631 |
+
print("β
Test 2: Groq API connection successful")
|
| 632 |
+
except Exception as e:
|
| 633 |
+
print(f"β Test 2: Groq API connection failed: {e}")
|
| 634 |
+
print(" Please check your API key and internet connection.")
|
| 635 |
+
|
| 636 |
+
# Test 3: JSON parsing function
|
| 637 |
+
test_json = '{"action": "QUESTION", "question": "Test question"}'
|
| 638 |
+
result = groq_analyzer.extract_json_from_text(test_json)
|
| 639 |
+
if result and "action" in result:
|
| 640 |
+
print("β
Test 3: JSON parsing function working")
|
| 641 |
+
else:
|
| 642 |
+
print("β Test 3: JSON parsing function failed")
|
| 643 |
+
|
| 644 |
+
print("π System test completed!")
|
| 645 |
+
|
| 646 |
+
# Run system test
|
| 647 |
+
test_system()
|
| 648 |
+
|
| 649 |
+
# Launch Gradio interface
|
| 650 |
+
print("π Creating Fixed Gradio Interface...")
|
| 651 |
+
interface = create_gradio_interface()
|
| 652 |
+
interface.launch(share=True, debug=True)
|
| 653 |
+
|
| 654 |
+
print("""
|
| 655 |
+
ποΈ FIXED CONSTRUCTION SITE SAFETY ANALYZER - READY TO USE!
|
| 656 |
+
|
| 657 |
+
π§ IMPROVEMENTS MADE:
|
| 658 |
+
- β
Fixed JSON parsing errors with robust extraction
|
| 659 |
+
- β
Added comprehensive error handling
|
| 660 |
+
- β
Reduced max Q&A rounds to prevent timeouts
|
| 661 |
+
- β
Added fallback questions for systematic analysis
|
| 662 |
+
- β
Improved response validation
|
| 663 |
+
- β
Better error messages and debugging
|
| 664 |
|
| 665 |
+
π INSTRUCTIONS:
|
| 666 |
+
1. Ensure your Groq API key is set correctly
|
| 667 |
+
2. Upload a construction site image
|
| 668 |
+
3. The system will now handle JSON errors gracefully
|
| 669 |
+
4. View comprehensive safety analysis with improved reliability
|
| 670 |
|
| 671 |
+
π READY TO ANALYZE CONSTRUCTION SITE SAFETY WITH IMPROVED RELIABILITY!
|
| 672 |
+
""")
|