license: mit
title: Construction Site Safety Analyzer
sdk: gradio
emoji: π’
colorFrom: blue
colorTo: green
short_description: Enhance workplace safety and compliance with AI
sdk_version: 5.1.0
ποΈ Construction Site Safety Analyzer
Enhance workplace safety and compliance with AI-powered image analysis using Llama 3.2 90B Vision and expert chat assistance.
Overview
This application leverages the power of Groq's AI models to analyze construction site images, identify safety issues, and provide detailed descriptions along with recommended resolutions. The Gradio interface allows users to upload images and interact with the system through a chatbot, making it easy to ask questions and get answers regarding safety measures and regulations.
Features
- Image Upload: Users can upload construction site images either as a file path or a PIL Image object.
- AI Analysis: The application uses advanced computer vision techniques to detect and categorize safety issues within the images.
- Chat Interaction: A built-in chatbot provides detailed responses to questions related to the analyzed images, offering insights and guidance on safety measures and regulations.
- Custom CSS: The interface is styled with custom CSS to enhance readability and usability.
Installation
To run this application, you need to have Python installed on your system. Additionally, ensure that you have the required dependencies installed. You can install these dependencies using pip:
pip install gradio pillow groq-python
Environment Variables
Before running the application, make sure to set the GROQ_API_KEY
environment variable. This key is necessary for authenticating requests to the Groq API.
export GROQ_API_KEY=your_groq_api_key_here
Running the Application
To launch the application, simply execute the main script:
python main.py
The application will start a local server, and you can access it via your web browser at http://localhost:7860
.
Code Explanation
Imports and Setup
The code begins by importing necessary libraries and setting up logging for debugging purposes. It also loads the GROQ_API_KEY
from the environment variables and initializes a Groq client.
import os
import base64
import gradio as gr
from PIL import Image
import io
import json
from groq import Groq
import logging
# Set up logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
Helper Functions
The encode_image
function encodes an image into a base64 string, which is useful for sending images over APIs. The analyze_construction_image
function handles the main logic of image analysis, including sending requests to the Groq API and processing the results.
def encode_image(image):
try:
if isinstance(image, str):
with open(image, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
elif isinstance(image, Image.Image):
buffered = io.BytesIO()
image.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')
else:
raise ValueError(f"Unsupported image type: {type(image)}")
except Exception as e:
logger.error(f"Error encoding image: {str(e)}")
raise
def analyze_construction_image(image):
if image is None:
logger.warning("No image provided")
return [("No image uploaded", "Error: Please upload an image for analysis.")]
...
Gradio Interface
The Gradio interface is created using the gr.Blocks
context manager. It includes a form for uploading images, a button to trigger analysis, and a chatbot for displaying results and handling user queries.
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
gr.HTML(
"""
<div class="container">
<div class="header">
<h1>ποΈ Construction Site Safety Analyzer</h1>
</div>
<p class="subheader">Enhance workplace safety and compliance with AI-powered image analysis using Llama 3.2 90B Vision and expert chat assistance.</p>
</div>
"""
)
with gr.Row():
with gr.Column(scale=1):
image_input = gr.Image(type="pil", label="Upload Construction Site Image", elem_classes="image-container")
analyze_button = gr.Button("π Analyze Safety Hazards", elem_classes="analyze-button")
with gr.Column(scale=2):
with gr.Group(elem_classes="chat-container"):
chatbot = gr.Chatbot(label="Safety Analysis Results and Expert Chat", elem_classes="chatbot")
with gr.Row(elem_classes="input-row"):
msg = gr.Textbox(
label="Ask about safety measures or regulations",
placeholder="E.g., 'What OSHA guidelines apply to this hazard?'",
show_label=False,
elem_classes="chat-input"
)
clear = gr.Button("ποΈ Clear", elem_classes="clear-button")
def update_chat(history, new_message):
history = history or []
history.append(new_message)
return history
analyze_button.click(
analyze_construction_image,
inputs=[image_input],
outputs=[chatbot],
postprocess=lambda x: update_chat(chatbot.value, x[0])
)
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)
gr.HTML(
"""
<div class="groq-badge">Powered by Groq</div>
"""
)
Launching the App
Finally, the application is launched with the iface.launch()
method, which starts a local development server.
if __name__ == "__main__":
iface.launch(debug=True)
Conclusion
This Construction Site Safety Analyzer is a powerful tool for enhancing workplace safety and compliance through AI-driven image analysis and expert chat assistance. With its intuitive interface and advanced capabilities, it helps users quickly identify and address potential hazards on construction sites.