--- 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: ```bash 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. ```bash export GROQ_API_KEY=your_groq_api_key_here ``` ### Running the Application To launch the application, simply execute the main script: ```bash 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. ```python 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. ```python 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. ```python with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface: gr.HTML( """
Enhance workplace safety and compliance with AI-powered image analysis using Llama 3.2 90B Vision and expert chat assistance.